Skip to content

Commit

Permalink
chore: CM-1300 Test DB Connection In Health Check (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayr974 authored Jan 7, 2025
1 parent 6ef5734 commit c8a2ca0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
13 changes: 9 additions & 4 deletions backend/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
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;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
providers: [
AppService,
{
provide: DataSource,
useFactory: dataSourceMockFactory,
},
],
}).compile();

appController = app.get<AppController>(AppController);
Expand Down
11 changes: 7 additions & 4 deletions backend/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -10,12 +10,15 @@ export class AppController {

@Get()
@Public()
getHello(): string {
return this.appService.getHello();
async getDBHealthCheck(): Promise<string> {
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();
}
}
12 changes: 12 additions & 0 deletions backend/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
try {
await this.dataSource.query("SELECT 1");
return "Success";
} catch (error) {
return "Failure";
}
}
}

0 comments on commit c8a2ca0

Please sign in to comment.