From 98f5d28169b9ac7adba06ad07593a576c204600c Mon Sep 17 00:00:00 2001 From: Mariela Date: Fri, 1 Jul 2022 03:48:27 -0600 Subject: [PATCH] update microservicios-pagos agregar schema y metodos de microservicios agregar conexion a bd y api gateway --- api-gateway/src/app.controller.ts | 33 ++++++++++++- api-gateway/src/app.module.ts | 10 ++++ api-gateway/src/app.service.ts | 40 ++++++++++++++++ servicio-pagos/package-lock.json | 1 + servicio-pagos/package.json | 1 + servicio-pagos/src/app.controller.spec.ts | 22 --------- servicio-pagos/src/app.controller.ts | 12 ----- servicio-pagos/src/app.module.ts | 24 ++++++++-- servicio-pagos/src/app.service.ts | 8 ---- servicio-pagos/src/main.ts | 20 ++++++-- .../src/payments/payments.controller.ts | 48 +++++++++++++++++++ .../src/payments/payments.module.ts | 15 ++++++ .../src/payments/payments.service.ts | 43 +++++++++++++++++ servicio-pagos/src/schemas/payment.schema.ts | 33 +++++++++++++ 14 files changed, 256 insertions(+), 54 deletions(-) delete mode 100644 servicio-pagos/src/app.controller.spec.ts delete mode 100644 servicio-pagos/src/app.controller.ts delete mode 100644 servicio-pagos/src/app.service.ts create mode 100644 servicio-pagos/src/payments/payments.controller.ts create mode 100644 servicio-pagos/src/payments/payments.module.ts create mode 100644 servicio-pagos/src/payments/payments.service.ts create mode 100644 servicio-pagos/src/schemas/payment.schema.ts diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index bf7ef2ad..a6dc7ab8 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -115,10 +115,10 @@ export class AppController { return this.appService.findCommonArea(paramCommonAreaId); } - + // #==== API GUEST //#API userService - create user @Post('guest/createGuest') - createAGuest( + createGuest( @Body('name') name: string, @Body('last_name') last_name: string, @Body('dni') dni: string, @@ -141,4 +141,33 @@ export class AppController { ) { return this.appService.findGuest(paramGuestDNI); } + + + // #==== API Payment + //#API userService - create user + @Post('payment/createPayment') + createPayment( + @Body('date_payment') date_payment: Date, + @Body('mount') mount: number, + @Body('description') description: string, + @Body('period') period: string, + @Body('status') status: string, + @Body('user_id') user_id: string, + @Body('communty_id') communty_id: string, + ) { + return this.appService.createPayment(date_payment, mount, description, + period, status, user_id, communty_id); + } + + @Get('payment/allPayments') + allPayments() { + return this.appService.allPayments(); + } + + @Get('payment/find/:dni') + findPayment( + @Param('dni') paramPaymentDNI: string + ) { + return this.appService.findPayment(paramPaymentDNI); + } } \ No newline at end of file diff --git a/api-gateway/src/app.module.ts b/api-gateway/src/app.module.ts index 6fd20d79..865408d1 100644 --- a/api-gateway/src/app.module.ts +++ b/api-gateway/src/app.module.ts @@ -45,6 +45,16 @@ import { AppService } from './app.service'; } } ]), + ClientsModule.register([ + { + name: "SERVICIO_PAGOS", + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3005 + } + } + ]), ], controllers: [AppController], providers: [AppService], diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index a09c9998..f8053bf4 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -10,6 +10,7 @@ export class AppService { @Inject('SERVICIO_COMUNIDADES') private readonly clientCommunityApp: ClientProxy, @Inject('SERVICIO_AREAS_COMUNES') private readonly clientCommonAreaApp: ClientProxy, @Inject('SERVICIO_INVITADOS') private readonly clientGuestApp: ClientProxy, + @Inject('SERVICIO_PAGOS') private readonly clientPaymentApp: ClientProxy, ) { } // ====================== USERS =============================== @@ -183,4 +184,43 @@ export class AppService { ); } + // ====================== PAYMENTS =============================== + + //POST parameter from API + createPayment(date_payment: Date, mount: number, description: string, period: string + , status: string, user_id: string, communty_id: string) { + const pattern = { cmd: 'createPayment' }; + const payload = { + date_payment: date_payment, mount: mount, description: description, + period: period, status: status, user_id: user_id, communty_id: communty_id + }; + return this.clientPaymentApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + allPayments() { + const pattern = { cmd: 'findAllPayments' }; + const payload = {}; + return this.clientPaymentApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + //GET parameter from API + findPayment(paramPaymentId: string) { + const pattern = { cmd: 'findOnePayment' }; + const payload = { id: paramPaymentId }; + return this.clientPaymentApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + } \ No newline at end of file diff --git a/servicio-pagos/package-lock.json b/servicio-pagos/package-lock.json index 3deb7e05..d9e177d2 100644 --- a/servicio-pagos/package-lock.json +++ b/servicio-pagos/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "@nestjs/common": "^8.0.0", "@nestjs/core": "^8.0.0", + "@nestjs/mapped-types": "*", "@nestjs/microservices": "^8.4.7", "@nestjs/mongoose": "^9.1.1", "@nestjs/platform-express": "^8.0.0", diff --git a/servicio-pagos/package.json b/servicio-pagos/package.json index f5744eb3..125a220f 100644 --- a/servicio-pagos/package.json +++ b/servicio-pagos/package.json @@ -23,6 +23,7 @@ "dependencies": { "@nestjs/common": "^8.0.0", "@nestjs/core": "^8.0.0", + "@nestjs/mapped-types": "*", "@nestjs/microservices": "^8.4.7", "@nestjs/mongoose": "^9.1.1", "@nestjs/platform-express": "^8.0.0", diff --git a/servicio-pagos/src/app.controller.spec.ts b/servicio-pagos/src/app.controller.spec.ts deleted file mode 100644 index d22f3890..00000000 --- a/servicio-pagos/src/app.controller.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; - -describe('AppController', () => { - let appController: AppController; - - beforeEach(async () => { - const app: TestingModule = await Test.createTestingModule({ - controllers: [AppController], - providers: [AppService], - }).compile(); - - appController = app.get(AppController); - }); - - describe('root', () => { - it('should return "Hello World!"', () => { - expect(appController.getHello()).toBe('Hello World!'); - }); - }); -}); diff --git a/servicio-pagos/src/app.controller.ts b/servicio-pagos/src/app.controller.ts deleted file mode 100644 index cce879ee..00000000 --- a/servicio-pagos/src/app.controller.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Controller, Get } from '@nestjs/common'; -import { AppService } from './app.service'; - -@Controller() -export class AppController { - constructor(private readonly appService: AppService) {} - - @Get() - getHello(): string { - return this.appService.getHello(); - } -} diff --git a/servicio-pagos/src/app.module.ts b/servicio-pagos/src/app.module.ts index 86628031..54966eb4 100644 --- a/servicio-pagos/src/app.module.ts +++ b/servicio-pagos/src/app.module.ts @@ -1,10 +1,24 @@ import { Module } from '@nestjs/common'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; +import { PaymentsModule } from './payments/payments.module'; +import { MongooseModule } from '@nestjs/mongoose'; +import { ClientsModule, Transport } from "@nestjs/microservices"; + @Module({ - imports: [], - controllers: [AppController], - providers: [AppService], + imports: [ + 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_pagos?retryWrites=true&w=majority`), + PaymentsModule], + controllers: [], + providers: [], }) export class AppModule {} diff --git a/servicio-pagos/src/app.service.ts b/servicio-pagos/src/app.service.ts deleted file mode 100644 index 927d7cca..00000000 --- a/servicio-pagos/src/app.service.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Injectable } from '@nestjs/common'; - -@Injectable() -export class AppService { - getHello(): string { - return 'Hello World!'; - } -} diff --git a/servicio-pagos/src/main.ts b/servicio-pagos/src/main.ts index 13cad38c..40f8c1af 100644 --- a/servicio-pagos/src/main.ts +++ b/servicio-pagos/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: 3005 + } + }); + app.listen().then(() => logger.log("Microservice Invitados is listening" )); } -bootstrap(); +bootstrap(); \ No newline at end of file diff --git a/servicio-pagos/src/payments/payments.controller.ts b/servicio-pagos/src/payments/payments.controller.ts new file mode 100644 index 00000000..04a170ac --- /dev/null +++ b/servicio-pagos/src/payments/payments.controller.ts @@ -0,0 +1,48 @@ +import { Controller } from '@nestjs/common'; +import { MessagePattern, Payload } from '@nestjs/microservices'; +import { PaymentsService } from './payments.service'; +import { Payment, PaymentDocument } from 'src/schemas/payment.schema'; + +@Controller() +export class PaymentsController { + constructor(private readonly paymentsService: PaymentsService) {} + + @MessagePattern({cmd: 'createPayment'}) + create(@Payload() payment: PaymentDocument) { + return this.paymentsService.create(payment); + } + + @MessagePattern({cmd: 'findAllPayments'}) + findAll() { + return this.paymentsService.findAll(); + } + + @MessagePattern({cmd: 'findOnePayment'}) + findOne(@Payload() id: string) { + let _id = id['_id']; + return this.paymentsService.findOneId(_id); + } + + @MessagePattern({cmd: 'findPaymentsByUser'}) + findByUser(@Payload() id: string) { + let user_id = id['user_id']; + return this.paymentsService.findByUser(user_id); + } + + @MessagePattern({cmd: 'findPaymentsByCommunity'}) + findByCommunity(@Payload() id: string) { + let community_id = id['community_id']; + return this.paymentsService.findByUser(community_id); + } + + @MessagePattern({cmd: 'updatePayment'}) + update(@Payload() payment: PaymentDocument) { + return this.paymentsService.update(payment.id, payment); + } + + @MessagePattern({cmd: 'removePayment'}) + remove(@Payload() id: string) { + let _id = id['_id']; + return this.paymentsService.remove(_id); + } +} diff --git a/servicio-pagos/src/payments/payments.module.ts b/servicio-pagos/src/payments/payments.module.ts new file mode 100644 index 00000000..9af09c19 --- /dev/null +++ b/servicio-pagos/src/payments/payments.module.ts @@ -0,0 +1,15 @@ +import { Module } from '@nestjs/common'; +import { PaymentsService } from './payments.service'; +import { PaymentsController } from './payments.controller'; +import { MongooseModule } from '@nestjs/mongoose'; +import { Payment, PaymentSchema } from 'src/schemas/payment.schema'; + + +@Module({ + imports: [ + MongooseModule.forFeature([{ name: Payment.name, schema: PaymentSchema }]), + ], + controllers: [PaymentsController], + providers: [PaymentsService] +}) +export class PaymentsModule {} diff --git a/servicio-pagos/src/payments/payments.service.ts b/servicio-pagos/src/payments/payments.service.ts new file mode 100644 index 00000000..2c9bbe2c --- /dev/null +++ b/servicio-pagos/src/payments/payments.service.ts @@ -0,0 +1,43 @@ +import { Injectable } from '@nestjs/common'; +import { Payment, PaymentDocument } from 'src/schemas/payment.schema'; +import { Model } from 'mongoose'; +import { InjectModel } from '@nestjs/mongoose'; + + +@Injectable() +export class PaymentsService { + constructor( + @InjectModel(Payment.name) private readonly paymentModel: Model, + ) {} + + async create(payment: PaymentDocument): Promise { + return this.paymentModel.create(payment); + } + + async findAll(): Promise { + return this.paymentModel + .find() + .setOptions({ sanitizeFilter: true }) + .exec(); + } + + + findOneId(id: string): Promise { + return this.paymentModel.findOne({ _id: id }).exec(); + } + + + findByUser(id: string): Promise { + return this.paymentModel.findOne({ user_id: id }).exec(); + } + + update(id: string, payment: PaymentDocument) { + return this.paymentModel.findOneAndUpdate({ _id: id }, payment, { + new: true, + }); + } + + async remove(id: string) { + return this.paymentModel.findByIdAndRemove({ _id: id }).exec(); + } +} diff --git a/servicio-pagos/src/schemas/payment.schema.ts b/servicio-pagos/src/schemas/payment.schema.ts new file mode 100644 index 00000000..41e7ef94 --- /dev/null +++ b/servicio-pagos/src/schemas/payment.schema.ts @@ -0,0 +1,33 @@ + +import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; +import { Document } from 'mongoose'; + + +export type PaymentDocument = Payment & Document; + +@Schema({ collection: 'payments' }) +export class Payment { + + @Prop() + date_payment: Date; + + @Prop() + mount: number; + + @Prop() + description: string; + + @Prop() + period: string; + + @Prop() + status: string; + + @Prop() + user_id: string; + + @Prop() + communty_id: string; +} + +export const PaymentSchema = SchemaFactory.createForClass(Payment);