diff --git a/src/app.module.ts b/src/app.module.ts index 86628031ca2a10fe172fe824f69d1720c44b43ce..fa223e379a10de69e9223a960612956607ceeff9 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 f98c28c05ed7d9ffcff38ba13184a76f4aefdd02..fde13bd62a635ea670e8526e4fbf97f3f6f60677 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 0000000000000000000000000000000000000000..20c7447bf0e17b3f8899c1502745c78d29a67767 --- /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 5c0fb19965b7677cb05b2a1fea1d52401984f254..da5451cc04f610adb3ce2e90759f1f2140c105fe 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 539f2989f31f66f7455c96d01017b9dc59fe1fd7..0e5276eac70f9b4c8c79a05cb395f9bea41b54e6 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