protected async Task <BrowserResponse> CreateCost(CostUser user, CreateCostModel costModel) { return(await Browser.Post("/v1/costs", w => { w.User(user); w.JsonBody(costModel); })); }
public async Task GrantUsersAccess(Cost cost, CreateCostModel model) { var usersToGrantAccessTo = await _efContext.CostUser .Include(user => user.UserBusinessRoles) .ThenInclude(ubr => ubr.BusinessRole) .Where(user => user.UserBusinessRoles.Any(ubr => ubr.ObjectType == core.Constants.AccessObjectType.Region || ubr.ObjectType == core.Constants.AccessObjectType.Smo )).ToListAsync(); var stageDetails = model.StageDetails.Data.ToModel <PgStageDetailsForm>(); foreach (var user in usersToGrantAccessTo) { foreach (var userBusinessRole in user.UserBusinessRoles) { switch (userBusinessRole.ObjectType) { case core.Constants.AccessObjectType.Region: if (userBusinessRole.Labels != null && ( userBusinessRole.Labels.Any(label => label == stageDetails?.BudgetRegion?.Name) || userBusinessRole.BusinessRole.Key == Constants.BusinessRole.RegionalAgencyUser && cost.Owner.Agency.GlobalAgencyRegion != null && userBusinessRole.Labels.Contains(cost.Owner.Agency.GlobalAgencyRegion.Region) )) { await _permissionService.GrantUserAccess <Cost>(userBusinessRole.BusinessRole.RoleId, cost.Id, user, BuType.Pg, null, null, false); } break; case core.Constants.AccessObjectType.Smo: if (userBusinessRole.Labels != null && !string.IsNullOrEmpty(stageDetails.SmoName) && userBusinessRole.Labels.Any(label => label == stageDetails.SmoName)) { await _permissionService.GrantUserAccess <Cost>(userBusinessRole.BusinessRole.RoleId, cost.Id, user, BuType.Pg, null, null, false); } break; } } } }
public async Task CreateCost_whenCyclonAgencyAnd_shouldSetIsExternalPurchaseEnabledToFalse(bool isCycloneAgency, string budgetRegion, bool isExternalPurchasesEnabled) { // Arrange var userId = Guid.NewGuid(); var agencyId = Guid.NewGuid(); var agencyAbstractTypeId = Guid.NewGuid(); var projectId = "Gdam Porject Id"; var agency = new Agency { Id = agencyId, Name = "Name", Labels = isCycloneAgency ? new[] { Constants.Agency.CycloneLabel, Constants.Agency.PAndGLabel, $"{core.Constants.BusinessUnit.CostModulePrimaryLabelPrefix}P&G" } : new[] { Constants.Agency.PAndGLabel, $"{core.Constants.BusinessUnit.CostModulePrimaryLabelPrefix}P&G" }, AbstractTypes = new List <AbstractType> { new AbstractType { Id = Guid.NewGuid(), ObjectId = agencyId, } } }; agency.AbstractTypes.First().Agency = agency; var costUser = new CostUser { Id = userId, Agency = agency }; var abstractTypeAgency = new AbstractType { Id = agencyAbstractTypeId, Agency = agency, ObjectId = agencyId, Parent = new AbstractType { Agency = agency } }; EFContext.AbstractType.Add(abstractTypeAgency); EFContext.CostUser.Add(costUser); EFContext.SaveChanges(); var costTemplateVersion = new CostTemplateVersion { Id = Guid.NewGuid(), CostTemplate = new CostTemplate { Id = Guid.NewGuid(), CostType = CostType.Production } }; CostTemplateVersionServiceMock.Setup(ctv => ctv.GetLatestTemplateVersion(It.Is <Guid>(id => id == costTemplateVersion.CostTemplate.Id))) .ReturnsAsync(costTemplateVersion); var stageDetailsForm = new PgStageDetailsForm { ProjectId = projectId, BudgetRegion = new AbstractTypeValue { Key = budgetRegion }, Agency = new PgStageDetailsForm.AbstractTypeAgency { Id = abstractTypeAgency.ObjectId, AbstractTypeId = abstractTypeAgency.Id, } }; ProjectServiceMock.Setup(p => p.GetByGadmid(projectId)).ReturnsAsync(new Project { GdamProjectId = projectId, AgencyId = agencyId }); var stageDetails = new StageDetails { Data = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(JsonConvert.SerializeObject(stageDetailsForm)) }; var createCostModel = new CreateCostModel { TemplateId = costTemplateVersion.CostTemplate.Id, StageDetails = stageDetails }; PgStageBuilderMock.Setup(sb => sb.GetStages(It.IsAny <PgStageRule>(), null)) .ReturnsAsync(new Dictionary <string, StageModel> { { CostStages.New.ToString(), new StageModel { Key = CostStages.New.ToString(), Transitions = new Dictionary <string, StageModel> { { CostStages.OriginalEstimate.ToString(), new StageModel() } } } }, { CostStages.OriginalEstimate.ToString(), new StageModel { Key = CostStages.OriginalEstimate.ToString() } } }); RuleServiceMock.Setup(r => r.GetCompiledByRuleType <SupportingDocumentRule>(RuleType.SupportingDocument)).ReturnsAsync(new List <CompiledRule <SupportingDocumentRule> >()); // Act var result = await CostBuilder.CreateCost(costUser, createCostModel); // Assert result.Cost.IsExternalPurchasesEnabled.Should().Be(isExternalPurchasesEnabled); }
public async Task <ICreateCostResponse> CreateCost(CostUser user, CreateCostModel createCostModel) { if (createCostModel == null) { throw new ArgumentNullException(nameof(createCostModel)); } if (createCostModel.StageDetails == null) { throw new Exception("StageDetails is missing"); } var stageDetailsForm = createCostModel.StageDetails.Data.ToModel <PgStageDetailsForm>(); if (string.IsNullOrEmpty(stageDetailsForm.ProjectId)) { throw new Exception("Project is missing"); } var templateVersion = await _costTemplateVersionService.GetLatestTemplateVersion(createCostModel.TemplateId); if (templateVersion == null) { throw new Exception("Template version missing"); } var project = await _projectService.GetByGadmid(stageDetailsForm.ProjectId); if (project == null) { throw new HttpException(HttpStatusCode.BadRequest, "ProjectId is invalid!"); } var agencyAbstractType = _efContext.AbstractType .Include(at => at.Agency) .FirstOrDefault(a => a.Agency != null && a.Parent.Agency.Id == project.AgencyId && user.Agency.Id == a.ObjectId); if (agencyAbstractType == null) { throw new Exception("AgencyAbstractType is missing"); } await _permissionService.CheckAccess(user.Id, agencyAbstractType.Id, AclActionType.Create, typeof(Cost).Name.ToLower()); var isExternalPurchasesEnabled = !agencyAbstractType.Agency.IsCyclone() || stageDetailsForm.BudgetRegion?.Key != Constants.BudgetRegion.NorthAmerica; var cost = new CostBuilderModel { CostType = templateVersion.CostTemplate.CostType, CostTemplateVersionId = templateVersion.Id, Stages = new[] { await BuildCostFirstStageModel(stageDetailsForm, user.Id, templateVersion.CostTemplate.CostType, createCostModel.StageDetails) }, ParentId = agencyAbstractType.Id, ProjectId = project.Id, IsExternalPurchasesEnabled = isExternalPurchasesEnabled, ContentType = stageDetailsForm.ContentType?.Value }; var response = new CreateCostResponse { Cost = cost }; return(response); }