예제 #1
0
        protected override Project InsertEntity(Project value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (String.IsNullOrWhiteSpace(value.OrganizationId) || !User.IsInOrganization(value.OrganizationId))
            {
                throw new HttpResponseException(InvalidOrganizationErrorResponseMessage());
            }

            if (!_billingManager.CanAddProject(value))
            {
                throw new HttpResponseException(PlanLimitReached("Please upgrade your plan to add an additional project."));
            }

            NotificationSettings settings = value.GetNotificationSettings(User.UserEntity.Id);

            value.NotificationSettings.Clear();
            value.NotificationSettings.Add(User.UserEntity.Id, settings);

            List <string> apiKeys = value.ApiKeys.Where(key => _repository.GetByApiKey(key) == null).ToList();

            value.ApiKeys = apiKeys.Any()
                ? new HashSet <string>(apiKeys)
                : new HashSet <string> {
                Guid.NewGuid().ToString("N").ToLower()
            };

            if (String.IsNullOrWhiteSpace(value.TimeZone))
            {
                value.TimeZone = "Central Standard Time";
            }

            value.NextSummaryEndOfDayTicks = TimeZoneInfo.ConvertTime(DateTime.Today.AddDays(1), value.DefaultTimeZone()).ToUniversalTime().Ticks;
            value.StackCount      = 0;
            value.ErrorCount      = 0;
            value.TotalErrorCount = 0;

            Project project = base.InsertEntity(value);

            _organizationRepository.IncrementStats(project.OrganizationId, projectCount: 1);
            _notificationSender.ProjectUpdated(project.OrganizationId, project.Id);

            return(project);
        }
예제 #2
0
        public ActionResult Add(ProjectModel model)
        {
            ViewBag.Organizations    = Organizations;
            ViewBag.HasOrganizations = Organizations.Any();
            ViewBag.TimeZones        = TimeZoneInfo.GetSystemTimeZones();

            #region Validation

            Organization organization = null;

            if (!String.IsNullOrEmpty(model.OrganizationName))
            {
                Organization existing = Organizations.FirstOrDefault(o => o.Name == model.OrganizationName);
                organization = existing ?? new Organization {
                    Name = model.OrganizationName.Trim()
                };
            }
            else if (!String.IsNullOrEmpty(model.OrganizationId))
            {
                organization = Organizations.FirstOrDefault(o => o.Id == model.OrganizationId);
                ModelState state = ModelState["OrganizationName"];
                if (state != null)
                {
                    state.Errors.Clear();
                }
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (organization == null)
            {
                ModelState.AddModelError("OrganizationName", "Organization Name is required.");
                return(View(model));
            }

            if (!String.IsNullOrEmpty(organization.Id) && Projects.Count(p => p.OrganizationId == organization.Id && String.Equals(p.Name, model.Name, StringComparison.OrdinalIgnoreCase)) > 0)
            {
                ModelState.AddModelError("Name", "A project with this name already exists.");
                return(View(model));
            }

            #endregion

            if (String.IsNullOrEmpty(organization.Id))
            {
                if (!_billingManager.CanAddOrganization(User.UserEntity))
                {
                    ModelState.AddModelError(String.Empty, "Please upgrade your plan to add an additional organization.");
                    return(View(model));
                }

                _billingManager.ApplyBillingPlan(organization, Settings.Current.EnableBilling ? BillingManager.FreePlan : BillingManager.UnlimitedPlan, User.UserEntity);
                organization = _organizationRepository.Add(organization);
                _notificationSender.OrganizationUpdated(organization.Id);

                User user = _userRepository.GetById(User.UserEntity.Id);
                user.OrganizationIds.Add(organization.Id);
                _userRepository.Update(user);
            }

            var project = new Project {
                Name = model.Name, TimeZone = model.TimeZone, OrganizationId = organization.Id
            };
            project.NextSummaryEndOfDayTicks = TimeZoneInfo.ConvertTime(DateTime.Today.AddDays(1), project.DefaultTimeZone()).ToUniversalTime().Ticks;
            project.ApiKeys.Add(Guid.NewGuid().ToString("N").ToLower());
            project.AddDefaultOwnerNotificationSettings(User.UserEntity.Id);

            if (!_billingManager.CanAddProject(project))
            {
                ModelState.AddModelError(String.Empty, "Please upgrade your plan to add an additional project.");
                return(View(model));
            }

            project = _projectRepository.Add(project);

            _organizationRepository.IncrementStats(project.OrganizationId, projectCount: 1);
            _notificationSender.ProjectUpdated(organization.Id, project.Id);

            return(RedirectToAction("Configure", "Project", new { id = project.Id }));
        }