public IHttpActionResult AddCustomer(int mvanumber, Customer c)
        {
            c.MvaNumber = mvanumber;
            c = new CustomerRepository().AddOrUpdateCustomer(c);

            return Ok(c);
        }
        public IHttpActionResult UpdateCustomer(int mvanumber, Customer c)
        {
            if (c.InternalID == null)
            {
                return BadRequest();
            }

            c.MvaNumber = mvanumber;
            c = new CustomerRepository().AddOrUpdateCustomer(c);

            return Ok(c);
        }
        public Customer AddOrUpdateCustomer(Customer c)
        {
            Guid id = c.InternalID ?? Guid.NewGuid();

            var ent = new CustomerTableEntity(id.ToString(),c.MvaNumber)
            {
                Name = c.Name,
                MvaNumber = c.MvaNumber,
                CustomerNumber = c.CustomerNumber,
                ActiveAccount = c.ActiveAccount,
                ExternalIDs = string.Join(Constants.MultiStringSeparator, c.ExternalIDs),
                IncludedTransactionsJSON = c.IncludedTransactions == null ? null : JsonConvert.SerializeObject(c.IncludedTransactions)
            };

            TableOperation op = TableOperation.InsertOrMerge(ent);
            CustomerTable.Execute(op);

            ClearCacheForMva(c.MvaNumber.Value);

            c.InternalID = id;

            return c;
        }
Example #4
0
        private static void AddTestCustomers(int customerCount)
        {
            var repo = new CustomerRepository();

            for (int i = 1; i <= customerCount; i++)
            {
                Customer c = new Customer()
                {
                    Name = $"Test {i}",
                    ActiveAccount = true,
                    InternalID = Guid.NewGuid(),
                    CustomerNumber = i,
                    ExternalIDs = new List<string> { i.ToString(), $"Test{i}"},
                    MvaNumber = (i / 10 + 1)
                };
                repo.AddOrUpdateCustomer(c);
            }
        }
        private static async Task ProcessTransactions(SearchRepository sRepo, TransactionRepository tRepo, Customer customer, string productID, int? dateVal)
        {
            //any transaction newer than 'dtLimit' should not be processed because it's kept as an individual search item
            DateTime dtLimit = DateTime.UtcNow.Date.AddDays(-Constants.DaysToKeepTransactions);
            
            int curDateVal;
            if(dateVal.HasValue)
            {
                curDateVal = dateVal.Value;
            }
            else
            {
                curDateVal = int.Parse(dtLimit.AddDays(-1).ToString(Constants.DateStringFormat));
            }

            var existingItems = sRepo.GetTotalsItemsForCustomer(customer.InternalID.Value, productID, curDateVal);
            HashSet<string> existingItemIDs = new HashSet<string>(existingItems.Select(e => e.DocUniqueID));

            Dictionary<string, int> productTotals = new Dictionary<string, int>();

            var res =await  tRepo.GetTransactionsForCustomer(customer.InternalID.Value, curDateVal);
            if(!string.IsNullOrWhiteSpace(productID))
            {
                res = res.Where(r => r.ProductID == productID);
            }
            
            res = res.Where(r => r.DateTime < dtLimit);

            foreach (var transaction in res)
            {
                if (productTotals.ContainsKey(transaction.ProductID))
                {
                    productTotals[transaction.ProductID] = productTotals[transaction.ProductID] + transaction.Amount.Value;
                }
                else
                {
                    productTotals.Add(transaction.ProductID, transaction.Amount.Value);
                }
            }

            var searchItems = new List<TotalsSearchItem>();
            foreach (var entry in productTotals)
            {
                var newItem = new TotalsSearchItem
                {
                    DocUniqueID = TotalsSearchItem.CreateUID(curDateVal, entry.Key, customer.InternalID.Value),
                    ClientInternalID = customer.InternalID.Value.ToString(),
                    CustomerNumber = customer.CustomerNumber,
                    Date = curDateVal,
                    MvaNumber = customer.MvaNumber,
                    ProductID = entry.Key,
                    Amount = entry.Value
                };

                searchItems.Add(newItem);
                existingItemIDs.Remove(newItem.DocUniqueID);
            }

            sRepo.AddOrUpdateTotalsItem(searchItems.ToArray());
            tRepo.AddOrUpdateTransactionTotalsItem(searchItems.Select(item => new TotalsItemTableEntity(item)).ToArray());

            //in case a transaction was deleted, remove all totals items from search index that are no longer valid
            sRepo.DeleteTotalsItemWithID(existingItemIDs.ToArray());
            tRepo.DeleteTransactionTotalsItemWithID(existingItemIDs.ToArray());
        }
        public void DeleteCustomer(Customer c)
        {
            var tableEnt = new CustomerTableEntity(c.InternalID.Value.ToString(),c.MvaNumber);
            tableEnt.ETag = "*";
            var op = TableOperation.Delete(tableEnt);
            CustomerTable.Execute(op);

            if (c.MvaNumber.HasValue) ClearCacheForMva(c.MvaNumber.Value);
        }