From 69d1e90544393cbe370f0e798fea3844066fdb6b Mon Sep 17 00:00:00 2001 From: vas Date: Wed, 8 Apr 2020 21:47:41 +0100 Subject: [PATCH] CON-1654 - Updated Unit Tests --- .../WhenIGetOrganisationAgreements.cs | 102 +++++++++++++ .../WhenIValidateTheRequest.cs | 37 +++++ .../WhenIGetOrganisationAgreements.cs | 28 ++-- .../Helpers/HtmlHelperExtensions.cs | 17 +++ .../Mappings/AgreementMappings.cs | 9 -- .../EmployerAgreementOrchestrator.cs | 14 +- .../SFA.DAS.EmployerAccounts.Web.csproj | 1 - .../OrganisationAgreementViewModel.cs | 42 ------ .../ViewAllAgreements.cshtml | 138 +++++++----------- .../Views/Shared/_NotAcceptedAgreement.cshtml | 7 +- .../Data/EmployerAgreementRepository.cs | 26 +--- .../Data/IEmployerAgreementRepository.cs | 2 +- .../Mappings/AgreementMappings.cs | 14 ++ .../OrganisationAgreement.cs | 33 ----- .../GetOrganisationAgreementsQueryHandler.cs | 76 ++++------ .../GetOrganisationAgreementsResponse.cs | 6 +- 16 files changed, 282 insertions(+), 270 deletions(-) create mode 100644 src/SFA.DAS.EmployerAccounts.UnitTests/Queries/GetOrganisationAgreements/WhenIGetOrganisationAgreements.cs create mode 100644 src/SFA.DAS.EmployerAccounts.UnitTests/Queries/GetOrganisationAgreements/WhenIValidateTheRequest.cs delete mode 100644 src/SFA.DAS.EmployerAccounts.Web/ViewModels/OrganisationAgreementViewModel.cs delete mode 100644 src/SFA.DAS.EmployerAccounts/Models/EmployerAgreement/OrganisationAgreement.cs diff --git a/src/SFA.DAS.EmployerAccounts.UnitTests/Queries/GetOrganisationAgreements/WhenIGetOrganisationAgreements.cs b/src/SFA.DAS.EmployerAccounts.UnitTests/Queries/GetOrganisationAgreements/WhenIGetOrganisationAgreements.cs new file mode 100644 index 0000000000..faaad9df91 --- /dev/null +++ b/src/SFA.DAS.EmployerAccounts.UnitTests/Queries/GetOrganisationAgreements/WhenIGetOrganisationAgreements.cs @@ -0,0 +1,102 @@ +using SFA.DAS.EmployerAccounts.Queries.GetOrganisationAgreements; +using Moq; +using NUnit.Framework; +using SFA.DAS.EmployerAccounts.Interfaces; +using SFA.DAS.HashingService; +using SFA.DAS.Validation; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using SFA.DAS.EmployerAccounts.Data; +using SFA.DAS.EmployerAccounts.MarkerInterfaces; +using AutoMapper; +using SFA.DAS.EmployerAccounts.Models.Account; +using SFA.DAS.EmployerAccounts.Dtos; +using SFA.DAS.Common.Domain.Types; + +namespace SFA.DAS.EmployerAccounts.UnitTests.Queries.GetOrganisationAgreements +{ + public class WhenIGetOrganisationAgreements : QueryBaseTest + { + public override GetOrganisationAgreementsRequest Query { get; set; } + public override GetOrganisationAgreementsQueryHandler RequestHandler { get; set; } + public override Mock> RequestValidator { get; set; } + private Mock _mockEmployerAgreementRepository; + private Mock _mockAccountLegalEntityPublicHashingService; + private Mock _mockhashingService; + private Mock _mockreferenceDataService; + private Mock _mockmapper; + private long _accountLegelEntityId = 123; + private string hashedId = "ABC123"; + + [SetUp] + public void Arrange() + { + SetUp(); + + _mockAccountLegalEntityPublicHashingService = new Mock(); + _mockAccountLegalEntityPublicHashingService.Setup(m => m.DecodeValue(It.IsAny())).Returns(_accountLegelEntityId); + + _mockEmployerAgreementRepository = new Mock(); + _mockEmployerAgreementRepository.Setup(m => m.GetOrganisationAgreement(It.IsAny())).ReturnsAsync + (new AccountLegalEntity { + + LegalEntity = new LegalEntity { Id=1, Source= OrganisationType.CompaniesHouse } , + Agreements = new List() + { new EmployerAgreement + { + SignedDate = DateTime.UtcNow, + } + + } }); + + _mockhashingService = new Mock(); + _mockhashingService.Setup(m => m.HashValue(It.IsAny())).Returns(hashedId); + + _mockreferenceDataService = new Mock(); + _mockreferenceDataService.Setup(m => m.IsIdentifiableOrganisationType(It.IsAny())).ReturnsAsync(true); + + var agreements = new List { + new EmployerAgreementDto { Id =1, SignedDate = DateTime.UtcNow , AccountLegalEntity = new AccountLegalEntity { Id = _accountLegelEntityId} }, + new EmployerAgreementDto { Id =2, SignedDate = DateTime.UtcNow , AccountLegalEntity = new AccountLegalEntity { Id = _accountLegelEntityId} }}; + + _mockmapper = new Mock(); + _mockmapper.Setup(m => m.Map, ICollection>(It.IsAny>(), + It.IsAny, ICollection>>>())) + .Returns(agreements); + + RequestHandler = new GetOrganisationAgreementsQueryHandler(RequestValidator.Object, _mockEmployerAgreementRepository.Object, _mockAccountLegalEntityPublicHashingService.Object, + _mockhashingService.Object, _mockreferenceDataService.Object, _mockmapper.Object); + + Query = new GetOrganisationAgreementsRequest + { + AccountLegalEntityHashedId = hashedId + }; + } + + + [Test] + public override async Task ThenIfTheMessageIsValidTheRepositoryIsCalled() + { + //Act + await RequestHandler.Handle(Query); + + //Assert + _mockEmployerAgreementRepository.Verify(x => x.GetOrganisationAgreement(It.IsAny()), Times.Once); + } + + [Test] + public override async Task ThenIfTheMessageIsValidTheValueIsReturnedInTheResponse() + { + //Act + var response = await RequestHandler.Handle(Query); + + //Assert + Assert.IsNotNull(response.Agreements); + Assert.IsTrue(response.Agreements.Any()); + } + + + } +} diff --git a/src/SFA.DAS.EmployerAccounts.UnitTests/Queries/GetOrganisationAgreements/WhenIValidateTheRequest.cs b/src/SFA.DAS.EmployerAccounts.UnitTests/Queries/GetOrganisationAgreements/WhenIValidateTheRequest.cs new file mode 100644 index 0000000000..d2e3b20e09 --- /dev/null +++ b/src/SFA.DAS.EmployerAccounts.UnitTests/Queries/GetOrganisationAgreements/WhenIValidateTheRequest.cs @@ -0,0 +1,37 @@ +using NUnit.Framework; +using SFA.DAS.EmployerAccounts.Queries.GetOrganisationAgreements; +using System.Threading.Tasks; + +namespace SFA.DAS.EmployerAccounts.UnitTests.Queries.GetOrganisationAgreements +{ + public class WhenIValidateTheRequest + { + private GetOrganisationAgreementsValidator _validator; + + [SetUp] + public void Arrange() + { + _validator = new GetOrganisationAgreementsValidator(); + } + + [Test] + public async Task ThenShouldReturnValidIfRequestIsValid() + { + //Act + var result = await _validator.ValidateAsync(new GetOrganisationAgreementsRequest { AccountLegalEntityHashedId = "Abc123" }); + + //Assert + Assert.IsTrue(result.IsValid()); + } + + [Test] + public async Task ThenShouldReturnInValidIfRequestIsNotValid() + { + //Act + var result = await _validator.ValidateAsync(new GetOrganisationAgreementsRequest { AccountLegalEntityHashedId = string.Empty }); + + //Assert + Assert.IsFalse(result.IsValid()); + } + } +} diff --git a/src/SFA.DAS.EmployerAccounts.Web.UnitTests/Orchestrators/EmployerAgreementOrchestratorTests/WhenIGetOrganisationAgreements.cs b/src/SFA.DAS.EmployerAccounts.Web.UnitTests/Orchestrators/EmployerAgreementOrchestratorTests/WhenIGetOrganisationAgreements.cs index 253b3d9f09..01d83b7979 100644 --- a/src/SFA.DAS.EmployerAccounts.Web.UnitTests/Orchestrators/EmployerAgreementOrchestratorTests/WhenIGetOrganisationAgreements.cs +++ b/src/SFA.DAS.EmployerAccounts.Web.UnitTests/Orchestrators/EmployerAgreementOrchestratorTests/WhenIGetOrganisationAgreements.cs @@ -6,12 +6,13 @@ using MediatR; using Moq; using NUnit.Framework; +using SFA.DAS.EmployerAccounts.Dtos; using SFA.DAS.EmployerAccounts.Interfaces; -using SFA.DAS.EmployerAccounts.Models.EmployerAgreement; using SFA.DAS.EmployerAccounts.Queries.GetOrganisationAgreements; using SFA.DAS.EmployerAccounts.Web.Orchestrators; using SFA.DAS.EmployerAccounts.Web.ViewModels; using SFA.DAS.Validation; +using System.Linq; namespace SFA.DAS.EmployerAccounts.Web.UnitTests.Orchestrators.EmployerAgreementOrchestratorTests { @@ -20,7 +21,7 @@ public class WhenIGetOrganisationAgreements private Mock _mediator; private Mock _referenceDataService; private Mock _mapper; - private EmployerAgreementOrchestrator _orchestrator; + private EmployerAgreementOrchestrator _orchestrator; public string AccountLegalEntityHashedId = "2K7J94"; @@ -32,21 +33,19 @@ public void Arrange() _mediator.Setup(x => x.SendAsync(It.IsAny())) .ReturnsAsync(new GetOrganisationAgreementsResponse { - OrganisationAgreements = new OrganisationAgreement - { - Address = "Address", - Name = "Name" - } - });; - - var organisationAgreementViewModel = new OrganisationAgreementViewModel + Agreements = new List() + { + new EmployerAgreementDto { SignedDate = DateTime.UtcNow } + } + }); + + var EmployerAgreementViewModelV1 = new List() { - Address = "Address", - Name = "Name" + new EmployerAgreementViewModelV1 { SignedDate = DateTime.UtcNow } }; _referenceDataService = new Mock(); - _mapper.Setup(m => m.Map(It.IsAny())).Returns(organisationAgreementViewModel); + _mapper.Setup(m => m.Map, ICollection>(It.IsAny>())).Returns(EmployerAgreementViewModelV1); _orchestrator = new EmployerAgreementOrchestrator(_mediator.Object, _mapper.Object, _referenceDataService.Object); } @@ -95,8 +94,7 @@ public async Task ThenTheValuesAreReturnedInTheResponseFromTheMediatorCall() var actual = await _orchestrator.GetOrganisationAgreements(AccountLegalEntityHashedId); //Assert - //TO DO - Assert.IsTrue(actual.Data.Address.Equals("Address")); + Assert.IsNotNull(actual.Data.Any()); } } } diff --git a/src/SFA.DAS.EmployerAccounts.Web/Helpers/HtmlHelperExtensions.cs b/src/SFA.DAS.EmployerAccounts.Web/Helpers/HtmlHelperExtensions.cs index 41a06d95a3..f13ff13c21 100644 --- a/src/SFA.DAS.EmployerAccounts.Web/Helpers/HtmlHelperExtensions.cs +++ b/src/SFA.DAS.EmployerAccounts.Web/Helpers/HtmlHelperExtensions.cs @@ -127,7 +127,24 @@ public static string GetClaimsHashedAccountId(this HtmlHelper htmlHelper) return (!string.IsNullOrEmpty(hashedAccountId)) ? hashedAccountId : string.Empty; } + public static string GetAgreementHistoryTabTitle(this HtmlHelper htmlHelper , int versionNumber) + { + if (versionNumber == 1) return "Published 9 January 2020"; + if (versionNumber == 2) return "Published 1 May 2018"; + if (versionNumber == 3) return "Published 1 May 2017"; + + return string.Empty; + } + public static string GetAgreementTabListId(this HtmlHelper htmlHelper, int versionNumber) + { + return $"#v{versionNumber}-agreement"; + } + + public static string GetAgreementTabPanelId(this HtmlHelper htmlHelper, int versionNumber) + { + return $"v{versionNumber}-agreement"; + } public static bool ViewExists(this HtmlHelper html, string viewName) { diff --git a/src/SFA.DAS.EmployerAccounts.Web/Mappings/AgreementMappings.cs b/src/SFA.DAS.EmployerAccounts.Web/Mappings/AgreementMappings.cs index ef14c25399..fc5c2733fc 100644 --- a/src/SFA.DAS.EmployerAccounts.Web/Mappings/AgreementMappings.cs +++ b/src/SFA.DAS.EmployerAccounts.Web/Mappings/AgreementMappings.cs @@ -1,7 +1,6 @@ using AutoMapper; using SFA.DAS.EAS.Account.Api.Types; using SFA.DAS.EmployerAccounts.Dtos; -using SFA.DAS.EmployerAccounts.Models.EmployerAgreement; using SFA.DAS.EmployerAccounts.Queries.GetEmployerAgreement; using SFA.DAS.EmployerAccounts.Web.ViewModels; @@ -37,14 +36,6 @@ public AgreementMappings() CreateMap() .ConvertUsing(new AgreementInfoConverter()); - CreateMap() - .ForMember(dest => dest.AgreementV1SignedDate, opts => opts.Ignore()) - .ForMember(dest => dest.AgreementV2SignedDate, opts => opts.Ignore()) - .ForMember(dest => dest.AgreementV3SignedDate, opts => opts.Ignore()) - .ForMember(dest => dest.AgreementV1Signed, opts => opts.Ignore()) - .ForMember(dest => dest.AgreementV2Signed, opts => opts.Ignore()) - .ForMember(dest => dest.AgreementV3Signed, opts => opts.Ignore()); - CreateMap() .ForMember(dest => dest.SignedDateText, opt => opt.MapFrom(src => src.SignedDate.HasValue ? src.SignedDate.Value.ToString("dd MMMM yyyy") : "")) .ForMember(dest => dest.AccountLegalEntityPublicHashedId, opts => opts.Ignore()); diff --git a/src/SFA.DAS.EmployerAccounts.Web/Orchestrators/EmployerAgreementOrchestrator.cs b/src/SFA.DAS.EmployerAccounts.Web/Orchestrators/EmployerAgreementOrchestrator.cs index 2ae74e5506..089cb78a98 100644 --- a/src/SFA.DAS.EmployerAccounts.Web/Orchestrators/EmployerAgreementOrchestrator.cs +++ b/src/SFA.DAS.EmployerAccounts.Web/Orchestrators/EmployerAgreementOrchestrator.cs @@ -369,9 +369,9 @@ public virtual async Task> GetOrganisationAgreements(string accountLegalEntityHashedId) + public virtual async Task>> GetOrganisationAgreements(string accountLegalEntityHashedId) { - var response = new OrchestratorResponse(); + var response = new OrchestratorResponse>(); try { @@ -379,21 +379,21 @@ public virtual async Task> { AccountLegalEntityHashedId = accountLegalEntityHashedId }); - - response.Data = _mapper.Map(result.OrganisationAgreements); + + response.Data = _mapper.Map, ICollection>(result.Agreements); } catch (InvalidRequestException ex) { - return new OrchestratorResponse + return new OrchestratorResponse> { Status = HttpStatusCode.BadRequest, - Data = new OrganisationAgreementViewModel(), + Data = new List(), Exception = ex }; } catch (UnauthorizedAccessException) { - return new OrchestratorResponse + return new OrchestratorResponse> { Status = HttpStatusCode.Unauthorized }; diff --git a/src/SFA.DAS.EmployerAccounts.Web/SFA.DAS.EmployerAccounts.Web.csproj b/src/SFA.DAS.EmployerAccounts.Web/SFA.DAS.EmployerAccounts.Web.csproj index d9871aa954..9931825e16 100644 --- a/src/SFA.DAS.EmployerAccounts.Web/SFA.DAS.EmployerAccounts.Web.csproj +++ b/src/SFA.DAS.EmployerAccounts.Web/SFA.DAS.EmployerAccounts.Web.csproj @@ -282,7 +282,6 @@ - diff --git a/src/SFA.DAS.EmployerAccounts.Web/ViewModels/OrganisationAgreementViewModel.cs b/src/SFA.DAS.EmployerAccounts.Web/ViewModels/OrganisationAgreementViewModel.cs deleted file mode 100644 index f74185de6f..0000000000 --- a/src/SFA.DAS.EmployerAccounts.Web/ViewModels/OrganisationAgreementViewModel.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Linq; -using System.Collections.Generic; -using SFA.DAS.EmployerAccounts.Models.Account; - -namespace SFA.DAS.EmployerAccounts.Web.ViewModels -{ - public class OrganisationAgreementViewModel - { - public long Id { get; set; } - public string Name { get; set; } - public string Address { get; set; } - public long AccountId { get; set; } - public string AccountLegalEntityPublicHashedId { get; set; } - - public ICollection Agreements { get; set; } = new List(); - - public bool AgreementV1Signed => DoCheck(Agreements.Where(x => x.Template.PartialViewName == "_Agreement_V1").FirstOrDefault()); - public bool AgreementV2Signed => DoCheck(Agreements.Where(x => x.Template.PartialViewName == "_Agreement_V2").FirstOrDefault()); - public bool AgreementV3Signed => DoCheck(Agreements.Where(x => x.Template.PartialViewName == "_Agreement_V3").FirstOrDefault()); - - public DateTime? AgreementV1SignedDate => Agreements.Where(x => x.Template.PartialViewName == "_Agreement_V1").FirstOrDefault()?.SignedDate; - public DateTime? AgreementV2SignedDate => Agreements.Where(x => x.Template.PartialViewName == "_Agreement_V2").FirstOrDefault()?.SignedDate; - public DateTime? AgreementV3SignedDate => Agreements.Where(x => x.Template.PartialViewName == "_Agreement_V3").FirstOrDefault()?.SignedDate; - - public EmployerAgreementViewModelV1 EmployerAgreementV1 { get; set; } - public EmployerAgreementViewModelV1 EmployerAgreementV2 { get; set; } - public EmployerAgreementViewModelV1 EmployerAgreementV3 { get; set; } - - public LegalEntity LegalEntity { get; set; } - - public string HashedAccountId { get; set; } - public string HashedLegalEntityId { get; set; } - - private bool DoCheck(EmployerAgreement employerAgreement) - { - return employerAgreement != null ? employerAgreement.SignedDate.HasValue : false; - } - - - } -} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerAccounts.Web/Views/EmployerAgreement/ViewAllAgreements.cshtml b/src/SFA.DAS.EmployerAccounts.Web/Views/EmployerAgreement/ViewAllAgreements.cshtml index a1ea221441..fcf643dc43 100644 --- a/src/SFA.DAS.EmployerAccounts.Web/Views/EmployerAgreement/ViewAllAgreements.cshtml +++ b/src/SFA.DAS.EmployerAccounts.Web/Views/EmployerAgreement/ViewAllAgreements.cshtml @@ -1,6 +1,6 @@ @{Layout = "~/Views/Shared/_Layout_CDN.cshtml"; } - -@model OrchestratorResponse +@using SFA.DAS.EmployerAccounts.Web.Helpers +@model OrchestratorResponse> @{ ViewBag.PageID = "organisations-agreements-details"; ViewBag.Title = "ViewAllAgreements"; @@ -16,98 +16,68 @@

Your agreements with the Education and Skills Funding Agency (ESFA)

-

- Contents -

- - V2 Agreement +
- @if (@Model.Data.AgreementV2Signed) - { -

Accepted on @Model.Data.AgreementV2SignedDate

+ @if (item.SignedDate != null) + { +

Accepted on @item.SignedDate

-

Agreement between @Model.Data.Name and ESFA

- - Published 1 May 2018 - +

Agreement between @item.AccountLegalEntity.Name and ESFA

- - This is a variation of the agreement that we published 1 May 2017. We updated it to add clause 7 (transfer of funding). - + ViewBag.BreadCrumbsTrail = @item.AccountLegalEntity.Name; - @Html.Partial("_AcceptedAgreement", @Model.Data.EmployerAgreementV2) - } + + @Html.GetAgreementHistoryTabTitle(@item.Template.VersionNumber) + -
+ + This is a new agreement. + -
- V1 Agreement - @if (@Model.Data.AgreementV1Signed) - { -

Accepted on @Model.Data.AgreementV1SignedDate

+ @Html.Partial("_AcceptedAgreement", item) + } + else + { +

NOT ACCEPTED YET

-

Agreement between @Model.Data.Name and ESFA

- - Published 1 May 2017 - +

Agreement between @item.AccountLegalEntity.Name and ESFA

- @Html.Partial("_AcceptedAgreement", @Model.Data.EmployerAgreementV1) - } + ViewBag.BreadCrumbsTrail = @item.AccountLegalEntity.Name; + + @Html.GetAgreementHistoryTabTitle(@item.Template.VersionNumber) + + + @Html.Partial("_NotAcceptedAgreement", item) + } +
+ } + +
+ +@section breadcrumb { + +
+
    +
  1. + Home +
  2. +
  3. + Your organisations and agreements +
  4. +
  5. @ViewBag.BreadCrumbsTrail
  6. +
- \ No newline at end of file + +} \ No newline at end of file diff --git a/src/SFA.DAS.EmployerAccounts.Web/Views/Shared/_NotAcceptedAgreement.cshtml b/src/SFA.DAS.EmployerAccounts.Web/Views/Shared/_NotAcceptedAgreement.cshtml index 3312e7fc08..0cbf6df22a 100644 --- a/src/SFA.DAS.EmployerAccounts.Web/Views/Shared/_NotAcceptedAgreement.cshtml +++ b/src/SFA.DAS.EmployerAccounts.Web/Views/Shared/_NotAcceptedAgreement.cshtml @@ -1,5 +1,5 @@  -@model SFA.DAS.EmployerAccounts.Web.ViewModels.OrganisationAgreementViewModel +@model SFA.DAS.EmployerAccounts.Web.ViewModels.EmployerAgreementViewModelV1 This is a new agreement. @@ -7,12 +7,11 @@

About the agreement

- This agreement creates a legal contract between @Model.Name and ESFA. It allows you to transfer funds to other employers and pay training providers for apprenticeships. + This agreement creates a legal contract between @Model.AccountLegalEntity.Name and ESFA. It allows you to transfer funds to other employers and pay training providers for apprenticeships. You need to ensure you have authority from your organisation before you accept this agreement on their behalf.

-
- @*HashedAgreementId TODO*@ + @Html.AntiForgeryToken()
\ No newline at end of file diff --git a/src/SFA.DAS.EmployerAccounts/Data/EmployerAgreementRepository.cs b/src/SFA.DAS.EmployerAccounts/Data/EmployerAgreementRepository.cs index d847d0452a..370eec1ec8 100644 --- a/src/SFA.DAS.EmployerAccounts/Data/EmployerAgreementRepository.cs +++ b/src/SFA.DAS.EmployerAccounts/Data/EmployerAgreementRepository.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Dapper; using SFA.DAS.EmployerAccounts.Configuration; +using SFA.DAS.EmployerAccounts.Dtos; using SFA.DAS.EmployerAccounts.Models.Account; using SFA.DAS.EmployerAccounts.Models.EmployerAgreement; using SFA.DAS.EmployerAccounts.Models.Organisation; @@ -169,33 +170,14 @@ public async Task SetAccountLegalEntityAgreementDetails(long accountLegalEntityI await _db.Value.SaveChangesAsync(); } - public async Task GetOrganisationAgreement(long accountLegalEntityId) + public async Task GetOrganisationAgreement(long accountLegalEntityId) { var accountLegalEntity = await _db.Value.AccountLegalEntities + .Include(x => x.LegalEntity) .Include(x => x.Agreements.Select(y => y.Template)) .SingleOrDefaultAsync(x => x.Id == accountLegalEntityId); - if (accountLegalEntity == null) - { - return null; - } - - var organisationAgreement = new OrganisationAgreement - { - Id = accountLegalEntity.Id, - Name = accountLegalEntity.Name, - Address = accountLegalEntity.Address, - AccountId = accountLegalEntity.AccountId, - AccountLegalEntityPublicHashedId = accountLegalEntity.PublicHashedId - }; - - var legalEntity = await _db.Value.LegalEntities.Where(x => x.Id == accountLegalEntity.LegalEntityId).ToListAsync(); - - organisationAgreement.Agreements = accountLegalEntity.Agreements; - - organisationAgreement.LegalEntity = legalEntity.FirstOrDefault(); - - return organisationAgreement; + return accountLegalEntity; } } diff --git a/src/SFA.DAS.EmployerAccounts/Data/IEmployerAgreementRepository.cs b/src/SFA.DAS.EmployerAccounts/Data/IEmployerAgreementRepository.cs index 1345682910..96e8a0a6b2 100644 --- a/src/SFA.DAS.EmployerAccounts/Data/IEmployerAgreementRepository.cs +++ b/src/SFA.DAS.EmployerAccounts/Data/IEmployerAgreementRepository.cs @@ -19,6 +19,6 @@ public interface IEmployerAgreementRepository Task> GetAccountAgreements(long accountId); Task GetEmployerAgreementStatus(long agreementId); Task SetAccountLegalEntityAgreementDetails(long accountLegalEntityId, long? pendingAgreementId, int? pendingAgreementVersion, long? signedAgreementId, int? signedAgreementVersion); - Task GetOrganisationAgreement(long accountLegalEntityId); + Task GetOrganisationAgreement(long accountLegalEntityId); } } \ No newline at end of file diff --git a/src/SFA.DAS.EmployerAccounts/Mappings/AgreementMappings.cs b/src/SFA.DAS.EmployerAccounts/Mappings/AgreementMappings.cs index 2ada07df54..0dddaf327c 100644 --- a/src/SFA.DAS.EmployerAccounts/Mappings/AgreementMappings.cs +++ b/src/SFA.DAS.EmployerAccounts/Mappings/AgreementMappings.cs @@ -53,6 +53,20 @@ public AgreementMappings() v => v.AgreementType, o => o.MapFrom(a => a.Template.AgreementType)) ; + + CreateMap() + .ForMember( + d => d.OrganisationLookupPossible, + o => o.Ignore()) + .ForMember( + d => d.HashedAccountId, + o => o.Ignore()) + .ForMember( + d => d.HashedAgreementId, + o => o.Ignore()) + .ForMember( + d => d.HashedLegalEntityId, + o => o.Ignore()); } } } \ No newline at end of file diff --git a/src/SFA.DAS.EmployerAccounts/Models/EmployerAgreement/OrganisationAgreement.cs b/src/SFA.DAS.EmployerAccounts/Models/EmployerAgreement/OrganisationAgreement.cs deleted file mode 100644 index f17993677e..0000000000 --- a/src/SFA.DAS.EmployerAccounts/Models/EmployerAgreement/OrganisationAgreement.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Collections.Generic; -using SFA.DAS.EmployerAccounts.Dtos; -using SFA.DAS.EmployerAccounts.Models.Account; - -namespace SFA.DAS.EmployerAccounts.Models.EmployerAgreement -{ - public class OrganisationAgreement - { - /* - 1. [AccountLegalEntity] - 2. [EmployerAgreement] - 3. [LegalEntity] - 4. [EmployerAgreementTemplate] - */ - - public long Id { get; set; } - public string Name { get; set; } - public string Address { get; set; } - public long AccountId { get; set; } - public string AccountLegalEntityPublicHashedId { get; set; } - - public virtual ICollection Agreements { get; set; } = new List(); - - public EmployerAgreementDto EmployerAgreementV1 { get; set; } - public EmployerAgreementDto EmployerAgreementV2 { get; set; } - public EmployerAgreementDto EmployerAgreementV3 { get; set; } - - public virtual LegalEntity LegalEntity { get; set; } - - public string HashedAccountId { get; set; } - public string HashedLegalEntityId { get; set; } - } -} diff --git a/src/SFA.DAS.EmployerAccounts/Queries/GetOrganisationAgreements/GetOrganisationAgreementsQueryHandler.cs b/src/SFA.DAS.EmployerAccounts/Queries/GetOrganisationAgreements/GetOrganisationAgreementsQueryHandler.cs index b2b97531c9..ba513acf3a 100644 --- a/src/SFA.DAS.EmployerAccounts/Queries/GetOrganisationAgreements/GetOrganisationAgreementsQueryHandler.cs +++ b/src/SFA.DAS.EmployerAccounts/Queries/GetOrganisationAgreements/GetOrganisationAgreementsQueryHandler.cs @@ -1,11 +1,13 @@ -using System.Threading.Tasks; +using System.Collections.Generic; +using System.Threading.Tasks; +using AutoMapper; using MediatR; +using System.Linq; using SFA.DAS.EmployerAccounts.Data; using SFA.DAS.EmployerAccounts.Dtos; using SFA.DAS.EmployerAccounts.Interfaces; using SFA.DAS.EmployerAccounts.MarkerInterfaces; using SFA.DAS.EmployerAccounts.Models.Account; -using SFA.DAS.EmployerAccounts.Models.EmployerAgreement; using SFA.DAS.HashingService; using SFA.DAS.Validation; @@ -18,20 +20,24 @@ public class GetOrganisationAgreementsQueryHandler : IAsyncRequestHandler validator, IEmployerAgreementRepository employerAgreementRepository, IAccountLegalEntityPublicHashingService accountLegalEntityPublicHashingService, IHashingService hashingService, - IReferenceDataService referenceDataService) + IReferenceDataService referenceDataService, + IMapper mapper) { _validator = validator; _employerAgreementRepository = employerAgreementRepository; _accountLegalEntityPublicHashingService = accountLegalEntityPublicHashingService; _hashingService = hashingService; _referenceDataService = referenceDataService; + _mapper = mapper; } + public async Task Handle(GetOrganisationAgreementsRequest message) { var validationResult = await _validator.ValidateAsync(message); @@ -43,59 +49,27 @@ public async Task Handle(GetOrganisationAgree var accountLegalEntityId = _accountLegalEntityPublicHashingService.DecodeValue(message.AccountLegalEntityHashedId); - var result = await _employerAgreementRepository.GetOrganisationAgreement(accountLegalEntityId); - if (result == null) return new GetOrganisationAgreementsResponse(); - - var organisationLookupByIdPossible = await _referenceDataService.IsIdentifiableOrganisationType(result.LegalEntity.Source); - - foreach (var agreement in result.Agreements) - { - switch (agreement.Template.PartialViewName) - { - case "_Agreement_V3": - OrganisationAgreement agreementV3 = GetAgreement(result, agreement, organisationLookupByIdPossible); - result.EmployerAgreementV3 = agreementV3.EmployerAgreementV2; - break; - case "_Agreement_V2": - OrganisationAgreement agreementV2 = GetAgreement(result, agreement, organisationLookupByIdPossible); - result.EmployerAgreementV2 = agreementV2.EmployerAgreementV2; - break; - case "_Agreement_V1": - OrganisationAgreement agreementV1 = GetAgreement(result, agreement, organisationLookupByIdPossible); - result.EmployerAgreementV1 = agreementV1.EmployerAgreementV1; - break; - } - } - - result.HashedAccountId = _hashingService.HashValue(result.AccountId); //AccountId - result.HashedLegalEntityId = _hashingService.HashValue(result.Id); //AccountLegalEntityId + var accountLegalEntity = await _employerAgreementRepository.GetOrganisationAgreement(accountLegalEntityId); + if (accountLegalEntity == null) return new GetOrganisationAgreementsResponse(); - return new GetOrganisationAgreementsResponse { OrganisationAgreements = result }; - } + var organisationLookupByIdPossible = await _referenceDataService.IsIdentifiableOrganisationType(accountLegalEntity.LegalEntity.Source); - private OrganisationAgreement GetAgreement(OrganisationAgreement result, EmployerAgreement agreement, bool organisationLookupByIdPossible) - { - return new OrganisationAgreement() - { - EmployerAgreementV2 = new EmployerAgreementDto + var agreements = _mapper.Map, ICollection>(accountLegalEntity.Agreements, + opt => { - SignedByName = agreement.SignedByName, - SignedDate = agreement.SignedDate, - AccountLegalEntity = new AccountLegalEntity + opt.AfterMap((src, dest) => { - Id = result.Id, - AccountId = result.AccountId, - Name = result.Name, - Address = result.Address, - PublicHashedId = result.AccountLegalEntityPublicHashedId + dest.ToList().ForEach(e => + { + e.HashedAccountId = _hashingService.HashValue(accountLegalEntity.AccountId); + e.HashedAgreementId = _hashingService.HashValue(e.Id); + e.HashedLegalEntityId = _hashingService.HashValue(accountLegalEntity.Id); + e.OrganisationLookupPossible = organisationLookupByIdPossible; + }); + }); + }); - }, - HashedAgreementId = _hashingService.HashValue(agreement.Id), - HashedAccountId = _hashingService.HashValue(result.AccountId), - HashedLegalEntityId = _hashingService.HashValue(result.Id), - OrganisationLookupPossible = organisationLookupByIdPossible - } - }; + return new GetOrganisationAgreementsResponse { Agreements = agreements }; } } } \ No newline at end of file diff --git a/src/SFA.DAS.EmployerAccounts/Queries/GetOrganisationAgreements/GetOrganisationAgreementsResponse.cs b/src/SFA.DAS.EmployerAccounts/Queries/GetOrganisationAgreements/GetOrganisationAgreementsResponse.cs index f9bc2ba606..1c4e86f96f 100644 --- a/src/SFA.DAS.EmployerAccounts/Queries/GetOrganisationAgreements/GetOrganisationAgreementsResponse.cs +++ b/src/SFA.DAS.EmployerAccounts/Queries/GetOrganisationAgreements/GetOrganisationAgreementsResponse.cs @@ -1,9 +1,13 @@ using SFA.DAS.EmployerAccounts.Models.EmployerAgreement; +using System.Collections.Generic; +using SFA.DAS.EmployerAccounts.Dtos; namespace SFA.DAS.EmployerAccounts.Queries.GetOrganisationAgreements { public class GetOrganisationAgreementsResponse { - public OrganisationAgreement OrganisationAgreements { get; set; } + //public OrganisationAgreement OrganisationAgreements { get; set; } + + public virtual ICollection Agreements { get; set; } = new List(); } } \ No newline at end of file