diff --git a/src/Spd.Manager.Licence.UnitTest/PermitAppManagerTest.cs b/src/Spd.Manager.Licence.UnitTest/PermitAppManagerTest.cs index 48bcf3438..7e0c7012b 100644 --- a/src/Spd.Manager.Licence.UnitTest/PermitAppManagerTest.cs +++ b/src/Spd.Manager.Licence.UnitTest/PermitAppManagerTest.cs @@ -474,6 +474,8 @@ public async void Handle_PermitAppUpdateCommand_Return_PermitAppCommandResponse( }); mockMapper.Setup(m => m.Map(It.IsAny())) .Returns(new CreateLicenceApplicationCmd() { OriginalApplicationId = licAppId }); + mockLicRepo.Setup(a => a.ManageAsync(It.IsAny(), CancellationToken.None)) + .ReturnsAsync(licenceResp); var existingdoc = fixture.Build() .With(r => r.ExpiryDate, expiryDate) diff --git a/src/Spd.Manager.Licence/Mappings.cs b/src/Spd.Manager.Licence/Mappings.cs index 822855906..4a974e71e 100644 --- a/src/Spd.Manager.Licence/Mappings.cs +++ b/src/Spd.Manager.Licence/Mappings.cs @@ -211,6 +211,20 @@ public Mappings() .ForMember(d => d.BizAddress, opt => opt.MapFrom(s => s.BusinessAddress)) .ForMember(d => d.BizBCAddress, opt => opt.MapFrom(s => s.BCBusinessAddress)) .ForMember(d => d.Branches, opt => opt.MapFrom(s => GetBranchInfo(s.BranchAddress))); + + CreateMap() + .ForMember(d => d.PermitPurposeEnums, opt => opt.MapFrom(s => GetPurposeEnums(s.BodyArmourPermitReasonCodes, s.ArmouredVehiclePermitReasonCodes))) + .ForMember(d => d.LicenceNumber, opt => opt.Ignore()) + .ForMember(d => d.ExpiryDate, opt => opt.Ignore()) + .ForMember(d => d.WorkerLicenceTypeCode, opt => opt.Ignore()) + .ForMember(d => d.LicenceTermCode, opt => opt.Ignore()) + .ForMember(d => d.LicenceHolderId, opt => opt.Ignore()) + .ForMember(d => d.LicenceHolderFirstName, opt => opt.Ignore()) + .ForMember(d => d.LicenceHolderLastName, opt => opt.Ignore()) + .ForMember(d => d.LicenceHolderMiddleName1, opt => opt.Ignore()) + .ForMember(d => d.LicenceStatusCode, opt => opt.Ignore()) + .ForMember(d => d.NameOnCard, opt => opt.Ignore()) + ; } private static WorkerCategoryTypeEnum[] GetCategories(IEnumerable codes) @@ -393,7 +407,7 @@ private static List GetBranchInfo(IEnumerable branchAddr { List branchInfos = new(); - foreach (BranchAddr branchAddr in branchAddrs) + foreach (BranchAddr branchAddr in branchAddrs) { BranchInfo branchInfo = new() { BranchAddress = new() }; branchInfo.BranchId = branchAddr.BranchId; diff --git a/src/Spd.Manager.Licence/PermitAppManager.cs b/src/Spd.Manager.Licence/PermitAppManager.cs index 623e2d327..0263c7a0e 100644 --- a/src/Spd.Manager.Licence/PermitAppManager.cs +++ b/src/Spd.Manager.Licence/PermitAppManager.cs @@ -254,6 +254,11 @@ public async Task Handle(PermitAppUpdateCommand cmd, C } + //update lic + await _licenceRepository.ManageAsync( + new UpdateLicenceCmd(_mapper.Map(cmd.LicenceAnonymousRequest), (Guid)originalLic.LicenceId), + cancellationToken); + //upload new files await UploadNewDocsAsync(request, cmd.LicAppFileInfos, diff --git a/src/Spd.Resource.Repository.IntegrationTest/IntegrationTestSetup.cs b/src/Spd.Resource.Repository.IntegrationTest/IntegrationTestSetup.cs index 988a52d23..4faf8d23c 100644 --- a/src/Spd.Resource.Repository.IntegrationTest/IntegrationTestSetup.cs +++ b/src/Spd.Resource.Repository.IntegrationTest/IntegrationTestSetup.cs @@ -3,6 +3,7 @@ using Spd.Resource.Repository.Alias; using Spd.Resource.Repository.Biz; using Spd.Resource.Repository.Contact; +using Spd.Resource.Repository.Licence; using Spd.Resource.Repository.LicenceApplication; using Spd.Resource.Repository.OptionSet; using Spd.Resource.Repository.Org; @@ -49,6 +50,7 @@ public IntegrationTestSetup() serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); + serviceCollection.AddTransient(); ServiceProvider = serviceCollection.BuildServiceProvider().CreateScope().ServiceProvider; } public IServiceProvider ServiceProvider { get; private set; } diff --git a/src/Spd.Resource.Repository.IntegrationTest/LicenceRepositoryTest.cs b/src/Spd.Resource.Repository.IntegrationTest/LicenceRepositoryTest.cs new file mode 100644 index 000000000..9b60fa09a --- /dev/null +++ b/src/Spd.Resource.Repository.IntegrationTest/LicenceRepositoryTest.cs @@ -0,0 +1,43 @@ +using Microsoft.Dynamics.CRM; +using Microsoft.Extensions.DependencyInjection; +using Spd.Resource.Repository.Licence; +using Spd.Utilities.Dynamics; + +namespace Spd.Resource.Repository.IntegrationTest; + +public class LicenceRepositoryTest : IClassFixture +{ + private readonly ILicenceRepository _licRepo; + private DynamicsContext _context; + + public LicenceRepositoryTest(IntegrationTestSetup testSetup) + { + _licRepo = testSetup.ServiceProvider.GetRequiredService(); + _context = testSetup.ServiceProvider.GetRequiredService().CreateChangeOverwrite(); + } + + [Fact] + public async Task ManageAsync_UpdateLicence_Correctly() + { + //Arrange + spd_licence lic = new(); + lic.spd_licenceid = Guid.NewGuid(); + lic.spd_employercontactname = IntegrationTestSetup.DataPrefix + "employername"; + lic.spd_employeremail = "test@test.com"; + _context.AddTospd_licences(lic); + await _context.SaveChangesAsync(CancellationToken.None); + PermitLicence pl = new() + { + EmployerName = "newEmployerName", + SupervisorPhoneNumber = "222222222" + }; + UpdateLicenceCmd cmd = new(pl, (Guid)lic.spd_licenceid); + + //Action + var response = await _licRepo.ManageAsync(cmd, CancellationToken.None); + + //Assert + Assert.NotNull(response); + Assert.Equal("newEmployerName", response.EmployerName); + } +} \ No newline at end of file diff --git a/src/Spd.Resource.Repository/Licence/Contract.cs b/src/Spd.Resource.Repository/Licence/Contract.cs index ad4710fd1..80f59a19a 100644 --- a/src/Spd.Resource.Repository/Licence/Contract.cs +++ b/src/Spd.Resource.Repository/Licence/Contract.cs @@ -5,7 +5,7 @@ namespace Spd.Resource.Repository.Licence public interface ILicenceRepository { public Task QueryAsync(LicenceQry query, CancellationToken cancellationToken); - public Task ManageAsync(LicenceCmd cmd, CancellationToken cancellationToken); + public Task ManageAsync(UpdateLicenceCmd cmd, CancellationToken cancellationToken); } public record LicenceQry @@ -24,12 +24,18 @@ public record LicenceListResp public IEnumerable Items { get; set; } = Array.Empty(); } - public abstract record LicenceCmd; - public record LicenceResp() + public record UpdateLicenceCmd(PermitLicence PermitLicence, Guid LicenceID); + + public record LicenceResp() : PermitLicence { public Guid? LicenceId { get; set; } public Guid? LicenceAppId { get; set; } - public string? LicenceNumber { get; set; } = null; + + } + + public record Licence + { + public string? LicenceNumber { get; set; } public DateOnly ExpiryDate { get; set; } public WorkerLicenceTypeEnum? WorkerLicenceTypeCode { get; set; } public LicenceTermEnum? LicenceTermCode { get; set; } @@ -39,7 +45,10 @@ public record LicenceResp() public string? LicenceHolderMiddleName1 { get; set; } public LicenceStatusEnum LicenceStatusCode { get; set; } public string? NameOnCard { get; set; } + } + public record PermitLicence : Licence + { //for permit public string? PermitOtherRequiredReason { get; set; } public string? EmployerName { get; set; } @@ -51,7 +60,6 @@ public record LicenceResp() public IEnumerable? PermitPurposeEnums { get; set; } //permit } - public enum LicenceStatusEnum { Active, diff --git a/src/Spd.Resource.Repository/Licence/LicenceRepository.cs b/src/Spd.Resource.Repository/Licence/LicenceRepository.cs index 06ff06197..88949cda7 100644 --- a/src/Spd.Resource.Repository/Licence/LicenceRepository.cs +++ b/src/Spd.Resource.Repository/Licence/LicenceRepository.cs @@ -1,6 +1,7 @@ using AutoMapper; using Microsoft.Dynamics.CRM; using Spd.Utilities.Dynamics; +using Spd.Utilities.Shared.Exceptions; namespace Spd.Resource.Repository.Licence; internal class LicenceRepository : ILicenceRepository @@ -63,9 +64,18 @@ public async Task QueryAsync(LicenceQry qry, CancellationToken }; } - public async Task ManageAsync(LicenceCmd cmd, CancellationToken ct) + public async Task ManageAsync(UpdateLicenceCmd cmd, CancellationToken ct) { - return null; + IQueryable lics = _context.spd_licences + .Expand(i => i.spd_LicenceHolder_contact) + .Where(i => i.spd_licenceid == cmd.LicenceID); + spd_licence? lic = lics.FirstOrDefault(); + if (lic == null) + throw new ApiException(System.Net.HttpStatusCode.BadRequest, "invalid licenceId"); + _mapper.Map(cmd.PermitLicence, lic); + _context.UpdateObject(lic); + await _context.SaveChangesAsync(ct); + return _mapper.Map(lic); } } diff --git a/src/Spd.Resource.Repository/Licence/Mappings.cs b/src/Spd.Resource.Repository/Licence/Mappings.cs index e3bacbeff..b6ec1b454 100644 --- a/src/Spd.Resource.Repository/Licence/Mappings.cs +++ b/src/Spd.Resource.Repository/Licence/Mappings.cs @@ -37,7 +37,25 @@ public Mappings() .ForMember(d => d.City, opt => opt.MapFrom(s => s.spd_employercity)) .ForMember(d => d.Province, opt => opt.MapFrom(s => s.spd_employerprovince)) .ForMember(d => d.Country, opt => opt.MapFrom(s => s.spd_employercountry)) - .ForMember(d => d.PostalCode, opt => opt.MapFrom(s => s.spd_employerpostalcode)); + .ForMember(d => d.PostalCode, opt => opt.MapFrom(s => s.spd_employerpostalcode)) + .ReverseMap(); + + _ = CreateMap() + .ForMember(d => d.spd_expirydate, opt => opt.Ignore()) + .ForMember(d => d.statuscode, opt => opt.Ignore()) + .ForMember(d => d.spd_permitpurposeother, opt => opt.MapFrom(s => s.PermitOtherRequiredReason)) + .ForMember(d => d.spd_employername, opt => opt.MapFrom(s => s.EmployerName)) + .ForMember(d => d.spd_employercontactname, opt => opt.MapFrom(s => s.SupervisorName)) + .ForMember(d => d.spd_employeremail, opt => opt.MapFrom(s => s.SupervisorEmailAddress)) + .ForMember(d => d.spd_employerphonenumber, opt => opt.MapFrom(s => s.SupervisorPhoneNumber)) + .ForMember(d => d.spd_employeraddress1, opt => opt.MapFrom(s => s.EmployerPrimaryAddress == null ? null : s.EmployerPrimaryAddress.AddressLine1)) + .ForMember(d => d.spd_employeraddress2, opt => opt.MapFrom(s => s.EmployerPrimaryAddress == null ? null : s.EmployerPrimaryAddress.AddressLine2)) + .ForMember(d => d.spd_employercity, opt => opt.MapFrom(s => s.EmployerPrimaryAddress == null ? null : s.EmployerPrimaryAddress.City)) + .ForMember(d => d.spd_employerprovince, opt => opt.MapFrom(s => s.EmployerPrimaryAddress == null ? null : s.EmployerPrimaryAddress.Province)) + .ForMember(d => d.spd_employercountry, opt => opt.MapFrom(s => s.EmployerPrimaryAddress == null ? null : s.EmployerPrimaryAddress.Country)) + .ForMember(d => d.spd_employerpostalcode, opt => opt.MapFrom(s => s.EmployerPrimaryAddress == null ? null : s.EmployerPrimaryAddress.PostalCode)) + .ForMember(d => d.spd_rationale, opt => opt.MapFrom(s => s.Rationale)) + .ForMember(d => d.spd_permitpurpose, opt => opt.MapFrom(s => SharedMappingFuncs.GetPermitPurposeOptionSets(s.PermitPurposeEnums))); } internal static LicenceStatusEnum? GetLicenceStatusEnum(int? optionset)