From 4ea802dd3f71f058f1ef010614b641206eea9e4b Mon Sep 17 00:00:00 2001 From: Ryan Rondeau Date: Mon, 6 Jan 2025 15:15:40 -0800 Subject: [PATCH 1/7] test DB in health check --- backend/src/app.controller.ts | 9 ++++++--- backend/src/app.service.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/backend/src/app.controller.ts b/backend/src/app.controller.ts index 6e72fa95a..11daec593 100644 --- a/backend/src/app.controller.ts +++ b/backend/src/app.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, UseGuards } from "@nestjs/common"; +import { Controller, Get, HttpException } from "@nestjs/common"; import { AppService } from "./app.service"; import { ApiTags } from "@nestjs/swagger"; import { Public } from "./auth/decorators/public.decorator"; @@ -10,8 +10,11 @@ export class AppController { @Get() @Public() - getHello(): string { - return this.appService.getHello(); + async getDBHealthCheck(): Promise { + const result = await this.appService.getDBHealthCheck(); + if (result === "Success") { + return Promise.resolve(result); + } else throw new HttpException("Unable to connect to database", 503); } @Get() diff --git a/backend/src/app.service.ts b/backend/src/app.service.ts index 8ce0cf3b4..13d8ffc1e 100644 --- a/backend/src/app.service.ts +++ b/backend/src/app.service.ts @@ -1,8 +1,20 @@ import { Injectable } from "@nestjs/common"; +import { DataSource } from "typeorm"; @Injectable() export class AppService { + constructor(private readonly dataSource: DataSource) {} getHello(): string { return "Hello Backend!"; } + + // return Success if able to Select 1 from database + async getDBHealthCheck(): Promise { + try { + await this.dataSource.query("SELECT 1"); + return "Success"; + } catch (error) { + return "Failure"; + } + } } From 9faa6efef6baa29ac2a754034c04bfd2e2610734 Mon Sep 17 00:00:00 2001 From: Ryan Rondeau Date: Mon, 6 Jan 2025 15:41:37 -0800 Subject: [PATCH 2/7] Update app.controller.ts --- backend/src/app.controller.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/backend/src/app.controller.ts b/backend/src/app.controller.ts index 11daec593..60553a13b 100644 --- a/backend/src/app.controller.ts +++ b/backend/src/app.controller.ts @@ -16,9 +16,4 @@ export class AppController { return Promise.resolve(result); } else throw new HttpException("Unable to connect to database", 503); } - - @Get() - getHelloPrivate(): string { - return this.appService.getHello(); - } } From 52f4050c166c32a857ec07de45b6602e7f259d0d Mon Sep 17 00:00:00 2001 From: Ryan Rondeau Date: Mon, 6 Jan 2025 16:04:22 -0800 Subject: [PATCH 3/7] Update app.controller.ts --- backend/src/app.controller.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/backend/src/app.controller.ts b/backend/src/app.controller.ts index 60553a13b..8846badc4 100644 --- a/backend/src/app.controller.ts +++ b/backend/src/app.controller.ts @@ -16,4 +16,9 @@ export class AppController { return Promise.resolve(result); } else throw new HttpException("Unable to connect to database", 503); } + + @Get() + getHello(): string { + return this.appService.getHello(); + } } From f3e02feb7906b66b9606f6f11a45c4bbab41e5b0 Mon Sep 17 00:00:00 2001 From: Ryan Rondeau Date: Mon, 6 Jan 2025 16:07:20 -0800 Subject: [PATCH 4/7] Update app.controller.spec.ts --- backend/src/app.controller.spec.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/src/app.controller.spec.ts b/backend/src/app.controller.spec.ts index 98a877323..25cfbb5cc 100644 --- a/backend/src/app.controller.spec.ts +++ b/backend/src/app.controller.spec.ts @@ -4,6 +4,7 @@ import { AppService } from "./app.service"; import { JwtAuthGuard } from "./auth/jwtauth.guard"; import { JwtRoleGuard } from "./auth/jwtrole.guard"; import { ROUTE_ARGS_METADATA } from "@nestjs/common/constants"; +import { DataSource } from "typeorm"; describe("AppController", () => { let appController: AppController; @@ -11,7 +12,7 @@ describe("AppController", () => { beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], - providers: [AppService], + providers: [AppService, DataSource], }).compile(); appController = app.get(AppController); From a0d0d2a25116d9ef4d856b8ef600efa44d7cadc6 Mon Sep 17 00:00:00 2001 From: Ryan Rondeau Date: Mon, 6 Jan 2025 16:24:37 -0800 Subject: [PATCH 5/7] Update app.controller.spec.ts --- backend/src/app.controller.spec.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/src/app.controller.spec.ts b/backend/src/app.controller.spec.ts index 25cfbb5cc..98a877323 100644 --- a/backend/src/app.controller.spec.ts +++ b/backend/src/app.controller.spec.ts @@ -4,7 +4,6 @@ import { AppService } from "./app.service"; import { JwtAuthGuard } from "./auth/jwtauth.guard"; import { JwtRoleGuard } from "./auth/jwtrole.guard"; import { ROUTE_ARGS_METADATA } from "@nestjs/common/constants"; -import { DataSource } from "typeorm"; describe("AppController", () => { let appController: AppController; @@ -12,7 +11,7 @@ describe("AppController", () => { beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], - providers: [AppService, DataSource], + providers: [AppService], }).compile(); appController = app.get(AppController); From e18b27e7cfed9e61d7b4da1c65d4bca57683b11c Mon Sep 17 00:00:00 2001 From: Ryan Rondeau Date: Mon, 6 Jan 2025 16:54:48 -0800 Subject: [PATCH 6/7] Update app.controller.spec.ts --- backend/src/app.controller.spec.ts | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/backend/src/app.controller.spec.ts b/backend/src/app.controller.spec.ts index 98a877323..03a9a326b 100644 --- a/backend/src/app.controller.spec.ts +++ b/backend/src/app.controller.spec.ts @@ -1,9 +1,8 @@ import { Test, TestingModule } from "@nestjs/testing"; import { AppController } from "./app.controller"; import { AppService } from "./app.service"; -import { JwtAuthGuard } from "./auth/jwtauth.guard"; -import { JwtRoleGuard } from "./auth/jwtrole.guard"; -import { ROUTE_ARGS_METADATA } from "@nestjs/common/constants"; +import { DataSource } from "typeorm"; +import { dataSourceMockFactory } from "../../../test/mocks/datasource"; describe("AppController", () => { let appController: AppController; @@ -11,7 +10,13 @@ describe("AppController", () => { beforeEach(async () => { const app: TestingModule = await Test.createTestingModule({ controllers: [AppController], - providers: [AppService], + providers: [ + AppService, + { + provide: DataSource, + useFactory: dataSourceMockFactory, + }, + ], }).compile(); appController = app.get(AppController); From abf6341b8dd5ee2705d0f248aaee1e25695dbea2 Mon Sep 17 00:00:00 2001 From: Ryan Rondeau Date: Mon, 6 Jan 2025 16:57:28 -0800 Subject: [PATCH 7/7] Update app.controller.spec.ts --- backend/src/app.controller.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/app.controller.spec.ts b/backend/src/app.controller.spec.ts index 03a9a326b..7c39bcf17 100644 --- a/backend/src/app.controller.spec.ts +++ b/backend/src/app.controller.spec.ts @@ -2,7 +2,7 @@ import { Test, TestingModule } from "@nestjs/testing"; import { AppController } from "./app.controller"; import { AppService } from "./app.service"; import { DataSource } from "typeorm"; -import { dataSourceMockFactory } from "../../../test/mocks/datasource"; +import { dataSourceMockFactory } from "../test/mocks/datasource"; describe("AppController", () => { let appController: AppController;