diff --git a/src/sistema/sistema.controller.ts b/src/sistema/sistema.controller.ts index b7c4e12..51d5009 100644 --- a/src/sistema/sistema.controller.ts +++ b/src/sistema/sistema.controller.ts @@ -8,6 +8,11 @@ import { ApiAccesoSistema, ApiCreateSistema, ApiGenerarToken } from './sistema.d export class SistemaController { constructor(private readonly sistemaService: SistemaService) {} + @Get() + findAll() { + return this.sistemaService.findAll(); + } + @Post() @ApiCreateSistema() create(@Body() createSistemaDto: CreateSistemaDto) { diff --git a/src/sistema/sistema.service.ts b/src/sistema/sistema.service.ts index 2efa8d1..5258af2 100644 --- a/src/sistema/sistema.service.ts +++ b/src/sistema/sistema.service.ts @@ -1,4 +1,8 @@ -import { Injectable, NotFoundException, UnauthorizedException } from '@nestjs/common'; +import { + Injectable, + NotFoundException, + UnauthorizedException, +} from '@nestjs/common'; import { CreateSistemaDto } from './dto/create-sistema.dto'; import { InjectRepository } from '@nestjs/typeorm'; import { Sistema } from 'src/typeorm/entidades'; @@ -9,21 +13,36 @@ import { encrypt } from 'src/crypto/crypto.util'; @Injectable() export class SistemaService { constructor( - @InjectRepository(Sistema) private readonly sistemaRepository: Repository, - private readonly authService: AuthService - ) { } + @InjectRepository(Sistema) + private readonly sistemaRepository: Repository, + private readonly authService: AuthService, + ) {} + + async findAll() { + const sistemas = await this.sistemaRepository.find(); + if (!sistemas || sistemas.length === 0) { + throw new NotFoundException('No hay sistemas registrados'); + } + return sistemas; + } async Generar_Token(nombreSistema: string, adminPassword: string) { if (adminPassword !== process.env.ADMIN_PASSWORD) { throw new UnauthorizedException('Password erróneo'); } - const sistema = await this.sistemaRepository.findOneBy({ nombre_sistema: nombreSistema }); + const sistema = await this.sistemaRepository.findOneBy({ + nombre_sistema: nombreSistema, + }); if (!sistema) { throw new NotFoundException('Sistema no encontrado'); } - const token = this.authService.generarToken({ sistemaId: sistema.id_sistema, nombre: sistema.nombre_sistema, ip: sistema.ip }); + const token = this.authService.generarToken({ + sistemaId: sistema.id_sistema, + nombre: sistema.nombre_sistema, + ip: sistema.ip, + }); return token; } @@ -33,9 +52,9 @@ export class SistemaService { throw new UnauthorizedException('Token inválido'); } - - - const sistema = await this.sistemaRepository.findOneBy({ id_sistema: decoded.sistemaId }); + const sistema = await this.sistemaRepository.findOneBy({ + id_sistema: decoded.sistemaId, + }); if (!sistema) { throw new UnauthorizedException('Sistema no encontrado'); } @@ -48,9 +67,12 @@ export class SistemaService { throw new UnauthorizedException('Password erróneo'); } - const { encryptedData: encryptedData_token, iv: encryptedData_iv } = encrypt(createSistemaDto.refreshToken) + const { encryptedData: encryptedData_token, iv: encryptedData_iv } = + encrypt(createSistemaDto.refreshToken); - const { encryptedData: encrypted_password, iv: iv_password } = encrypt(createSistemaDto.email_password); + const { encryptedData: encrypted_password, iv: iv_password } = encrypt( + createSistemaDto.email_password, + ); const sistema = this.sistemaRepository.create({ nombre_sistema: createSistemaDto.Nombre, @@ -60,22 +82,22 @@ export class SistemaService { email_password_iv: iv_password, clientId: createSistemaDto.clientId, refreshToken_encrypted: encryptedData_token, - refreshToken_iv: encryptedData_iv - - - - + refreshToken_iv: encryptedData_iv, }); await this.sistemaRepository.save(sistema); - return this.Generar_Token(createSistemaDto.Nombre, createSistemaDto.password); + return this.Generar_Token( + createSistemaDto.Nombre, + createSistemaDto.password, + ); } async findByNombre(sistema: string) { - const sistem = this.sistemaRepository.findOne({ where: { nombre_sistema: sistema } }) + const sistem = this.sistemaRepository.findOne({ + where: { nombre_sistema: sistema }, + }); if (!sistem) { throw new UnauthorizedException('Password erróneo'); - } return sistem; }