From 255e51e2f0461f50e9c3dc904b603c05bf1ccaf8 Mon Sep 17 00:00:00 2001 From: xXpuma99Xx <51341582+xXpuma99Xx@users.noreply.github.com> Date: Thu, 16 Jun 2022 19:18:00 -0500 Subject: [PATCH] listo socket io --- src/app.gateway.ts | 24 ++++++-------- src/app.module.ts | 2 -- src/prestamo/prestamo.module.ts | 3 +- src/prestamo/prestamo.service.ts | 55 +++++++++++++++++++++++++------- 4 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/app.gateway.ts b/src/app.gateway.ts index 9c41e7c..3cf1249 100644 --- a/src/app.gateway.ts +++ b/src/app.gateway.ts @@ -1,12 +1,10 @@ import { - SubscribeMessage, WebSocketGateway, OnGatewayInit, WebSocketServer, OnGatewayConnection, OnGatewayDisconnect, } from '@nestjs/websockets'; -import { Logger } from '@nestjs/common'; import { Socket, Server } from 'socket.io'; @WebSocketGateway({ @@ -18,22 +16,18 @@ export class AppGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect { @WebSocketServer() server: Server; - private logger: Logger = new Logger('AppGateway'); - // @SubscribeMessage('msgToServer') - // handleMessage(client: Socket, payload: string): void { - // this.server.emit('msgToClient', payload); - // } - - afterInit(server: Server) { - this.logger.log('Init'); + actualizarUsuario(id_usuario: number) { + this.server.emit('actualizar-usuario', { id_usuario }); } - handleDisconnect(client: Socket) { - this.logger.log(`Client disconnected: ${client.id}`); + actualizarOperador(id_institucion: number) { + this.server.emit('actualizar-operador', { id_institucion }); } - handleConnection(client: Socket, ...args: any[]) { - this.logger.log(`Client connected: ${client.id}`); - } + afterInit(server: Server) {} + + handleConnection(client: Socket, ...args: any[]) {} + + handleDisconnect(client: Socket) {} } diff --git a/src/app.module.ts b/src/app.module.ts index ada42d7..249a431 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,7 +1,6 @@ import { Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; import { TypeOrmModule } from '@nestjs/typeorm'; -// import { AppGateway } from './app.gateway'; import { AuthModule } from './auth/auth.module'; import { BcryptModule } from './bcrypt/bcrypt.module'; @@ -135,6 +134,5 @@ import { SoapClientModule } from './soap-client/soap-client.module'; UsuarioModule, SoapClientModule, ], - // providers: [AppGateway], }) export class AppModule {} diff --git a/src/prestamo/prestamo.module.ts b/src/prestamo/prestamo.module.ts index b109ac5..a6eefd3 100644 --- a/src/prestamo/prestamo.module.ts +++ b/src/prestamo/prestamo.module.ts @@ -16,6 +16,7 @@ import { MultaModule } from 'src/multa/multa.module'; import { StatusModule } from '../status/status.module'; import { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module'; import { UsuarioModule } from '../usuario/usuario.module'; +import { AppGateway } from '../app.gateway'; @Module({ imports: [ EquipoModule, @@ -34,7 +35,7 @@ import { UsuarioModule } from '../usuario/usuario.module'; UsuarioModule, ], controllers: [PrestamoController], - providers: [PrestamoService], + providers: [AppGateway, PrestamoService], exports: [PrestamoService], }) export class PrestamoModule {} diff --git a/src/prestamo/prestamo.service.ts b/src/prestamo/prestamo.service.ts index 5c6ebd1..95b4bfb 100644 --- a/src/prestamo/prestamo.service.ts +++ b/src/prestamo/prestamo.service.ts @@ -27,6 +27,7 @@ import { OperadorService } from '../operador/operador.service'; import { StatusService } from '../status/status.service'; import { TipoUsuarioService } from '../tipo-usuario/tipo-usuario.service'; import { UsuarioService } from '../usuario/usuario.service'; +import { AppGateway } from '../app.gateway'; @Injectable() export class PrestamoService { @@ -46,6 +47,7 @@ export class PrestamoService { private statusService: StatusService, private tipoUsuarioService: TipoUsuarioService, private usuarioService: UsuarioService, + private appGateway: AppGateway, ) {} async cancelarOperador( @@ -75,7 +77,12 @@ export class PrestamoService { ), ) .then((_) => this.repository.save(prestamo)) - .then((_) => ({ message: 'Se canceló correctamente este préstamo.' })); + .then((_) => { + this.appGateway.actualizarUsuario(prestamo.usuario.id_usuario); + return { + message: 'Se canceló correctamente este préstamo.', + }; + }); } async cancelarUsuario(id_prestamo: number) { @@ -94,7 +101,14 @@ export class PrestamoService { return this.equipoService .update(prestamo.equipo) .then((_) => this.repository.save(prestamo)) - .then((_) => ({ message: 'Se canceló correctamente este préstamo.' })); + .then((_) => { + this.appGateway.actualizarOperador( + prestamo.usuario.institucionCarrera.institucion.id_institucion, + ); + return { + message: 'Se canceló correctamente este préstamo.', + }; + }); } async create( @@ -135,7 +149,6 @@ export class PrestamoService { // const horaMin = moment( // `${ahora.format('YYYY-MM-DD')} ${institucionDia.hora_inicio}`, // ); - // if (!institucionDia.activo) // throw new ConflictException( // 'El día de hoy no se esta realizando préstamos de equipos.', @@ -187,7 +200,13 @@ export class PrestamoService { .toDate(), }), ), - ); + ) + .then((prestamo) => { + this.appGateway.actualizarOperador( + prestamo.usuario.institucionCarrera.institucion.id_institucion, + ); + return prestamo; + }); } async desactivarPrestamos() { @@ -208,7 +227,14 @@ export class PrestamoService { prestamos[i].equipo.status = await this.statusService.findById(1); await this.equipoService .update(prestamos[i].equipo) - .then((_) => this.repository.save(prestamos[i])); + .then((_) => this.repository.save(prestamos[i])) + .then((prestamo) => { + this.appGateway.actualizarOperador( + prestamo.usuario.institucionCarrera.institucion + .id_institucion, + ); + this.appGateway.actualizarUsuario(prestamo.usuario.id_usuario); + }); } }); } @@ -233,9 +259,13 @@ export class PrestamoService { return this.equipoService .update(prestamo.equipo) .then((_) => this.repository.save(prestamo)) - .then((_) => ({ - message: 'Se entregó el equipo de cómputo correctamente.', - })); + .then((_) => { + this.appGateway.actualizarUsuario(prestamo.usuario.id_usuario); + return { + message: 'Se entregó el equipo de cómputo correctamente.', + equipo: prestamo.equipo, + }; + }); } async findAll(filtros: { @@ -448,9 +478,12 @@ export class PrestamoService { return this.equipoService .update(prestamo.equipo) .then((_) => this.repository.save(prestamo)) - .then((_) => ({ - message: 'Se regresó el equipo de cómputo correctamente.', - })); + .then((_) => { + this.appGateway.actualizarUsuario(prestamo.usuario.id_usuario); + return { + message: 'Se regresó el equipo de cómputo.', + }; + }); } async regresarIdPrestamo(