Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: CM-1300 Test DB Connection In Health Check #115

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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";
}
}
}
Loading