public async Task <Result> SubtractMoney(int counterpartyAccountId, PaymentCancellationData data, UserInfo user) { return(await GetCounterpartyAccount(counterpartyAccountId) .Ensure(a => AreCurrenciesMatch(a, data), "Account and payment currency mismatch") .Ensure(IsAmountPositive, "Payment amount must be a positive number") .BindWithLock(_locker, a => Result.Success(a) .BindWithTransaction(_context, account => Result.Success(account) .Map(SubtractMoney) .Map(WriteAuditLog)))); bool IsAmountPositive(CounterpartyAccount account) => data.Amount.IsGreaterThan(decimal.Zero); async Task <CounterpartyAccount> SubtractMoney(CounterpartyAccount account) { account.Balance -= data.Amount; _context.Update(account); await _context.SaveChangesAsync(); return(account); } async Task <CounterpartyAccount> WriteAuditLog(CounterpartyAccount account) { var eventData = new CounterpartyAccountBalanceLogEventData(null, account.Balance); await _auditService.Write(AccountEventType.CounterpartySubtract, account.Id, data.Amount, user, eventData, null); return(account); } }
private bool AreCurrenciesMatch(CounterpartyAccount account, PaymentCancellationData data) => account.Currency == data.Currency;
public async Task <IActionResult> SubtractCounterpartyAccount(int counterpartyAccountId, [FromBody] PaymentCancellationData cancellationData) { var(_, _, administrator, _) = await _administratorContext.GetCurrent(); var(isSuccess, _, error) = await _counterpartyAccountService.SubtractMoney(counterpartyAccountId, cancellationData, administrator.ToUserInfo()); return(isSuccess ? NoContent() : (IActionResult)BadRequest(ProblemDetailsBuilder.Build(error))); }