internal async Task <GovernorsGridViewModel> CreateGovernorsViewModel(int?groupUId = null, int?establishmentUrn = null, EstablishmentModel establishmentModel = null, IPrincipal user = null)
        {
            user             = user ?? User;
            establishmentUrn = establishmentUrn ?? establishmentModel?.Urn;

            var domainModel = await _governorsReadService.GetGovernorListAsync(establishmentUrn, groupUId, user);

            var viewModel = new GovernorsGridViewModel(domainModel, false, groupUId, establishmentUrn, _nomenclatureService,
                                                       (await _cachedLookupService.NationalitiesGetAllAsync()), (await _cachedLookupService.GovernorAppointingBodiesGetAllAsync()));

            if (establishmentUrn.HasValue || establishmentModel != null)
            {
                var estabDomainModel = establishmentModel ?? (await _establishmentReadService.GetAsync(establishmentUrn.Value, user)).GetResult();
                var items            = await _establishmentReadService.GetPermissibleLocalGovernorsAsync(establishmentUrn.Value, user); // The API uses 1 as a default value, hence we have to call another API to deduce whether to show the Governance mode UI section

                viewModel.GovernanceMode = items.Any() ? estabDomainModel.GovernanceMode : null;
            }

            if (groupUId.HasValue)
            {
                var groupModel = (await _groupReadService.GetAsync(groupUId.Value, user)).GetResult();
                viewModel.ShowDelegationInformation = groupModel.GroupTypeId == (int)eLookupGroupType.MultiacademyTrust;
                viewModel.DelegationInformation     = groupModel.DelegationInformation;
            }

            return(viewModel);
        }
Example #2
0
        public async Task <ActionResult> Edit(int?groupUId, int?establishmentUrn, int?removalGid, int?duplicateGovernorId, bool roleAlreadyExists = false)
        {
            Guard.IsTrue(groupUId.HasValue || establishmentUrn.HasValue, () => new InvalidParameterException($"Both parameters '{nameof(groupUId)}' and '{nameof(establishmentUrn)}' are null."));

            var domainModel = await _governorsReadService.GetGovernorListAsync(establishmentUrn, groupUId, User);

            var governorPermissions = await _governorsReadService.GetGovernorPermissions(establishmentUrn, groupUId, User);

            var viewModel = new GovernorsGridViewModel(domainModel,
                                                       true,
                                                       groupUId,
                                                       establishmentUrn,
                                                       _nomenclatureService,
                                                       await _cachedLookupService.NationalitiesGetAllAsync(),
                                                       await _cachedLookupService.GovernorAppointingBodiesGetAllAsync(),
                                                       governorPermissions);

            var applicableRoles = domainModel.ApplicableRoles.Cast <int>();

            viewModel.GovernorRoles = (await _cachedLookupService.GovernorRolesGetAllAsync()).Where(x => applicableRoles.Contains(x.Id)).Select(x => new LookupItemViewModel(x)).ToList();

            await _layoutHelper.PopulateLayoutProperties(viewModel, establishmentUrn, groupUId, User, x => viewModel.GovernanceMode = x.GovernanceMode, x =>
            {
                viewModel.ShowDelegationAndCorpContactInformation = x.GroupTypeId.GetValueOrDefault() == (int)eLookupGroupType.MultiacademyTrust;
                viewModel.DelegationInformation = x.DelegationInformation;
                viewModel.CorporateContact      = x.CorporateContact;
            });

            viewModel.RemovalGid     = removalGid;
            viewModel.GovernorShared = false;
            if (removalGid.HasValue)
            {
                var govToBeRemoved = domainModel.CurrentGovernors.SingleOrDefault(g => g.Id == removalGid.Value);
                if (govToBeRemoved != null && EnumSets.SharedGovernorRoles.Contains(govToBeRemoved.RoleId.Value))
                {
                    viewModel.GovernorShared = true;
                }
            }

            if (duplicateGovernorId.HasValue)
            {
                var duplicate = await _governorsReadService.GetGovernorAsync(duplicateGovernorId.Value, User);

                ViewData.Add("DuplicateGovernor", duplicate);
            }

            if (roleAlreadyExists)
            {
                ModelState.AddModelError("role", "The selected role already contains an appointee.");
            }

            return(View(VIEW_EDIT_GOV_VIEW_NAME, viewModel));
        }
Example #3
0
        internal async Task <GovernorsGridViewModel> CreateGovernorsViewModel(int?groupUId = null, int?establishmentUrn = null, EstablishmentModel establishmentModel = null, IPrincipal user = null)
        {
            user             = user ?? User;
            establishmentUrn = establishmentUrn ?? establishmentModel?.Urn;
            GovernorsGridViewModel viewModel;

            try
            {
                var domainModel = await _governorsReadService.GetGovernorListAsync(establishmentUrn, groupUId, user);

                var governorPermissions = await _governorsReadService.GetGovernorPermissions(establishmentUrn, groupUId, user);

                viewModel = new GovernorsGridViewModel(domainModel,
                                                       false,
                                                       groupUId,
                                                       establishmentUrn,
                                                       _nomenclatureService,
                                                       await _cachedLookupService.NationalitiesGetAllAsync(),
                                                       await _cachedLookupService.GovernorAppointingBodiesGetAllAsync(),
                                                       governorPermissions);

                if (establishmentUrn.HasValue || establishmentModel != null)
                {
                    var estabDomainModel = establishmentModel ?? (await _establishmentReadService.GetAsync(establishmentUrn.Value, user)).GetResult();
                    var items            = await _establishmentReadService.GetPermissibleLocalGovernorsAsync(establishmentUrn.Value, user); // The API uses 1 as a default value, hence we have to call another API to deduce whether to show the Governance mode UI section

                    viewModel.GovernanceMode = items.Any() ? estabDomainModel.GovernanceMode : null;
                }

                if (groupUId.HasValue)
                {
                    var groupModel = (await _groupReadService.GetAsync(groupUId.Value, user)).GetResult();
                    viewModel.ShowDelegationAndCorpContactInformation = groupModel.GroupTypeId == (int)eLookupGroupType.MultiacademyTrust;
                    viewModel.DelegationInformation = groupModel.DelegationInformation;
                    viewModel.CorporateContact      = groupModel.CorporateContact;
                }
            }
            catch (Exception)   // to more gracefully handle Texuna services UsageQuotaExceededException
            {
                viewModel = new GovernorsGridViewModel {
                    DomainModel = new GovernorsDetailsDto()
                };
            }

            return(viewModel);
        }
Example #4
0
 public ActionResult View(int?groupUId, int?establishmentUrn, GovernorsGridViewModel viewModel = null)
 {
     if (viewModel != null)
     {
         return(View(VIEW_EDIT_GOV_VIEW_NAME, viewModel));
     }
     else
     {
         // KHD Hack: Async child actions are not supported; but we have an async stack, so we have to wrap the async calls in an sync wrapper.  Hopefully won't deadlock.
         // Need to use ASP.NET Core really now; that supports ViewComponents which are apparently the solution.
         return(Task.Run(async() =>
         {
             viewModel = await CreateGovernorsViewModel(groupUId, establishmentUrn);
             return View(VIEW_EDIT_GOV_VIEW_NAME, viewModel);
         }).Result);
     }
 }
Example #5
0
        public async Task <ActionResult> DeleteOrRetireGovernor(GovernorsGridViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                if (viewModel.Action == "Save") // retire selected governor with the chosen appt. end date
                {
                    if (viewModel.GovernorShared.HasValue && viewModel.GovernorShared.Value && viewModel.EstablishmentUrn.HasValue)
                    {
                        var sharedGovernor = await _governorsReadService.GetGovernorAsync(viewModel.RemovalGid.Value, User);

                        var appointment = sharedGovernor.Appointments.Single(a => a.EstablishmentUrn == viewModel.EstablishmentUrn.Value);

                        var response = await _governorsWriteService.UpdateSharedGovernorAppointmentAsync(viewModel.RemovalGid.Value,
                                                                                                         viewModel.EstablishmentUrn.Value, appointment.AppointmentStartDate.Value,
                                                                                                         viewModel.RemovalAppointmentEndDate.ToDateTime().Value, User);

                        response.ApplyToModelState(ControllerContext);
                    }
                    else
                    {
                        var response = await _governorsWriteService.UpdateDatesAsync(viewModel.RemovalGid.Value, viewModel.RemovalAppointmentEndDate.ToDateTime().Value, User);

                        if (!response.Success)
                        {
                            if (response.Errors.Length == 0)
                            {
                                throw new TexunaApiSystemException($"The TEX-API said no (but gave no details!)...");
                            }

                            response.ApplyToModelState(ControllerContext);
                        }
                    }
                }
                else if (viewModel.Action == "Remove") // mark the governor record as deleted
                {
                    if (viewModel.GovernorShared.GetValueOrDefault() && viewModel.EstablishmentUrn.HasValue)
                    {
                        await _governorsWriteService.DeleteSharedGovernorAppointmentAsync(viewModel.RemovalGid.Value, viewModel.EstablishmentUrn.Value, User);
                    }
                    else
                    {
                        await _governorsWriteService.DeleteAsync(viewModel.RemovalGid.Value, User);
                    }
                }
                else
                {
                    throw new InvalidParameterException($"The parameter for action is invalid: '{viewModel.Action}'");
                }

                if (ModelState.IsValid)
                {
                    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));
                }
            }

            await _layoutHelper.PopulateLayoutProperties(viewModel, viewModel.EstablishmentUrn, viewModel.GroupUId, User);

            return(await Edit(viewModel.GroupUId, viewModel.EstablishmentUrn, viewModel.RemovalGid, null));
        }