public async Task <bool> Handle(InvestInvestmentToolToAccountCommand request, CancellationToken cancellationToken) { //Notify for reporting var accountBalanceChangedIntegrationEvent = new AccountBalanceChangedIntegrationEvent(); accountBalanceChangedIntegrationEvent.EffectedAccountIds.Add(Guid.Parse(request.AccountId)); await _imsIntegrationEventService.AddAndSaveEventAsync(accountBalanceChangedIntegrationEvent); var transaction = new AccountTransaction(Guid.Parse(request.AccountId), request.TransactionTypeId, request.TransactionDate, request.Amount, request.Rate ?? 1); _logger.LogInformation("----- Creating Transaction - Transaction: {@Transaction}", transaction); _accountTransactionRepository.Add(transaction); return(await _accountTransactionRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken)); }
public async Task <bool> Handle(BuyInvestmentToolCommand request, CancellationToken cancellationToken) { //Start source account process var sourceAccount = await _accountRepository.FindOrDefaultAsync(Guid.Parse(request.SourceAccountId)); if (sourceAccount == null || !sourceAccount.CheckBalanceForWithdraw(request.Amount * request.Rate ?? 1)) { return(false); } var accountBalanceChangedIntegrationEvent = new AccountBalanceChangedIntegrationEvent(); accountBalanceChangedIntegrationEvent.EffectedAccountIds.Add(Guid.Parse(request.SourceAccountId)); var transactionForSourceAccount = new AccountTransaction(Guid.Parse(request.SourceAccountId), TransactionType.PullInvestmentToolFromAccount.EnumId, request.TransactionDate, request.Amount * request.Rate ?? 1, request.Rate ?? 1); _logger.LogInformation("----- Creating Transaction - Transaction: {@Transaction}", transactionForSourceAccount); _accountTransactionRepository.Add(transactionForSourceAccount); //Start destination account process var destinationAccount = await _accountRepository.FindOrDefaultAsync(Guid.Parse(request.DestinationAccountId)); if (destinationAccount == null) { return(false); } accountBalanceChangedIntegrationEvent.EffectedAccountIds.Add(Guid.Parse(request.DestinationAccountId)); await _imsIntegrationEventService.AddAndSaveEventAsync(accountBalanceChangedIntegrationEvent); var transactionForDestinationAccount = new AccountTransaction(Guid.Parse(request.DestinationAccountId), TransactionType.BuyInvestmentTool.EnumId, request.TransactionDate, request.Amount, request.Rate ?? 1); _logger.LogInformation("----- Creating Transaction - Transaction: {@Transaction}", transactionForDestinationAccount); _accountTransactionRepository.Add(transactionForDestinationAccount); return(await _accountTransactionRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken)); }