private static string CheckManager(Manager manager) { var errors = new StringBuilder(); if (string.IsNullOrEmpty(manager.ManagerName)) errors.AppendFormat("Managers name required"); if (manager.ManagerName.Split(' ').Length != 2) errors.AppendFormat("Users name must be in the form First Name Last Name"); return errors.ToString(); }
public JsonResult RemoveManagersForEmployee(Manager[] managers) { using (var orgSession = OrgStore.OpenSession()) { foreach (var manager in managers) { var employee = orgSession.Load<User>(manager.EmployeeId); employee.Relationships.RemoveAll(m => m.UserId == manager.ManagerEmployeeId && m.RelationshipName == "LineManager"); } var vm = new AccountsManagersAdminViewModel(); //save the changes to Raven and send a success message to the client try { orgSession.SaveChanges(); vm.SuccessMessage = string.Format("Succesfully removed managers"); return JsonSuccess(vm, vm.SuccessMessage); } catch (Exception ex) { GetLogger().Error(string.Format("Administration-RemoveManagersForEmployee: Problem"), ex); return JsonError("Cannot remove managers"); } } }
public JsonResult AddManagersForEmployee(Manager[] managers) { //check the all the details are correct and if not send message to client var errors = CheckManagers(managers); if (errors.Length > 0) { GetLogger().Info(string.Format("Administration:AddManagersForEmployee: Problem with manager details {0}", errors)); return JsonError(new AccountsManagersAdminViewModel { ErrorMessage = errors }, errors); } using (var orgSession = OrgStore.OpenSession()) { var errorBuilder = new StringBuilder(); foreach (var newManager in managers) { var employee = orgSession.Load<User>(newManager.EmployeeId); if (employee == null) errorBuilder.Append("Cannot find user"); else { if (employee.Relationships.All(manager => manager.UserId != newManager.ManagerEmployeeId && manager.RelationshipName == "LineManager")) { var newManagerUser = orgSession.Load<User>(newManager.ManagerEmployeeId); try { employee.AddManager(newManagerUser); } catch { errorBuilder.Append(string.Format("Cannot add {0} as a manager because they are not in the Manager role",newManagerUser.Name)); } } } } if (errorBuilder.Length > 0) { GetLogger().Info(string.Format("AdministrationController:AddManagersForEmployee - {0}", errors)); return JsonError(new AccountsManagersAdminViewModel { ErrorMessage = errorBuilder.ToString() }, errors); } var vm = new AccountsManagersAdminViewModel(); //save the changes to Raven and send a success message to the client try { orgSession.SaveChanges(); vm.SuccessMessage = string.Format("Succesfully added {0} managers to employee account", managers.Length); return JsonSuccess(vm, vm.SuccessMessage); } catch (Exception ex) { GetLogger().Error("Admin: Add managers for employee: Cannot save to Raven", ex); vm.ErrorMessage = "Cannot save changes"; return JsonError(vm, vm.ErrorMessage); } } }
public void AddManagers() { var testUser=GetAndSaveTestUsers(2, OrgStore).FirstOrDefault(); var controller = GetAuthenticatedControllerMock<AdministrationController>(Orguser); controller.ControllerContext = new ControllerContext(GetContext(false), new RouteData(), controller); var manager = new Manager { EmployeeId = testUser.Id, ManagerEmployeeId = Manager.Id, ManagerName = Manager.Name }; var result = controller.AddManagersForEmployee(new[] { manager }); var jsonresponse = ValidateAndUnwrapJsonResult(result); var data = jsonresponse.Data as AccountsManagersAdminViewModel; Assert.IsNull(data.ErrorMessage); Assert.IsNotNull(data.SuccessMessage); using (var session = OrgStore.OpenSession()) { var changedUser = session.Load<User>(testUser.Id); Assert.AreEqual(changedUser.Relationships.Count, 1); Assert.AreEqual(changedUser.Relationships.FirstOrDefault().FullName,manager.ManagerName); } }