/// <summary>
        /// Look for <see cref="Data.Models.Common.CommonAccount" /> in cache. If found, return it. If not found,
        /// load it from the database and cache it.
        /// </summary>
        /// <param name="accountNumber"></param>
        /// <returns></returns>
        public async Task <CachedAccount> GetCachedAccountAsync(long accountNumber)
        {
            var cacheKey = WebCacheKey.CommonAccount(accountNumber);

            // Try to pull it from the cache first.
            var cachedAccount = await _cache.GetWithSlidingExpirationAsync <CachedAccount>(cacheKey, _commonAccountCacheExpiry);

            if (cachedAccount != null)
            {
                return(cachedAccount);
            }

            // Not in cache. Check the database.
            var commonAccount = await _commonCtx.CommonAccounts.AsNoTracking()
                                .Include(ca => ca.Partition)
                                .Include(ca => ca.Settings)
                                .Include(ca => ca.City)
                                .Include(ca => ca.CommonAccountViolationTypes)
                                .SingleOrDefaultAsync(ca => ca.Number == accountNumber);

            if (commonAccount == null)
            {
                // Account not found.
                return(null);
            }

            cachedAccount = Mapper.Map <CachedAccount>(commonAccount);

            var commonViolationTypes = await _commonCtx.CommonAccountViolationTypes.Where(m => m.AccountId == commonAccount.Id).Select(m => m.ViolationTypeId).ToListAsync();

            var accountCtx     = ContextsUtility.CreateAccountContext(Cryptography.Decrypt(commonAccount.Partition.ConnectionString));
            var violationTypes = await accountCtx.ViolationTypes.ForAccount(commonAccount.Id).Where(m => commonViolationTypes.Contains(m.CommonViolationTypeId)).ToListAsync();


            cachedAccount.ViolationTypes = Mapper.Map <ViolationTypeModel[]>(violationTypes);


            // Found it. Add it to the cache.
            await _cache.SetAsync(cacheKey, cachedAccount, _commonAccountCacheExpiry);

            return(cachedAccount);
        }
Exemple #2
0
        public async Task <IActionResult> PurgeCache()
        {
            var accountUsers = await CommonContext.UserAccounts.Include(m => m.Account).AsNoTracking().ToListAsync();

            var cachKeys = new List <string>();

            var accounts = accountUsers.Select(m => m.Account).Distinct();

            //CommonAccounts
            foreach (var account in accounts)
            {
                cachKeys.Add(WebCacheKey.CommonAccount(account.Number));
            }
            cachKeys.Add(WebCacheKey.CommonAccounts);

            //Account Users
            foreach (var accountUser in accountUsers)
            {
                cachKeys.Add(WebCacheKey.CommonUserAccount(accountUser.Account.Number, accountUser.UserId.Value));
            }


            var users = await CommonContext.Users.Select(m => m.Id).ToListAsync();

            foreach (var user in users)
            {
                cachKeys.Add(WebCacheKey.LoggedInUser(user));
            }

            cachKeys.Add(WebCacheKey.HealthCheck);
            cachKeys.Add(WebCacheKey.CommonViolationTypes);
            cachKeys.Add(WebCacheKey.CommonViolationCategories);
            cachKeys.Add(WebCacheKey.CommonViolations);
            cachKeys.Add(WebCacheKey.Violations);

            await _cache.RemoveAsync(cachKeys.ToArray());

            return(RedirectToAction("Index"));
        }
        /// <summary>
        /// Update an existing Account in the Common and Account partitions.
        /// Also update a user in the Account Partition.
        /// </summary>
        /// <param name="commonAccount"></param>
        /// <param name="createdById"></param>
        /// <returns></returns>
        public async Task <ServiceResponse <CommonAccount> > UpdateAccount(CommonAccount commonAccount, Guid createdById, int AccountNumber, List <AccountViolationType> AccViolationType)
        {
            var result = new ServiceResponse <CommonAccount>();

            try
            {
                using (var scope = _commonCtx.Database.BeginTransaction())
                {
                    //update common account table
                    var accountDetail = await _commonCtx.CommonAccounts.Include(m => m.Partition).Include(m => m.Settings).SingleOrDefaultAsync(a => a.Id == commonAccount.Id);


                    var accountCtx = ContextsUtility.CreateAccountContext(Cryptography.Decrypt(accountDetail.Partition.ConnectionString));


                    accountDetail.UpdateUserId      = createdById;
                    accountDetail.UpdateUtc         = DateTime.Now;
                    accountDetail.Name              = commonAccount.Name;
                    accountDetail.OwnerUserId       = commonAccount.OwnerUserId;
                    accountDetail.StorageBucketName = commonAccount.StorageBucketName;
                    accountDetail.CitationWorkflow  = commonAccount.CitationWorkflow;
                    accountDetail.Features          = commonAccount.Features;

                    _commonCtx.CommonAccounts.Update(accountDetail);
                    await _commonCtx.SaveChangesAsync();


                    //update accounts table from selected partition
                    var AccountsDetail = await accountCtx.Accounts.SingleOrDefaultAsync(a => a.Id == commonAccount.Id);

                    AccountsDetail.Name         = commonAccount.Name;
                    AccountsDetail.UpdateUserId = createdById;
                    AccountsDetail.UpdateUtc    = DateTime.UtcNow;

                    accountCtx.Accounts.Update(AccountsDetail);
                    await accountCtx.SaveChangesAsync();

                    //update user accounts table
                    if (commonAccount.OwnerUserId != null)
                    {
                        //Check to see if the owner exists in the User Account Table.
                        var userAccount = await _commonCtx.UserAccounts.SingleOrDefaultAsync(a => a.AccountId == commonAccount.Id && a.UserId == commonAccount.OwnerUserId);

                        if (userAccount == null)
                        {
                            CommonUserAccount NewuserAccount = new CommonUserAccount();
                            NewuserAccount.CreateUserId = createdById;
                            NewuserAccount.AccountId    = commonAccount.Id;
                            NewuserAccount.UpdateUserId = createdById;
                            NewuserAccount.UserId       = commonAccount.OwnerUserId;
                            NewuserAccount.Permissions  = (AccountPermissions)Enum.GetValues(typeof(AccountPermissions)).Cast <int>().Sum();

                            _commonCtx.UserAccounts.Add(NewuserAccount);

                            await _commonCtx.SaveChangesAsync();

                            var commonUser = await _commonCtx.Users.SingleAsync(m => m.Id == commonAccount.OwnerUserId);

                            //Add any new users to acconts
                            await SaveAccountUser(commonUser, createdById);
                        }
                    }


                    //Remove the current violation types
                    var AccountViolationType = await _commonCtx.CommonAccountViolationTypes.Where(av => av.AccountId == commonAccount.Id).ToListAsync();

                    _commonCtx.CommonAccountViolationTypes.RemoveRange(AccountViolationType);
                    await _commonCtx.SaveChangesAsync();


                    //Add new violatoin types from the page
                    foreach (var Types in AccViolationType)
                    {
                        if (Types.IsCheckedViolation == true)
                        {
                            CommonAccountViolationType AcntViolationType = new CommonAccountViolationType();
                            AcntViolationType.CreateUserId    = createdById;
                            AcntViolationType.AccountId       = commonAccount.Id;
                            AcntViolationType.UpdateUserId    = createdById;
                            AcntViolationType.ViolationTypeId = Types.TypeId;

                            _commonCtx.CommonAccountViolationTypes.Add(AcntViolationType);
                            await _commonCtx.SaveChangesAsync();
                        }
                    }
                    scope.Commit();

                    //Purge common accounts cache
                    var cacheKey = WebCacheKey.CommonAccount(accountDetail.Number);
                    await _cache.RemoveAsync(cacheKey);
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "Error updating an Account");

                throw ex;
            }

            return(result);
        }