From 309359118aa60bf9e2c087d7788778db31346a60 Mon Sep 17 00:00:00 2001 From: nikhila-aot Date: Mon, 11 Mar 2024 17:51:58 -0700 Subject: [PATCH 1/3] health check api --- .../src/app/controllers/form.controller.ts | 28 +++++++++++++++---- .../src/app/services/form.service.ts | 16 +++++++---- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/backend/applications/src/app/controllers/form.controller.ts b/backend/applications/src/app/controllers/form.controller.ts index c4f374a6..2a43738f 100644 --- a/backend/applications/src/app/controllers/form.controller.ts +++ b/backend/applications/src/app/controllers/form.controller.ts @@ -1,5 +1,5 @@ import { Body, Controller, Get, Param, Patch, Post, Put } from '@nestjs/common'; -import { Resource, Unprotected } from 'nest-keycloak-connect'; +import { Unprotected } from 'nest-keycloak-connect'; import { SubmissionResponse } from '../dto/submissionResponse.dto'; import { Form } from '../entities/form.entity'; import { FormService } from '../services/form.service'; @@ -8,7 +8,24 @@ import { FormService } from '../services/form.service'; //@Resource('application-service') @Unprotected() export class FormController { - constructor(private formService: FormService) {} + constructor(private formService: FormService) { } + + /** + * Gets count of submitted forms + * @returns form count + */ + @Get('health') + async getFormCount(): Promise { + const formCount = await this.formService.formCount(); + + if (formCount == 0) { + return Promise.reject({ + statusCode: 404, + message: 'Form data not found', + }); + } + return formCount; + } /** * Get a submitted form using @@ -25,12 +42,11 @@ export class FormController { formId, ); - if(!savedSubmission) - { + if (!savedSubmission) { return Promise.reject({ statusCode: 404, - message: 'Form data not found' - }) + message: 'Form data not found', + }); } const submissionResponse: SubmissionResponse = diff --git a/backend/applications/src/app/services/form.service.ts b/backend/applications/src/app/services/form.service.ts index 53fff6d9..08c5ce73 100644 --- a/backend/applications/src/app/services/form.service.ts +++ b/backend/applications/src/app/services/form.service.ts @@ -7,7 +7,15 @@ import { Form } from '../entities/form.entity'; export class FormService { constructor( @InjectRepository(Form) private readonly formRepository: Repository
, - ) {} + ) { } + + /** + * Gets count of submitted forms + * @returns form count + */ + async formCount(): Promise { + return this.formRepository.count(); + } /** * Creates new form submission @@ -36,7 +44,7 @@ export class FormService { buildUpdateString = (pathText, newValue) => { const returnString = - 'jsonb_set("form_data"::jsonb,' + pathText + ',' + newValue + ')'; + 'jsonb_set("form_data"::jsonb,' + pathText + ',' + newValue + ')'; return returnString; }; @@ -46,7 +54,6 @@ export class FormService { formId, submissionId, ) => { - for (const property in partialUpdateObject) { if (typeof partialUpdateObject[property] === 'object') { this.processContent( @@ -64,14 +71,11 @@ export class FormService { } else { objectName = property; } - const pathText = "'{" + objectName + "}'"; const newValue = '\'"' + partialUpdateObject[property] + '"\''; - - await this.formRepository .createQueryBuilder() .update(Form) From 997bd8e7e110ed4d16f57f5970227e10ffe55c37 Mon Sep 17 00:00:00 2001 From: nikhila-aot Date: Wed, 13 Mar 2024 22:39:32 -0700 Subject: [PATCH 2/3] added healthcheck api --- .../src/app/controllers/form.controller.ts | 8 ++++---- .../applications/src/app/services/form.service.ts | 15 ++++++++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/backend/applications/src/app/controllers/form.controller.ts b/backend/applications/src/app/controllers/form.controller.ts index 2a43738f..8d6b3fd1 100644 --- a/backend/applications/src/app/controllers/form.controller.ts +++ b/backend/applications/src/app/controllers/form.controller.ts @@ -15,13 +15,13 @@ export class FormController { * @returns form count */ @Get('health') - async getFormCount(): Promise { - const formCount = await this.formService.formCount(); + async healthCheck(): Promise { + const formCount = await this.formService.healthCheck(); - if (formCount == 0) { + if (!formCount) { return Promise.reject({ statusCode: 404, - message: 'Form data not found', + message: 'Table not found', }); } return formCount; diff --git a/backend/applications/src/app/services/form.service.ts b/backend/applications/src/app/services/form.service.ts index 08c5ce73..4bc52c71 100644 --- a/backend/applications/src/app/services/form.service.ts +++ b/backend/applications/src/app/services/form.service.ts @@ -10,11 +10,20 @@ export class FormService { ) { } /** - * Gets count of submitted forms + * Checks if table exists * @returns form count */ - async formCount(): Promise { - return this.formRepository.count(); + async healthCheck(): Promise { + const tableExists = ( + await this.formRepository.manager.query( + `SELECT exists ( + SELECT FROM information_schema.tables + WHERE table_schema = 'epd_applications' + AND table_name = 'form' + )`, + ) + )[0].exists; + return tableExists; } /** From 25c8f297b1096c608a1867b06bb2ef16ecdfab97 Mon Sep 17 00:00:00 2001 From: nikhila-aot Date: Wed, 13 Mar 2024 22:40:57 -0700 Subject: [PATCH 3/3] corrected comment --- backend/applications/src/app/controllers/form.controller.ts | 4 ++-- backend/applications/src/app/services/form.service.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/applications/src/app/controllers/form.controller.ts b/backend/applications/src/app/controllers/form.controller.ts index 8d6b3fd1..f2f972ef 100644 --- a/backend/applications/src/app/controllers/form.controller.ts +++ b/backend/applications/src/app/controllers/form.controller.ts @@ -11,8 +11,8 @@ export class FormController { constructor(private formService: FormService) { } /** - * Gets count of submitted forms - * @returns form count + * Checks if table exists + * @returns boolean */ @Get('health') async healthCheck(): Promise { diff --git a/backend/applications/src/app/services/form.service.ts b/backend/applications/src/app/services/form.service.ts index 4bc52c71..1c03e090 100644 --- a/backend/applications/src/app/services/form.service.ts +++ b/backend/applications/src/app/services/form.service.ts @@ -11,7 +11,7 @@ export class FormService { /** * Checks if table exists - * @returns form count + * @returns boolean */ async healthCheck(): Promise { const tableExists = (