Exemple #1
0
 public Transaction(Inbound.AddTransactionRequest request)
 {
     Cost       = request.Cost;
     Comment    = request.Comment;
     UserId     = request.UserId;
     CategoryId = request.CategoryId;
     CreatedAt  = DateTime.UtcNow;
 }
Exemple #2
0
 public async Task AddTransaction(Inbound.AddTransactionRequest request, CancellationToken cancellationToken)
 {
     _unitOfWork.Transactions.RegisterNew(new Transaction(request));
     await _unitOfWork.Complete(cancellationToken);
 }
Exemple #3
0
            public async Task <PluginResult> Handle(BotMessage message, CommandItems commandItems,
                                                    CancellationToken token)
            {
                var           text   = message.Text;
                var           chatId = message.ChatId;
                StringBuilder result = new();

                var cost = commandItems.GetDecimal("cost");
                var user = await _userService.GetUserAsync(chatId, token);

                if (user == null)
                {
                    var sourceAdapter = _sourceAdapterFactory.GetAdapter(message.SourceId);
                    var name          = await sourceAdapter.GetName(message.ChatId, token);

                    user = await _userService.AddUserAsync(new AddUserRequest { ChatId = chatId, Name = name }, token);

                    result.AppendLine(AccountantText.WelcomeUser(name));
                }

                var comment         = commandItems.GetString("comment");
                var categoryName    = commandItems.GetString("category");
                var categoryExpense = await _accountantService.GetExpenseCategory(user.Id, categoryName, token);

                var categoryIncome = await _accountantService.GetIncomeCategory(user.Id, categoryName, token);

                var from = GetCurrentMonthDate(10);
                var to   = from.AddMonths(1);

                if (categoryExpense == null && categoryIncome == null)
                {
                    categoryExpense = await _accountantService.AddCategory(new Inbound.AddExpenseCategoryRequest
                    {
                        UserId = user.Id,
                        Name   = categoryName
                    }, token);

                    result.AppendLine(AccountantText.AddedNewCategory(categoryName));
                }

                if (categoryExpense != null)
                {
                    var expense = new Inbound.AddTransactionRequest
                    {
                        Cost       = cost,
                        Comment    = comment,
                        CategoryId = categoryExpense.Id,
                        UserId     = user.Id
                    };
                    await _accountantService.AddTransaction(expense, token);

                    var categoryBalance =
                        await _accountantService.GetBalance(user.Id, categoryExpense.Id, from, to, token);

                    result.AppendLine(
                        AccountantText.AddedNewExpenseTransaction(categoryName, from, to, categoryBalance));
                }
                else if (categoryIncome != null)
                {
                    var income = new Inbound.AddTransactionRequest()
                    {
                        Cost       = cost,
                        Comment    = comment,
                        CategoryId = categoryIncome.Id,
                        UserId     = user.Id
                    };
                    await _accountantService.AddTransaction(income, token);

                    var categoryBalance =
                        await _accountantService.GetBalance(user.Id, categoryIncome.Id, from, to, token);

                    result.AppendLine(
                        AccountantText.AddedNewIncomeTransaction(categoryName, from, to, categoryBalance));
                }

                var balance = await _accountantService.GetBalance(user.Id, token);

                result.AppendLine();
                result.AppendLine(AccountantText.RemainingBalance(balance));

                return(PluginResult.Success(result.ToString()));
            }