From 02ebc62a56d576a93fa97b446e840f20f9e54e88 Mon Sep 17 00:00:00 2001 From: Mariela Date: Fri, 15 Jul 2022 19:02:17 -0600 Subject: [PATCH] envio de correos desde servicio usuarios --- api-gateway/src/app.controller.ts | 17 +++++ api-gateway/src/app.service.ts | 14 ++++ servicio-usuarios/src/app.module.ts | 2 +- .../src/users/users.controller.ts | 30 ++++++--- servicio-usuarios/src/users/users.module.ts | 11 +++ servicio-usuarios/src/users/users.service.ts | 67 +++++++++++++------ 6 files changed, 107 insertions(+), 34 deletions(-) diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index f0dfdb79..d3a45f7e 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -313,4 +313,21 @@ export class AppController { return this.appService.html(email, name); } + + // #==== API Users + @Post('user/testSendMail') + testSendMail( + @Body('dni') dni: string, + @Body('name') name: string, + @Body('last_name') last_name: string, + @Body('email') email: string, + @Body('phone') phone: number, + @Body('password') password: string, + @Body('user_type') user_type: string, + @Body('status') status: string, + @Body('date_entry') date_entry: Date, + ) { + return this.appService.testSendMail(dni, name, last_name, email, phone, password, + user_type, status, date_entry); + } } \ No newline at end of file diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 6d4016c5..8af78e63 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -50,6 +50,20 @@ export class AppService { ); } + testSendMail(dni: string, name: string, last_name: string, email: string, phone: number + , password: string, user_type: string, status: string, date_entry: Date) { + const pattern = { cmd: 'testSendMail' }; + const payload = { + dni: dni, name: name, last_name: last_name, email: email, phone: phone, + password: password, user_type: user_type, status: status, date_entry: date_entry + }; + return this.clientUserApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + allUsers() { const pattern = { cmd: 'findAllUsers' }; const payload = {}; diff --git a/servicio-usuarios/src/app.module.ts b/servicio-usuarios/src/app.module.ts index 36bdf0c3..8aaf93a8 100644 --- a/servicio-usuarios/src/app.module.ts +++ b/servicio-usuarios/src/app.module.ts @@ -7,7 +7,7 @@ import { UsersModule } from './users/users.module'; imports: [ ClientsModule.register([ { - name: "SERVICIO_NOTIFICACIONES", + name: "SERVICIO_USUARIOS", transport: Transport.TCP, options: { host: "127.0.0.1", diff --git a/servicio-usuarios/src/users/users.controller.ts b/servicio-usuarios/src/users/users.controller.ts index d65748ce..87ed3541 100644 --- a/servicio-usuarios/src/users/users.controller.ts +++ b/servicio-usuarios/src/users/users.controller.ts @@ -5,7 +5,7 @@ import { UsersService } from './users.service'; @Controller() export class UsersController { - constructor(private readonly userService: UsersService) {} + constructor(private readonly userService: UsersService) { } @MessagePattern({ cmd: 'createUser' }) create(@Payload() user: UserDocument) { @@ -17,6 +17,8 @@ export class UsersController { return this.userService.create(user); } + + @MessagePattern({ cmd: 'findAllUsers' }) findAll() { return this.userService.findAll(); @@ -27,7 +29,7 @@ export class UsersController { let dni = id['dni']; return this.userService.findOneByDNI(dni); } - + @MessagePattern({ cmd: 'updateUser' }) update(@Payload() user: UserDocument) { return this.userService.update(user.id, user); @@ -41,10 +43,10 @@ export class UsersController { //inicio de sesion @MessagePattern({ cmd: 'loginUser' }) - findLogin(@Payload() body:string) { - let pemail= body['email']; - let ppassword= body['password']; - return this.userService.findLogin(pemail,ppassword); + findLogin(@Payload() body: string) { + let pemail = body['email']; + let ppassword = body['password']; + return this.userService.findLogin(pemail, ppassword); } //buscar solo admins del sistema @@ -53,9 +55,15 @@ export class UsersController { return this.userService.allUsersAdminSistema(); } - //buscar solo admins de comunidad - @MessagePattern({ cmd: 'findAdminComunidad' }) - allUsersAdminComunidad() { - return this.userService.allUsersAdminComunidad(); - } + //buscar solo admins de comunidad + @MessagePattern({ cmd: 'findAdminComunidad' }) + allUsersAdminComunidad() { + return this.userService.allUsersAdminComunidad(); + } + + //Prueba de envio de correo despues de registro, llamando a microservicio notificaciones + @MessagePattern({ cmd: 'testSendMail' }) + testSendMail(@Payload() user: UserDocument) { + return this.userService.testSendMail(user); + } } diff --git a/servicio-usuarios/src/users/users.module.ts b/servicio-usuarios/src/users/users.module.ts index 85a0925d..75fc3bd5 100644 --- a/servicio-usuarios/src/users/users.module.ts +++ b/servicio-usuarios/src/users/users.module.ts @@ -4,9 +4,20 @@ import { MongooseModule } from '@nestjs/mongoose'; import { UsersController } from './users.controller'; import { User, UserSchema } from '../schemas/user.schema'; +import { ClientsModule, Transport } from "@nestjs/microservices"; @Module({ imports: [ + ClientsModule.register([ + { + name: "SERVICIO_NOTIFICACIONES", + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3009 + } + } + ]), MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), ], controllers: [UsersController], diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index b7071691..63438e01 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -1,25 +1,32 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Inject } from '@nestjs/common'; import { Model } from 'mongoose'; import { User, UserDocument } from '../schemas/user.schema'; import { InjectModel } from '@nestjs/mongoose'; -import {Md5} from "md5-typescript"; +import { Md5 } from "md5-typescript"; +import { map } from 'rxjs/operators'; + +import { RpcException, ClientProxy } from '@nestjs/microservices'; + + @Injectable() export class UsersService { constructor( @InjectModel(User.name) private readonly userModel: Model, - ) {} + @Inject('SERVICIO_NOTIFICACIONES') private readonly clientNotificationtApp: ClientProxy, + + ) { } private publicKey: string; async create(user: UserDocument): Promise { - let passwordEncriptada=Md5.init(user.password); - user.password=passwordEncriptada; + let passwordEncriptada = Md5.init(user.password); + user.password = passwordEncriptada; return this.userModel.create(user); } - async findAll(): Promise { + async findAll(): Promise { return this.userModel - .find() - .setOptions({ sanitizeFilter: true }) + .find() + .setOptions({ sanitizeFilter: true }) .exec(); } async findOne(id: string): Promise { @@ -41,18 +48,18 @@ export class UsersService { } //inicio de sesion - async findLogin(email: string, password: string) : Promise { - let repo1=this.userModel; + async findLogin(email: string, password: string): Promise { + let repo1 = this.userModel; let userReturn = new Promise((resolve, reject) => { - let repo =repo1; + let repo = repo1; - repo.find({ email : email }).exec((err, res) => { + repo.find({ email: email }).exec((err, res) => { if (err) { reject(err); } else { - let passwordEncriptada=Md5.init(password); - if (res[0].password==passwordEncriptada) { + let passwordEncriptada = Md5.init(password); + if (res[0].password == passwordEncriptada) { resolve(res[0]); } else { @@ -61,19 +68,35 @@ export class UsersService { } }); }); - - return userReturn; + + return userReturn; } //find admin del sistema - async allUsersAdminSistema(): Promise { + async allUsersAdminSistema(): Promise { return this.userModel.find({ user_type: 1 }).exec(); } - //find admin de comunidad - async allUsersAdminComunidad(): Promise { - return this.userModel.find({ user_type: 2 }).exec(); - } + //find admin de comunidad + async allUsersAdminComunidad(): Promise { + return this.userModel.find({ user_type: 2 }).exec(); + } + + async testSendMail(user: UserDocument) { + let passwordEncriptada = Md5.init(user.password); + user.password = passwordEncriptada; + this.userModel.create(user) + /*.then(() => { + + } );*/ + + const pattern = { cmd: 'html' }; + const payload = { email: user['email'], name: user['name'] }; + return this.clientNotificationtApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } - }