public IActionResult SaveIsPreviousPrincipalAuthority(IsPreviousPrincipalAuthorityViewModel model) { Session.SetSubmittedPage(FormSection.DirectorOrPartner, 1); if (!ModelState.IsValid || !model.IsPreviousPrincipalAuthority.HasValue) { return(View(GetViewPath(FormSection.DirectorOrPartner, 1), model)); } var dopId = LicenceApplicationPostDataHandler.Update(Session.GetCurrentLicenceId(), l => l.DirectorOrPartners, model, Session.GetCurrentDopId()); Session.SetCurrentDopStatus(dopId, model.IsPreviousPrincipalAuthority.Value); if (model.IsPreviousPrincipalAuthority.Value) { var paId = LicenceApplicationPostDataHandler.UpsertPrincipalAuthorityAndLinkToDirectorOrPartner( Session.GetCurrentLicenceId(), dopId, Session.GetCurrentPaId(), model); Session.SetCurrentPaStatus(paId, true); } else { LicenceApplicationPostDataHandler.UnlinkPrincipalAuthorityFromDirectorOrPartner(Session.GetCurrentDopId()); } return(CheckParentValidityAndRedirect(FormSection.DirectorOrPartner, 1)); }
public void it_creates_and_updates_a_new_principal_authority_if_one_does_not_exist() { const int licenceId = 1; const int paId = 2; const int dopId = 3; const string expectedName = "Name"; var licence = new Licence { Id = licenceId, PrincipalAuthorities = new List <PrincipalAuthority>() }; var model = new FullNameViewModel { FullName = expectedName }; licenceRepository.GetById(licenceId).Returns(licence); repository.GetById <DirectorOrPartner>(dopId).Returns(new DirectorOrPartner { Id = dopId }); repository.Create <PrincipalAuthority>().Returns(new PrincipalAuthority { Id = paId }); repository.Upsert(Arg.Any <PrincipalAuthority>()).Returns(paId); var pdh = new LicenceApplicationPostDataHandler(mapper, repository, licenceRepository, statusRepository, dateTimeProvider); var result = pdh.UpsertPrincipalAuthorityAndLinkToDirectorOrPartner(licenceId, dopId, paId, model); Assert.AreEqual(paId, result); repository.Received(1).Upsert(Arg.Is <PrincipalAuthority>(p => p.FullName.Equals(expectedName) && p.DirectorOrPartner.Id == dopId && p.Licence.Id == licenceId)); repository.Received(1).Create <PrincipalAuthority>(); repository.Received(1).GetById <DirectorOrPartner>(dopId); }