Skip to content

Commit

Permalink
chore: CM-1300 Test DB Connection In Health Check (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayr974 authored Jan 7, 2025
1 parent 0c6cc62 commit 79ce1f9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion backend/src/app.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { Test, TestingModule } from "@nestjs/testing";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { PrismaService } from "nestjs-prisma";

describe("AppController", () => {
let appController: AppController;

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

appController = app.get<AppController>(AppController);
Expand Down
10 changes: 9 additions & 1 deletion backend/src/app.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get } from "@nestjs/common";
import { Controller, Get, HttpException } from "@nestjs/common";
import { AppService } from "./app.service";
import { Public } from "./auth/decorators/public.decorator";
import { ApiTags } from "@nestjs/swagger";
Expand All @@ -8,6 +8,14 @@ import { ApiTags } from "@nestjs/swagger";
export class AppController {
constructor(private readonly appService: AppService) {}

@Get()
@Public()
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()
@Public()
getHello(): string {
Expand Down
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 { PrismaService } from "nestjs-prisma";

@Injectable()
export class AppService {
constructor(private readonly prisma: PrismaService) {}

getHello(): string {
return "Hello from Emerald!";
}

async getDBHealthCheck(): Promise<string> {
try {
await this.prisma.$queryRaw`SELECT 1`;
return "Success";
} catch (error) {
return "Failure";
}
}
}

0 comments on commit 79ce1f9

Please sign in to comment.