Exemplo n.º 1
0
        public async Task <IActionResult> Update(Guid commissionStatementId, [FromBody] CommissionStatementEdit commissionStatement)
        {
            commissionStatement.Id = commissionStatementId;

            var scope = AuthenticationService.GetScope(User);

            var result = await CommissionStatementService.UpdateCommissionStatement(scope, commissionStatement);

            if (!result.Success)
            {
                return(BadRequest(result.ValidationFailures));
            }

            return(Ok(result));
        }
        public async Task UpdateCommissionStatement()
        {
            var options = TestHelper.GetDbContext("UpdateCommissionStatement");

            var user1 = TestHelper.InsertUserDetailed(options);
            var user2 = TestHelper.InsertUserDetailed(options);

            var cs1 = new CommissionStatementEntity
            {
                Id                 = Guid.NewGuid(),
                CompanyId          = Guid.NewGuid(),
                AmountIncludingVAT = 100,
                VAT                = 10,
                Date               = DateTime.Now,
                Processed          = true,
                OrganisationId     = user1.Organisation.Id,
                Notes              = "note 1"
            };

            var cs2 = new CommissionStatementEntity
            {
                Id                 = Guid.NewGuid(),
                CompanyId          = Guid.NewGuid(),
                AmountIncludingVAT = 200,
                VAT                = 20,
                Date               = DateTime.Now.AddDays(-1),
                Processed          = false,
                OrganisationId     = user1.Organisation.Id,
                Notes              = "note 2"
            };

            using (var context = new DataContext(options))
            {
                context.CommissionStatement.Add(cs1);
                context.CommissionStatement.Add(cs2);

                context.SaveChanges();
            }

            using (var context = new DataContext(options))
            {
                var auditService = new AuditServiceMock();
                var service      = new CommissionStatementService(context, null, auditService);

                var model = new CommissionStatementEdit
                {
                    Id                 = cs2.Id,
                    CompanyId          = Guid.NewGuid(),
                    AmountIncludingVAT = 300,
                    VAT                = 30,
                    Date               = DateTime.Now.AddDays(-10),
                    Processed          = true,
                    Notes              = "note 2 updated"
                };

                //When
                var scopeOptions = TestHelper.GetScopeOptions(user1);
                var result       = await service.UpdateCommissionStatement(scopeOptions, model);

                //Then
                Assert.True(result.Success);

                var actual = await context.CommissionStatement.FindAsync(model.Id);

                Assert.Equal(model.CompanyId, actual.CompanyId);
                Assert.Equal(model.Date.Value.Date, actual.Date);
                Assert.Equal(model.AmountIncludingVAT, actual.AmountIncludingVAT);
                Assert.Equal(model.VAT, actual.VAT);
                Assert.Equal(user1.Organisation.Id, user1.Organisation.Id);
                Assert.Equal(model.Processed, actual.Processed);
                Assert.Equal(model.Notes, actual.Notes);

                //Out of scope
                scopeOptions = TestHelper.GetScopeOptions(user2, Scope.User);
                result       = await service.UpdateCommissionStatement(scopeOptions, model);

                Assert.False(result.Success);
            }
        }