/// <summary> /// When replacing an existing chair of trustees/local govs with data from an existing governor, this method prepopulates the fields from the governor record. /// </summary> /// <param name="model"></param> /// <param name="viewModel"></param> private void PrepopulateFields(GovernorModel model, ReplaceChairViewModel viewModel) { viewModel.NewLocalGovernor = viewModel.NewLocalGovernor ?? new GovernorViewModel(); viewModel.NewLocalGovernor.AppointingBodyId = model.AppointingBodyId; viewModel.NewLocalGovernor.AppointmentEndDate = new DateTimeViewModel(model.AppointmentEndDate); viewModel.NewLocalGovernor.AppointmentStartDate = new DateTimeViewModel(model.AppointmentStartDate); viewModel.NewLocalGovernor.DOB = new DateTimeViewModel(model.DOB); viewModel.NewLocalGovernor.EmailAddress = model.EmailAddress; viewModel.NewLocalGovernor.GovernorTitleId = model.Person_TitleId; viewModel.NewLocalGovernor.FirstName = model.Person_FirstName; viewModel.NewLocalGovernor.MiddleName = model.Person_MiddleName; viewModel.NewLocalGovernor.LastName = model.Person_LastName; viewModel.NewLocalGovernor.PreviousTitleId = model.PreviousPerson_TitleId; viewModel.NewLocalGovernor.PreviousFirstName = model.PreviousPerson_FirstName; viewModel.NewLocalGovernor.PreviousMiddleName = model.PreviousPerson_MiddleName; viewModel.NewLocalGovernor.PreviousLastName = model.PreviousPerson_LastName; viewModel.NewLocalGovernor.TelephoneNumber = model.TelephoneNumber; viewModel.NewLocalGovernor.PostCode = model.PostCode; viewModel.Urn = model.EstablishmentUrn; viewModel.SelectedPreviousExistingNonChairId = model.Id; }
/// <summary> /// When replacing an existing chair of governor with data from an existing governor, this method prepopulates the fields from the governor record. /// </summary> /// <param name="model"></param> /// <param name="viewModel"></param> private void PrepopulateFields(GovernorModel model, CreateEditGovernorViewModel viewModel) { viewModel.AppointingBodyId = model.AppointingBodyId; viewModel.AppointmentEndDate = new DateTimeViewModel(model.AppointmentEndDate); viewModel.AppointmentStartDate = new DateTimeViewModel(model.AppointmentStartDate); viewModel.DOB = new DateTimeViewModel(model.DOB); viewModel.EmailAddress = model.EmailAddress; viewModel.GovernorTitleId = model.Person_TitleId; viewModel.FirstName = model.Person_FirstName; viewModel.MiddleName = model.Person_MiddleName; viewModel.LastName = model.Person_LastName; viewModel.PreviousTitleId = model.PreviousPerson_TitleId; viewModel.PreviousFirstName = model.PreviousPerson_FirstName; viewModel.PreviousMiddleName = model.PreviousPerson_MiddleName; viewModel.PreviousLastName = model.PreviousPerson_LastName; viewModel.TelephoneNumber = model.TelephoneNumber; viewModel.PostCode = model.PostCode; viewModel.EstablishmentUrn = model.EstablishmentUrn; viewModel.GroupUId = model.GroupUId; viewModel.SelectedPreviousGovernorId = model.Id; }
private async Task PopulateEstablishmentName(IPrincipal principal, GovernorModel governor) { foreach (var appt in governor.Appointments ?? Enumerable.Empty <GovernorAppointment>()) { appt.EstablishmentName = await _establishmentReadService.GetEstablishmentNameAsync(appt.EstablishmentUrn.Value, principal); } }
public static async Task <SharedGovernorViewModel> MapFromGovernor(GovernorModel governor, int establishmentUrn, ICachedLookupService cachedLookupService) { var dateNow = DateTime.Now.Date; var appointment = governor.Appointments?.SingleOrDefault(g => g.EstablishmentUrn == establishmentUrn); var sharedWith = governor.Appointments? .Where(a => a.AppointmentStartDate < dateNow && (a.AppointmentEndDate == null || a.AppointmentEndDate > dateNow)) .Select(a => new EstablishmentViewModel { Urn = a.EstablishmentUrn.Value, EstablishmentName = a.EstablishmentName }) .ToList(); var appointingBodies = await cachedLookupService.GovernorAppointingBodiesGetAllAsync(); var nationalities = await cachedLookupService.NationalitiesGetAllAsync(); return(new SharedGovernorViewModel { AppointingBodyName = appointingBodies.Single(g => g.Id == governor.AppointingBodyId).Name, AppointmentStartDate = appointment?.AppointmentStartDate != null ? new DateTimeViewModel(appointment.AppointmentStartDate) : new DateTimeViewModel(), AppointmentEndDate = appointment?.AppointmentEndDate != null ? new DateTimeViewModel(appointment.AppointmentEndDate) : new DateTimeViewModel(), DOB = governor.DOB, FullName = governor.GetFullName(), Id = governor.Id.Value, PostCode = governor.PostCode, Selected = appointment != null, PreExisting = appointment != null, SharedWith = sharedWith ?? new List <EstablishmentViewModel>(), MultiSelect = IsSharedGovernorRoleMultiSelect((eLookupGovernorRole)governor.RoleId) }); }
public async Task <ApiResponse <int> > SaveAsync(GovernorModel model, IPrincipal principal) { if (model.IsNewEntity) { var response = await _httpClient.PostAsync <NumericResultDto>("governor", model, principal); return(new ApiResponse <int> { Success = response.Success, Response = response.Response?.Value ?? 0, Errors = response.Errors }); } var postResponse = await _httpClient.PostAsync("governor", model, principal); return(new ApiResponse <int> { Success = postResponse.Success, Response = model.Id.Value, Errors = postResponse.Errors }); }
public async Task <ValidationEnvelopeDto> ValidateAsync(GovernorModel model, IPrincipal principal) { return((await _httpClient.PostAsync <ValidationEnvelopeDto>("governor/validate", model, principal)).GetResponse()); }
public async Task <ActionResult> ReplaceChair(ReplaceChairViewModel model) { if (ModelState.IsValid) { if (model.NewChairType == ReplaceChairViewModel.ChairType.SharedChair) { var newGovernor = model.SharedGovernors.SingleOrDefault(s => s.Id == model.SelectedGovernorId); var validation = await _governorsWriteService.AddSharedGovernorAppointmentAsync(model.SelectedGovernorId, model.Urn.Value, model.DateTermEnds.ToDateTime().Value.AddDays(1), newGovernor.AppointmentEndDate.ToDateTime(), User); if (!validation.HasErrors) { var url = $"{Url.RouteUrl("EstabDetails", new {id = model.Urn, saved = true})}#school-governance"; return(Redirect(url)); } validation.ApplyToModelState(ControllerContext); } else { var newGovernor = new GovernorModel { AppointingBodyId = model.NewLocalGovernor.AppointingBodyId, AppointmentEndDate = model.NewLocalGovernor.AppointmentEndDate.ToDateTime(), AppointmentStartDate = model.DateTermEnds.ToDateTime().Value.AddDays(1), DOB = model.NewLocalGovernor.DOB.ToDateTime(), EmailAddress = model.NewLocalGovernor.EmailAddress, EstablishmentUrn = model.Urn, Person_FirstName = model.NewLocalGovernor.FirstName, Person_MiddleName = model.NewLocalGovernor.MiddleName, Person_LastName = model.NewLocalGovernor.LastName, Person_TitleId = model.NewLocalGovernor.GovernorTitleId, PreviousPerson_FirstName = model.NewLocalGovernor.PreviousFirstName, PreviousPerson_MiddleName = model.NewLocalGovernor.PreviousMiddleName, PreviousPerson_LastName = model.NewLocalGovernor.PreviousLastName, PreviousPerson_TitleId = model.NewLocalGovernor.PreviousTitleId, PostCode = model.NewLocalGovernor.PostCode, RoleId = (int)(RoleEquivalence.GetLocalEquivalentToSharedRole(model.Role) ?? model.Role), TelephoneNumber = model.NewLocalGovernor.TelephoneNumber }; var validation = await _governorsWriteService.ValidateAsync(newGovernor, User); if (!validation.HasErrors) { GovernorModel oldGovernorModel = null; if (model.Reinstate) { oldGovernorModel = await _governorsReadService.GetGovernorAsync(model.ExistingGovernorId, User); } await _governorsWriteService.SaveAsync(newGovernor, User); if (model.SelectedPreviousExistingNonChairId.HasValue) { await RetireGovernorAsync(model.SelectedPreviousExistingNonChairId.Value, model.DateTermEnds.ToDateTime().GetValueOrDefault()); } if (model.Reinstate) // re-instate the old chair to be the non-chair equivalent role. { await ReInstateChairAsNonChairAsync(model.ExistingGovernorId, newGovernor.AppointmentStartDate.GetValueOrDefault(), (oldGovernorModel?.AppointmentEndDate).GetValueOrDefault(), eLookupGovernorRole.ChairOfLocalGoverningBody); } var url = $"{Url.RouteUrl("EstabDetails", new {id = model.Urn, saved = true})}#school-governance"; return(Redirect(url)); } validation.ApplyToModelState(ControllerContext, nameof(model.NewLocalGovernor), true); } } var governor = await _governorsReadService.GetGovernorAsync(model.ExistingGovernorId, User); var roles = new List <eLookupGovernorRole> { (eLookupGovernorRole)governor.RoleId }; if (EnumSets.SharedGovernorRoles.Contains(governor.RoleId.Value)) { var localEquivalent = RoleEquivalence.GetLocalEquivalentToSharedRole((eLookupGovernorRole)governor.RoleId); if (localEquivalent != null) { roles.Add(localEquivalent.Value); } } else { roles.AddRange(RoleEquivalence.GetEquivalentToLocalRole((eLookupGovernorRole)governor.RoleId)); } var governors = (await _governorsReadService.GetSharedGovernorsAsync(model.Urn.Value, User)).Where(g => roles.Contains((eLookupGovernorRole)g.RoleId) && g.Id != model.ExistingGovernorId).ToList(); model.NewLocalGovernor.DisplayPolicy = await _governorsReadService.GetEditorDisplayPolicyAsync( (RoleEquivalence.GetLocalEquivalentToSharedRole((eLookupGovernorRole)governor.RoleId.Value) ?? (eLookupGovernorRole)governor.RoleId.Value), false, User); var sourceGovernors = (await Task.WhenAll(governors.Select(async g => await SharedGovernorViewModel.MapFromGovernor(g, model.Urn.Value, _cachedLookupService)))).ToList(); if (model.SharedGovernors == null) { model.SharedGovernors = sourceGovernors; } else { for (var i = 0; i < model.SharedGovernors?.Count; i++) { // if this is the one the user selected, we dont want to change any of the values they entered if (model.SharedGovernors[i].Selected) { model.SharedGovernors[i].SharedWith = sourceGovernors.First(x => x.Id == model.SharedGovernors[i].Id).SharedWith; } else { model.SharedGovernors[i] = sourceGovernors.First(x => x.Id == model.SharedGovernors[i].Id); model.SharedGovernors[i].Selected = false; } } } await PopulateSelectLists(model.NewLocalGovernor); await _layoutHelper.PopulateLayoutProperties(model, model.Urn, null, User); var models = await _governorsReadService.GetGovernorListAsync(model.Urn, principal : User); var localGovernors = models.CurrentGovernors.Where(x => x.RoleId == (int)eLookupGovernorRole.LocalGovernor).OrderBy(x => x.Person_LastName).ToArray(); if (model.SelectedPreviousExistingNonChairId.HasValue) { model.SelectedNonChair = localGovernors.FirstOrDefault(x => x.Id == model.SelectedPreviousExistingNonChairId); } model.ExistingNonChairs = localGovernors.Select(x => new SelectListItem { Text = x.Person_FirstName + " " + x.Person_LastName, Value = x.Id.ToString(), Selected = model.SelectedPreviousExistingNonChairId.HasValue && model.SelectedPreviousExistingNonChairId.Value == x.Id }); return(View(model)); }
public async Task <ActionResult> AddEditOrReplace(CreateEditGovernorViewModel viewModel) { await PopulateSelectLists(viewModel); viewModel.DisplayPolicy = await _governorsReadService.GetEditorDisplayPolicyAsync(viewModel.GovernorRole, viewModel.GroupUId.HasValue, User); var governorModel = new GovernorModel { AppointingBodyId = viewModel.AppointingBodyId, AppointmentEndDate = viewModel.AppointmentEndDate.ToDateTime(), AppointmentStartDate = viewModel.Mode == CreateEditGovernorViewModel.EditMode.Replace ? viewModel.ReplaceGovernorViewModel.AppointmentEndDate.ToDateTime()?.AddDays(1) : viewModel.AppointmentStartDate.ToDateTime(), DOB = viewModel.DOB.ToDateTime(), EmailAddress = viewModel.EmailAddress, GroupUId = viewModel.GroupUId, EstablishmentUrn = viewModel.EstablishmentUrn, Id = viewModel.GID, Person_FirstName = viewModel.FirstName, Person_MiddleName = viewModel.MiddleName, Person_LastName = viewModel.LastName, Person_TitleId = viewModel.GovernorTitleId, PreviousPerson_FirstName = viewModel.PreviousFirstName, PreviousPerson_MiddleName = viewModel.PreviousMiddleName, PreviousPerson_LastName = viewModel.PreviousLastName, PreviousPerson_TitleId = viewModel.PreviousTitleId, PostCode = viewModel.PostCode, RoleId = (int)viewModel.GovernorRole, TelephoneNumber = viewModel.TelephoneNumber }; var validationResults = await _governorsWriteService.ValidateAsync(governorModel, User); validationResults.ApplyToModelState(ControllerContext, true); if (ModelState.IsValid) { if (!viewModel.EstablishmentUrn.HasValue && !viewModel.GID.HasValue && EnumSets.eSharedGovernorRoles.Contains(viewModel.GovernorRole)) { var existingGovernors = await _governorsReadService.GetGovernorListAsync(null, viewModel.GroupUId, User); var duplicates = existingGovernors.CurrentGovernors.Where(g => g.RoleId == (int)viewModel.GovernorRole && string.Equals($"{g.Person_TitleId} {g.Person_FirstName} {g.Person_MiddleName} {g.Person_LastName}", $"{viewModel.GovernorTitleId} {viewModel.FirstName} {viewModel.MiddleName} {viewModel.LastName}", StringComparison.OrdinalIgnoreCase)); if (duplicates.Any()) { ModelState.Clear(); return(RedirectToRoute("GroupEditGovernance", new { groupUId = viewModel.GroupUId, duplicateGovernorId = duplicates.First().Id })); } } GovernorModel oldGovernorModel = null; if (viewModel.ReinstateAsGovernor && (viewModel.ReplaceGovernorViewModel?.GID.HasValue).GetValueOrDefault()) { oldGovernorModel = await _governorsReadService.GetGovernorAsync(viewModel.ReplaceGovernorViewModel.GID.Value, User); } var response = await _governorsWriteService.SaveAsync(governorModel, User); if (response.Success) { viewModel.GID = response.Response; ModelState.Clear(); if (viewModel.SelectedPreviousGovernorId.HasValue) { await RetireGovernorAsync(viewModel.SelectedPreviousGovernorId.Value, viewModel.ReplaceGovernorViewModel.AppointmentEndDate.ToDateTime().GetValueOrDefault()); if (viewModel.ReinstateAsGovernor && viewModel.ReplaceGovernorViewModel.GID.HasValue) { await ReInstateChairAsNonChairAsync(viewModel.ReplaceGovernorViewModel.GID.Value, governorModel.AppointmentStartDate.GetValueOrDefault(), (oldGovernorModel?.AppointmentEndDate).GetValueOrDefault(), viewModel.GovernorRole); } } var url = viewModel.EstablishmentUrn.HasValue ? $"{Url.RouteUrl("EstabDetails", new { id = viewModel.EstablishmentUrn, saved = true })}#school-governance" : $"{Url.RouteUrl("GroupDetails", new { id = viewModel.GroupUId, saved = true })}#governance"; return(Redirect(url)); } ErrorsToModelState <GovernorModel>(response.Errors); } await _layoutHelper.PopulateLayoutProperties(viewModel, viewModel.EstablishmentUrn, viewModel.GroupUId, User); return(View(viewModel)); }
public async Task <ActionResult> ReplaceChair(ReplaceChairViewModel model) { if (ModelState.IsValid) { if (model.NewChairType == ReplaceChairViewModel.ChairType.SharedChair) { var newGovernor = model.SharedGovernors.SingleOrDefault(s => s.Id == model.SelectedGovernorId); await _governorsWriteService.AddSharedGovernorAppointmentAsync(model.SelectedGovernorId, model.Urn.Value, model.DateTermEnds.ToDateTime().Value.AddDays(1), newGovernor.AppointmentEndDate.ToDateTime(), User); var url = $"{Url.RouteUrl("EstabDetails", new { id = model.Urn, saved = true })}#school-governance"; return(Redirect(url)); } else { var newGovernor = new GovernorModel { AppointingBodyId = model.NewLocalGovernor.AppointingBodyId, AppointmentEndDate = model.NewLocalGovernor.AppointmentEndDate.ToDateTime(), AppointmentStartDate = model.DateTermEnds.ToDateTime().Value.AddDays(1), DOB = model.NewLocalGovernor.DOB.ToDateTime(), EmailAddress = model.NewLocalGovernor.EmailAddress, EstablishmentUrn = model.Urn, Person_FirstName = model.NewLocalGovernor.FirstName, Person_MiddleName = model.NewLocalGovernor.MiddleName, Person_LastName = model.NewLocalGovernor.LastName, Person_TitleId = model.NewLocalGovernor.GovernorTitleId, PreviousPerson_FirstName = model.NewLocalGovernor.PreviousFirstName, PreviousPerson_MiddleName = model.NewLocalGovernor.PreviousMiddleName, PreviousPerson_LastName = model.NewLocalGovernor.PreviousLastName, PreviousPerson_TitleId = model.NewLocalGovernor.PreviousTitleId, PostCode = model.NewLocalGovernor.PostCode, RoleId = (int)(RoleEquivalence.GetLocalEquivalentToSharedRole(model.Role) ?? model.Role), TelephoneNumber = model.NewLocalGovernor.TelephoneNumber }; var validation = await _governorsWriteService.ValidateAsync(newGovernor, User); if (!validation.HasErrors) { await _governorsWriteService.SaveAsync(newGovernor, User); var url = $"{Url.RouteUrl("EstabDetails", new { id = model.Urn, saved = true })}#school-governance"; return(Redirect(url)); } validation.ApplyToModelState(ControllerContext, nameof(model.NewLocalGovernor)); } } var governor = await _governorsReadService.GetGovernorAsync(model.ExistingGovernorId, User); var roles = new List <eLookupGovernorRole> { (eLookupGovernorRole)governor.RoleId }; if (EnumSets.SharedGovernorRoles.Contains(governor.RoleId.Value)) { var localEquivalent = RoleEquivalence.GetLocalEquivalentToSharedRole((eLookupGovernorRole)governor.RoleId); if (localEquivalent != null) { roles.Add(localEquivalent.Value); } } else { roles.AddRange(RoleEquivalence.GetEquivalentToLocalRole((eLookupGovernorRole)governor.RoleId)); } var governors = (await _governorsReadService.GetSharedGovernorsAsync(model.Urn.Value, User)).Where(g => roles.Contains((eLookupGovernorRole)g.RoleId) && g.Id != model.ExistingGovernorId).ToList(); model.NewLocalGovernor.DisplayPolicy = await _governorsReadService.GetEditorDisplayPolicyAsync((RoleEquivalence.GetLocalEquivalentToSharedRole((eLookupGovernorRole)governor.RoleId.Value) ?? (eLookupGovernorRole)governor.RoleId.Value), false, User); model.SharedGovernors = (await Task.WhenAll(governors.Select(async g => await SharedGovernorViewModel.MapFromGovernor(g, model.Urn.Value, _cachedLookupService)))).ToList(); await PopulateSelectLists(model.NewLocalGovernor); await _layoutHelper.PopulateLayoutProperties(model, model.Urn, null, User); return(View(model)); }