public async Task AddTransactionAsync(AddTransaction command)
        {
            var results = addTransactionValidator.Validate(command);

            if (!results.IsValid)
            {
                throw new ArgumentException(results.ToString("~"));
            }

            var user = await userRepository.GetFirstUserAsync();

            if (command.Type == ExtendedTransactionType.Transfer)
            {
                if (command.Amount > 0)
                {
                    command.Amount *= (-1);
                }
                if (command.TransferAmount < 0)
                {
                    command.TransferAmount *= (-1);
                }
                var transactionExpense = Transaction.Create(command.AccountId, TransactionType.Expense, command.Amount, command.Date, command.Name, command.Comment, user.Id, false, command.CategoryId, command.ExternalId, command.Latitude, command.Longitude);
                await transactionRepository.AddTransactionAsync(transactionExpense);

                var transactionIncome = Transaction.Create(command.TransferAccountId.Value, TransactionType.Income, command.TransferAmount, command.TransferDate, command.Name, command.Comment, user.Id, false, command.CategoryId, command.TransferExternalId, command.Latitude, command.Longitude);
                await transactionRepository.AddTransactionAsync(transactionIncome);

                var transfer = Transfer.Create(transactionExpense.Id, transactionIncome.Id, command.Rate);
                await transactionRepository.AddTransferAsync(transfer);

                await this.CreateTagsToTransaction(command.Tags, transactionExpense.Id);

                await this.MergeFiles(command.FileGuid, transactionExpense);
            }
            else
            {
                var type = command.Type.ToTransactionType();
                if (type == TransactionType.Expense && command.Amount > 0)
                {
                    command.Amount *= (-1);
                }
                else if (type == TransactionType.Income && command.Amount < 0)
                {
                    command.Amount *= (-1);
                }
                var transaction = Transaction.Create(command.AccountId, type, command.Amount, command.Date, command.Name, command.Comment, user.Id, false, command.CategoryId, command.ExternalId, command.Latitude, command.Longitude);
                await this.transactionRepository.AddTransactionAsync(transaction);

                await this.CreateTagsToTransaction(command.Tags, transaction.Id);

                await this.MergeFiles(command.FileGuid, transaction);
            }
        }
Exemplo n.º 2
0
        private async Task <int> HandleTransactions(Transaction transaction, TransactionType transactionType)
        {
            using var transactionScope = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeAsyncFlowOption.Enabled);
            var numberOfRowsAffected = 0;
            var account = await _accountRepository.GetAccountAsync(transaction.Account_Code);

            if (transactionType == TransactionType.Insert)
            {
                transaction.Capture_Date = DateTime.Now;
                numberOfRowsAffected     = await _transactionRepository.AddTransactionAsync(transaction);

                account.Outstanding_Balance += transaction.Amount;
            }
            else if (transactionType == TransactionType.Update)
            {
                var outDatedTransaction = await _transactionRepository.GetTransaction(transaction.Code);

                numberOfRowsAffected = await _transactionRepository.UpdateTransactionAsync(transaction);

                account.Outstanding_Balance -= outDatedTransaction.Amount;
                account.Outstanding_Balance += transaction.Amount;
            }
            else if (transactionType == TransactionType.Delete)
            {
                account.Outstanding_Balance -= transaction.Amount;
            }

            await _accountRepository.UpdateAccountAsync(account);

            transactionScope.Complete();
            return(numberOfRowsAffected);
        }
Exemplo n.º 3
0
        public async Task <Transaction> AddTransactionAsync(Transaction transaction)
        {
            // It only adds the transaction if it's valid
            //if (!CheckTransaction(transaction))
            //    return null;

            await _transactions.AddTransactionAsync(transaction);

            _logger?.LogInformation($"Transaction added: {transaction.Id}");
            _logger?.LogDebug($"Transaction added: {JsonConvert.SerializeObject(transaction, _jsonSettings)}");

            return(transaction);
        }
Exemplo n.º 4
0
        public async Task <bool> AddTransactionAsync(Transaction transaction, int portfolioId)
        {
            // this probably needs to be redesigned to be wrapped in a database transaction
            try
            {
                int securityId;
                int holdingId;

                securityId = await securityRepository.CheckSecurityExists(transaction.Symbol);

                if (securityId is 0)
                {
                    securityId = await securityRepository.AddSecurity(transaction.Symbol, transaction.Description);
                }

                await transactionRepository.AddTransactionAsync(transaction, securityId);

                holdingId = await portfolioRepository.CheckIfHoldingInPortfolio();

                var position = new PortfolioPositionDto()
                {
                    PortfolioId = portfolioId,
                    SecurityId  = securityId,
                    Shares      = transaction.Quantity
                };

                if (holdingId is 0)
                {
                    await portfolioRepository.AddPortfolioPosition(position);
                }
                else
                {
                    await portfolioRepository.UpdatePortfolioPosition(position, transaction.Action, holdingId);
                }

                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Exemplo n.º 5
0
        public async Task <Transaction> ValidateTransaction(object records, string key)
        {
            IEnumerable <Record> referenceRecords = null;

            if (records is JArray)
            {
                referenceRecords = recordBuilder.ValidateRecords((JArray)records);
            }
            else if (records is IEnumerable <Record> )
            {
                referenceRecords = (IEnumerable <Record>)records;
            }
            else if (records is Record)
            {
                referenceRecords = new[] { (Record)records };
            }
            else
            {
                return(null);
            }

            DateTime date = DateTime.UtcNow;

            var recordValue     = referenceRecords.Select(s => s.Value.ToString()).ToList();
            var concatenateData = Serializer.ConcatenateData(recordValue);
            var transactionHash = Serializer.ComputeHash(concatenateData.ToHexString());

            string         secret = null;
            ECKeyValidator eckey  = new ECKeyValidator();

            try
            {
                secret = File.ReadAllText("/DataChain/privKey/key.xml", UTF8Encoding.UTF8);
            }
            catch (FileNotFoundException)
            {
                return(null);
            }
            catch (IOException)
            {
                return(null);
            }

            string sign = null;

            sign = eckey.SignData(concatenateData);

            if (!eckey.VerifyMessage(concatenateData, sign, key))
            {
                throw new InvalidTransactionException("Signature is not valid");
            }

            SignatureEvidence auth = new SignatureEvidence(new HexString(sign.ToHexString()), new HexString(key.ToHexString()));

            byte[] rawSign = SerializeSignature(auth);


            List <Transaction> tx_list     = new List <Transaction>();
            Transaction        transaction = null;

            tx_list.AddRange(await Task.WhenAll(referenceRecords.Select(async rec =>
            {
                await Task.Delay(100);
                transaction = new Transaction(date, referenceRecords, new HexString(transactionHash),
                                              new HexString(sign.ToHexString()),
                                              new HexString(key.ToHexString()));

                return(transaction);
            }
                                                                        )));

            try
            {
                await txSubscribe.AddTransactionAsync(tx_list);
            }

            catch (InvalidTransactionException)
            {
                return(null);
            }

            try
            {
                await recordBuilder.PostRecordsAsync(referenceRecords);
            }
            catch (Exception)
            {
                return(null);
            }

            return(transaction);
        }