예제 #1
0
        public async Task ArchiveReceipt(string id)
        {
            var command = new DeleteReceiptCommand
            {
                Id = id
            };

            await Mediator.Send(command);
        }
예제 #2
0
        public void Handle_Invalid_ShouldThrowNotFoundException()
        {
            var command = new DeleteReceiptCommand
            {
                Id = "asdasd"
            };

            FluentActions.Invoking(async() => await SendAsync(command)).Should().Throw <NotFoundException>();
        }
예제 #3
0
        public async Task Handle_ValidIdButWrongUser_ShouldThrowNotFoundException()
        {
            var receiptId = await CreateReceipt();

            var delete = new DeleteReceiptCommand {
                Id = receiptId
            };

            await SendAsync(delete);

            await RunAsUserAsync("*****@*****.**", "nah");

            var activate = new ActivateReceiptCommand {
                ReceiptId = receiptId
            };

            FluentActions.Invoking(async() => await SendAsync(activate)).Should().Throw <NotFoundException>();
        }
        public async Task Handle_ValidUserId_ShouldReturnListOfReceipts()
        {
            var projectId = await CreateFinancialProject();

            var receiptId = await CreateReceipt(projectId);

            var deleteCommand = new DeleteReceiptCommand
            {
                Id = receiptId
            };

            await SendAsync(deleteCommand);

            var query = new GetArchiveReceiptsByUserQuery();

            var entities = await SendAsync(query);

            entities.Should().NotBeNull();
            entities.Count.Should().Be(1);
            entities.First().Id.Should().Be(receiptId);
            entities.First().FinancialProject.Id.Should().Be(projectId);
            entities.First().Deleted.Should().BeCloseTo(DateTime.Now, 1000);
        }
예제 #5
0
        public async Task Handle_ValidId_ShouldActivateReceipt(int amount)
        {
            var receiptId = await CreateReceipt();

            var item = GetReceiptItem(new Random().Next(10, 100000));

            var command = new CreateReceiptItemCommand
            {
                Name      = "dd",
                ItemGroup = item.ItemGroup.Value,
                Price     = item.Price,
                Count     = item.Count,
                ReceiptId = receiptId
            };

            command.UserIds = amount switch
            {
                1 => new List <string> {
                    SecondUser.Id
                },
                2 => new List <string> {
                    SecondUser.Id, User.Id
                },
                _ => command.UserIds
            };

            await SendAsync(command);

            var context = CreateContext();
            //create
            var firstOweRecord = context.OweRecords.FirstOrDefault(x => x.UserId == SecondUser.Id && x.OwedUserId == User.Id);

            firstOweRecord.Should().NotBeNull();
            firstOweRecord !.Amount.Should().Be(command.Count * command.Price / command.UserIds.Count); //10k

            var deleteCommand = new DeleteReceiptCommand {
                Id = receiptId
            };

            await SendAsync(deleteCommand);

            context = CreateContext();

            //delete
            var deletedEntity = context.Receipts.FirstOrDefault(x => x.Id == receiptId);

            deletedEntity.Should().NotBeNull();
            deletedEntity !.Deleted.Should().BeCloseTo(DateTime.Now, 1000);
            deletedEntity !.DeletedByUserId.Should().Be(User.Id);

            var secondOweRecord = context.OweRecords.FirstOrDefault(x => x.UserId == SecondUser.Id && x.OwedUserId == User.Id);

            secondOweRecord.Should().NotBeNull();
            secondOweRecord !.Amount.Should().Be(0); //0


            //activate
            var reactivateCommand = new ActivateReceiptCommand {
                ReceiptId = receiptId
            };

            await SendAsync(reactivateCommand);

            context = CreateContext();

            var thirdOweRecord = context.OweRecords.FirstOrDefault(x => x.UserId == SecondUser.Id && x.OwedUserId == User.Id);

            thirdOweRecord.Should().NotBeNull();
            thirdOweRecord !.Amount.Should().Be(command.Count * command.Price / command.UserIds.Count);

            var activatedEntity = context.Receipts.FirstOrDefault(x => x.Id == receiptId);

            activatedEntity.Should().NotBeNull();
            activatedEntity !.Deleted.Should().BeNull();
            activatedEntity !.DeletedByUserId.Should().BeNull();
        }
예제 #6
0
        public void Handle_IdEmpty_ShouldThrowValidationException()
        {
            var command = new DeleteReceiptCommand();

            FluentActions.Invoking(async() => await SendAsync(command)).Should().Throw <ValidationException>();
        }
예제 #7
0
        public async Task Handle_ValidID_ShouldDeleteAndUpdateOweRecord(int userAmount)
        {
            var projectId = await CreateFinancialProject();

            var id = await CreateReceipt(projectId);


            var createReceiptItemCommand = new CreateReceiptItemCommand
            {
                ItemGroup = 0,
                Count     = 100,
                Name      = "test",
                Price     = 10,
                ReceiptId = id,
            };

            createReceiptItemCommand.UserIds = userAmount switch
            {
                1 => new List <string> {
                    SecondUser.Id
                },
                2 => new List <string> {
                    User.Id, SecondUser.Id
                },
                _ => createReceiptItemCommand.UserIds
            };

            await SendAsync(createReceiptItemCommand);

            var command = new DeleteReceiptCommand
            {
                Id = id
            };

            var context = CreateContext();

            var oweRecord = context.OweRecords.FirstOrDefault(x =>
                                                              x.OwedUserId == User.Id && x.UserId == SecondUser.Id && x.FinancialProjectId == projectId);

            oweRecord.Should().NotBeNull();
            oweRecord.Amount.Should().Be(createReceiptItemCommand.Count * createReceiptItemCommand.Price / createReceiptItemCommand.UserIds.Count);

            var notDeleted = await FindAsync <Receipt>(command.Id);

            notDeleted.Should().NotBeNull();

            await SendAsync(command);

            var entity = await FindAsync <Receipt>(command.Id);

            context = CreateContext();

            var updatedOweRecord = context.OweRecords.FirstOrDefault(x =>
                                                                     x.OwedUserId == User.Id && x.UserId == SecondUser.Id && x.FinancialProjectId == projectId);

            entity.Should().NotBeNull();
            entity.Deleted.Should().BeCloseTo(DateTime.Now, 1000);
            entity.DeletedByUserId.Should().Be(User.Id);
            updatedOweRecord.Should().NotBeNull();
            updatedOweRecord.Amount.Should().Be(0);
        }