diff --git a/src/Spd.Manager.Licence/PersonalLicenceAppContract.cs b/src/Spd.Manager.Licence/PersonalLicenceAppContract.cs index 19bb56687..12e11ca1a 100644 --- a/src/Spd.Manager.Licence/PersonalLicenceAppContract.cs +++ b/src/Spd.Manager.Licence/PersonalLicenceAppContract.cs @@ -53,8 +53,8 @@ public abstract record WorkerLicenceAppBase public GenderCode? GenderCode { get; set; } public bool? OneLegalName { get; set; } public string? ExpiredLicenceNumber { get; set; } - public Guid? ExpiredLicenceId { get; set; } = null;//for new, it should be from user answering the question. for renew, replace, update, it should be original licence id. - public bool? HasExpiredLicence { get; set; } = null; //for new, it should be from user answering the question. for renew, replace, update, it should always be true. + public Guid? ExpiredLicenceId { get; set; } = null;//for new application type, for renew, replace, update, it should be null. + public bool? HasExpiredLicence { get; set; } = null; //for new application type public LicenceTermCode? LicenceTermCode { get; set; } public bool? HasCriminalHistory { get; set; } public bool? HasPreviousName { get; set; } @@ -175,7 +175,8 @@ public record WorkerLicenceAppAnonymousSubmitRequestJson : WorkerLicenceAppBase public WorkerCategoryTypeCode[] CategoryCodes { get; set; } = Array.Empty(); public DocumentBase[]? DocumentInfos { get; set; } public Guid[]? FileKeyCodes { get; set; } - public Guid? OriginalApplicationId { get; set; } = null;//for new, it should be null. for renew, replace, update, it should be original application id. + public Guid? OriginalApplicationId { get; set; } //for new, it should be null. for renew, replace, update, it should be original application id. + public Guid? OriginalLicenceId { get; set; } //for new, it should be null. for renew, replace, update, it should be original licence id. } public record WorkerLicenceCreateResponse diff --git a/src/Spd.Manager.Licence/PersonalLicenceAppValidation.cs b/src/Spd.Manager.Licence/PersonalLicenceAppValidation.cs index 0067bf927..c95b3bdb7 100644 --- a/src/Spd.Manager.Licence/PersonalLicenceAppValidation.cs +++ b/src/Spd.Manager.Licence/PersonalLicenceAppValidation.cs @@ -318,8 +318,7 @@ public WorkerLicenceAppAnonymousSubmitRequestJsonValidator(IConfiguration config .WithMessage("Some category cannot be in the same licence request."); RuleFor(r => r.OriginalApplicationId).NotEmpty().When(r => r.ApplicationTypeCode != ApplicationTypeCode.New); - RuleFor(r => r.HasExpiredLicence).Must(r => r == true).When(r => r.ApplicationTypeCode != ApplicationTypeCode.New); - RuleFor(r => r.ExpiredLicenceId).NotEmpty().When(r => r.ApplicationTypeCode != ApplicationTypeCode.New); + RuleFor(r => r.OriginalLicenceId).NotEmpty().When(r => r.ApplicationTypeCode != ApplicationTypeCode.New); } } diff --git a/src/Spd.Resource.Applicants/LicenceApplication/Contract.cs b/src/Spd.Resource.Applicants/LicenceApplication/Contract.cs index e92a116df..bf6264091 100644 --- a/src/Spd.Resource.Applicants/LicenceApplication/Contract.cs +++ b/src/Spd.Resource.Applicants/LicenceApplication/Contract.cs @@ -61,7 +61,6 @@ public record LicenceApplication public bool? IsDogsPurposeDetectionExplosives { get; set; } public WorkerLicenceAppCategory[] CategoryData { get; set; } = Array.Empty(); public bool? IsCanadianCitizen { get; set; } - public Guid? OriginalApplicationId { get; set; } = null; } public record WorkerLicenceAppCategory @@ -79,7 +78,8 @@ public record SaveLicenceApplicationCmd() : LicenceApplication public record CreateLicenceApplicationCmd() : LicenceApplication { public ApplicationStatusEnum ApplicationStatusEnum { get; set; } = ApplicationStatusEnum.Incomplete; - public Guid? OriginalApplicationId { get; set;} = null; + public Guid? OriginalApplicationId { get; set; } + public Guid? OriginalLicenceId { get; set; } }; public record LicenceApplicationResp() : LicenceApplication diff --git a/src/Spd.Resource.Applicants/LicenceApplication/LicenceApplicationRepository.cs b/src/Spd.Resource.Applicants/LicenceApplication/LicenceApplicationRepository.cs index 03e421dd1..dc09b8476 100644 --- a/src/Spd.Resource.Applicants/LicenceApplication/LicenceApplicationRepository.cs +++ b/src/Spd.Resource.Applicants/LicenceApplication/LicenceApplicationRepository.cs @@ -25,10 +25,10 @@ public async Task CreateLicenceApplicationAsync(Creat app.statuscode = (int)ApplicationStatusOptionSet.Incomplete; _context.AddTospd_applications(app); LinkServiceType(cmd.WorkerLicenceTypeCode, app); - if (cmd.HasExpiredLicence == true && cmd.ExpiredLicenceId != null) LinkExpiredLicence(cmd.ExpiredLicenceId, app); contact contact = _mapper.Map(cmd); if (cmd.ApplicationTypeCode == ApplicationTypeEnum.New) { + if (cmd.HasExpiredLicence == true && cmd.ExpiredLicenceId != null) LinkExpiredLicence(cmd.ExpiredLicenceId, app); //for new, always create a new contact contact = await _context.CreateContact(contact, null, _mapper.Map>(cmd.Aliases), ct); } @@ -36,7 +36,7 @@ public async Task CreateLicenceApplicationAsync(Creat { if (cmd.OriginalApplicationId != null) { - spd_application originApp = _context.spd_applications.Where(a => a.spd_applicationid == cmd.OriginalApplicationId).FirstOrDefault(); + spd_application originApp = await _context.spd_applications.Where(a => a.spd_applicationid == cmd.OriginalApplicationId).FirstOrDefaultAsync(ct); //for replace, renew, update, "contact" is already exists, so, do update. contact = await _context.UpdateContact((Guid)originApp._spd_applicantid_value, contact, null, _mapper.Map>(cmd.Aliases), ct); } @@ -44,6 +44,15 @@ public async Task CreateLicenceApplicationAsync(Creat { throw new ArgumentException("for replace, renew or update, original application id cannot be null"); } + + if (cmd.OriginalLicenceId != null) + { + LinkExpiredLicence(cmd.OriginalLicenceId, app); + } + else + { + throw new ArgumentException("for replace, renew or update, original licence id cannot be null"); + } } _context.SetLink(app, nameof(app.spd_ApplicantId_contact), contact); await _context.SaveChangesAsync();