public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var subTransaction = await _writeDbContext.Transactions
                                     .Include(x => x.SubTransactions)
                                     .SelectMany(x => x.SubTransactions)
                                     .FirstOrDefaultAsync(x => x.SubTransactionId == request.SubTransactionId, cancellationToken: cancellationToken)
                                     ?? throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));

                if (!await _accessControlService.HasTransactionAccess(subTransaction.ParentTransactionId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));
                }

                subTransaction.SetDescription(request.Description);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                var transaction = _writeDbContext.Transactions.FirstOrDefault(x => x.TransactionId == subTransaction.ParentTransactionId);

                _ = _mediator.Publish(new Notification()
                {
                    Transaction    = transaction,
                    SubTransaction = subTransaction
                }, cancellationToken);
                return(new Result()
                {
                    Data = subTransaction.Description
                });
            }
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var transactionTemplate = await _writeDbContext.TransactionTemplates
                                          .FirstOrDefaultAsync(x => x.TransactionTemplateId == request.TransactionTemplateId, cancellationToken : cancellationToken)
                                          ?? throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));

                if (!await _accessControlService.HasTransactionTemplateAccess(transactionTemplate.TransactionTemplateId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));
                }

                transactionTemplate.SoftDelete();

                await _writeDbContext.SaveChangesAsync(cancellationToken);


                _ = _mediator.Publish(new Notification()
                {
                    TransactionTemplate = transactionTemplate
                }, cancellationToken);

                return(new Result()
                {
                });
            }
Exemplo n.º 3
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasBudgetCategoryAccessAsync(request.BudgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound));
                }

                var budget = await _writeDbContext.Budgets
                             .Include(x => x.BudgetCategories)
                             .FirstAsync(x => x.BudgetCategories.Any(s => s.BudgetCategoryId == request.BudgetCategoryId), cancellationToken);

                var budgetCategory = budget.BudgetCategories.First(x => x.BudgetCategoryId == request.BudgetCategoryId);

                budget.BudgetCategories.MoveUp(budgetCategory);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    ReferenceBudgetCategory = budgetCategory,
                }, cancellationToken);

                return(new Result()
                {
                });
            }
Exemplo n.º 4
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var transaction = await _writeDbContext.Transactions
                                  .Include(x => x.SubTransactions)
                                  .FirstOrDefaultAsync(x => x.SubTransactions.Any(s => s.SubTransactionId == request.SubTransactionId), cancellationToken: cancellationToken)
                                  ?? throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));

                if (!await _accessControlService.HasBudgetCategoryAccessAsync(transaction.BudgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));
                }

                var subTransaction = transaction.SubTransactions.First(x => x.SubTransactionId == request.SubTransactionId);

                subTransaction.SoftDelete();

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    Transaction    = transaction,
                    SubTransaction = subTransaction
                }, cancellationToken);

                return(new Result()
                {
                });
            }
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasBudgetCategoryAccessAsync(request.BudgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound));
                }

                var budgetCategory = await _writeDbContext.BudgetCategories
                                     .FirstOrDefaultAsync(x => x.BudgetCategoryId == request.BudgetCategoryId, cancellationToken);

                var icon = await _writeDbContext.BudgetCategoryIcons
                           .FirstOrDefaultAsync(x => x.BudgetCategoryIconId == request.BudgetCategoryIconId, cancellationToken);

                budgetCategory.SetIcon(icon);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    ReferenceBudgetCategory = budgetCategory,
                }, cancellationToken);

                return(new Result()
                {
                    Data = new IconDto()
                    {
                        IconId = icon.BudgetCategoryIconId,
                        IconKey = icon.IconKey
                    }
                });
            }
Exemplo n.º 6
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var budgetCategory = await _writeDbContext.BudgetCategories
                                     .Include(x => x.BudgetedAmounts)
                                     .FirstOrDefaultAsync(x => x.BudgetedAmounts.Any(s => s.BudgetedAmountId == request.BudgetedAmountId), cancellationToken);

                if (!await _accessControlService.HasBudgetCategoryAccessAsync(budgetCategory.BudgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound));
                }

                var budgetedAmount = budgetCategory.BudgetedAmounts.FirstOrDefault(x => x.BudgetedAmountId == request.BudgetedAmountId);

                budgetCategory.BudgetedAmounts.SetItemValidFromDate(budgetedAmount, request.ValidFrom);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    ReferenceBudgetCategory = budgetCategory,
                    ReferenceBudgetedAmount = budgetedAmount
                }, cancellationToken);

                return(new Result()
                {
                    Data = request.ValidFrom
                });
            }
Exemplo n.º 7
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasBudgetCategoryAccessAsync(request.BudgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound));
                }

                var budgetCategory = await _writeDbContext.BudgetCategories
                                     .FirstOrDefaultAsync(x => x.BudgetCategoryId == request.BudgetCategoryId, cancellationToken);

                var budgetedAmount = budgetCategory.AddBudgetedAmount(request.Amount, request.ValidFrom);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    ReferenceBudgetCategory = budgetCategory,
                    ReferenceBudgetedAmount = budgetedAmount
                }, cancellationToken);

                return(new Result()
                {
                    Data = new BudgetedAmountDto()
                    {
                        Amount = budgetedAmount.Amount,
                        BudgetedAmountId = budgetedAmount.BudgetedAmountId,
                        ValidFrom = budgetedAmount.ValidFrom,
                        ValidTo = budgetedAmount.ValidTo
                    }
                });
            }
Exemplo n.º 8
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasAllocationAccess(request.AllocationId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.AllocationNotFound));
                }
                var allocation = await _writeDbContext.Allocations
                                 .FirstOrDefaultAsync(x => x.AllocationId == request.AllocationId, cancellationToken : cancellationToken)
                                 ?? throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));

                var oldDate = allocation.AllocationDate;

                allocation.SetAllocationDate(request.AllocationDate);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    ReferenceAllocation = allocation,
                    OldAllocationDate   = oldDate,
                    NewAllocationDate   = allocation.AllocationDate
                }, cancellationToken);

                return(new Result()
                {
                    Data = allocation.AllocationDate
                });
            }
Exemplo n.º 9
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var budgetCategoryId = request.BudgetCategoryId;

                if (!await _accessControlService.HasBudgetCategoryAccessAsync(budgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound));
                }

                var budgetCategory = _writeDbContext.BudgetCategories
                                     .First(x => x.BudgetCategoryId == request.BudgetCategoryId);

                var transactionTemplate = TransactionTemplate.Create(request.Description,
                                                                     budgetCategory,
                                                                     request.Amount);

                _writeDbContext.TransactionTemplates.Add(transactionTemplate);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    TransactionTemplate = transactionTemplate
                }, cancellationToken);

                return(new Result()
                {
                    Id = transactionTemplate.TransactionTemplateId
                });
            }
Exemplo n.º 10
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var budget = Domain.Entities.Budget.Create(request.Name, request.StartingDate, Currency.Get(request.CurrencyCode));

                budget.SetOwner(_userContext.UserId);
                _writeDbContext.Budgets.Add(budget);
                await _writeDbContext.SaveChangesAsync(cancellationToken);

                return(new Result()
                {
                    Id = budget.BudgetId
                });
            }
Exemplo n.º 11
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasAllocationAccess(request.AllocationId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));
                }

                if (request.SourceBudgetCategoryId != null && !await _accessControlService.HasBudgetCategoryAccessAsync(request.SourceBudgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound));
                }

                var allocation = await _writeDbContext.Allocations
                                 .FirstAsync(x => x.AllocationId == request.AllocationId, cancellationToken : cancellationToken);

                var oldBudgetCategory = allocation.SourceBudgetCategoryId != null
                                        ? await _writeDbContext.BudgetCategories.FirstAsync(x => x.BudgetCategoryId == allocation.SourceBudgetCategoryId, cancellationToken : cancellationToken)
                                        : null;

                var newBudgetCategory = request.SourceBudgetCategoryId != null
                                        ? await _writeDbContext.BudgetCategories.FirstAsync(x => x.BudgetCategoryId == request.SourceBudgetCategoryId, cancellationToken : cancellationToken)
                                        : null;

                if (oldBudgetCategory != null && newBudgetCategory != null && oldBudgetCategory?.BudgetCategoryType != newBudgetCategory?.BudgetCategoryType)
                {
                    throw new BusinessException(Localization.For(() => ErrorMessages.NotSameBudgetCategoryType));
                }

                var oldCategoryId = allocation.SourceBudgetCategoryId;

                allocation.SetSourceBudgetCategory(newBudgetCategory);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    ReferenceAllocation = allocation,
                    OldBudgetCategoryId = oldCategoryId,
                    NewBudgetCategoryId = allocation.SourceBudgetCategoryId
                }, cancellationToken);
                return(new Result()
                {
                    Id = allocation.SourceBudgetCategoryId
                });
            }
Exemplo n.º 12
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasBudgetAccessAsync(request.BudgetId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetNotFound));
                }

                var stored = await _writeDbContext.Budgets.FirstOrDefaultAsync(x => x.BudgetId == request.BudgetId &&
                                                                               x.OwnerUserId == _userContext.UserId, cancellationToken);

                stored.SetName(request.Name);
                await _writeDbContext.SaveChangesAsync(cancellationToken);

                return(new Result()
                {
                    Data = stored.Name
                });
            }
Exemplo n.º 13
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var targetBudgetCategoryId = request.TargetBudgetCategoryId;

                if (!await _accessControlService.HasBudgetCategoryAccessAsync(targetBudgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetNotFound));
                }

                var sourceBudgetCategoryId = request.SourceBudgetCategoryId;

                if (sourceBudgetCategoryId != null && !await _accessControlService.HasBudgetCategoryAccessAsync(sourceBudgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetNotFound));
                }

                var targetBudgetCategory = _writeDbContext.BudgetCategories
                                           .First(x => x.BudgetCategoryId == request.TargetBudgetCategoryId);
                var sourceBudgetCategory = sourceBudgetCategoryId != null
                                        ? _writeDbContext.BudgetCategories
                                           .First(x => x.BudgetCategoryId == request.SourceBudgetCategoryId)
                                        : null;

                var allocation = Allocation.Create(request.Description,
                                                   targetBudgetCategory,
                                                   sourceBudgetCategory,
                                                   request.Amount,
                                                   request.AllocationDate);

                _writeDbContext.Allocations.Add(allocation);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    ReferenceAllocation = allocation
                }, cancellationToken);

                return(new Result()
                {
                    Id = allocation.AllocationId
                });
            }
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasTransactionTemplateAccess(request.TransactionTemplateId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));
                }

                if (!await _accessControlService.HasBudgetCategoryAccessAsync(request.BudgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound));
                }

                var transactionTemplate = await _writeDbContext.TransactionTemplates
                                          .FirstAsync(x => x.TransactionTemplateId == request.TransactionTemplateId, cancellationToken : cancellationToken);

                var oldBudgetCategory = await _writeDbContext.BudgetCategories
                                        .FirstAsync(x => x.BudgetCategoryId == transactionTemplate.BudgetCategoryId, cancellationToken : cancellationToken);

                var newBudgetCategory = await _writeDbContext.BudgetCategories
                                        .FirstAsync(x => x.BudgetCategoryId == request.BudgetCategoryId, cancellationToken : cancellationToken);

                if (oldBudgetCategory.BudgetCategoryType != newBudgetCategory.BudgetCategoryType)
                {
                    throw new BusinessException(Localization.For(() => ErrorMessages.NotSameBudgetCategoryType));
                }

                transactionTemplate.SetBudgetCategory(newBudgetCategory);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    NewBudgetCategoryId = newBudgetCategory.BudgetCategoryId,
                    OldBudgetCategoryId = oldBudgetCategory.BudgetCategoryId,
                    TransactionTemplate = transactionTemplate
                }, cancellationToken);

                return(new Result()
                {
                    Data = transactionTemplate.BudgetCategoryId
                });
            }
Exemplo n.º 15
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var budgetId = request.BudgetId;

                if (!await _accessControlService.HasBudgetAccessAsync(budgetId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetNotFound));
                }

                var budget = await _writeDbContext.Budgets
                             .FirstOrDefaultAsync(x => x.BudgetId == budgetId &&
                                                  x.OwnerUserId == _userContext.UserId, cancellationToken);

                var icon = await _writeDbContext.BudgetCategoryIcons
                           .FirstOrDefaultAsync(x => x.BudgetCategoryIconId == request.BudgetCategoryIconId, cancellationToken);

                var budgetCategory = Domain.Entities.BudgetCategory.Create(budget, request.Name, icon, request.BudgetCategoryType);

                foreach (var requestBudgetedAmount in request.BudgetedAmounts)
                {
                    budgetCategory.AddBudgetedAmount(requestBudgetedAmount.Amount, requestBudgetedAmount.ValidFrom);
                }
                _writeDbContext.BudgetCategories.Add(budgetCategory);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    ReferenceBudgetCategory = budgetCategory,
                }, cancellationToken);

                return(new Result()
                {
                    Id = budgetCategory.BudgetCategoryId.Value
                });
            }
Exemplo n.º 16
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasTransactionAccess(request.TransactionId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));
                }
                var transaction = await _writeDbContext.Transactions
                                  .FirstOrDefaultAsync(x => x.TransactionId == request.TransactionId, cancellationToken : cancellationToken)
                                  ?? throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));

                transaction.SetAmount(request.Amount);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    Transaction = transaction
                }, cancellationToken);

                return(new Result()
                {
                    Data = transaction.Amount
                });
            }
Exemplo n.º 17
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var allocation = await _writeDbContext.Allocations
                                 .FirstOrDefaultAsync(x => x.AllocationId == request.AllocationId, cancellationToken : cancellationToken)
                                 ?? throw new NotFoundException(Localization.For(() => ErrorMessages.AllocationNotFound));

                if (!await _accessControlService.HasAllocationAccess(allocation.AllocationId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.AllocationNotFound));
                }

                allocation.SoftDelete();

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    ReferenceAllocation = allocation
                }, cancellationToken);

                return(new Result()
                {
                });
            }
Exemplo n.º 18
0
        public async Task CalculateBudgetBalance(BudgetId budgetId, CancellationToken cancellationToken)
        {
            var currency = (await _writeDb.Budgets.FirstOrDefaultAsync(x => x.BudgetId == budgetId, cancellationToken: cancellationToken)).Currency;

            var incomeCategoryIds = _writeDb.BudgetCategories
                                    .Where(x => x.BudgetId == budgetId && x.BudgetCategoryType == eBudgetCategoryType.Income)
                                    .Select(x => x.BudgetCategoryId);

            var incomeSum = _writeDb.Transactions
                            .Include(x => x.SubTransactions)
                            .Where(x => incomeCategoryIds.Any(s => s == x.BudgetCategoryId))
                            .Sum(x => x.Amount.Amount + x.SubTransactions.Sum(s => s.Amount.Amount));

            var spendingCategoryIds = _writeDb.BudgetCategories
                                      .Where(x => x.BudgetId == budgetId && x.BudgetCategoryType == eBudgetCategoryType.Spending)
                                      .Select(x => x.BudgetCategoryId);

            var spendingSum = _writeDb.Transactions
                              .Include(x => x.SubTransactions)
                              .Where(x => spendingCategoryIds.Any(s => s == x.BudgetCategoryId))
                              .Sum(x => x.Amount.Amount + x.SubTransactions.Sum(s => s.Amount.Amount));

            var savingCategoryIds = _writeDb.BudgetCategories
                                    .Where(x => x.BudgetId == budgetId && x.BudgetCategoryType == eBudgetCategoryType.Saving)
                                    .Select(x => x.BudgetCategoryId);

            var savingSum = _writeDb.Transactions
                            .Include(x => x.SubTransactions)
                            .Where(x => savingCategoryIds.Any(s => s == x.BudgetCategoryId))
                            .Sum(x => x.Amount.Amount + x.SubTransactions.Sum(s => s.Amount.Amount));

            var storedBalance = await _writeDb.BudgetBalances.FirstOrDefaultAsync(x => x.BudgetId == budgetId, cancellationToken);

            if (storedBalance == null)
            {
                storedBalance = BudgetBalance.Create(budgetId);
                _writeDb.BudgetBalances.Add(storedBalance);
            }

            var totalBalance  = new MoneyAmount(currency.CurrencyCode, incomeSum - spendingSum - savingSum);
            var incomeTotal   = new MoneyAmount(currency.CurrencyCode, incomeSum);
            var spendingTotal = new MoneyAmount(currency.CurrencyCode, spendingSum);
            var savingTotal   = new MoneyAmount(currency.CurrencyCode, savingSum);

            var categoriesBudgetedLeftoverTotal = 0m;
            var budgetCategoryIds = _writeDb.BudgetCategories
                                    .Where(x => x.BudgetId == budgetId && x.BudgetCategoryType == eBudgetCategoryType.Spending)
                                    .Select(x => x.BudgetCategoryId)
                                    .ToList();

            foreach (var budgetCategoryId in budgetCategoryIds)
            {
                categoriesBudgetedLeftoverTotal += Math.Max(0, (await GetCategoryBalance(budgetCategoryId, null, null, cancellationToken)).Amount);
            }

            var unassignedFunds = new MoneyAmount(currency.CurrencyCode, totalBalance.Amount - categoriesBudgetedLeftoverTotal);

            storedBalance.Update(totalBalance, unassignedFunds, spendingTotal, incomeTotal, savingTotal);

            await _writeDb.SaveChangesAsync(cancellationToken);
        }