public async Task <PluginResult> Handle(BotMessage message, CommandItems commandItems, CancellationToken token) { var chatId = message.ChatId; StringBuilder result = new(); 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)); result.AppendLine(); } var balance = await _accountantService.GetBalance(user.Id, token); result.AppendLine(AccountantText.RemainingBalance(balance)); return(PluginResult.Success(result.ToString())); }
public async Task <PluginResult> Handle(BotMessage message, CommandItems commandItems, CancellationToken cancellationToken) { var chatId = message.ChatId; StringBuilder result = new(); var user = await _userService.GetUserAsync(chatId, cancellationToken); if (user == null) { var sourceAdapter = _sourceAdapterFactory.GetAdapter(message.SourceId); var name = await sourceAdapter.GetName(message.ChatId, cancellationToken); user = await _userService.AddUserAsync(new AddUserRequest { ChatId = chatId, Name = name }, cancellationToken); result.AppendLine(AccountantText.WelcomeUser(name)); result.AppendLine(); } var from = GetCurrentMonthDate(10); var to = from.AddMonths(1); var transactions = await _accountantService .GetTransactions(user.Id, null as Guid?, from, to, cancellationToken); if (!transactions.Any()) { result.AppendLine(AccountantText.NoTransactionsFound); } else { var categories = (await _accountantService.GetCategories(user.Id, cancellationToken)) .ToDictionary(c => c.Id, c => c); var groupedTransactions = transactions .GroupBy(tr => GetCurrentDateTime(tr.CreatedAt).Date); foreach (var group in groupedTransactions) { if (group.Count() > 1) { result.AppendLine($"{group.Key:dd.MM}"); foreach (var transaction in group) { GetShortTransaction(transaction, result, categories); } } else { result.Append($"{group.Key:dd.MM} "); GetShortTransaction(group.First(), result, categories); } } } return(PluginResult.Success(result.ToString())); }
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())); }