diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index edbba811..8544d41b 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -245,4 +245,30 @@ export class AppController { ) { return this.appService.findComment(paramComment); } + + + + // #==== API Report + + @Post('report/createReport') + createReport( + @Body('action') action: string, + @Body('description') description: string, + @Body('date_entry') date_entry: Date, + @Body('user_id') user_id: string, + ) { + return this.appService.createReport(action, description, date_entry, user_id); + } + + @Get('report/allReports') + allReports() { + return this.appService.allReports(); + } + + @Get('report/find/:id') + findReport( + @Param('id') paramReport: string + ) { + return this.appService.findReport(paramReport); + } } \ No newline at end of file diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index ba7b1bb9..60ea184f 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -13,7 +13,7 @@ export class AppService { @Inject('SERVICIO_PAGOS') private readonly clientPaymentApp: ClientProxy, @Inject('SERVICIO_RESERVACIONES') private readonly clientReservationApp: ClientProxy, @Inject('SERVICIO_POSTS') private readonly clientPostApp: ClientProxy, - + @Inject('SERVICIO_REPORTES') private readonly clientReportApp: ClientProxy, @Inject('SERVICIO_NOTIFICACIONES') private readonly clientNotificationtApp: ClientProxy, ) { } @@ -341,4 +341,42 @@ export class AppService { map((message: string) => ({ message })), ); } + + // ====================== REPORTS =============================== + + //Report parameter from API + createReport(action: string, description: string, date_entry: Date, + user_id: string) { + const pattern = { cmd: 'createReport' }; + const payload = { + action: action, description: description, date_entry: date_entry, + user_id: user_id + }; + return this.clientReportApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + allReports() { + const pattern = { cmd: 'findAllReports' }; + const payload = {}; + return this.clientReportApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + //GET parameter from API + findReport(paramReportId: string) { + const pattern = { cmd: 'findOneReport' }; + const payload = { id: paramReportId }; + return this.clientReportApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } } \ No newline at end of file diff --git a/servicio-reportes/src/app.module.ts b/servicio-reportes/src/app.module.ts index 884da55e..099c3d4b 100644 --- a/servicio-reportes/src/app.module.ts +++ b/servicio-reportes/src/app.module.ts @@ -17,16 +17,6 @@ import { ClientsModule, Transport } from "@nestjs/microservices"; } } ]), - ClientsModule.register([ - { - name: "SERVICIO_PAGOS", - transport: Transport.TCP, - options: { - host: "127.0.0.1", - port: 3005 - } - } - ]), MongooseModule.forRoot(`mongodb+srv://proyecto_4:proyecto_4@proyecto4.yv4fb.mongodb.net/servicio_reportes?retryWrites=true&w=majority`), ReportsModule], controllers: [], diff --git a/servicio-reportes/src/main.ts b/servicio-reportes/src/main.ts index 13cad38c..38819543 100644 --- a/servicio-reportes/src/main.ts +++ b/servicio-reportes/src/main.ts @@ -1,8 +1,18 @@ -import { NestFactory } from '@nestjs/core'; -import { AppModule } from './app.module'; +import { NestFactory } from "@nestjs/core"; +import { Transport } from "@nestjs/microservices"; +import { AppModule } from "./app.module"; +import { Logger } from "@nestjs/common"; + +const logger = new Logger(); async function bootstrap() { - const app = await NestFactory.create(AppModule); - await app.listen(3000); + const app = await NestFactory.createMicroservice(AppModule, { + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3008 + } + }); + app.listen().then(() => logger.log("Microservice Reportes is listening" )); } -bootstrap(); +bootstrap(); \ No newline at end of file diff --git a/servicio-reportes/src/reports/reports.controller.ts b/servicio-reportes/src/reports/reports.controller.ts new file mode 100644 index 00000000..e2454c74 --- /dev/null +++ b/servicio-reportes/src/reports/reports.controller.ts @@ -0,0 +1,37 @@ +import { Controller } from '@nestjs/common'; +import { MessagePattern, Payload } from '@nestjs/microservices'; +import { ReportsService } from './reports.service'; +import { Report, ReportDocument } from '../schemas/report.schema'; + +@Controller() +export class ReportsController { + constructor(private readonly reportsService: ReportsService) {} + + + @MessagePattern({ cmd: 'createReport' }) + create(@Payload() report: ReportDocument) { + return this.reportsService.create(report); + } + + @MessagePattern({ cmd: 'findAllReports' }) + findAll() { + return this.reportsService.findAll(); + } + + @MessagePattern({ cmd: 'findOneReport' }) + findOne(@Payload() id: string) { + let _id = id['id']; + return this.reportsService.findOne(_id); + } + + @MessagePattern({ cmd: 'updateReport' }) + update(@Payload() report: ReportDocument) { + return this.reportsService.update(report.id, report); + } + + @MessagePattern({ cmd: 'removeReport' }) + remove(@Payload() id: string) { + let _id = id['id']; + return this.reportsService.remove(_id); + } +} diff --git a/servicio-reportes/src/reports/reports.module.ts b/servicio-reportes/src/reports/reports.module.ts new file mode 100644 index 00000000..a070c768 --- /dev/null +++ b/servicio-reportes/src/reports/reports.module.ts @@ -0,0 +1,14 @@ +import { Module } from '@nestjs/common'; +import { ReportsService } from './reports.service'; +import { ReportsController } from './reports.controller'; +import { MongooseModule } from '@nestjs/mongoose'; +import { Report, ReportSchema } from '../schemas/report.schema'; + +@Module({ + imports: [ + MongooseModule.forFeature([{ name: Report.name, schema:ReportSchema }]), + ], + controllers: [ReportsController], + providers: [ReportsService] +}) +export class ReportsModule {} diff --git a/servicio-reportes/src/reports/reports.service.ts b/servicio-reportes/src/reports/reports.service.ts new file mode 100644 index 00000000..1ab3e4b7 --- /dev/null +++ b/servicio-reportes/src/reports/reports.service.ts @@ -0,0 +1,41 @@ +import { Injectable, Inject } from '@nestjs/common'; +import { ClientProxy } from "@nestjs/microservices"; +import { Model } from 'mongoose'; +import { InjectModel } from '@nestjs/mongoose'; +import { Report, ReportDocument } from '../schemas/report.schema'; +import { map } from "rxjs/operators"; + +@Injectable() +export class ReportsService { + constructor( + @InjectModel(Report.name) private readonly reportModel: Model, + + // + ) { } + + async create(report: ReportDocument): Promise { + return this.reportModel.create(report); + } + + async findAll(): Promise { + return this.reportModel + .find() + .setOptions({ sanitizeFilter: true }) + .exec(); + } + + async findOne(id: string): Promise { + return this.reportModel.findOne({ _id: id }).exec(); + } + + + async update(id: string, report: ReportDocument) { + return this.reportModel.findOneAndUpdate({ _id: id }, report, { + new: true, + }); + } + + async remove(id: string) { + return this.reportModel.findByIdAndRemove({ _id: id }).exec(); + } +} diff --git a/servicio-reportes/src/schemas/report.schema.ts b/servicio-reportes/src/schemas/report.schema.ts new file mode 100644 index 00000000..c2817f38 --- /dev/null +++ b/servicio-reportes/src/schemas/report.schema.ts @@ -0,0 +1,24 @@ +import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; +import { Document, ObjectId } from 'mongoose'; + + +export type ReportDocument = Report & Document; + +@Schema({ collection: 'reports' }) +export class Report { + + @Prop() + action: string; + + @Prop() + description: string; + + @Prop() + date_entry: Date; + + @Prop() + user_id: string +} + + +export const ReportSchema = SchemaFactory.createForClass(Report); \ No newline at end of file