Skip to content

Commit

Permalink
update permit fix (#1015)
Browse files Browse the repository at this point in the history
# Description

This PR includes the following proposed change(s):

- Add update permit licence function in LicenceRepo
- Add unit test and integration test
  • Loading branch information
peggy-quartech authored May 7, 2024
1 parent e146e94 commit e28ecc6
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/Spd.Manager.Licence.UnitTest/PermitAppManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,8 @@ public async void Handle_PermitAppUpdateCommand_Return_PermitAppCommandResponse(
});
mockMapper.Setup(m => m.Map<CreateLicenceApplicationCmd>(It.IsAny<PermitAppSubmitRequest>()))
.Returns(new CreateLicenceApplicationCmd() { OriginalApplicationId = licAppId });
mockLicRepo.Setup(a => a.ManageAsync(It.IsAny<UpdateLicenceCmd>(), CancellationToken.None))
.ReturnsAsync(licenceResp);

var existingdoc = fixture.Build<DocumentResp>()
.With(r => r.ExpiryDate, expiryDate)
Expand Down
16 changes: 15 additions & 1 deletion src/Spd.Manager.Licence/Mappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PermitAppSubmitRequest, PermitLicence>()
.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<WorkerCategoryTypeCode> codes)
Expand Down Expand Up @@ -393,7 +407,7 @@ private static List<BranchInfo> GetBranchInfo(IEnumerable<BranchAddr> branchAddr
{
List<BranchInfo> branchInfos = new();

foreach (BranchAddr branchAddr in branchAddrs)
foreach (BranchAddr branchAddr in branchAddrs)
{
BranchInfo branchInfo = new() { BranchAddress = new() };
branchInfo.BranchId = branchAddr.BranchId;
Expand Down
5 changes: 5 additions & 0 deletions src/Spd.Manager.Licence/PermitAppManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ public async Task<PermitAppCommandResponse> Handle(PermitAppUpdateCommand cmd, C
}


//update lic
await _licenceRepository.ManageAsync(
new UpdateLicenceCmd(_mapper.Map<PermitLicence>(cmd.LicenceAnonymousRequest), (Guid)originalLic.LicenceId),
cancellationToken);

//upload new files
await UploadNewDocsAsync(request,
cmd.LicAppFileInfos,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,6 +50,7 @@ public IntegrationTestSetup()
serviceCollection.AddTransient<IOrgRepository, OrgRepository>();
serviceCollection.AddTransient<IPortalUserRepository, PortalUserRepository>();
serviceCollection.AddTransient<IOptionSetRepository, OptionSetRepository>();
serviceCollection.AddTransient<ILicenceRepository, LicenceRepository>();
ServiceProvider = serviceCollection.BuildServiceProvider().CreateScope().ServiceProvider;
}
public IServiceProvider ServiceProvider { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -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<IntegrationTestSetup>
{
private readonly ILicenceRepository _licRepo;
private DynamicsContext _context;

public LicenceRepositoryTest(IntegrationTestSetup testSetup)
{
_licRepo = testSetup.ServiceProvider.GetRequiredService<ILicenceRepository>();
_context = testSetup.ServiceProvider.GetRequiredService<IDynamicsContextFactory>().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);
}
}
18 changes: 13 additions & 5 deletions src/Spd.Resource.Repository/Licence/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Spd.Resource.Repository.Licence
public interface ILicenceRepository
{
public Task<LicenceListResp> QueryAsync(LicenceQry query, CancellationToken cancellationToken);
public Task<LicenceResp> ManageAsync(LicenceCmd cmd, CancellationToken cancellationToken);
public Task<LicenceResp> ManageAsync(UpdateLicenceCmd cmd, CancellationToken cancellationToken);
}

public record LicenceQry
Expand All @@ -24,12 +24,18 @@ public record LicenceListResp
public IEnumerable<LicenceResp> Items { get; set; } = Array.Empty<LicenceResp>();
}

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; }
Expand All @@ -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; }
Expand All @@ -51,7 +60,6 @@ public record LicenceResp()
public IEnumerable<PermitPurposeEnum>? PermitPurposeEnums { get; set; }
//permit
}

public enum LicenceStatusEnum
{
Active,
Expand Down
14 changes: 12 additions & 2 deletions src/Spd.Resource.Repository/Licence/LicenceRepository.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -63,9 +64,18 @@ public async Task<LicenceListResp> QueryAsync(LicenceQry qry, CancellationToken
};
}

public async Task<LicenceResp> ManageAsync(LicenceCmd cmd, CancellationToken ct)
public async Task<LicenceResp> ManageAsync(UpdateLicenceCmd cmd, CancellationToken ct)
{
return null;
IQueryable<spd_licence> 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<PermitLicence, spd_licence>(cmd.PermitLicence, lic);
_context.UpdateObject(lic);
await _context.SaveChangesAsync(ct);
return _mapper.Map<LicenceResp>(lic);
}
}

Expand Down
20 changes: 19 additions & 1 deletion src/Spd.Resource.Repository/Licence/Mappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PermitLicence, spd_licence>()
.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)
Expand Down

0 comments on commit e28ecc6

Please sign in to comment.