Example #1
0
        public async Task <BusinessViewModel> BusinessById(int id, BPMainContext context)
        {
            // we're going to get the business by id
            var business = await context.Accounts.FindAsync(id);

            if (business == null)
            {
                return(new BusinessViewModel());
            }
            // This business actually exists!
            var returnModel = new BusinessViewModel {
                ID           = business.ID,
                Name         = business.BusinessAttribute.BusinessName,
                BusinessType = new NameIdLists {
                    ID   = business.BusinessAttribute.BusinessType.ID,
                    Name = business.BusinessAttribute.BusinessType.Name
                },
                Logins = business?.LoginAttribute?.LoginIds.Select(y => new NameIdValueNoteLists {
                    ID   = y.ID,
                    Name = y.UserId
                }) ?? new List <NameIdValueNoteLists>()
            };

            return(returnModel);
        }
Example #2
0
        public async Task <MethodResults> RemoveBusinessAccount(int id, BPMainContext context, CoreLoggerProvider logger, string IPAddress, string instigator)
        {
            // we're removing this business from the website
            var account = await context.Accounts.FindAsync(id);

            if (account == null)
            {
                return new MethodResults {
                           Success = false, Message = "This business could not be deleted because it could not be found."
                }
            }
            ;
            var subject      = "Account removal";
            var system       = "Business Service";
            var businessName = account.BusinessAttribute.BusinessName;

            if (account.LoginAttribute != null)
            {
                context.LoginAttributes.Remove(account.LoginAttribute);
            }
            if (account.EntityAttribute != null)
            {
                context.EntityAttributes.Remove(account.EntityAttribute);
            }
            if (account.BusinessAttribute != null)
            {
                context.BusinessAttributes.Remove(account.BusinessAttribute);
            }
            if (account.UserAttribute != null)
            {
                context.UserAttributes.Remove(account.UserAttribute);
            }
            context.Accounts.Remove(account);
            var returnResults = await context.SaveChangesAsync(context);

            if (returnResults.Success)
            {
                await logger.CreateNewLog($"Business account {businessName} successfully removed by {instigator} on {IPAddress}", subject, instigator, system);
            }
            else
            {
                await logger.CreateNewLog($"Could not remove business account {businessName} by {instigator} on {IPAddress}", subject, instigator, system);
            }
            return(returnResults);
        }
Example #3
0
        internal string GetAccountName(BPAccount account, BPMainContext context)
        {
            var created = account.EntityAttribute.AccountDates.FirstOrDefault(x => x.AccountDateType.Index == 1).DateLine.ToString("dd MMM yyyy");

            if (account?.UserAttribute != null)
            {
                if (account?.LoginAttribute != null)
                {
                    // this account actually has login attributes
                    if (account.LoginAttribute.LoginIds.Count > 0)
                    {
                        var loginId = account.LoginAttribute.LoginIds.FirstOrDefault(x => x.PresidenceType.Index == 1);
                        return($"{account.ID} {loginId.EmailAddress} created {created}");
                    }
                }
                return($"{account.ID} User account created {created}");
            }
            if (!string.IsNullOrEmpty(account?.BusinessAttribute?.BusinessName ?? ""))
            {
                return($"{account.ID} {account.BusinessAttribute.BusinessName ?? ""} created {created}");
            }
            return($"{account.ID} Business account created {created}");
        }
Example #4
0
        public async Task <EditUser> UpdateEditPullModel(EditUser pullModel, string selectedRole, int selectedAccount, IEnumerable <string> rolesList, BPMainContext context, string userId)
        {
            var roleSelectList = new List <NameStringIdSelected>();

            foreach (var role in rolesList)
            {
                roleSelectList.Add(new NameStringIdSelected
                {
                    ID       = role,
                    Name     = role,
                    Selected = role.Equals(selectedRole)
                });
            }
            var accountList = await context.Accounts.ToListAsync();

            var accountSelectList = new List <NameIdSelectedLists>();

            if (selectedAccount == 0)
            {
                accountSelectList.Add(new NameIdSelectedLists {
                    ID = 0, Name = "Account List"
                });
            }
            if (selectedAccount < 0)
            {
                if (!string.IsNullOrEmpty(userId))
                {
                    var thisLogin = await context.LoginIds.FirstOrDefaultAsync(x => x.UserId.Equals(userId));

                    if (thisLogin != null)
                    {
                        // there's a login
                        selectedAccount = thisLogin.LoginAttribute.Account.ID;
                    }
                }
            }
            foreach (var account in accountList)
            {
                accountSelectList.Add(new NameIdSelectedLists
                {
                    ID       = account.ID,
                    Name     = GetAccountName(account, context),
                    Selected = account.ID.Equals(selectedAccount)
                });
            }
            pullModel.Roles        = roleSelectList;
            pullModel.AccountLists = accountSelectList;
            return(pullModel);
        }
Example #5
0
        public async Task <CreateUser> UpdateUserPullModel(CreateUser pullModel, string selectedRole, int selectedAccount, IEnumerable <string> rolesList, BPMainContext context)
        {
            var roleSelectList = new List <NameStringIdSelected>();

            foreach (var role in rolesList)
            {
                roleSelectList.Add(new NameStringIdSelected {
                    ID       = role,
                    Name     = role,
                    Selected = role.Equals(selectedRole)
                });
            }
            var accountList = await context.Accounts.ToListAsync();

            var accountSelectList = new List <NameIdSelectedLists>();

            if (selectedAccount == 0)
            {
                accountSelectList.Add(new NameIdSelectedLists {
                    ID = 0, Name = "Account List"
                });
            }
            foreach (var account in accountList)
            {
                accountSelectList.Add(new NameIdSelectedLists {
                    ID       = account.ID,
                    Name     = GetAccountName(account, context),
                    Selected = account.ID.Equals(selectedAccount)
                });
            }
            pullModel.Roles        = roleSelectList;
            pullModel.AccountLists = accountSelectList;
            return(pullModel);
        }
Example #6
0
        public async Task <MethodResults> AddUserToAccount(string userId, string emailAddress, string userName, CoreLoggerProvider logger, string IPAddress, string instigator, BPMainContext context, int accountId, int loginTypeId, int presidenceTypeId)
        {
            var methodResults = new MethodResults {
                Success = false, Message = "Something went wrong.  Nothing was changed.  Please try again or contact your network or system administrator."
            };
            // we're adding a user to either an existing account, or to a new account
            var account = new BPAccount();

            if (accountId == 0)
            {
                var dateType = await context.AccountDateTypes.FirstOrDefaultAsync(x => x.Index == 1);

                var accountType = await context.AccountTypes.FirstOrDefaultAsync(x => x.Index == 1);

                var accountDates = new List <AccountDate> {
                    new AccountDate {
                        DateLine = DateTimeOffset.Now, AccountDateType = dateType
                    }
                };

                account.EntityAttribute = new EntityAttribute
                {
                    AccountDates = accountDates,
                    AccountType  = accountType
                };
                account.UserAttribute = new UserAttribute();
            }
            else
            {
                account = await context.Accounts.FindAsync(accountId);
            }
            var loginType = await context.LoginIdTypes.FirstOrDefaultAsync(x => x.Index == loginTypeId);

            var presidenceType = await context.PresidenceTypes.FirstOrDefaultAsync(x => x.Index == presidenceTypeId);

            var loginIds = new List <LoginId> {
                new LoginId {
                    UserId         = userId,
                    EmailAddress   = emailAddress,
                    PresidenceType = presidenceType,
                    LoginIdType    = loginType
                }
            };
            var loginAttribute = new LoginAttribute
            {
                LoginIds = loginIds
            };

            if (accountId > 0)
            {
                var entry = context.Entry(account);
                entry.Entity.LoginAttribute = loginAttribute;
                entry.State = EntityState.Modified;
            }
            else
            {
                account.LoginAttribute = loginAttribute;
                context.Accounts.Add(account);
            }
            methodResults = await context.SaveChangesAsync(context);

            var subject = "Add user to account";
            var system  = "User Service";

            if (methodResults.Success)
            {
                await logger.CreateNewLog($"Successfully added user {emailAddress} to account {account.ID} on {IPAddress} by {instigator}.", subject, instigator, system);
            }
            else
            {
                await logger.CreateNewLog($"Unable to add user {emailAddress} to an account on {IPAddress} by {instigator}.", subject, instigator, system);
            }
            return(methodResults);
        }
Example #7
0
        internal async Task <BusinessPullModel> UpdatePullModel(BusinessPullModel pullModel, BPMainContext context)
        {
            var businessTypeList = new List <SelectListItem>();

            if (pullModel.BusinessTypeId == 0)
            {
                businessTypeList.Add(new SelectListItem {
                    Text  = "Select a business type...",
                    Value = "0"
                });
            }
            businessTypeList.AddRange(await context.BusinessTypes.Select(y => new SelectListItem {
                Text     = y.Name,
                Value    = y.ID.ToString(),
                Selected = y.ID == pullModel.BusinessTypeId
            }).ToListAsync());

            pullModel.BusinessTypes = businessTypeList;
            return(pullModel);
        }
Example #8
0
        public IEnumerable <BusinessViewModel> PopulateBusinessValues(IEnumerable <BPAccount> businessList, BPMainContext context, IEnumerable <NameStringId> userList)
        {
            var returnList = new List <BusinessViewModel>();

            foreach (var business in businessList)
            {
                returnList.Add(new BusinessViewModel
                {
                    BusinessType = new NameIdLists
                    {
                        ID   = business.BusinessAttribute.BusinessType.ID,
                        Name = business.BusinessAttribute.BusinessType.Name
                    },
                    ID     = business.ID,
                    Name   = business.BusinessAttribute.BusinessName,
                    Logins = business?.LoginAttribute?.LoginIds.Select(x => new NameIdValueNoteLists
                    {
                        ID    = x.ID,
                        Value = x.LoginIdType.Name,
                        Note  = x.UserId,
                        Index = x.LoginIdType.Index,
                        Name  = userList.FirstOrDefault(y => y.ID == x.UserId).Name
                    }).OrderBy(n => n.Index).ToList() ?? new List <NameIdValueNoteLists>()
                });
            }
            return(returnList.OrderBy(x => x.Name));
        }
Example #9
0
        public async Task <MethodResults> CreateNewBusiness(BusinessPullModel pullModel, BPMainContext context, string IPAddress, string instigator, CoreLoggerProvider logger)
        {
            var methodResults = new MethodResults {
                Success = false, Message = "Something went wrong.  Please try again, or contact your system administrator."
            };

            // the pull model should have been validated at the controller
            // first, create the account
            var account = new BPAccount()
            {
                BusinessAttribute = new BusinessAttribute(),
                EntityAttribute   = new EntityAttribute()
            };

            account.BusinessAttribute.BusinessName = pullModel.BusinessName;
            var subject = "Business Account Creation";
            var system  = "Business Service";

            account.EntityAttribute.AccountType = await context.AccountTypes.FirstOrDefaultAsync(x => x.Index == 2);

            account.BusinessAttribute.BusinessType = await context.BusinessTypes.FirstOrDefaultAsync(x => x.Index == pullModel.BusinessTypeId);

            var accountDateType = await context.AccountDateTypes.FirstOrDefaultAsync(x => x.Index == 1);

            var accountDate = new AccountDate {
                AccountDateType = accountDateType,
                DateLine        = DateTimeOffset.Now
            };

            account.EntityAttribute.AccountDates.Add(accountDate);

            context.Accounts.Add(account);

            methodResults = await context.SaveChangesAsync(context);

            if (methodResults.Success)
            {
                await logger.CreateNewLog($"Successfully create {pullModel.BusinessName} as {pullModel.BusinessTypes.FirstOrDefault(x => x.Value == pullModel.BusinessTypeId.ToString())} by {instigator} on {IPAddress}", subject, instigator, system);
            }
            else
            {
                await logger.CreateNewLog($"Failed to create new business {pullModel.BusinessName} by {instigator} on {IPAddress}", subject, instigator, system);
            }
            return(methodResults);
        }
Example #10
0
        private async Task <IEnumerable <AccountAdminViewModel> > PrepopulateAccountsWithUserName(BPMainContext context, ApplicationUserManager manager)
        {
            var list = await context.Accounts.ToListAsync();

            var returnList = new List <AccountAdminViewModel>();
            var userList   = await manager.Users.ToListAsync();

            foreach (var account in list)
            {
                var accountAdminViewModel = new AccountAdminViewModel();
                var verifiedString        = "";
                accountAdminViewModel.ID = account.ID;
                if (account.LoginAttribute != null)
                {
                    var loginId = account.LoginAttribute.LoginIds.FirstOrDefault(x => x.LoginIdType.Index == 4);
                    if (loginId != null)
                    {
                        // we're all set for this account
                        var user = userList.FirstOrDefault(x => x.Id.Equals(loginId.UserId));
                        if (user != null)
                        {
                            verifiedString = user.EmailConfirmed ? $"<p style=\"color: green\">{user.Email} verified</p>" :
                                             $"<p style=\"color: red;\">{user.Email}</p>";
                            accountAdminViewModel.UserName     = user.UserName;
                            accountAdminViewModel.EmailAddress = verifiedString;
                            accountAdminViewModel.AccountName  = user.UserName;
                        }
                    }
                }
                if (account.BusinessAttribute != null)
                {
                    accountAdminViewModel.AccountType = "Business";
                    accountAdminViewModel.AccountName = account.BusinessAttribute.BusinessName;
                }
                else if (account.UserAttribute != null)
                {
                    accountAdminViewModel.AccountType = "Personal";
                }
                else
                {
                    accountAdminViewModel.AccountType = "UnOwned";
                }
                accountAdminViewModel.Created = account?.EntityAttribute?.AccountDates.FirstOrDefault(x => x.AccountDateType.Index == 1).DateLine ?? DateTime.Now;
                returnList.Add(accountAdminViewModel);
            }
            return(returnList);
        }
Example #11
0
        private async Task <IEnumerable <AccountAdminViewModel> > AccountByName(int returnFirst, int sorted, BPMainContext context, ApplicationUserManager manager)
        {
            var checkList = await PrepopulateAccountsWithUserName(context, manager);

            if (sorted > 0)
            {
                switch (sorted)
                {
                case 1:
                    checkList = checkList.OrderBy(x => x.UserName);
                    break;

                case 2:
                    checkList = checkList.OrderByDescending(x => x.UserName);
                    break;

                default:
                    checkList = checkList.OrderBy(x => x.ID);
                    break;
                }
            }
            if (returnFirst > 0)
            {
                return(checkList.Take(returnFirst));
            }
            return(checkList);
        }
Example #12
0
        private async Task <IEnumerable <AccountAdminViewModel> > AccountFirstN(int returnFirst, BPMainContext context, ApplicationUserManager manager)
        {
            var checkList = await PrepopulateAccountsWithUserName(context, manager);

            if (returnFirst > 0)
            {
                return(checkList.OrderBy(x => x.ID).Take(returnFirst));
            }
            return(checkList.OrderBy(x => x.ID));
        }
Example #13
0
        public async Task <IEnumerable <AccountAdminViewModel> > ReturnAccountList(int returnFirst, bool byName, bool byDate, int sorted, BPMainContext context, ApplicationUserManager manager)
        {
            IEnumerable <AccountAdminViewModel> returnList = new List <AccountAdminViewModel>();

            if (byName)
            {
                if (byDate)
                {
                }
            }
            if (sorted > 0)
            {
            }
            return(await AccountFirstN(returnFirst, context, manager));
        }
Example #14
0
        private async Task <IEnumerable <AccountAdminViewModel> > Generate(IEnumerable <BPAccount> accountList, BPMainContext context)
        {
            IEnumerable <AccountAdminViewModel> returnList = new List <AccountAdminViewModel>();

            return(returnList);
        }