diff --git a/backend/applications/src/app/controllers/form.controller.ts b/backend/applications/src/app/controllers/form.controller.ts index c4f374a6..f2f972ef 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) { } + + /** + * Checks if table exists + * @returns boolean + */ + @Get('health') + async healthCheck(): Promise { + const formCount = await this.formService.healthCheck(); + + if (!formCount) { + return Promise.reject({ + statusCode: 404, + message: 'Table 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..1c03e090 100644 --- a/backend/applications/src/app/services/form.service.ts +++ b/backend/applications/src/app/services/form.service.ts @@ -7,7 +7,24 @@ import { Form } from '../entities/form.entity'; export class FormService { constructor( @InjectRepository(Form) private readonly formRepository: Repository
, - ) {} + ) { } + + /** + * Checks if table exists + * @returns boolean + */ + 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; + } /** * Creates new form submission @@ -36,7 +53,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 +63,6 @@ export class FormService { formId, submissionId, ) => { - for (const property in partialUpdateObject) { if (typeof partialUpdateObject[property] === 'object') { this.processContent( @@ -64,14 +80,11 @@ export class FormService { } else { objectName = property; } - const pathText = "'{" + objectName + "}'"; const newValue = '\'"' + partialUpdateObject[property] + '"\''; - - await this.formRepository .createQueryBuilder() .update(Form)