diff --git a/backend/src/app.controller.spec.ts b/backend/src/app.controller.spec.ts index 98a877323..7c39bcf17 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); diff --git a/backend/src/app.controller.ts b/backend/src/app.controller.ts index 6e72fa95a..8846badc4 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,12 +10,15 @@ 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() - getHelloPrivate(): string { + getHello(): string { return this.appService.getHello(); } } 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"; + } + } }