Esempio n. 1
0
        private async Task <ActionResult> AddFacility(AddFacilityViewModelBase viewModel)
        {
            PreventSiteAddressNameValidationErrors();
            SetBreadcrumb(viewModel.FacilityType);
            viewModel = await PopulateViewModelLists(viewModel);

            if (!ModelState.IsValid)
            {
                if (!viewModel.ModelValidated)
                {
                    ModelState.RunCustomValidation(viewModel);
                }

                ModelState.ApplyCustomValidationSummaryOrdering(AddFacilityViewModelBase.ValidationMessageDisplayOrder);
                return(View(nameof(Add), viewModel));
            }

            using (var client = apiClient())
            {
                var result = await validationWrapper.Validate(User.GetAccessToken(), viewModel);

                if (!result.IsValid)
                {
                    ModelState.AddModelError("ApprovalNumber", Constants.ApprovalNumberExistsError);
                    return(View(nameof(Add), viewModel));
                }

                var request = new AddAatf()
                {
                    Aatf           = AatfHelper.CreateFacilityData(viewModel),
                    AatfContact    = viewModel.ContactData,
                    OrganisationId = viewModel.OrganisationId,
                };

                await client.SendAsync(User.GetAccessToken(), request);

                await cache.InvalidateAatfCache(request.OrganisationId);

                await client.SendAsync(User.GetAccessToken(), new CompleteOrganisationAdmin()
                {
                    OrganisationId = viewModel.OrganisationId
                });

                await cache.InvalidateOrganisationSearch();

                return(RedirectToAction("ManageAatfs", "Aatf", new { viewModel.FacilityType }));
            }
        }
Esempio n. 2
0
        public async Task AddAatfPost_ValidViewModel_ReturnsRedirect()
        {
            AddAatfViewModel viewModel = new AddAatfViewModel()
            {
                SizeValue   = 1,
                StatusValue = 1
            };

            var validationResult = new ValidationResult();

            A.CallTo(() => validationWrapper.Validate(A <string> ._, viewModel)).Returns(validationResult);
            A.CallTo(() => weeeClient.SendAsync(A <string> ._, A <GetApiUtcDate> ._)).Returns(new DateTime(2019, 1, 1));

            RedirectToRouteResult result = await controller.AddAatf(viewModel) as RedirectToRouteResult;

            result.RouteValues["action"].Should().Be("ManageAatfs");
            result.RouteValues["controller"].Should().Be("Aatf");
        }
Esempio n. 3
0
        private async Task <ActionResult> ManageFacilityDetails(FacilityViewModelBase viewModel)
        {
            PreventSiteAddressNameValidationErrors();
            SetBreadcrumb(viewModel.FacilityType, null);

            using (var client = apiClient())
            {
                var doesApprovalNumberExist = false;

                var existingAatf = await client.SendAsync(User.GetAccessToken(), new GetAatfById(viewModel.Id));

                if (existingAatf.ApprovalNumber != viewModel.ApprovalNumber)
                {
                    var result = await validationWrapper.Validate(User.GetAccessToken(), viewModel);

                    if (!result.IsValid)
                    {
                        doesApprovalNumberExist = true;
                    }
                }

                if (ModelState.IsValid && !doesApprovalNumberExist)
                {
                    viewModel.CompetentAuthoritiesList = await client.SendAsync(User.GetAccessToken(), new GetUKCompetentAuthorities());

                    viewModel.PanAreaList = await client.SendAsync(User.GetAccessToken(), new GetPanAreas());

                    viewModel.LocalAreaList = await client.SendAsync(User.GetAccessToken(), new GetLocalAreas());

                    var request = detailsRequestCreator.ViewModelToRequest(viewModel);

                    if (existingAatf.ApprovalDate != viewModel.ApprovalDate)
                    {
                        var approvalDateFlags = await client.SendAsync(User.GetAccessToken(),
                                                                       new CheckAatfApprovalDateChange(existingAatf.Id, viewModel.ApprovalDate.Value));

                        if (approvalDateFlags.HasFlag(CanApprovalDateBeChangedFlags.DateChanged))
                        {
                            TempData["aatfRequest"] = request;

                            return(RedirectToAction(nameof(UpdateApproval), new { id = existingAatf.Id, organisationId = existingAatf.Organisation.Id }));
                        }
                    }

                    await client.SendAsync(User.GetAccessToken(), request);

                    await cache.InvalidateAatfCache(existingAatf.Organisation.Id);

                    return(Redirect(Url.Action("Details", new { area = "Admin", viewModel.Id })));
                }

                if (!viewModel.ModelValidated)
                {
                    ModelState.RunCustomValidation(viewModel);
                }

                var accessToken = User.GetAccessToken();
                viewModel.StatusList = Enumeration.GetAll <AatfStatus>();
                viewModel.SizeList   = Enumeration.GetAll <AatfSize>();
                viewModel.CompetentAuthoritiesList = await client.SendAsync(accessToken, new GetUKCompetentAuthorities());

                viewModel.PanAreaList = await client.SendAsync(accessToken, new GetPanAreas());

                viewModel.LocalAreaList = await client.SendAsync(accessToken, new GetLocalAreas());

                viewModel.SiteAddressData.Countries = await client.SendAsync(accessToken, new GetCountries(false));

                ModelState.ApplyCustomValidationSummaryOrdering(FacilityViewModelBase.ValidationMessageDisplayOrder);

                if (doesApprovalNumberExist)
                {
                    ModelState.AddModelError("ApprovalNumber", Constants.ApprovalNumberExistsError);
                }

                return(View(nameof(ManageAatfDetails), viewModel));
            }
        }