From e62e7acfc818440ae088b53da9c8d788047993a7 Mon Sep 17 00:00:00 2001 From: devendra <devendra@devendras-MacBook-Pro.local> Date: Thu, 15 Jun 2023 17:38:50 +0530 Subject: [PATCH] Feat: Updated logEvent api to return success or failure response --- src/app.module.ts | 6 ++++-- src/controllers/events.controller.ts | 7 ++++--- src/interfaces/common.interface.ts | 4 ++++ src/main.ts | 2 -- src/services/logger.service.ts | 14 ++++++++++---- 5 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 src/interfaces/common.interface.ts diff --git a/src/app.module.ts b/src/app.module.ts index 8662803..fa223e3 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,10 +1,12 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; +import { EventsController } from './controllers/events.controller'; +import { LoggerService } from './services/logger.service'; @Module({ imports: [], - controllers: [AppController], - providers: [AppService], + controllers: [AppController, EventsController], + providers: [AppService, LoggerService], }) export class AppModule {} diff --git a/src/controllers/events.controller.ts b/src/controllers/events.controller.ts index f98c28c..fde13bd 100644 --- a/src/controllers/events.controller.ts +++ b/src/controllers/events.controller.ts @@ -1,12 +1,13 @@ import { Controller, Post, Body, Inject } from '@nestjs/common'; import { LoggerService } from '../services/logger.service'; +import { Response } from "../interfaces/common.interface"; @Controller('events') export class EventsController { constructor(@Inject(LoggerService) private readonly loggerService: LoggerService) {} - @Post() - logEvent(@Body() event: any): void { - this.loggerService.logEvent(event); + @Post('/logEvent') + logEvent(@Body() event: any): Response { + return this.loggerService.logEvent(event); } } \ No newline at end of file diff --git a/src/interfaces/common.interface.ts b/src/interfaces/common.interface.ts new file mode 100644 index 0000000..20c7447 --- /dev/null +++ b/src/interfaces/common.interface.ts @@ -0,0 +1,4 @@ +export interface Response { + success: boolean, + message: string, +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 5c0fb19..da5451c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,9 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; -import { ValidationPipe } from '@nestjs/common'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.enableCors(); - app.useGlobalPipes(new ValidationPipe()); await app.listen(3000); } bootstrap(); diff --git a/src/services/logger.service.ts b/src/services/logger.service.ts index 539f298..0e5276e 100644 --- a/src/services/logger.service.ts +++ b/src/services/logger.service.ts @@ -1,12 +1,18 @@ import { Injectable } from "@nestjs/common"; import * as fs from 'fs'; +import { Response } from "../interfaces/common.interface"; @Injectable() export class LoggerService { - private readonly logFilePath: string = "../logs/events.log"; + private readonly logFilePath: string = "./logs/events.log"; - public logEvent(event: any): void { - const logEntry = JSON.stringify(event); - fs.appendFileSync(this.logFilePath, `${logEntry}\n`); + public logEvent(event: any): Response { + try { + const logEntry = JSON.stringify(event); + fs.appendFileSync(this.logFilePath, `${logEntry}\n`); + return { success: true, message: 'Event logged successfully' }; + } catch(error) { + return { success: false, message: 'Failed to log event' }; + } } } \ No newline at end of file -- GitLab