public void ComputeHash_OnMyMachine_SameResult()
        {
            // Asserts that ComputeHash for a Transaction returns the expected result.
            // This test is to protect against machine / framework variations and changes, as well as code changes

            // arrange
            const string expected = "E33CAA7C7F181C2A421293E27AD6EB96D121DAA9AEFA21F80D3B1FBF7279F4A0";
            var          command  = new CreditAccountCommand
            {
                Id                  = Guid.NewGuid(),
                AccountNumber       = "1234567890123456",
                Amount              = 1234.56m,
                AuthorizationCode   = "abcd1234",
                CreditAmount        = 1234.56m,
                Filename            = $"{Guid.NewGuid()}.csv",
                MerchantId          = "a1b2c3d4e5f6",
                TransactionDateTime = new DateTime(2019, 9, 8, 11, 45, 0, DateTimeKind.Utc),
                TransactionId       = "1000123456"
            };

            // act
            string actual = command.ComputeHash();

            // assert
            Assert.AreEqual(expected, actual, true);
        }
        public static Transaction MapToTransaction(CreditAccountCommand command)
        {
            var transaction = MapToTransaction((TransactionCommand)command);

            transaction.Amount  = command.CreditAmount;
            transaction.IsDebit = false;
            return(transaction);
        }
Exemple #3
0
        private static TransactionCommand ParseLineToCommand(ILogger log, string filename, int lineNumber, string line)
        {
            // id,acc_number,date_time,amount,merchant,authorization
            const int IdField            = 0;
            const int AccountNumberField = 1;
            const int DateTimeField      = 2;
            const int AmountField        = 3;
            const int MerchantField      = 4;
            const int AuthorizationField = 5;

            string[] fields = line.Split(',');

            if (!decimal.TryParse(fields[AmountField].Replace("$", ""), out decimal amount))
            {
                string errorMessage = $"Could not parse amount to decimal: line #{lineNumber} field #{AmountField} \"{fields[AmountField]}\"";
                log.LogError(errorMessage);
                throw new InvalidOperationException(errorMessage);
            }

            if (!DateTime.TryParse(fields[DateTimeField], out DateTime dateTime))
            {
                string errorMessage = $"Could not parse date_time to DateTime: line #{lineNumber} field #{DateTimeField} \"{fields[DateTimeField]}\"";
                log.LogError(errorMessage);
                throw new InvalidOperationException(errorMessage);
            }

            TransactionCommand command;

            if (amount >= 0)
            {
                // Credit
                command = new CreditAccountCommand
                {
                    CreditAmount = amount
                };
            }
            else
            {
                // Debit
                command = new DebitAccountCommand
                {
                    DebitAmount = Math.Abs(amount)
                };
            }

            command.Id                  = Guid.NewGuid();
            command.Filename            = filename;
            command.AccountNumber       = fields[AccountNumberField];
            command.AuthorizationCode   = fields[AuthorizationField];
            command.MerchantId          = fields[MerchantField];
            command.TransactionDateTime = dateTime;
            command.Amount              = amount;
            command.TransactionId       = fields[IdField];

            return(command);
        }
        public ICommandResult Handle(CommandContext context, CreditAccountCommand command)
        {
            var account = context.GetById <BankAccount>(command.Id);

            account.CreditAccount(command.Amount);

            context.Finalize(account);

            return(CommandResult.Successful);
        }
        public async Task <Response <Guid> > Handle(CreditAccountCommand request, CancellationToken cancellationToken)
        {
            if (request.Invalid)
            {
                return(new Response <Guid>(request.Notifications.Select(p => p.Message)));
            }

            // Get Account from Database
            var account = await _accountReadRepository.GetAccountAsync(request.BankId, request.Number, request.Agency);

            account.Credit(request.Amount);

            // Create Account Transaction
            var transactionId = await _transactionWriteRepository.CreateAsync(
                new Transaction(account.Id, request.Amount, Domain.Enums.OperationType.Credit)
                );

            // Update Account overbalance
            await _accountWriteRepository.UpdateAsync(account);

            return(new Response <Guid>(transactionId));
        }
Exemple #6
0
        protected override async Task <bool> Handle(string message)
        {
            try
            {
                var transaction = Newtonsoft.Json.JsonConvert.DeserializeObject <Transaction>(message);
                var commandBus  = Startup.ServiceProvider.GetService <ICommandBus>();

                var command = new CreditAccountCommand(new AccountId("account-439fa12b-18ac-4c82-87c6-ddc74f591284"),
                                                       transaction);

                var result = await commandBus.PublishAsync(command, CancellationToken.None);
            }
            catch (Exception exception)
            {
                _producer.Send("transactionExchange",
                               $"Message: {message} \nException:{exception.Message}",
                               "transactions.failed",
                               "transactions.failed");
                return(false);
            }

            return(true);
        }
        public void Handle(CreditAccountCommand message)
        {
            Console.WriteLine("Debiting account message received");

            Host.GetCommandBus().Send(message);
        }