Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 28 additions & 6 deletions src/Spd.Manager.Licence/BizLicAppManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public async Task<BizLicAppResponse> Handle(GetLatestBizLicenceAppQuery query, C
public async Task<BizLicAppCommandResponse> Handle(BizLicAppUpsertCommand cmd, CancellationToken cancellationToken)
{
bool hasDuplicate = await HasDuplicates(cmd.BizLicAppUpsertRequest.BizId,
Enum.Parse<WorkerLicenceTypeEnum>(cmd.BizLicAppUpsertRequest.WorkerLicenceTypeCode.ToString()),
Enum.Parse<WorkerLicenceTypeEnum>(cmd.BizLicAppUpsertRequest.WorkerLicenceTypeCode.Value.ToString()),
cmd.BizLicAppUpsertRequest.LicenceAppId,
cancellationToken);

Expand All @@ -109,11 +109,17 @@ await UpdateDocumentsAsync(
(List<Document>?)cmd.BizLicAppUpsertRequest.DocumentInfos,
cancellationToken);

//link biz members to this application
await _bizContactRepository.ManageBizContactsAsync(
new BizContactsLinkBizAppCmd(cmd.BizLicAppUpsertRequest.BizId, response.LicenceAppId),
cancellationToken);
return _mapper.Map<BizLicAppCommandResponse>(response);
}

public async Task<BizLicAppCommandResponse> Handle(BizLicAppSubmitCommand cmd, CancellationToken cancellationToken)
{
if (cmd.BizLicAppUpsertRequest.LicenceAppId == null)
throw new ApiException(HttpStatusCode.BadRequest, "LicenceAppId cannot be null");
var response = await this.Handle((BizLicAppUpsertCommand)cmd, cancellationToken);
//move files from transient bucket to main bucket when app status changed to Submitted.
await MoveFilesAsync((Guid)cmd.BizLicAppUpsertRequest.LicenceAppId, cancellationToken);
Expand Down Expand Up @@ -144,6 +150,10 @@ public async Task<BizLicAppCommandResponse> Handle(BizLicAppReplaceCommand cmd,
createApp = await SetBizManagerInfo((Guid)originalLic.BizId, createApp, (bool)request.ApplicantIsBizManager, request.ApplicantContactInfo, cancellationToken);
BizLicApplicationCmdResp response = await _bizLicApplicationRepository.CreateBizLicApplicationAsync(createApp, cancellationToken);

//link biz members to this application
await _bizContactRepository.ManageBizContactsAsync(
new BizContactsLinkBizAppCmd(response.AccountId, response.LicenceAppId),
cancellationToken);
decimal cost = await CommitApplicationAsync(request, response.LicenceAppId, cancellationToken);

return new BizLicAppCommandResponse { LicenceAppId = response.LicenceAppId, Cost = cost };
Expand Down Expand Up @@ -185,17 +195,17 @@ await ValidateFilesForRenewUpdateAppAsync(cmd.LicenceRequest,
createApp = await SetBizManagerInfo((Guid)originalBizLic.BizId, createApp, (bool)request.ApplicantIsBizManager, request.ApplicantContactInfo, cancellationToken);
createApp.UploadedDocumentEnums = GetUploadedDocumentEnums(cmd.LicAppFileInfos, existingFiles);
BizLicApplicationCmdResp response = await _bizLicApplicationRepository.CreateBizLicApplicationAsync(createApp, cancellationToken);

if (response == null) throw new ApiException(HttpStatusCode.InternalServerError, "create biz application failed.");
// Upload new files
await UploadNewDocsAsync(null,
cmd.LicAppFileInfos,
response?.LicenceAppId,
response.LicenceAppId,
null,
null,
null,
null,
null,
response?.AccountId,
response.AccountId,
cancellationToken);

// Copying all old files to new application in PreviousFileIds
Expand All @@ -208,6 +218,12 @@ await _documentRepository.ManageAsync(
cancellationToken);
}
}

//link biz members to this application
await _bizContactRepository.ManageBizContactsAsync(
new BizContactsLinkBizAppCmd(response.AccountId, response.LicenceAppId),
cancellationToken);

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

return new BizLicAppCommandResponse { LicenceAppId = response.LicenceAppId, Cost = cost };
Expand Down Expand Up @@ -247,16 +263,18 @@ public async Task<BizLicAppCommandResponse> Handle(BizLicAppUpdateCommand cmd, C
createApp.UploadedDocumentEnums = GetUploadedDocumentEnums(cmd.LicAppFileInfos, existingFiles);
response = await _bizLicApplicationRepository.CreateBizLicApplicationAsync(createApp, cancellationToken);

if (response == null) throw new ApiException(HttpStatusCode.InternalServerError, "create biz application failed.");

// Upload new files
await UploadNewDocsAsync(null,
cmd.LicAppFileInfos,
response?.LicenceAppId,
response.LicenceAppId,
null,
null,
null,
null,
null,
response?.AccountId,
response.AccountId,
cancellationToken);

// Copying all old files to new application in PreviousFileIds
Expand All @@ -270,6 +288,10 @@ await _documentRepository.ManageAsync(
}
}

//link biz members to this application
await _bizContactRepository.ManageBizContactsAsync(
new BizContactsLinkBizAppCmd(response.AccountId, response.LicenceAppId),
cancellationToken);
cost = await CommitApplicationAsync(request, response.LicenceAppId, cancellationToken);
}

Expand Down
15 changes: 13 additions & 2 deletions src/Spd.Manager.Payment/PaymentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ public async Task<Guid> Handle(PaymenCreateCommand command, CancellationToken ct

//if application is sole-proprietor combo application, set combo swl applicatoin to be Paid too.
BizLicApplicationResp bizApp = await _bizAppRepository.GetBizLicApplicationAsync(command.PaybcPaymentResult.ApplicationId, ct);
if (bizApp.BizTypeCode == BizTypeEnum.NonRegisteredSoleProprietor || bizApp.BizTypeCode == BizTypeEnum.RegisteredSoleProprietor)
if (bizApp.WorkerLicenceTypeCode == WorkerLicenceTypeEnum.SecurityBusinessLicence) //first, the application must be bizLicApp
{
if (bizApp.SoleProprietorSWLAppId != null)
if (bizApp.SoleProprietorSWLAppId != null && (bizApp.BizTypeCode == BizTypeEnum.NonRegisteredSoleProprietor || bizApp.BizTypeCode == BizTypeEnum.RegisteredSoleProprietor))
{
createCmd.ApplicationId = bizApp.SoleProprietorSWLAppId.Value;
createCmd.PaymentId = Guid.NewGuid();
Expand All @@ -208,6 +208,17 @@ public async Task<Guid> Handle(PaymenCreateCommand command, CancellationToken ct
updateCmd.PaymentStatus = command.PaybcPaymentResult.Success ? PaymentStatusEnum.Successful : PaymentStatusEnum.Failure;
await _paymentRepository.ManageAsync(updateCmd, ct);
}
//if application has non-swl controlling member crc applications, we need to set all those to paid.
foreach (Guid cmCrcAppId in bizApp.NonSwlControllingMemberCrcAppIds)
{
createCmd.ApplicationId = cmCrcAppId;
createCmd.PaymentId = Guid.NewGuid();
createCmd.TransAmount = 0;
await _paymentRepository.ManageAsync(createCmd, ct);
updateCmd.PaymentId = createCmd.PaymentId;
updateCmd.PaymentStatus = command.PaybcPaymentResult.Success ? PaymentStatusEnum.Successful : PaymentStatusEnum.Failure;
await _paymentRepository.ManageAsync(updateCmd, ct);
}
}
return paymentId;
}
Expand Down
25 changes: 25 additions & 0 deletions src/Spd.Resource.Repository/BizContact/BizContactRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,35 @@ public async Task<IEnumerable<BizContactResp>> QueryBizContactsAsync(BizContactQ
BizContactCreateCmd c => await CreateBizContactAsync(c, ct),
BizContactUpdateCmd c => await UpdateBizContactAsync(c, ct),
BizContactDeleteCmd c => await DeleteBizContactAsync(c, ct),
BizContactsLinkBizAppCmd c => await LinkBizContactsToBizApp(c.BizId, c.BizAppId, ct),
_ => throw new NotSupportedException($"{cmd.GetType().Name} is not supported")
};
}

private async Task<Guid?> LinkBizContactsToBizApp(Guid bizId, Guid bizAppId, CancellationToken ct)
{
IQueryable<spd_businesscontact> bizContacts = _context.spd_businesscontacts
.Expand(c => c.spd_businesscontact_spd_application)
.Where(c => c._spd_organizationid_value == bizId);
spd_application? bizApp = _context.spd_applications.Where(a => a.spd_applicationid == bizAppId).FirstOrDefault();
foreach (spd_businesscontact bc in bizContacts.Where(c => c.statecode == DynamicsConstants.StateCode_Active))
{
if (!bc.spd_businesscontact_spd_application.Any(a => a.spd_applicationid == bizAppId))
{
_context.AddLink(bc, nameof(bc.spd_businesscontact_spd_application), bizApp);
}
}
foreach (spd_businesscontact bc in bizContacts.Where(c => c.statecode == DynamicsConstants.StateCode_Inactive))
{
if (!bc.spd_businesscontact_spd_application.Any(a => a.spd_applicationid == bizAppId))
{
_context.DeleteLink(bc, nameof(bc.spd_businesscontact_spd_application), bizApp);
}
}
await _context.SaveChangesAsync(ct);
return null;
}

private async Task<Guid?> CreateBizContactAsync(BizContactCreateCmd cmd, CancellationToken ct)
{
account? biz = await _context.GetOrgById(cmd.BizContact.BizId, ct);
Expand Down
1 change: 1 addition & 0 deletions src/Spd.Resource.Repository/BizContact/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public record BizContactUpsertCmd(Guid BizId, List<BizContactResp> Data) : BizCo
public record BizContactCreateCmd(BizContact BizContact) : BizContactCmd;
public record BizContactUpdateCmd(Guid BizContactId, BizContact BizContact) : BizContactCmd;
public record BizContactDeleteCmd(Guid BizContactId) : BizContactCmd;
public record BizContactsLinkBizAppCmd(Guid BizId, Guid BizAppId) : BizContactCmd;

//query
public record BizContactQry(Guid? BizId, Guid? AppId, BizContactRoleEnum? RoleCode = null, bool IncludeInactive = false);
Expand Down
6 changes: 5 additions & 1 deletion src/Spd.Resource.Repository/BizContact/Mappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public Mappings()

private (Guid? AppId, ApplicationPortalStatusEnum? PortalStatus) GetLastestControllingMemberCrcApp(IEnumerable<spd_application> apps)
{
spd_application? app = apps.OrderByDescending(app => app.createdon).FirstOrDefault();
Guid? cmServiceTypeGuid = DynamicsContextLookupHelpers.GetServiceTypeGuid(ServiceTypeEnum.SECURITY_BUSINESS_LICENCE_CONTROLLING_MEMBER_CRC.ToString());
spd_application? app = apps
.Where(a => a._spd_servicetypeid_value == cmServiceTypeGuid)
.OrderByDescending(app => app.createdon)
.FirstOrDefault();
if (app == null) return (null, null);
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/Spd.Resource.Repository/BizLicApplication/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public record BizLicApplicationResp() : BizLicApplication
public LicenceTermEnum? OriginalLicenceTermCode { get; set; }
public Guid? ExpiredLicenceId { get; set; }
public bool? HasExpiredLicence { get; set; }
public Guid? SoleProprietorSWLAppId { get; set; }
public Guid? SoleProprietorSWLAppId { get; set; } //sole proprietor swl appliation id, for sole proprietor combo flow
public IEnumerable<Guid> NonSwlControllingMemberCrcAppIds { get; set; }
}

public record PrivateInvestigatorSwlContactInfo : ContactInfo
Expand Down
11 changes: 10 additions & 1 deletion src/Spd.Resource.Repository/BizLicApplication/Mappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public Mappings()
.ForMember(d => d.HasExpiredLicence, opt => opt.MapFrom(s => s.spd_CurrentExpiredLicenceId == null ? false : true))
.ForMember(d => d.PrivateInvestigatorSwlInfo, opt => opt.Ignore())
.ForMember(d => d.SoleProprietorSWLAppId, opt => opt.MapFrom(s => GetSwlAppId(s.spd_businessapplication_spd_workerapplication.ToList())))
.ForMember(d => d.NonSwlControllingMemberCrcAppIds, opt => opt.MapFrom(s => GetNonSwlCmAppIds(s.spd_businessapplication_spd_workerapplication.ToList())))
.IncludeBase<spd_application, BizLicApplication>();

_ = CreateMap<PrivateInvestigatorSwlContactInfo, spd_businesscontact>()
Expand Down Expand Up @@ -107,7 +108,15 @@ private static bool IsApplicantBizManager(spd_application application)

private static Guid? GetSwlAppId(List<spd_application> apps)
{
return apps.OrderByDescending(a => a.createdon)
Guid? swlServiceTypeId = DynamicsContextLookupHelpers.GetServiceTypeGuid(ServiceTypeEnum.SecurityWorkerLicence.ToString());
return apps.Where(a => a._spd_servicetypeid_value == swlServiceTypeId)
.OrderByDescending(a => a.createdon)
.FirstOrDefault()?.spd_applicationid;
}

private static IEnumerable<Guid> GetNonSwlCmAppIds(List<spd_application> apps)
{
Guid? serviceTypeId = DynamicsContextLookupHelpers.GetServiceTypeGuid(ServiceTypeEnum.SECURITY_BUSINESS_LICENCE_CONTROLLING_MEMBER_CRC.ToString());
return apps.Where(a => a._spd_servicetypeid_value == serviceTypeId).Select(a => a.spd_applicationid.Value).ToList();
}
}