diff --git a/services/apps/alcs/src/alcs/application/application-tag/application-tag.service.ts b/services/apps/alcs/src/alcs/application/application-tag/application-tag.service.ts index 1cb61c763..d3168d984 100644 --- a/services/apps/alcs/src/alcs/application/application-tag/application-tag.service.ts +++ b/services/apps/alcs/src/alcs/application/application-tag/application-tag.service.ts @@ -82,4 +82,13 @@ export class ApplicationTagService { return application.tags && application.tags.length > 0 ? application.tags : []; } + + async applicationHasTag(fileNumber: string, tagName: string): Promise { + const application = await this.applicationRepository.findOne({ + where: { fileNumber: fileNumber }, + relations: ['tags'], + }); + + return application?.tags.some((tag) => tag.name === tagName) ?? false; + } } diff --git a/services/apps/alcs/src/portal/application-submission/application-submission.service.spec.ts b/services/apps/alcs/src/portal/application-submission/application-submission.service.spec.ts index d326ff2f2..9de498b6b 100644 --- a/services/apps/alcs/src/portal/application-submission/application-submission.service.spec.ts +++ b/services/apps/alcs/src/portal/application-submission/application-submission.service.spec.ts @@ -32,6 +32,7 @@ import { ValidatedApplicationSubmission } from './application-submission-validat import { ApplicationSubmission } from './application-submission.entity'; import { ApplicationSubmissionService } from './application-submission.service'; import { NaruSubtype } from './naru-subtype/naru-subtype.entity'; +import { ApplicationTagService } from '../../alcs/application/application-tag/application-tag.service'; describe('ApplicationSubmissionService', () => { let service: ApplicationSubmissionService; @@ -41,6 +42,7 @@ describe('ApplicationSubmissionService', () => { >; let mockNaruSubtypeRepository: DeepMocked>; let mockApplicationService: DeepMocked; + let mockApplicationTagService: DeepMocked; let mockLGService: DeepMocked; let mockAppDocService: DeepMocked; let mockGenerateSubmissionDocumentService: DeepMocked; @@ -54,6 +56,7 @@ describe('ApplicationSubmissionService', () => { mockRepository = createMock(); mockStatusRepository = createMock(); mockApplicationService = createMock(); + mockApplicationTagService = createMock(); mockLGService = createMock(); mockAppDocService = createMock(); mockGenerateSubmissionDocumentService = createMock(); @@ -75,6 +78,10 @@ describe('ApplicationSubmissionService', () => { providers: [ ApplicationSubmissionService, ApplicationSubmissionProfile, + { + provide: ApplicationTagService, + useValue: mockApplicationTagService, + }, { provide: getRepositoryToken(ApplicationSubmission), useValue: mockRepository, diff --git a/services/apps/alcs/src/portal/application-submission/application-submission.service.ts b/services/apps/alcs/src/portal/application-submission/application-submission.service.ts index c08061172..9e79f33df 100644 --- a/services/apps/alcs/src/portal/application-submission/application-submission.service.ts +++ b/services/apps/alcs/src/portal/application-submission/application-submission.service.ts @@ -35,6 +35,7 @@ import { } from './application-submission.dto'; import { ApplicationSubmission } from './application-submission.entity'; import { NaruSubtype } from './naru-subtype/naru-subtype.entity'; +import { ApplicationTagService } from '../../alcs/application/application-tag/application-tag.service'; const LG_VISIBLE_STATUSES = [ SUBMISSION_STATUS.INCOMPLETE, @@ -82,6 +83,7 @@ export class ApplicationSubmissionService { private generateReviewDocumentService: GenerateReviewDocumentService, private applicationSubmissionStatusService: ApplicationSubmissionStatusService, @InjectMapper() private mapper: Mapper, + private applicationTagService: ApplicationTagService, ) {} async getOrFailByFileNumber(fileNumber: string) { @@ -197,6 +199,8 @@ export class ApplicationSubmissionService { this.setInclusionExclusionFields(applicationSubmission, updateDto); this.setCovenantFields(applicationSubmission, updateDto); + await this.applyTags(applicationSubmission); + await this.applicationSubmissionRepository.save(applicationSubmission); if (!applicationSubmission.isDraft && updateDto.localGovernmentUuid) { @@ -1225,4 +1229,52 @@ export class ApplicationSubmissionService { localGovernmentGuid: result[0]?.localGovernment_bceid_business_guid, }; } + + private async applyTags(applicationSubmission: ApplicationSubmission) { + switch (applicationSubmission.typeCode) { + case 'NARU': + await this.applyNaruTags(applicationSubmission); + break; + } + } + + private async applyNaruTags(applicationSubmission: ApplicationSubmission) { + await this.conditionallyApplyTag( + applicationSubmission.fileNumber, + applicationSubmission.naruWillBeOverFiveHundredM2 ?? false, + 'Principal Residence > 500m2', + ); + await this.conditionallyApplyTag( + applicationSubmission.fileNumber, + applicationSubmission.naruWillHaveTemporaryForeignWorkerHousing ?? false, + 'Temporary Foreign Worker Housing', + ); + await this.conditionallyApplyTag( + applicationSubmission.fileNumber, + applicationSubmission.naruWillRetainResidence ?? false, + 'Reside & Replace', + ); + await this.conditionallyApplyTag( + applicationSubmission.fileNumber, + applicationSubmission.naruWillHaveAdditionalResidence ?? false, + 'Additional Residence', + ); + await this.conditionallyApplyTag( + applicationSubmission.fileNumber, + applicationSubmission.naruWillImportFill ?? false, + 'Fill Placement', + ); + } + + private async conditionallyApplyTag(fileNumber: string, condition: boolean, tagName: string) { + const tagExists = await this.applicationTagService.applicationHasTag(fileNumber, tagName); + + if (condition && !tagExists) { + await this.applicationTagService.addTagToApplication(fileNumber, tagName); + } + + if (!condition && tagExists) { + await this.applicationTagService.removeTagFromApplication(fileNumber, tagName); + } + } }