public async Task <ActionResult> AddUserRevenue(IncomingRevenueData incomingRevenue)
        {
            //If Revenue Amount equals 0, return BadRequest. Revenues must be greater than 0.
            if (incomingRevenue.RevenueAmount == 0)
            {
                return(BadRequest("Amount must be greater than 0."));
            }

            //Else, get User ID from Claims
            var userId = int.Parse(User.Claims.FirstOrDefault(claim => claim.Type == "ID").Value);

            //Create new Revenue
            var newRevenue = new Revenue
            {
                RevenueCategory = incomingRevenue.RevenueCategory,
                RevenueName     = incomingRevenue.RevenueName,
                RevenueDate     = incomingRevenue.RevenueDate,
                RevenueAmount   = incomingRevenue.RevenueAmount,
                UserID          = userId
            };

            //If Revenue is recurring, create new Recurring Transaction and project it.
            if (incomingRevenue.RecurringFrequency != "One Time")
            {
                var newRecurringTransaction = new RecurringTransaction
                {
                    TransactionType     = "Revenue",
                    TransactionCategory = incomingRevenue.RevenueCategory,
                    TransactionName     = incomingRevenue.RevenueName,
                    FirstPaymentDate    = incomingRevenue.RevenueDate,
                    TransactionAmount   = incomingRevenue.RevenueAmount,
                    RecurringFrequency  = incomingRevenue.RecurringFrequency,
                    UserID = userId
                };

                _context.RecurringTransactions.Add(newRecurringTransaction);

                await _context.SaveChangesAsync();

                RecurringTransactionManager.ProjectIndividualPayment(newRecurringTransaction);

                newRevenue.RecurringTransactionID = newRecurringTransaction.ID;
            }
            //Else, save One Time Revenue record
            else
            {
                _context.Revenues.Add(newRevenue);

                await _context.SaveChangesAsync();
            }

            //Return new Revenue
            return(new ContentResult()
            {
                Content = JsonConvert.SerializeObject(newRevenue),
                ContentType = "application/json",
                StatusCode = 201
            });
        }
Beispiel #2
0
        private void PerformMaintenance(object state)
        {
            _logger.LogInformation("Background Service is executing.");

            //Remove expired demo user accounts
            //tokens are valid for 10 hours so only remove accounts for which tokens have expired
            var expirationTime = DateTime.Now.AddHours(-10);

            var accountsToDelete = _context.Users
                                   .Where(user => user.IsDemoAccount == true &&
                                          user.AccountCreatedTime < expirationTime);

            _context.Users.RemoveRange(accountsToDelete);

            _context.SaveChanges();

            //Project Recurring Payments in the system
            RecurringTransactionManager.ProjectAllPayments();
        }
Beispiel #3
0
        public async Task <ActionResult> UpdateUsersRecurringTransaction(RecurringTransaction transactionToUpdate)
        {
            //If Recurring Transaction Amount equals 0, return BadRequest. Recurring Transactions must be greater than 0
            if (transactionToUpdate.TransactionAmount == 0)
            {
                return(BadRequest("Amount must be greater than 0."));
            }

            //Else, set state of incoming entry to "Modified"
            _context.Entry(transactionToUpdate).State = EntityState.Modified;

            //Save changes
            await _context.SaveChangesAsync();

            //delete previous projections
            if (transactionToUpdate.TransactionType == "Revenue")
            {
                var transactionsToDelete = _context.Revenues.Where(rev => rev.RecurringTransactionID == transactionToUpdate.ID).ToList();
                _context.Revenues.RemoveRange(transactionsToDelete);
            }
            else
            {
                var transactionsToDelete = _context.Expenses.Where(exp => exp.RecurringTransactionID == transactionToUpdate.ID).ToList();
                _context.Expenses.RemoveRange(transactionsToDelete);
            }

            //Save changes
            await _context.SaveChangesAsync();

            //Re-project based on updated recurring transaction entry
            RecurringTransactionManager.ProjectIndividualPayment(transactionToUpdate);

            //Return updated Recurring Transaction
            return(new ContentResult()
            {
                Content = JsonConvert.SerializeObject(transactionToUpdate),
                ContentType = "application/json",
                StatusCode = 200
            });
        }