From 8b40e41e0c2a9394ef67c94a9819fa2243c67a78 Mon Sep 17 00:00:00 2001 From: VasanthaKasirajan3008 <56582101+VasanthaKasirajan3008@users.noreply.github.com> Date: Wed, 21 Apr 2021 15:50:00 +0100 Subject: [PATCH] Con 3093 migrate stop date (#190) * CON-3093-Migrate Edit Stop Date * CON-3093-Fix mapping and test * CON-3093 * con-3093 * CON-3093 - PR comment * Revert "Merge branch 'master' into CON-3093-migrate-stop-date" This reverts commit 374ed6c65db7a3fb80f7020a16b14aca80dc347c, reversing changes made to 0a080963ef50215caeed6135024e3028156f52a1. * Revert "Revert "Merge branch 'master' into CON-3093-migrate-stop-date"" This reverts commit c8bda47e51277c61b90ebb8433db3e4bba99121d. * Revert "Revert "Revert "Merge branch 'master' into CON-3093-migrate-stop-date""" This reverts commit 669f94a22aca1c382e9cc1f9868770407b23f948. * Revert "Revert "Revert "Revert "Merge branch 'master' into CON-3093-migrate-stop-date"""" This reverts commit 915a1173b003fbc8ff21a5606835be9ae1083f89. * con-3093 * CON-3093- changed to V2 link * CON-3093 * CON-3093-updated package --- .../WhenCallingEditStopDateTests.cs | 61 ++++++++++ .../WhenPostingEditStopDateTests.cs | 83 +++++++++++++ ...itStopDateRequestToViewModelMapperTests.cs | 109 ++++++++++++++++++ ...pprenticeshipStopDateRequestMapperTests.cs | 53 +++++++++ ...EmployerCommitmentsV2.Web.UnitTests.csproj | 4 +- .../EditStopDateViewModelValidatorTests.cs | 86 ++++++++++++++ .../Controllers/ApprenticeController.cs | 33 +++++- .../EditStopDateRequestToViewModelMapper.cs | 49 ++++++++ ...elToApprenticeshipStopDateRequestMapper.cs | 32 +++++ .../Models/Apprentice/EditStopDateRequest.cs | 16 +++ .../Apprentice/EditStopDateViewModel.cs | 39 +++++++ .../SFA.DAS.EmployerCommitmentsV2.Web.csproj | 8 +- .../EditStopDateViewModelValidator.cs | 29 +++++ .../Views/Apprentice/Details.cshtml | 4 +- .../Views/Apprentice/EditStopDate.cshtml | 73 ++++++++++++ .../SFA.DAS.EmployerCommitmentsV2.csproj | 6 +- 16 files changed, 670 insertions(+), 15 deletions(-) create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenCallingEditStopDateTests.cs create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingEditStopDateTests.cs create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Mappers/Apprentice/EditStopDateRequestToViewModelMapperTests.cs create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Mappers/Apprentice/EditStopDateViewModelToApprenticeshipStopDateRequestMapperTests.cs create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/EditStopDateViewModelValidatorTests.cs create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web/Mappers/Apprentice/EditStopDateRequestToViewModelMapper.cs create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web/Mappers/Apprentice/EditStopDateViewModelToApprenticeshipStopDateRequestMapper.cs create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web/Models/Apprentice/EditStopDateRequest.cs create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web/Models/Apprentice/EditStopDateViewModel.cs create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web/Validators/EditStopDateViewModelValidator.cs create mode 100644 src/SFA.DAS.EmployerCommitmentsV2.Web/Views/Apprentice/EditStopDate.cshtml diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenCallingEditStopDateTests.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenCallingEditStopDateTests.cs new file mode 100644 index 000000000..af7736dc9 --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenCallingEditStopDateTests.cs @@ -0,0 +1,61 @@ +using AutoFixture; +using Microsoft.AspNetCore.Mvc; +using Moq; +using NUnit.Framework; +using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice; +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests +{ + public class WhenCallingEditStopDateTests + { + WhenCallingEditStopDateTestsFixture _fixture; + + [SetUp] + public void Arrange() + { + _fixture = new WhenCallingEditStopDateTestsFixture(); + } + + [Test] + public async Task ThenTheCorrectViewIsReturned() + { + //Act + var result = await _fixture.EditStopDate(); + + //Assert + _fixture.VerifyViewModel(result as ViewResult); + } + } + + public class WhenCallingEditStopDateTestsFixture : ApprenticeControllerTestFixtureBase + { + private readonly EditStopDateRequest _request; + private readonly EditStopDateViewModel _viewModel; + + public WhenCallingEditStopDateTestsFixture() : base() + { + _request = _autoFixture.Create(); + _autoFixture.Customize(c => c.Without(x => x.NewStopDate)); + _viewModel = _autoFixture.Create(); + _viewModel.NewStopDate = new CommitmentsV2.Shared.Models.MonthYearModel("062020"); + + _mockMapper.Setup(m => m.Map(_request)) + .ReturnsAsync(_viewModel); + } + + public async Task EditStopDate() + { + return await _controller.EditStopDate(_request); + } + + public void VerifyViewModel(ViewResult viewResult) + { + var viewModel = viewResult.Model as EditStopDateViewModel; + + Assert.IsInstanceOf(viewModel); + Assert.AreEqual(_viewModel, viewModel); + } + + } +} diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingEditStopDateTests.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingEditStopDateTests.cs new file mode 100644 index 000000000..e6c902385 --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Controllers/ApprenticeControllerTests/WhenPostingEditStopDateTests.cs @@ -0,0 +1,83 @@ +using AutoFixture; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Microsoft.Extensions.Logging; +using Moq; +using NUnit.Framework; +using SFA.DAS.Authorization.Services; +using SFA.DAS.CommitmentsV2.Api.Client; +using SFA.DAS.CommitmentsV2.Api.Types.Requests; +using SFA.DAS.CommitmentsV2.Shared.Interfaces; +using SFA.DAS.EmployerCommitmentsV2.Web.Controllers; +using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice; +using SFA.DAS.EmployerUrlHelper; +using System.Threading; +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Controllers.ApprenticeControllerTests +{ + public class WhenPostingEditStopDateTests : ApprenticeControllerTestBase + { + private const string ApprenticeEditStopDate = "New stop date confirmed"; + private const string FlashMessage = "FlashMessage"; + private const string FlashMessageLevel = "FlashMessageLevel"; + private Fixture _autoFixture; + private EditStopDateViewModel _viewModel; + + [SetUp] + public void Arrange() + { + _mockModelMapper = new Mock(); + _mockCookieStorageService = new Mock>(); + _mockCommitmentsApiClient = new Mock(); + _mockLinkGenerator = new Mock(); + _autoFixture = new Fixture(); + _autoFixture.Customize(c => c.Without(x => x.NewStopDate)); + _viewModel = _autoFixture.Create(); + _viewModel.NewStopDate = new CommitmentsV2.Shared.Models.MonthYearModel("062020"); + + _controller = new ApprenticeController(_mockModelMapper.Object, + _mockCookieStorageService.Object, + _mockCommitmentsApiClient.Object, + _mockLinkGenerator.Object, + Mock.Of>(), + Mock.Of()); + _controller.TempData = new TempDataDictionary(new Mock().Object, new Mock().Object); + } + + [Test] + public async Task AndSubmit_ThenCommitmentsApiUpdateApprenticeshipStopDateIsCalled() + { + //Act + var result = await _controller.UpdateApprenticeshipStopDate(_viewModel) as RedirectToActionResult; + + //Assert + _mockCommitmentsApiClient.Verify(p => + p.UpdateApprenticeshipStopDate(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); + } + + [Test] + public async Task AndAfterUpdateDate_ThenRedirectToApprenticeDetailsPage() + { + //Act + var result = await _controller.UpdateApprenticeshipStopDate(_viewModel) as RedirectToActionResult; + + //Assert + Assert.AreEqual("ApprenticeshipDetails", result.ActionName); + } + + [Test] + public async Task AndAfterUpdateDate_ThenRedirectToApprenticeDetailsPageWithFlashMessage() + { + //Act + var result = await _controller.UpdateApprenticeshipStopDate(_viewModel) as RedirectToActionResult; + + //Assert + Assert.AreEqual("ApprenticeshipDetails", result.ActionName); + Assert.IsTrue(_controller.TempData.Values.Contains(ApprenticeEditStopDate)); + Assert.IsTrue(_controller.TempData.ContainsKey(FlashMessage)); + Assert.IsTrue(_controller.TempData.ContainsKey(FlashMessageLevel)); + } + } +} diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Mappers/Apprentice/EditStopDateRequestToViewModelMapperTests.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Mappers/Apprentice/EditStopDateRequestToViewModelMapperTests.cs new file mode 100644 index 000000000..89fe57437 --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Mappers/Apprentice/EditStopDateRequestToViewModelMapperTests.cs @@ -0,0 +1,109 @@ +using AutoFixture; +using Microsoft.Extensions.Logging; +using Moq; +using NUnit.Framework; +using SFA.DAS.CommitmentsV2.Api.Client; +using SFA.DAS.CommitmentsV2.Api.Types.Responses; +using SFA.DAS.EmployerCommitmentsV2.Web.Mappers.Apprentice; +using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice; +using System.Threading; +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Mappers.Apprentice +{ + public class EditStopDateRequestToViewModelMapperTests + { + private Mock _mockCommitmentsApiClient; + private Mock> _logger; + private EditStopDateRequest _request; + private GetApprenticeshipResponse _apprenticeshipResponse; + private EditStopDateRequestToViewModelMapper _mapper; + + [SetUp] + public void Arrange() + { + var _autoFixture = new Fixture(); + _request = _autoFixture.Create(); + + _mockCommitmentsApiClient = new Mock(); + _logger = new Mock>(); + _apprenticeshipResponse = _autoFixture.Create(); + _mockCommitmentsApiClient = new Mock(); + _mockCommitmentsApiClient.Setup(m => m.GetApprenticeship(It.IsAny(), It.IsAny())) + .ReturnsAsync(_apprenticeshipResponse); + + + _mapper = new EditStopDateRequestToViewModelMapper(_mockCommitmentsApiClient.Object, _logger.Object); + } + + [Test] + public async Task ApprenticeshipHashedId_IsMapped() + { + //Act + var result = await _mapper.Map(_request); + + //Assert + Assert.AreEqual(_request.ApprenticeshipHashedId, result.ApprenticeshipHashedId); + } + + [Test] + public async Task AccountHashedId_IsMapped() + { + //Act + var result = await _mapper.Map(_request); + + //Assert + Assert.AreEqual(_request.AccountHashedId, result.AccountHashedId); + } + + [Test] + public async Task ApprenticeshipId_IsMapped() + { + //Act + var result = await _mapper.Map(_request); + + //Assert + Assert.AreEqual(_request.ApprenticeshipId, result.ApprenticeshipId); + } + + [Test] + public async Task WhenRequestingEditStopDate_ThenTheGetApprenticeshipIsCalled() + { + //Act + var result = await _mapper.Map(_request); + + //Assert + _mockCommitmentsApiClient.Verify(m => m.GetApprenticeship(_request.ApprenticeshipId, It.IsAny()), Times.Once()); + } + + [Test] + public async Task ApprenticeshipULN_IsMapped() + { + //Act + var result = await _mapper.Map(_request); + + //Assert + Assert.AreEqual(_apprenticeshipResponse.Uln, result.ApprenticeshipULN); + } + + [Test] + public async Task ApprenticeshipStartDate_IsMapped() + { + //Act + var result = await _mapper.Map(_request); + + //Assert + Assert.AreEqual(_apprenticeshipResponse.StartDate, result.ApprenticeshipStartDate); + } + + [Test] + public async Task CurrentStopDate_IsMapped() + { + //Act + var result = await _mapper.Map(_request); + + //Assert + Assert.AreEqual(_apprenticeshipResponse.StopDate.Value, result.CurrentStopDate); + } + } +} diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Mappers/Apprentice/EditStopDateViewModelToApprenticeshipStopDateRequestMapperTests.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Mappers/Apprentice/EditStopDateViewModelToApprenticeshipStopDateRequestMapperTests.cs new file mode 100644 index 000000000..2fb3b4b47 --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Mappers/Apprentice/EditStopDateViewModelToApprenticeshipStopDateRequestMapperTests.cs @@ -0,0 +1,53 @@ +using AutoFixture; +using Moq; +using NUnit.Framework; +using SFA.DAS.CommitmentsV2.Types; +using SFA.DAS.EmployerCommitmentsV2.Web.Authentication; +using SFA.DAS.EmployerCommitmentsV2.Web.Mappers.Apprentice; +using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice; +using System; +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Mappers.Apprentice +{ + public class EditStopDateViewModelToApprenticeshipStopDateRequestMapperTests + { + private EditStopDateViewModelToApprenticeshipStopDateRequestMapper _mapper; + private Mock _mockAuthenticationService; + private Fixture _autoFixture; + private EditStopDateViewModel _viewModel; + + [SetUp] + public void Arrange() + { + _mockAuthenticationService = new Mock(); + _mockAuthenticationService.Setup(x => x.UserInfo).Returns(It.IsAny()); + _autoFixture = new Fixture(); + _autoFixture.Customize(c => c.Without(x => x.NewStopDate)); + _mapper = new EditStopDateViewModelToApprenticeshipStopDateRequestMapper(_mockAuthenticationService.Object); + _viewModel = _autoFixture.Create(); + _viewModel.NewStopDate = new CommitmentsV2.Shared.Models.MonthYearModel("062020"); + } + + [Test] + public async Task ApprenticeshipHashedId_IsMapped() + { + //Act + var result = await _mapper.Map(_viewModel); + + //Assert + Assert.AreEqual(_viewModel.AccountId, result.AccountId); + } + + [Test] + public async Task NewStopDate_IsMapped() + { + //Act + var result = await _mapper.Map(_viewModel); + + //Assert + Assert.AreEqual(new DateTime(_viewModel.NewStopDate.Year.Value, _viewModel.NewStopDate.Month.Value, _viewModel.NewStopDate.Day.Value), result.NewStopDate); + } + + } +} diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.csproj b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.csproj index 868f78038..b52a2a326 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.csproj +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.csproj @@ -17,8 +17,8 @@ - - + + diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/EditStopDateViewModelValidatorTests.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/EditStopDateViewModelValidatorTests.cs new file mode 100644 index 000000000..22f1dd77a --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web.UnitTests/Validators/EditStopDateViewModelValidatorTests.cs @@ -0,0 +1,86 @@ +using System; +using System.Linq; +using NUnit.Framework; +using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice; +using SFA.DAS.EmployerCommitmentsV2.Web.Validators; +using AutoFixture; +using SFA.DAS.CommitmentsV2.Shared.Interfaces; +using Moq; + +namespace SFA.DAS.EmployerCommitmentsV2.Web.UnitTests.Validators +{ + public class EditStopDateViewModelValidatorTests + { + private EditStopDateViewModelValidator _validator; + private Fixture _autoFixture; + private Mock _currentDateTime; + + [SetUp] + public void Arrange() + { + _autoFixture = new Fixture(); + _autoFixture.Customize(c => + c.With(m => m.ApprenticeshipStartDate, new DateTime(2020, 9, 1)) + .With(m => m.CurrentStopDate, new DateTime(2021, 2, 1)) + .With(m => m.NewStopDate, new CommitmentsV2.Shared.Models.MonthYearModel("032021"))); + _currentDateTime = new Mock(); + _currentDateTime.Setup(x => x.UtcNow).Returns(new DateTime(2021, 3 ,1)); + _validator = new EditStopDateViewModelValidator(_currentDateTime.Object); + } + + + [TestCase("5143541", true)] + [TestCase(" ", false)] + [TestCase("", false)] + [TestCase(null, false)] + public void WhenValidatingEditStopDate_ValidateTheAccountHashedId(string accountHashedId, bool expectedValid) + { + //Arrange + var viewModel = _autoFixture.Create(); + viewModel.AccountHashedId = accountHashedId; + + //Act + var result = _validator.Validate(viewModel); + + //Assert + Assert.AreEqual(expectedValid, result.IsValid); + } + + [TestCase("5143541", true)] + [TestCase(" ", false)] + [TestCase("", false)] + [TestCase(null, false)] + public void WhenValidatingEditStopDate_ValidateTheApprenticeshipHashedId(string apprenticeshipHashedId, bool expectedValid) + { + //Arrange + var viewModel = _autoFixture.Create(); + viewModel.ApprenticeshipHashedId = apprenticeshipHashedId; + + //Act + var result = _validator.Validate(viewModel); + + //Assert + Assert.AreEqual(expectedValid, result.IsValid); + } + + [TestCase(null, null, false, "Enter the stop date for this apprenticeship")] + [TestCase(0, 0, false, "Enter the stop date for this apprenticeship")] + [TestCase(07, 2021, false, "The stop date cannot be in the future")] + [TestCase(08, 2020, false, "The stop month cannot be before the apprenticeship started")] + [TestCase(02, 2021, false, "Enter a date that is different to the current stopped date")] + public void WhenValidatingEditStopDate_ValidateTheNewStopDate(int? newStopMonth, int? newStopYear, bool expectedValid, string errorMessage) + { + //Arrange + var viewModel = _autoFixture.Create(); + viewModel.NewStopMonth = newStopMonth; + viewModel.NewStopYear = newStopYear; + + //Act + var result = _validator.Validate(viewModel); + + //Assert + Assert.AreEqual(expectedValid, result.IsValid); + if (errorMessage != null) Assert.AreEqual(errorMessage, result.Errors.Single().ErrorMessage); + } + } +} diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ApprenticeController.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ApprenticeController.cs index ab77e7637..63d83675a 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ApprenticeController.cs +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Controllers/ApprenticeController.cs @@ -37,6 +37,8 @@ public class ApprenticeController : Controller private const string ApprenticePausedMessage = "Apprenticeship paused"; private const string ApprenticeResumeMessage = "Apprenticeship resumed"; private const string ApprenticeStoppedMessage = "Apprenticeship stopped"; + private const string ApprenticeEditStopDate = "New stop date confirmed"; + private const string FlashMessageTempDataKey = "FlashMessage"; public ApprenticeController(IModelMapper modelMapper, ICookieStorageService cookieStorage, ICommitmentsApiClient commitmentsApiClient, ILinkGenerator linkGenerator, ILogger logger, IAuthorizationService authorizationService) { @@ -496,13 +498,36 @@ public async Task ResumeApprenticeship(ResumeRequestViewModel vie public async Task ApprenticeshipDetails(ApprenticeshipDetailsRequest request) { var viewModel = await _modelMapper.Map(request); + viewModel.IsV2Edit = _authorizationService.IsAuthorized(EmployerFeature.EditApprenticeV2); - if (viewModel.ApprenticeshipStatus == ApprenticeshipStatus.Stopped) - { - TempData.AddFlashMessage(ApprenticeStoppedMessage, ITempDataDictionaryExtensions.FlashMessageLevel.Success); - } + + if (!TempData.ContainsKey(FlashMessageTempDataKey) && viewModel.ApprenticeshipStatus == ApprenticeshipStatus.Stopped) + { + TempData.AddFlashMessage(ApprenticeStoppedMessage, ITempDataDictionaryExtensions.FlashMessageLevel.Success); + } return View("details", viewModel); + } + + [HttpGet] + [Route("{apprenticeshipHashedId}/details/editstopdate", Name = "EditStopDateOption")] + public async Task EditStopDate(EditStopDateRequest request) + { + var viewModel = await _modelMapper.Map(request); + return View(viewModel); + } + + [HttpPost] + [Route("{apprenticeshipHashedId}/details/editstopdate", Name = "PostEditStopDate")] + public async Task UpdateApprenticeshipStopDate(EditStopDateViewModel viewModel) + { + var request = await _modelMapper.Map(viewModel); + + await _commitmentsApiClient.UpdateApprenticeshipStopDate(viewModel.ApprenticeshipId, request, CancellationToken.None); + + TempData.AddFlashMessage(ApprenticeEditStopDate, ITempDataDictionaryExtensions.FlashMessageLevel.Success); + + return RedirectToAction(nameof(ApprenticeshipDetails), new { viewModel.AccountHashedId, viewModel.ApprenticeshipHashedId }); } [HttpGet] diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Mappers/Apprentice/EditStopDateRequestToViewModelMapper.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Mappers/Apprentice/EditStopDateRequestToViewModelMapper.cs new file mode 100644 index 000000000..8cb1fc772 --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Mappers/Apprentice/EditStopDateRequestToViewModelMapper.cs @@ -0,0 +1,49 @@ +using Microsoft.Extensions.Logging; +using SFA.DAS.CommitmentsV2.Api.Client; +using SFA.DAS.CommitmentsV2.Shared.Interfaces; +using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice; +using SFA.DAS.Encoding; +using System; +using System.Threading; +using System.Threading.Tasks; + + +namespace SFA.DAS.EmployerCommitmentsV2.Web.Mappers.Apprentice +{ + public class EditStopDateRequestToViewModelMapper : IMapper + { + private readonly ICommitmentsApiClient _commitmentsApiClient; + private readonly ILogger _logger; + + public EditStopDateRequestToViewModelMapper(ICommitmentsApiClient commitmentsApiClient, ILogger logger) + { + _commitmentsApiClient = commitmentsApiClient; + _logger = logger; + } + + public async Task Map(EditStopDateRequest source) + { + try + { + var apprenticeship = await _commitmentsApiClient.GetApprenticeship(source.ApprenticeshipId, CancellationToken.None); + + var result = new EditStopDateViewModel + { + ApprenticeshipId = source.ApprenticeshipId, + AccountHashedId = source.AccountHashedId, + ApprenticeshipULN = apprenticeship.Uln, + ApprenticeshipHashedId = source.ApprenticeshipHashedId, + ApprenticeshipStartDate = apprenticeship.StartDate, + CurrentStopDate = apprenticeship.StopDate.Value + }; + + return result; + } + catch (Exception e) + { + _logger.LogError(e, $"Error mapping for accountId {source.AccountHashedId} and apprenticeship {source.ApprenticeshipHashedId} to EditStopDateViewModel"); + throw; + } + } + } +} diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Mappers/Apprentice/EditStopDateViewModelToApprenticeshipStopDateRequestMapper.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Mappers/Apprentice/EditStopDateViewModelToApprenticeshipStopDateRequestMapper.cs new file mode 100644 index 000000000..aa890cbfd --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Mappers/Apprentice/EditStopDateViewModelToApprenticeshipStopDateRequestMapper.cs @@ -0,0 +1,32 @@ +using SFA.DAS.CommitmentsV2.Api.Client; +using SFA.DAS.CommitmentsV2.Api.Types.Requests; +using SFA.DAS.CommitmentsV2.Shared.Interfaces; +using SFA.DAS.EmployerCommitmentsV2.Web.Authentication; +using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice; +using System; +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerCommitmentsV2.Web.Mappers.Apprentice +{ + public class EditStopDateViewModelToApprenticeshipStopDateRequestMapper : IMapper + { + private readonly IAuthenticationService _authenticationService; + + public EditStopDateViewModelToApprenticeshipStopDateRequestMapper(IAuthenticationService authenticationService) + { + _authenticationService = authenticationService; + } + + public Task Map(EditStopDateViewModel source) + { + var result = new ApprenticeshipStopDateRequest() + { + AccountId = source.AccountId, + NewStopDate = new DateTime(source.NewStopDate.Year.Value, source.NewStopDate.Month.Value, source.NewStopDate.Day.Value), + UserInfo = _authenticationService.UserInfo + }; + + return Task.FromResult(result); + } + } +} diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Models/Apprentice/EditStopDateRequest.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Models/Apprentice/EditStopDateRequest.cs new file mode 100644 index 000000000..868febfaa --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Models/Apprentice/EditStopDateRequest.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Mvc; +using SFA.DAS.Authorization.ModelBinding; + +namespace SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice +{ + public class EditStopDateRequest : IAuthorizationContextModel + { + [FromRoute] + public string AccountHashedId { get; set; } + + [FromRoute] + public string ApprenticeshipHashedId { get; set; } + + public long ApprenticeshipId { get; set; } + } +} diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Models/Apprentice/EditStopDateViewModel.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Models/Apprentice/EditStopDateViewModel.cs new file mode 100644 index 000000000..3f3ebea02 --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Models/Apprentice/EditStopDateViewModel.cs @@ -0,0 +1,39 @@ +using SFA.DAS.Authorization.ModelBinding; +using SFA.DAS.CommitmentsV2.Shared.Models; +using System; +using System.ComponentModel.DataAnnotations; + +namespace SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice +{ + public class EditStopDateViewModel : IAuthorizationContextModel + { + public EditStopDateViewModel() + { + NewStopDate = new MonthYearModel(""); + } + + public long ApprenticeshipId { get; set; } + + public string AccountHashedId { get; set; } + + public string ApprenticeshipULN { get; set; } + + public string ApprenticeshipHashedId { get; set; } + + public string ApprenticeshipName { get; set; } + + public DateTime CurrentStopDate { get; set; } + + public DateTime ApprenticeshipStartDate { get; set; } + + public MonthYearModel NewStopDate { get; set; } + + public long AccountId { get; set; } + + [Display(Name = "Month")] + public int? NewStopMonth { get => NewStopDate.Month; set => NewStopDate.Month = value; } + + [Display(Name = "Year")] + public int? NewStopYear { get => NewStopDate.Year; set => NewStopDate.Year = value; } + } +} diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/SFA.DAS.EmployerCommitmentsV2.Web.csproj b/src/SFA.DAS.EmployerCommitmentsV2.Web/SFA.DAS.EmployerCommitmentsV2.Web.csproj index 7d73652cc..7a36f1d50 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/SFA.DAS.EmployerCommitmentsV2.Web.csproj +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/SFA.DAS.EmployerCommitmentsV2.Web.csproj @@ -25,10 +25,10 @@ - - - - + + + + diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Validators/EditStopDateViewModelValidator.cs b/src/SFA.DAS.EmployerCommitmentsV2.Web/Validators/EditStopDateViewModelValidator.cs new file mode 100644 index 000000000..4a73c8220 --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Validators/EditStopDateViewModelValidator.cs @@ -0,0 +1,29 @@ +using FluentValidation; +using SFA.DAS.CommitmentsV2.Shared.Interfaces; +using SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice; +using System; + +namespace SFA.DAS.EmployerCommitmentsV2.Web.Validators +{ + public class EditStopDateViewModelValidator : AbstractValidator + { + private readonly ICurrentDateTime _currentDateTime; + + public EditStopDateViewModelValidator(ICurrentDateTime currentDateTime) + { + _currentDateTime = currentDateTime; + + RuleFor(x => x.ApprenticeshipHashedId).NotEmpty(); + + RuleFor(x => x.AccountHashedId).NotEmpty(); + + RuleFor(r => r.NewStopDate) + .Cascade(CascadeMode.StopOnFirstFailure) + .NotNull().WithMessage("Enter the stop date for this apprenticeship") + .Must(d => d.Date.HasValue).WithMessage("Enter the stop date for this apprenticeship") + .Must(d => d.Date <= new DateTime(_currentDateTime.UtcNow.Year, _currentDateTime.UtcNow.Month, 1)).WithMessage("The stop date cannot be in the future") + .Must((model, newStopDate) => newStopDate.Date >= new DateTime(model.ApprenticeshipStartDate.Year, model.ApprenticeshipStartDate.Month, 1)).WithMessage("The stop month cannot be before the apprenticeship started") + .Must((model, newStopDate) => newStopDate.Date != model.CurrentStopDate).WithMessage("Enter a date that is different to the current stopped date"); + } + } +} diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Views/Apprentice/Details.cshtml b/src/SFA.DAS.EmployerCommitmentsV2.Web/Views/Apprentice/Details.cshtml index 69534cc0c..5a2fd83c0 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2.Web/Views/Apprentice/Details.cshtml +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Views/Apprentice/Details.cshtml @@ -105,8 +105,8 @@ @Model.StopDate.Value.ToGdsFormatLongMonthWithoutDay() } - - Edit + + Edit diff --git a/src/SFA.DAS.EmployerCommitmentsV2.Web/Views/Apprentice/EditStopDate.cshtml b/src/SFA.DAS.EmployerCommitmentsV2.Web/Views/Apprentice/EditStopDate.cshtml new file mode 100644 index 000000000..491755f38 --- /dev/null +++ b/src/SFA.DAS.EmployerCommitmentsV2.Web/Views/Apprentice/EditStopDate.cshtml @@ -0,0 +1,73 @@ +@inject ILinkGenerator LinkGenerator +@using SFA.DAS.CommitmentsV2.Shared.Extensions +@model SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice.EditStopDateViewModel + +@{ + ViewBag.Title = "When did this apprenticeship training stop?"; + ViewBag.PageId = "edit-stopdate"; + ViewBag.Section = "apprentices"; +} + +
+
+ + +

+ When did this apprenticeship training stop? +

+ +
+ + @Html.AntiForgeryToken() + +
+ +
+ +

+ The earliest date you can enter is this apprenticeship's start date of @Model.ApprenticeshipStartDate.ToGdsFormatWithoutDay(). Any outstanding payments will be made for activity up to this date. +

+ + + For example, 06 2018 + + + @if (!ViewData.ModelState.IsValid) + { + + @ViewData.ModelState[nameof(SFA.DAS.EmployerCommitmentsV2.Web.Models.Apprentice.EditStopDateViewModel.NewStopDate)].Errors.First().ErrorMessage + + } + +
+
+
+ + +
+
+
+
+ + +
+
+
+ +
+ +
+ + + +
+
+
+ +@section Back +{ + +} + diff --git a/src/SFA.DAS.EmployerCommitmentsV2/SFA.DAS.EmployerCommitmentsV2.csproj b/src/SFA.DAS.EmployerCommitmentsV2/SFA.DAS.EmployerCommitmentsV2.csproj index 14f133ece..fec5380e7 100644 --- a/src/SFA.DAS.EmployerCommitmentsV2/SFA.DAS.EmployerCommitmentsV2.csproj +++ b/src/SFA.DAS.EmployerCommitmentsV2/SFA.DAS.EmployerCommitmentsV2.csproj @@ -11,9 +11,9 @@ - - - + + +