Skip to content

Commit

Permalink
IEN-912 | Journey Complete (#685)
Browse files Browse the repository at this point in the history
* test 1

* test 2

* test 3

* test 4

* test 5

* test 6

* test 7

* test 8

* test 9

* test 10

* test 11

* test 12

* test 13

* test 14

* finalize

* fix sonarqube

---------

Co-authored-by: Jerry Wang <jerryappleid761208@gmail.com>
  • Loading branch information
jerry-ey and c1495616js authored Nov 26, 2024
1 parent bf9fe15 commit 565b0d9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
30 changes: 25 additions & 5 deletions apps/api/src/applicant/endofjourney.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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();
}
}
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/applicant/entity/ienapplicant.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
12 changes: 7 additions & 5 deletions apps/api/src/endofjourney.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
};

/**
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/components/applicant/ApplicantProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const ApplicantProfile = () => {
{applicant.pathway && (
<DetailsItem title='Pathway' text={applicant?.pathway.name} span={6} />
)}
<DetailsItem title='End of Journey' text={applicant?.end_of_journey as string} span={6} />
</div>
</div>
</>
Expand Down

0 comments on commit 565b0d9

Please sign in to comment.