Beispiel #1
0
        public async Task <Transaction> UpdateAsync(UpdateTransaction update, Action <UpdateTransactionOptions>?setOpts = null)
        {
            var tran = await _repo.GetAsync(update.Id, true);

            if (tran.Amount is AmountIncomeCompleted || tran.Amount is AmountOutcomeCompleted)
            {
                var options = new UpdateTransactionOptions();
                setOpts?.Invoke(options);
                if (options.FailOnUpdateDone)
                {
                    throw new WPayException(TransactionErrors.AlreadyCompleted);
                }
                return(tran);
            }
            var mergedTran = new Calculator().UpdateToTransaction(tran, update);
            var account    = await _repo.GetAsync(tran.AccountId, true);

            var balance = mergedTran.Amount switch
            {
                AmountOutcomeCompleted _ => account.Balance,
                AmountOutcomeCancelled am => account.Balance + am,
                _ => throw new InvalidOperationException("Invalid state")
            };

            account = new Account(account.Id, balance, account.Locked);
            try
            {
                await _repo.UpdateAsync(account);

                await _repo.UpdateAsync(mergedTran.Id, mergedTran.Amount, mergedTran.Metadata, mergedTran.Description, mergedTran.UpdatedOn);

                return(mergedTran);
            }
            catch (TransactionNotFoundException)
            {
                var info = new Dictionary <string, string>()
                {
                    ["Id"] = update.Id.ToString()
                };
                throw new WPayException(TransactionErrors.NotExist, info);
            }
        }
Beispiel #2
0
     public static TransactionEvent From(Transaction transaction)
     {
         var(amountType, amountValue, amountCurrency) = transaction.Amount switch
         {
             AmountIncomeCompleted am => (_completedIncome, am.Amount.ToString(), am.Amount.Currency().Code()),
             AmountOutcomeProcessing am => (_processingOutcome, am.Amount.ToString(), am.Amount.Currency().Code()),
             AmountOutcomeCancelled am => (_cancelledOutcome, am.Amount.ToString(), am.Amount.Currency().Code()),
             AmountOutcomeCompleted am => (_completedOutcome, am.Amount.ToString(), am.Amount.Currency().Code()),
             _ => throw new Exception("Invalid amount")
         };
         return(new TransactionEvent
         {
             Id = transaction.Id.Value.Value,
             AccountId = transaction.AccountId.Value.Value,
             Label = transaction.Label.Value,
             Metadata = transaction.Metadata.Value,
             Description = transaction.Description.Value,
             AmountType = amountType,
             AmountCurrency = amountCurrency,
             AmountValue = amountValue
         });
     }
 }