public void ViewModelToRequest_GivenValidViewModel_PropertiesShouldBeMapped() { var viewModel = fixture.Build <AatfEditDetailsViewModel>() .With(e => e.StatusValue, AatfStatus.Approved.Value) .With(e => e.SizeValue, AatfSize.Large.Value) .Create(); viewModel.CompetentAuthorityId = viewModel.CompetentAuthoritiesList.Last().Abbreviation; var result = requestCreator.ViewModelToRequest(viewModel); result.Should().NotBeNull(); result.Data.Id.Should().Be(viewModel.Id); result.Data.Name.Should().Be(viewModel.Name); result.Data.ApprovalNumber.Should().Be(viewModel.ApprovalNumber); result.Data.CompetentAuthority.Abbreviation.Should().Be(viewModel.CompetentAuthorityId); result.Data.AatfStatus.Should().Be(AatfStatus.Approved); result.Data.SiteAddress.Should().Be(viewModel.SiteAddressData); result.Data.Size.Should().Be(AatfSize.Large); result.Data.FacilityType.Should().Be(viewModel.FacilityType); result.Data.ApprovalDate.Should().Be(viewModel.ApprovalDate.GetValueOrDefault()); result.Data.AatfSizeValue.Should().Be(viewModel.SizeValue); result.Data.AatfStatusValue.Should().Be(viewModel.StatusValue); }
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)); } }