update microservicios-pagos
agregar schema y metodos de microservicios agregar conexion a bd y api gateway
This commit is contained in:
		
							parent
							
								
									247d038f42
								
							
						
					
					
						commit
						98f5d28169
					
				|  | @ -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); | ||||
|   } | ||||
| } | ||||
|  | @ -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], | ||||
|  |  | |||
|  | @ -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<string>(pattern, payload) | ||||
|       .pipe( | ||||
|         map((message: string) => ({ message })), | ||||
|       ); | ||||
|   } | ||||
| 
 | ||||
|   allPayments() { | ||||
|     const pattern = { cmd: 'findAllPayments' }; | ||||
|     const payload = {}; | ||||
|     return this.clientPaymentApp | ||||
|       .send<string>(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<string>(pattern, payload) | ||||
|       .pipe( | ||||
|         map((message: string) => ({ message })), | ||||
|       ); | ||||
|   } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
|  | @ -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", | ||||
|  |  | |||
|  | @ -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", | ||||
|  |  | |||
|  | @ -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>(AppController); | ||||
|   }); | ||||
| 
 | ||||
|   describe('root', () => { | ||||
|     it('should return "Hello World!"', () => { | ||||
|       expect(appController.getHello()).toBe('Hello World!'); | ||||
|     }); | ||||
|   }); | ||||
| }); | ||||
|  | @ -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(); | ||||
|   } | ||||
| } | ||||
|  | @ -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 {} | ||||
|  |  | |||
|  | @ -1,8 +0,0 @@ | |||
| import { Injectable } from '@nestjs/common'; | ||||
| 
 | ||||
| @Injectable() | ||||
| export class AppService { | ||||
|   getHello(): string { | ||||
|     return 'Hello World!'; | ||||
|   } | ||||
| } | ||||
|  | @ -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(); | ||||
|  | @ -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); | ||||
|   } | ||||
| } | ||||
|  | @ -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 {} | ||||
|  | @ -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<PaymentDocument>, | ||||
|   ) {} | ||||
| 
 | ||||
|   async create(payment: PaymentDocument): Promise<Payment> { | ||||
|     return this.paymentModel.create(payment); | ||||
|   } | ||||
| 
 | ||||
|   async findAll(): Promise<Payment[]> {  | ||||
|     return this.paymentModel | ||||
|       .find()  | ||||
|       .setOptions({ sanitizeFilter: true })  | ||||
|       .exec(); | ||||
|   } | ||||
| 
 | ||||
| 
 | ||||
|   findOneId(id: string): Promise<Payment> { | ||||
|     return this.paymentModel.findOne({ _id: id }).exec(); | ||||
|   } | ||||
| 
 | ||||
| 
 | ||||
|   findByUser(id: string): Promise<Payment> { | ||||
|     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(); | ||||
|   } | ||||
| } | ||||
|  | @ -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);  | ||||
		Loading…
	
		Reference in New Issue