public async Task <ActionResult> SelectSharedGovernor(SelectSharedGovernorViewModel model)
        {
            if (ModelState.IsValid)
            {
                foreach (var governor in model.Governors.Where(g => (g.Selected && !g.PreExisting) || string.Equals(g.Id.ToString(), model.SelectedGovernorId)))
                {
                    var response = await _governorsWriteService.AddSharedGovernorAppointmentAsync(governor.Id, model.Urn.Value, governor.AppointmentStartDate.ToDateTime().Value, governor.AppointmentEndDate.ToDateTime(), User);

                    if (!response.Success)
                    {
                        response.ApplyToModelState(ControllerContext);
                    }
                }

                if (ModelState.IsValid)
                {
                    var url = $"{Url.RouteUrl("EstabDetails", new { id = model.Urn, saved = true })}#school-governance";

                    return(Redirect(url));
                }
            }

            var governors = (await Task.WhenAll(
                                 (await _governorsReadService.GetSharedGovernorsAsync(model.Urn.Value, User))
                                 .Where(g => RoleEquivalence.GetEquivalentRole(model.Role).Contains((eLookupGovernorRole)g.RoleId))
                                 .Select(g => SharedGovernorViewModel.MapFromGovernor(g, model.Urn.Value, _cachedLookupService)))).ToList();

            foreach (var previousGovernor in model.Governors)
            {
                var newGovernor = governors.Single(g => g.Id == previousGovernor.Id);
                if (!newGovernor.PreExisting)
                {
                    newGovernor.Selected = previousGovernor.Selected;
                }
            }

            model.Governors = governors;
            await _layoutHelper.PopulateLayoutProperties(model, model.Urn.Value, null, User);

            return(View(model));
        }
Ejemplo n.º 2
0
        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> 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));
        }