public static void CreateBranchUserAdminRolesForUserForAllBranches(ApplicationDbContext db, BranchUser branchUser, UserRoleEnum userRole) { List <Branch> companyBranches = BranchHelpers.GetBranchesForCompany(db, branchUser.CompanyId); foreach (Branch branch in companyBranches) { BranchUser thisBranchUser = BranchUserHelpers.GetBranchUser(db, branchUser.UserId, branch.BranchId, branch.CompanyId); //Update if required else create new if missing if (thisBranchUser != null) { //if this branchuser is having the status changed then just check any outstanding actions and remove if (userRole != thisBranchUser.UserRole) { thisBranchUser.UserRole = userRole; thisBranchUser.EntityStatus = EntityStatusEnum.Active; db.Entry(branchUser).State = EntityState.Modified; db.SaveChanges(); } } else { BranchUserHelpers.CreateBranchUser(db, branchUser.UserId, branch.BranchId, branch.CompanyId, userRole, EntityStatusEnum.Active); } } }
/// <summary> /// Adds all Admin users for the company of the new branch to the new branch /// </summary> /// <param name="db"></param> /// <param name="branchId">new branch id</param> public static void CreateBranchAdminUsersForNewBranch(ApplicationDbContext db, Branch branch, UserRoleEnum role) { List <AppUser> adminAppUsersForCompany = AppUserHelpers.GetAdminAppUsersForCompany(db, branch.CompanyId); foreach (AppUser adminUser in adminAppUsersForCompany) { BranchUser branchUser = BranchUserHelpers.GetBranchUser(db, adminUser.AppUserId, branch.BranchId, branch.CompanyId); //Only add if not already there if (branchUser == null) { BranchUserHelpers.CreateBranchUser(db, adminUser.AppUserId, branch.BranchId, branch.CompanyId, role, EntityStatusEnum.Active); } } }
public static bool UpdateBranchesFromBranchAdminView(ApplicationDbContext db, List <BranchAdminView> branchesAdminView, IPrincipal user) { //Get logged in user details for Task creation (if required) AppUser loggedInUser = AppUserHelpers.GetAppUser(db, user); try { foreach (BranchAdminView branchAdminVeiw in branchesAdminView) { //Get original branch record so that we can compare previous and current entity status' Branch branch = BranchHelpers.GetBranch(db, branchAdminVeiw.BranchId); EntityStatusEnum previousEntityStatus = branch.EntityStatus; //Update branch branch = BranchHelpers.UpdateBranch(db, branchAdminVeiw.BranchId, branchAdminVeiw.CompanyId, branchAdminVeiw.BusinessType, branchAdminVeiw.BranchName, branchAdminVeiw.AddressLine1, branchAdminVeiw.AddressLine2, branchAdminVeiw.AddressLine3, branchAdminVeiw.AddressTownCity, branchAdminVeiw.AddressCounty, branchAdminVeiw.AddressPostcode, branchAdminVeiw.TelephoneNumber, branchAdminVeiw.Email, branchAdminVeiw.ContactName, branchAdminVeiw.PrivacyLevel, branchAdminVeiw.EntityStatus); //if change of status from on-hold - anything then look for outstanding task and set to closed if (branchAdminVeiw.EntityStatus != EntityStatusEnum.OnHold && previousEntityStatus == EntityStatusEnum.OnHold) { List <UserTask> activeTasksForThisBranch = UserTaskHelpers.GetUserTasksForBranch(db, branch.BranchId); foreach (UserTask activeTaskForThisBranch in activeTasksForThisBranch) { UserTaskHelpers.UpdateEntityStatus(activeTaskForThisBranch.UserTaskId, EntityStatusEnum.Closed); } } //If change of status to on-hold then create a Task if (branchAdminVeiw.EntityStatus == EntityStatusEnum.OnHold && previousEntityStatus != EntityStatusEnum.OnHold) { UserTaskHelpers.CreateUserTask(TaskTypeEnum.BranchOnHold, "New branch on hold, awaiting administrator activation", branch.BranchId, loggedInUser.AppUserId, EntityStatusEnum.Active); } //Update Users link with Branch foreach (BranchAdminViewCompanyUser companyUser in branchAdminVeiw.RelatedCompanyUsers) { //Try to find the user BranchUser branchUser = BranchUserHelpers.GetBranchUser(db, companyUser.AppUserId, branchAdminVeiw.BranchId, branchAdminVeiw.CompanyId); //Now check if user is checked in list then ensure it is on branchUser, else remove if it is on branchUser if (companyUser.LinkedToThisBranch) { //if company user linked but not on BranchUser, add to BranchUser if (branchUser == null) { BranchUserHelpers.CreateBranchUser(db, companyUser.AppUserId, branchAdminVeiw.BranchId, branchAdminVeiw.CompanyId, UserRoleEnum.User, EntityStatusEnum.Active); } //if company user linked but not ACTIVE on BranchUser else if (branchUser.EntityStatus != EntityStatusEnum.Active) { BranchUserHelpers.UpdateBranchUserStatus(db, branchUser, EntityStatusEnum.Active, companyUser.AppUserId); } } else { //if company user not linked but is on BranchUser, remove from BranchUser by setting status to Inactive if (branchUser != null) { BranchUserHelpers.UpdateBranchUserStatus(db, branchUser, EntityStatusEnum.Inactive, companyUser.AppUserId); } } } } return(true); } catch (Exception e) { Console.WriteLine("{0} Exception caught.", e); return(false); } }