Skip to content

Commit

Permalink
Peggy/get lastest app (#1182)
Browse files Browse the repository at this point in the history
# Description

This PR includes the following proposed change(s):

- {List all the changes, if possible add the jira ticket #}
  • Loading branch information
peggy-quartech authored Jun 28, 2024
1 parent 0619b2b commit b07c2c8
Show file tree
Hide file tree
Showing 16 changed files with 276 additions and 17 deletions.
52 changes: 45 additions & 7 deletions src/Spd.Manager.Licence.UnitTest/BizLicenceAppManagerTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using AutoFixture;
using AutoFixture;
using AutoMapper;
using Moq;
using Spd.Manager.Shared;
using Spd.Resource.Repository.Biz;
using Spd.Resource.Repository;
using Spd.Resource.Repository.BizContact;
using Spd.Resource.Repository.BizLicApplication;
using Spd.Resource.Repository.Document;
Expand Down Expand Up @@ -80,6 +81,43 @@ public async void Handle_GetBizLicAppQuery_Return_BizLicAppResponse()
Assert.Equal(licAppId, viewResult.LicenceAppId);
}

[Fact]
public async void Handle_GetLatestBizLicenceAppQuery_WithoutApp_Throw_Exception()
{
//Arrange
Guid applicantId = Guid.NewGuid();
mockLicAppRepo.Setup(a => a.QueryAsync(It.IsAny<LicenceAppQuery>(), CancellationToken.None))
.ReturnsAsync(new List<LicenceAppListResp> { });

//Act
Func<Task> act = () => sut.Handle(new GetLatestBizLicenceAppQuery(applicantId), CancellationToken.None);

//Assert
await Assert.ThrowsAsync<ApiException>(act);
}

[Fact]
public async void Handle_GetLatestBizLicenceAppQuery_ReturnCorrect()
{
//Arrange
Guid bizId = Guid.NewGuid();
Guid applicationId = Guid.NewGuid();
mockLicAppRepo.Setup(a => a.QueryAsync(It.IsAny<LicenceAppQuery>(), CancellationToken.None))
.ReturnsAsync(new List<LicenceAppListResp> {
new() {ApplicationTypeCode = ApplicationTypeEnum.Update, LicenceAppId = applicationId}
});
mockBizLicAppRepo.Setup(a => a.GetBizLicApplicationAsync(It.Is<Guid>(p => p == applicationId), It.IsAny<CancellationToken>()))
.ReturnsAsync(new BizLicApplicationResp() { LicenceAppId = applicationId, ApplicantIsBizManager = true });
mockDocRepo.Setup(m => m.QueryAsync(It.Is<DocumentQry>(p => p.ApplicationId == applicationId), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DocumentListResp { Items = new List<DocumentResp>() });

//Act
var viewResult = await sut.Handle(new GetLatestBizLicenceAppQuery(bizId), CancellationToken.None);

//Assert
Assert.Equal(applicationId, viewResult.LicenceAppId);
}

[Fact]
public async void Handle_BizLicAppUpsertCommand_WithoutLicAppId_Return_BizLicAppCommandResponse()
{
Expand Down Expand Up @@ -413,8 +451,8 @@ public async void Handle_BizLicAppRenewCommand_WithMissingFiles_Throw_Exception(
Items = new List<LicenceResp> { originalLicence }
});
mockBizLicAppRepo.Setup(a => a.GetBizLicApplicationAsync(It.IsAny<Guid>(), CancellationToken.None))
.ReturnsAsync(new BizLicApplicationResp() { LicenceAppId = Guid.NewGuid(), BizId = Guid.NewGuid() });
.ReturnsAsync(new BizLicApplicationResp() { LicenceAppId = Guid.NewGuid(), BizId = Guid.NewGuid() });

BizLicAppSubmitRequest request = new()
{
ApplicationTypeCode = ApplicationTypeCode.Renewal,
Expand Down Expand Up @@ -498,10 +536,10 @@ public async void Handle_BizLicAppUpdateCommand_UpdateBiz_Return_BizLicAppComman
Items = new List<LicenceResp> { originalLicence }
});
mockBizLicAppRepo.Setup(a => a.GetBizLicApplicationAsync(It.Is<Guid>(m => m == originalApplicationId), CancellationToken.None))
.ReturnsAsync(new BizLicApplicationResp()
{
LicenceAppId = originalApplicationId,
BizId = bizId ,
.ReturnsAsync(new BizLicApplicationResp()
{
LicenceAppId = originalApplicationId,
BizId = bizId,
UseDogs = true,
CategoryCodes = new List<WorkerCategoryTypeEnum>() { WorkerCategoryTypeEnum.ArmouredCarGuard }
});
Expand Down
37 changes: 37 additions & 0 deletions src/Spd.Manager.Licence.UnitTest/PermitAppManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,43 @@ public PermitAppManagerTest()
mockLicAppRepo.Object);
}

[Fact]
public async void Handle_GetLatestPermitApplicationQuery_WithoutApp_Throw_Exception()
{
//Arrange
Guid applicantId = Guid.NewGuid();
mockLicAppRepo.Setup(a => a.QueryAsync(It.IsAny<LicenceAppQuery>(), CancellationToken.None))
.ReturnsAsync(new List<LicenceAppListResp> { });

//Act
Func<Task> act = () => sut.Handle(new GetLatestPermitApplicationQuery(applicantId, WorkerLicenceTypeCode.SecurityBusinessLicence), CancellationToken.None);

//Assert
await Assert.ThrowsAsync<ApiException>(act);
}

[Fact]
public async void Handle_GetLatestPermitApplicationQuery_ReturnCorrect()
{
//Arrange
Guid applicantId = Guid.NewGuid();
Guid applicationId = Guid.NewGuid();
mockLicAppRepo.Setup(a => a.QueryAsync(It.IsAny<LicenceAppQuery>(), CancellationToken.None))
.ReturnsAsync(new List<LicenceAppListResp> {
new() {ApplicationTypeCode = ApplicationTypeEnum.Update, LicenceAppId = applicationId}
});
mockPersonLicAppRepo.Setup(a => a.GetLicenceApplicationAsync(It.Is<Guid>(p => p == applicationId), It.IsAny<CancellationToken>()))
.ReturnsAsync(new LicenceApplicationResp() { LicenceAppId = applicationId });
mockDocRepo.Setup(m => m.QueryAsync(It.Is<DocumentQry>(p => p.ApplicationId == applicationId), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DocumentListResp { Items = new List<DocumentResp>() });

//Act
var viewResult = await sut.Handle(new GetLatestPermitApplicationQuery(applicantId, WorkerLicenceTypeCode.BodyArmourPermit), CancellationToken.None);

//Assert
Assert.Equal(applicationId, viewResult.LicenceAppId);
}

[Fact]
public async void Handle_PermitUpsertCommand_WithoutLicAppId_Return_PermitCommandResponse()
{
Expand Down
41 changes: 41 additions & 0 deletions src/Spd.Manager.Licence.UnitTest/SecurityWorkerAppManagerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,47 @@ public SecurityWorkerAppManagerTest()
mockTransientFileStorageService.Object);
}

[Fact]
public async void Handle_GetLatestWorkerLicenceQuery_WithoutApp_Throw_Exception()
{
//Arrange
Guid applicantId = Guid.NewGuid();
mockLicAppRepo.Setup(a => a.QueryAsync(It.IsAny<LicenceAppQuery>(), CancellationToken.None))
.ReturnsAsync(new List<LicenceAppListResp> {
new() {ApplicationTypeCode = ApplicationTypeEnum.Replacement}
});

//Act
Func<Task> act = () => sut.Handle(new GetLatestWorkerLicenceQuery(applicantId), CancellationToken.None);

//Assert
await Assert.ThrowsAsync<ApiException>(act);
}

[Fact]
public async void Handle_GetLatestWorkerLicenceQuery_ReturnCorrect()
{
//Arrange
Guid applicantId = Guid.NewGuid();
Guid applicationId = Guid.NewGuid();
mockLicAppRepo.Setup(a => a.QueryAsync(It.IsAny<LicenceAppQuery>(), CancellationToken.None))
.ReturnsAsync(new List<LicenceAppListResp> {
new() {ApplicationTypeCode = ApplicationTypeEnum.Update, LicenceAppId=applicationId}
});
mockPersonLicAppRepo.Setup(a => a.GetLicenceApplicationAsync(It.Is<Guid>(p => p == applicationId), It.IsAny<CancellationToken>()))
.ReturnsAsync(new LicenceApplicationResp() { LicenceAppId = applicationId });
mockMapper.Setup(m => m.Map<WorkerLicenceAppResponse>(It.IsAny<LicenceApplicationResp>()))
.Returns(new WorkerLicenceAppResponse() { LicenceAppId = applicationId });
mockDocRepo.Setup(m => m.QueryAsync(It.Is<DocumentQry>(p => p.ApplicationId == applicationId), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DocumentListResp { Items = new List<DocumentResp>() });

//Act
var viewResult = await sut.Handle(new GetLatestWorkerLicenceQuery(applicantId), CancellationToken.None);

//Assert
Assert.Equal(applicationId, viewResult.LicenceAppId);
}

[Fact]
public async void Handle_WorkerLicenceUpsertCommand_WithDuplicateApp_Throw_Exception()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Spd.Manager.Licence/BizLicAppContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Spd.Manager.Licence;
public interface IBizLicAppManager
{
public Task<BizLicAppResponse> Handle(GetBizLicAppQuery query, CancellationToken ct);
public Task<BizLicAppResponse> Handle(GetLatestBizLicenceAppQuery query, CancellationToken ct);
public Task<BizLicAppCommandResponse> Handle(BizLicAppUpsertCommand command, CancellationToken ct);
public Task<BizLicAppCommandResponse> Handle(BizLicAppSubmitCommand command, CancellationToken ct);
public Task<BizLicAppCommandResponse> Handle(BizLicAppReplaceCommand command, CancellationToken ct);
Expand All @@ -21,6 +22,7 @@ public record BizLicAppUpsertCommand(BizLicAppUpsertRequest BizLicAppUpsertReque
public record BizLicAppSubmitCommand(BizLicAppUpsertRequest BizLicAppUpsertRequest)
: BizLicAppUpsertCommand(BizLicAppUpsertRequest), IRequest<BizLicAppCommandResponse>;
public record GetBizLicAppQuery(Guid LicenceApplicationId) : IRequest<BizLicAppResponse>;
public record GetLatestBizLicenceAppQuery(Guid BizId) : IRequest<BizLicAppResponse>;
public record GetBizLicAppListQuery(Guid BizId) : IRequest<IEnumerable<LicenceAppListResponse>>;
public record BizLicAppReplaceCommand(
BizLicAppSubmitRequest LicenceRequest,
Expand Down
27 changes: 24 additions & 3 deletions src/Spd.Manager.Licence/BizLicAppManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AutoMapper;
using MediatR;
using Spd.Manager.Shared;
using Spd.Resource.Repository;
using Spd.Resource.Repository.Application;
using Spd.Resource.Repository.Biz;
using Spd.Resource.Repository.BizContact;
Expand All @@ -21,6 +22,7 @@ namespace Spd.Manager.Licence;
internal class BizLicAppManager :
LicenceAppManagerBase,
IRequestHandler<GetBizLicAppQuery, BizLicAppResponse>,
IRequestHandler<GetLatestBizLicenceAppQuery, BizLicAppResponse>,
IRequestHandler<BizLicAppUpsertCommand, BizLicAppCommandResponse>,
IRequestHandler<BizLicAppSubmitCommand, BizLicAppCommandResponse>,
IRequestHandler<BizLicAppReplaceCommand, BizLicAppCommandResponse>,
Expand Down Expand Up @@ -76,6 +78,25 @@ public async Task<BizLicAppResponse> Handle(GetBizLicAppQuery query, Cancellatio
return result;
}

public async Task<BizLicAppResponse> Handle(GetLatestBizLicenceAppQuery query, CancellationToken cancellationToken)
{
//get the latest app id
IEnumerable<LicenceAppListResp> list = await _licAppRepository.QueryAsync(
new LicenceAppQuery(
null,
query.BizId,
new List<WorkerLicenceTypeEnum> { WorkerLicenceTypeEnum.SecurityBusinessLicence },
null),
cancellationToken);
LicenceAppListResp? app = list.Where(a => a.ApplicationTypeCode != ApplicationTypeEnum.Replacement)
.OrderByDescending(a => a.SubmittedOn)
.FirstOrDefault();
if (app == null)
throw new ApiException(HttpStatusCode.BadRequest, $"there is no Security Business Licence Application for this business.");

return await Handle(new GetBizLicAppQuery(app.LicenceAppId), cancellationToken);
}

public async Task<BizLicAppCommandResponse> Handle(BizLicAppUpsertCommand cmd, CancellationToken cancellationToken)
{
bool hasDuplicate = await HasDuplicates(cmd.BizLicAppUpsertRequest.BizId,
Expand Down Expand Up @@ -139,7 +160,7 @@ public async Task<BizLicAppCommandResponse> Handle(BizLicAppReplaceCommand cmd,
BizLicApplicationCmdResp response = await _bizLicApplicationRepository.CreateBizLicApplicationAsync(createApp, cancellationToken);

decimal cost = await CommitApplicationAsync(request, response.LicenceAppId, cancellationToken);

// Update members
if (cmd.LicenceRequest.Members != null)
await UpdateMembersAsync(cmd.LicenceRequest.Members,
Expand Down Expand Up @@ -490,7 +511,7 @@ private async Task ValidateFilesForRenewUpdateAppAsync(BizLicAppSubmitRequest re
}
}

private async Task<ChangeSpec> MakeChanges(BizLicApplicationResp originalApp,
private async Task<ChangeSpec> MakeChanges(BizLicApplicationResp originalApp,
BizLicAppSubmitRequest newRequest,
CancellationToken ct)
{
Expand Down Expand Up @@ -537,7 +558,7 @@ await _taskRepository.ManageAsync(new CreateTaskCmd()
}, ct);
}

if (changes.UseDogsChanged)
if (changes.UseDogsChanged)
{
await _taskRepository.ManageAsync(new CreateTaskCmd()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Spd.Manager.Licence/PermitAppContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface IPermitAppManager
public Task<PermitLicenceAppResponse> Handle(GetPermitApplicationQuery query, CancellationToken ct);
public Task<PermitAppCommandResponse> Handle(PermitUpsertCommand command, CancellationToken ct);
public Task<PermitAppCommandResponse> Handle(PermitSubmitCommand command, CancellationToken ct);
public Task<PermitLicenceAppResponse> Handle(GetLatestPermitApplicationQuery query, CancellationToken ct);
}

public record PermitLicenceAppBase : PersonalLicenceAppBase
Expand All @@ -30,7 +31,7 @@ public record PermitLicenceAppBase : PersonalLicenceAppBase
public record PermitUpsertCommand(PermitAppUpsertRequest PermitUpsertRequest) : IRequest<PermitAppCommandResponse>;
public record PermitSubmitCommand(PermitAppUpsertRequest PermitUpsertRequest)
: PermitUpsertCommand(PermitUpsertRequest), IRequest<PermitAppCommandResponse>;

public record GetLatestPermitApplicationQuery(Guid ApplicantId, WorkerLicenceTypeCode WorkerLicenceTypeCode) : IRequest<PermitLicenceAppResponse>;
public record PermitAppUpsertRequest : PermitLicenceAppBase
{
public IEnumerable<Document>? DocumentInfos { get; set; }
Expand Down
29 changes: 27 additions & 2 deletions src/Spd.Manager.Licence/PermitAppManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AutoMapper;
using MediatR;
using Spd.Manager.Shared;
using Spd.Resource.Repository;
using Spd.Resource.Repository.Contact;
using Spd.Resource.Repository.Document;
using Spd.Resource.Repository.LicApp;
Expand All @@ -18,6 +19,7 @@ namespace Spd.Manager.Licence;
internal class PermitAppManager :
LicenceAppManagerBase,
IRequestHandler<GetPermitApplicationQuery, PermitLicenceAppResponse>,
IRequestHandler<GetLatestPermitApplicationQuery, PermitLicenceAppResponse>,
IRequestHandler<PermitUpsertCommand, PermitAppCommandResponse>,
IRequestHandler<PermitSubmitCommand, PermitAppCommandResponse>,
IRequestHandler<PermitAppNewCommand, PermitAppCommandResponse>,
Expand Down Expand Up @@ -89,9 +91,30 @@ public async Task<PermitAppCommandResponse> Handle(PermitSubmitCommand cmd, Canc
return new PermitAppCommandResponse { LicenceAppId = response.LicenceAppId, Cost = cost };
}

public async Task<PermitLicenceAppResponse> Handle(GetLatestPermitApplicationQuery query, CancellationToken cancellationToken)
{
if (query.WorkerLicenceTypeCode != WorkerLicenceTypeCode.ArmouredVehiclePermit && query.WorkerLicenceTypeCode != WorkerLicenceTypeCode.BodyArmourPermit)
throw new ApiException(HttpStatusCode.BadRequest, $"Invalid WorkerLicenceTypeCode");

//get the latest app id
IEnumerable<LicenceAppListResp> list = await _licAppRepository.QueryAsync(
new LicenceAppQuery(
query.ApplicantId,
null,
new List<WorkerLicenceTypeEnum> { Enum.Parse<WorkerLicenceTypeEnum>(query.WorkerLicenceTypeCode.ToString()) },
null),
cancellationToken);
LicenceAppListResp? app = list.Where(a => a.ApplicationTypeCode != ApplicationTypeEnum.Replacement)
.OrderByDescending(a => a.SubmittedOn)
.FirstOrDefault();
if (app == null)
throw new ApiException(HttpStatusCode.BadRequest, $"there is no {query.WorkerLicenceTypeCode} for this applicant.");

return await Handle(new GetPermitApplicationQuery(app.LicenceAppId), cancellationToken);
}
#endregion

#region anonymous


public async Task<PermitLicenceAppResponse> Handle(GetPermitApplicationQuery query, CancellationToken cancellationToken)
{
Expand All @@ -102,6 +125,7 @@ public async Task<PermitLicenceAppResponse> Handle(GetPermitApplicationQuery que
return result;
}

#region anonymous new
public async Task<PermitAppCommandResponse> Handle(PermitAppNewCommand cmd, CancellationToken cancellationToken)
{
PermitAppSubmitRequest request = cmd.LicenceAnonymousRequest;
Expand All @@ -114,6 +138,7 @@ public async Task<PermitAppCommandResponse> Handle(PermitAppNewCommand cmd, Canc
decimal cost = await CommitApplicationAsync(request, response.LicenceAppId, cancellationToken);
return new PermitAppCommandResponse { LicenceAppId = response.LicenceAppId, Cost = cost };
}
#endregion

public async Task<PermitAppCommandResponse> Handle(PermitAppReplaceCommand cmd, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -282,7 +307,7 @@ await UploadNewDocsAsync(request.DocumentExpiredInfos,
return new PermitAppCommandResponse() { LicenceAppId = createLicResponse?.LicenceAppId, Cost = 0 };
}

#endregion


private static void ValidateFilesForNewApp(PermitAppNewCommand cmd)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Spd.Manager.Licence/SecurityWorkerAppContract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface ISecurityWorkerAppManager
public Task<WorkerLicenceCommandResponse> Handle(WorkerLicenceUpsertCommand command, CancellationToken ct);
public Task<WorkerLicenceCommandResponse> Handle(WorkerLicenceSubmitCommand command, CancellationToken ct);
public Task<WorkerLicenceAppResponse> Handle(GetWorkerLicenceQuery query, CancellationToken ct);
public Task<WorkerLicenceAppResponse> Handle(GetLatestWorkerLicenceQuery query, CancellationToken ct);
public Task<IEnumerable<LicenceAppListResponse>> Handle(GetLicenceAppListQuery query, CancellationToken ct);
public Task<WorkerLicenceCommandResponse> Handle(WorkerLicenceAppNewCommand command, CancellationToken ct);
public Task<WorkerLicenceCommandResponse> Handle(WorkerLicenceAppReplaceCommand command, CancellationToken ct);
Expand Down Expand Up @@ -41,6 +42,7 @@ public record WorkerLicenceAppUpdateCommand(
: IRequest<WorkerLicenceCommandResponse>;

public record GetWorkerLicenceQuery(Guid LicenceApplicationId) : IRequest<WorkerLicenceAppResponse>;
public record GetLatestWorkerLicenceQuery(Guid ApplicantId) : IRequest<WorkerLicenceAppResponse>;
public record GetLicenceAppListQuery(Guid ApplicantId) : IRequest<IEnumerable<LicenceAppListResponse>>;

public record WorkerLicenceAppResponse : WorkerLicenceAppBase
Expand Down
Loading

0 comments on commit b07c2c8

Please sign in to comment.