Beispiel #1
0
        public IActionResult EmployeeEdit(EmployeeModelView viewEmployee)
        {
            LogRestaurant();
            string role;
            var    userId             = EmployeesManager.GetUserIdByEmployeeId(viewEmployee.EmployeeId);
            var    authId             = UsersManager.GetAuthIdByUserId(userId);
            var    restaurantEmployee = RestaurantsManager.RestaurantByEmployeeId(viewEmployee.RestaurantId, viewEmployee.EmployeeId);
            var    authMatrix         = UsersManager.GetAuthenticationMatrixByIDs(viewEmployee.RestaurantId, authId);

            viewEmployee.Status = GetEmployeeStatusValue(viewEmployee.Status);
            viewEmployee.Role   = GetRestaurantRolesValue(viewEmployee.Role);
            if (restaurantEmployee.Active == false && viewEmployee.Active == true)
            {
                if (viewEmployee.Status == "Employee")
                {
                    restaurantEmployee.RequestStatus = "Approved";
                    viewEmployee.EndDate             = null;
                    if (authMatrix != null)
                    {
                        if (viewEmployee.Role == null)
                        {
                            authMatrix.Role = "Employee";
                        }
                        else
                        {
                            authMatrix.Role = viewEmployee.Role;
                        }
                        UsersManager.UpdateAuthenticationMatrixByIDs(authMatrix);
                    }
                    else
                    {
                        if (viewEmployee.Role == null)
                        {
                            role = "Employee";
                        }
                        else
                        {
                            role = viewEmployee.Role;
                        }
                        var newAuthMatrix = new AuthenticationMatrix
                        {
                            AuthenticationId = authId,
                            RestaurantId     = viewEmployee.RestaurantId,
                            Role             = role
                        };
                        UsersManager.AddEmployeeToAutheticationMatrix(newAuthMatrix);
                    }
                }
            }
            else
            {
                if (restaurantEmployee.Active == true && viewEmployee.Active == false)
                {
                    if (viewEmployee.Status == "Employee" || viewEmployee.Status == "Employee on Leave")
                    {
                        if (authMatrix == null)
                        {
                            var newAuthMatrix = new AuthenticationMatrix
                            {
                                AuthenticationId = authId,
                                RestaurantId     = viewEmployee.RestaurantId,
                            };
                        }
                        authMatrix.Role = "Employee on Leave";
                        UsersManager.UpdateAuthenticationMatrixByIDs(authMatrix);
                    }
                    else
                    {
                        if (authMatrix != null)
                        {
                            UsersManager.DeleteAuthMatrixByIds(viewEmployee.RestaurantId, authId);
                        }
                    }
                }
                else
                {
                    if (viewEmployee.Status != "Employee")
                    {
                        if (authMatrix != null)
                        {
                            UsersManager.DeleteAuthMatrixByIds(viewEmployee.RestaurantId, authId);
                        }
                    }
                    else
                    {
                        if (authMatrix == null)
                        {
                            var newAuthMatrix = new AuthenticationMatrix
                            {
                                AuthenticationId = authId,
                                RestaurantId     = viewEmployee.RestaurantId,
                            };
                        }
                        authMatrix.Role = viewEmployee.Role;
                        UsersManager.UpdateAuthenticationMatrixByIDs(authMatrix);
                    }
                }
            }
            restaurantEmployee.Status  = viewEmployee.Status;
            restaurantEmployee.EndDate = viewEmployee.EndDate;
            restaurantEmployee.Active  = viewEmployee.Active;
            RestaurantsManager.UpdateRestaurantEmployee(restaurantEmployee);
            return(RedirectToAction("RestaurantEmployeesShow"));
        }