コード例 #1
0
            public async Task InsertCost()
            {
                //Setup
                var adCostNumber = "adCostNumber";
                var templateId   = Guid.NewGuid();
                var requestModel = CreateCostModel(templateId);
                var costModel    = new CostBuilderModel
                {
                    Stages = new[]
                    {
                        new CostStageModel
                        {
                            Revisions = new []
                            {
                                new CostStageRevisionModel
                                {
                                    SupportingDocuments = new List <SupportingDocumentModel>()
                                }
                            }
                        }
                    }
                };

                var costUser = new CostUser
                {
                    Id     = User.Id,
                    Agency = new Agency
                    {
                        Id = User.AgencyId,
                    }
                };

                var responseMock = new Mock <ICreateCostResponse>();

                responseMock.SetupGet(f => f.Cost).Returns(costModel);
                PgUserServiceMock.Setup(a => a.GrantUsersAccess(It.IsAny <Cost>(), It.IsAny <CreateCostModel>())).Returns(Task.CompletedTask);
                CostBuilderMock.Setup(e => e.CreateCost(It.IsAny <CostUser>(), It.IsAny <CreateCostModel>()))
                .ReturnsAsync(responseMock.Object);

                EFContext.Project.Add(new Project {
                    Id = Guid.NewGuid(), AdCostNumber = adCostNumber
                });
                EFContext.CostUser.Add(costUser);
                await EFContext.SaveChangesAsync();

                PermissionService.Setup(a => a.CreateDomainNode(It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <Guid>()))
                .ReturnsAsync(new[] { Guid.NewGuid().ToString() });
                //Act
                await CostService.CreateCost(User, requestModel);

                //Assert
                CostBuilderMock.Verify(e => e.CreateCost(costUser, requestModel), Times.Once);
                EFContext.Cost.Should().HaveCount(1);
                EFContext.NotificationSubscriber.Should().HaveCount(0);
                Mapper.Verify(m => m.Map <CostModel>(It.Is <Cost>(c => c != null && c.Status == CostStageRevisionStatus.Draft)));
            }
コード例 #2
0
            public async Task GrandEditPermissionToOwner()
            {
                //Setup
                var adCostNumber = "adCostNumber";
                var templateId   = Guid.NewGuid();
                var requestModel = CreateCostModel(templateId);
                var costModel    = new CostBuilderModel
                {
                    Stages = new[]
                    {
                        new CostStageModel
                        {
                            Revisions = new []
                            {
                                new CostStageRevisionModel
                                {
                                    SupportingDocuments = new List <SupportingDocumentModel>()
                                }
                            }
                        }
                    }
                };

                var costUser = new CostUser
                {
                    Id     = User.Id,
                    Agency = new Agency
                    {
                        Id = User.AgencyId,
                    }
                };

                var responseMock = new Mock <ICreateCostResponse>();

                responseMock.SetupGet(f => f.Cost).Returns(costModel);
                PgUserServiceMock.Setup(a => a.GrantUsersAccess(It.IsAny <Cost>(), It.IsAny <CreateCostModel>())).Returns(Task.CompletedTask);
                CostBuilderMock.Setup(e => e.CreateCost(It.IsAny <CostUser>(), It.IsAny <CreateCostModel>()))
                .ReturnsAsync(responseMock.Object);

                EFContext.Project.Add(new Project {
                    Id = Guid.NewGuid(), AdCostNumber = adCostNumber
                });
                EFContext.CostUser.Add(costUser);
                await EFContext.SaveChangesAsync();

                PermissionService.Setup(a => a.CreateDomainNode(It.IsAny <string>(), It.IsAny <Guid>(), It.IsAny <Guid>(), It.IsAny <Guid>()))
                .ReturnsAsync(new[] { Guid.NewGuid().ToString() });
                //Act
                await CostService.CreateCost(User, requestModel);

                //Assert
                CostStageRevisionPermissionServiceMock.Verify(p =>
                                                              p.GrantCostPermission(It.IsAny <Guid>(), Roles.CostEditor, It.Is <IEnumerable <CostUser> >(c => c.Any(i => i == costUser)), BuType.Pg, It.IsAny <Guid?>(), true),
                                                              Times.Once);
            }
コード例 #3
0
            public async Task InsertDependentItems()
            {
                // Arrange
                var adCostNumber = "adCostNumber";
                var templateId   = Guid.NewGuid();
                var requestModel = CreateCostModel(templateId);

                var supportingDocumentModel = new SupportingDocumentModel();
                var costStageRevisionModel  = new CostStageRevisionModel
                {
                    StageDetails        = @"{ ""abc"": ""abc"" }",
                    SupportingDocuments = new[] { supportingDocumentModel }
                };
                var costStageModel = new CostStageModel
                {
                    Revisions = new[] { costStageRevisionModel }
                };
                var costModel = new CostBuilderModel
                {
                    Stages = new[] { costStageModel }
                };
                var costUser = new CostUser
                {
                    Id     = User.Id,
                    Agency = new Agency
                    {
                        Id = User.AgencyId,
                    }
                };

                var responseMock = new Mock <ICreateCostResponse>();

                responseMock.SetupGet(f => f.Cost).Returns(costModel);
                CostBuilderMock.Setup(e => e.CreateCost(It.IsAny <CostUser>(), It.IsAny <CreateCostModel>()))
                .ReturnsAsync(responseMock.Object);

                EFContext.Project.Add(new Project {
                    Id = Guid.NewGuid(), AdCostNumber = adCostNumber
                });
                EFContext.CostUser.Add(costUser);
                await EFContext.SaveChangesAsync();

                // Act
                await CostService.CreateCost(User, requestModel);

                // Assert
                EFContext.Cost.Should().HaveCount(1);
                var cost = EFContext.Cost.First();

                EFContext.CostStage.Should().HaveCount(1);
                var costStage = EFContext.CostStage.First();

                costStage.CostId.Should().Be(cost.Id);

                EFContext.CostStageRevision.Should().HaveCount(1);
                var costStageRevision = EFContext.CostStageRevision.First();

                costStageRevision.CostStageId.Should().Be(costStage.Id);
                costStageRevision.StageDetails.Should().NotBeNull();
                costStageRevision.ProductDetails.Should().NotBeNull();
                costStageRevision.SupportingDocuments.Should().NotBeNull();
            }
コード例 #4
0
        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);
        }