diff --git a/apps/api/src/applicant/endofjourney.service.ts b/apps/api/src/applicant/endofjourney.service.ts index 2256c6d0..f39015cf 100644 --- a/apps/api/src/applicant/endofjourney.service.ts +++ b/apps/api/src/applicant/endofjourney.service.ts @@ -1,6 +1,12 @@ -import { Inject, Injectable, InternalServerErrorException, Logger } from '@nestjs/common'; - -import { getConnection, EntityManager } from 'typeorm'; +import { + Inject, + Injectable, + InternalServerErrorException, + Logger, + OnModuleInit, +} from '@nestjs/common'; + +import { Connection, EntityManager, createConnection } from 'typeorm'; import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import timezone from 'dayjs/plugin/timezone'; @@ -28,12 +34,20 @@ type IEN_APPLICANT_END_OF_JOURNEY = { }; @Injectable() -export class EndOfJourneyService { +export class EndOfJourneyService implements OnModuleInit { constructor( @Inject(Logger) private readonly logger: AppLogger, @Inject(IENMasterService) private readonly ienMasterService: IENMasterService, + private connection: Connection, ) {} + async onModuleInit() { + // Create the connection when the module initializes + if (!this.connection?.isConnected) { + this.connection = await createConnection(); + this.logger.log('EOJ Database connection established', 'END-OF-JOURNEY'); + } + } /** * Entry point @@ -44,7 +58,12 @@ export class EndOfJourneyService { 'END-OF-JOURNEY', ); - const queryRunner = getConnection().createQueryRunner(); + const connection = this.connection; + if (!connection) { + this.logger.error('Connection failed', 'END-OF-JOURNEY'); + return; + } + const queryRunner = connection.createQueryRunner('master'); await queryRunner.startTransaction(); const manager = queryRunner.manager; @@ -69,6 +88,7 @@ export class EndOfJourneyService { throw new InternalServerErrorException('Transaction failed with an unknown error'); } } finally { + this.logger.log(`finally`, 'END-OF-JOURNEY'); await manager.queryRunner?.release(); } } diff --git a/apps/api/src/applicant/entity/ienapplicant.entity.ts b/apps/api/src/applicant/entity/ienapplicant.entity.ts index 5b835385..4daed84f 100755 --- a/apps/api/src/applicant/entity/ienapplicant.entity.ts +++ b/apps/api/src/applicant/entity/ienapplicant.entity.ts @@ -123,6 +123,7 @@ export class IENApplicant { @Column({ type: 'enum', + name: 'end_of_journey', enum: END_OF_JOURNEY_FLAG, nullable: true, // Optional: to make the column nullable }) diff --git a/apps/api/src/endofjourney.ts b/apps/api/src/endofjourney.ts index 78f6dd61..6c88e49c 100644 --- a/apps/api/src/endofjourney.ts +++ b/apps/api/src/endofjourney.ts @@ -3,16 +3,20 @@ import { Context, Handler } from 'aws-lambda'; import { AppModule } from './app.module'; import { AppLogger } from './common/logger.service'; import { EndOfJourneyService } from './applicant/endofjourney.service'; +import { INestApplicationContext } from '@nestjs/common'; +let appContext: INestApplicationContext | null = null; /** * Design this function to trigger existing NestJs application services without Api-Gateway * All the schedule and background job trigger will be added here. * Operation like sync data, update database view or trigger db function, etc. */ export const handler: Handler = async (event, context: Context) => { - const app = await NestFactory.createApplicationContext(AppModule); - const eojService = app.get(EndOfJourneyService); - const logger = app.get(AppLogger); + if (!appContext) { + appContext = await NestFactory.createApplicationContext(AppModule); + } + const eojService = appContext.get(EndOfJourneyService); + const logger = appContext.get(AppLogger); logger.log(event, 'END-OF-JOURNEY'); logger.log(context, 'END-OF-JOURNEY'); @@ -27,8 +31,6 @@ export const handler: Handler = async (event, context: Context) => { } catch (e) { logger.error(e, 'END-OF-JOURNEY'); } - logger.log('...end end of journey complete check', 'END-OF-JOURNEY'); - await app.close(); }; /** diff --git a/apps/web/src/components/applicant/ApplicantProfile.tsx b/apps/web/src/components/applicant/ApplicantProfile.tsx index 05b1a0a8..f634f887 100644 --- a/apps/web/src/components/applicant/ApplicantProfile.tsx +++ b/apps/web/src/components/applicant/ApplicantProfile.tsx @@ -96,6 +96,7 @@ export const ApplicantProfile = () => { {applicant.pathway && ( )} +