Example #1
0
        private async Task <decimal?> UpdateCustomerNetWorthAsync(CustomerOrderDTO orderDTO)
        {
            var customer = await db.Customers.FindAsync(orderDTO.CustomerId);

            if (customer == null)
            {
                throw new Exception(String.Format("Customer with id {0} not found while updating its networth", orderDTO.CustomerId));
            }
            customer.NetWorth       += orderDTO.CustomerBillingSummary.PayAmount;
            db.Entry(customer).State = EntityState.Modified;
            return(customer.NetWorth);
        }
Example #2
0
        public async Task <IHttpActionResult> Post(CustomerOrderDTO orderDetail)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (orderDetail == null)
            {
                return(BadRequest("OrderDetails should not have been null while placing the customer order"));
            }
            decimal deductWalletAmount = 0;

            //TODO: Verify pay amount.
            try
            {
                await UpdateStockOfProductsAsync(orderDetail.ProductsConsumed);

                deductWalletAmount = await ComputeTransactionAmountAsync(orderDetail);

                var customerOrder  = CreateNewCustomerOrder(orderDetail, deductWalletAmount);
                var transactionDTO = new CustomerTransactionDTO()
                {
                    CustomerId        = orderDetail.CustomerId,
                    TransactionAmount = Math.Abs(deductWalletAmount),
                    IsCredit          = deductWalletAmount > 0 ? true : false,
                    Description       = customerOrder.CustomerOrderNo,
                };
                var transaction = await transactionDTO.CreateNewTransactionAsync(db);

                var customerOrderTransaction = CreateNewCustomerOrderTransaction(customerOrder, transaction);

                await AddIntoCustomerOrderProductAsync(orderDetail.ProductsConsumed, customerOrder.CustomerOrderId);

                //Data Analytics call
                await UpdateCustomerNetWorthAsync(orderDetail);

                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                throw;
            }
            catch (Exception e)
            { return(BadRequest(e.ToString())); }
            return(Ok(deductWalletAmount));
        }