From c52e5b7850aafe1b1dadd7d070856a663f91e8c4 Mon Sep 17 00:00:00 2001 From: xXpuma99Xx <51341582+xXpuma99Xx@users.noreply.github.com> Date: Wed, 20 Apr 2022 16:50:34 -0500 Subject: [PATCH] operador y usuario a medias --- src/operador/dto/operador-create.dto.ts | 12 +++ src/operador/dto/operador-login.dto.ts | 9 +++ src/operador/dto/operador-operadores.dto.ts | 14 ++++ src/operador/dto/operador-update.dto.ts | 18 +++++ src/operador/dto/operador.dto.ts | 6 ++ src/operador/operador.controller.ts | 32 ++++++-- src/operador/operador.module.ts | 8 +- src/operador/operador.service.ts | 85 ++++++++++++++++++++- src/usuario/usuario.service.ts | 3 +- 9 files changed, 177 insertions(+), 10 deletions(-) create mode 100644 src/operador/dto/operador-create.dto.ts create mode 100644 src/operador/dto/operador-login.dto.ts create mode 100644 src/operador/dto/operador-operadores.dto.ts create mode 100644 src/operador/dto/operador-update.dto.ts create mode 100644 src/operador/dto/operador.dto.ts diff --git a/src/operador/dto/operador-create.dto.ts b/src/operador/dto/operador-create.dto.ts new file mode 100644 index 0000000..2b7d147 --- /dev/null +++ b/src/operador/dto/operador-create.dto.ts @@ -0,0 +1,12 @@ +import { IsInt, IsString } from 'class-validator'; + +export class OperadorCreateDto { + @IsInt() + id_institucion: number; + + @IsString() + operador: string; + + @IsString() + password: string; +} diff --git a/src/operador/dto/operador-login.dto.ts b/src/operador/dto/operador-login.dto.ts new file mode 100644 index 0000000..9cc20e3 --- /dev/null +++ b/src/operador/dto/operador-login.dto.ts @@ -0,0 +1,9 @@ +import { IsNumberString, IsString } from 'class-validator'; + +export class OperadorLoginDto { + @IsNumberString() + operador: string; + + @IsString() + password: string; +} diff --git a/src/operador/dto/operador-operadores.dto.ts b/src/operador/dto/operador-operadores.dto.ts new file mode 100644 index 0000000..3d7e83d --- /dev/null +++ b/src/operador/dto/operador-operadores.dto.ts @@ -0,0 +1,14 @@ +import { IsNumberString, IsOptional, IsString } from 'class-validator'; + +export class OperadorOperadoresDto { + @IsNumberString() + pagina: string; + + @IsNumberString() + @IsOptional() + id_institucion?: string; + + @IsString() + @IsOptional() + operador?: string; +} diff --git a/src/operador/dto/operador-update.dto.ts b/src/operador/dto/operador-update.dto.ts new file mode 100644 index 0000000..a8fad9a --- /dev/null +++ b/src/operador/dto/operador-update.dto.ts @@ -0,0 +1,18 @@ +import { IsBoolean, IsInt, IsOptional, IsString } from 'class-validator'; + +export class OperadorUpdateDto { + @IsInt() + id_operador: number; + + @IsBoolean() + @IsOptional() + activo?: boolean; + + @IsString() + @IsOptional() + operador?: string; + + @IsString() + @IsOptional() + password?: string; +} diff --git a/src/operador/dto/operador.dto.ts b/src/operador/dto/operador.dto.ts new file mode 100644 index 0000000..eebc66e --- /dev/null +++ b/src/operador/dto/operador.dto.ts @@ -0,0 +1,6 @@ +import { IsString } from 'class-validator'; + +export class OperadorDto { + @IsString() + operador: string; +} diff --git a/src/operador/operador.controller.ts b/src/operador/operador.controller.ts index 225d2a4..faf7ac6 100644 --- a/src/operador/operador.controller.ts +++ b/src/operador/operador.controller.ts @@ -1,25 +1,43 @@ -import { Controller, Get, Post, Put } from '@nestjs/common'; +import { Body, Controller, Get, Post, Put, Query } from '@nestjs/common'; import { OperadorService } from './operador.service'; - +import { OperadorCreateDto } from './dto/operador-create.dto'; +import { OperadorLoginDto } from './dto/operador-login.dto'; +import { OperadorOperadoresDto } from './dto/operador-operadores.dto'; +import { OperadorUpdateDto } from './dto/operador-update.dto'; +import { OperadorDto } from './dto/operador.dto'; @Controller('operador') export class OperadorController { constructor(private operadorService: OperadorService) {} @Post() - create() {} + create(@Body() body: OperadorCreateDto) { + return this.operadorService.create( + body.id_institucion, + body.operador, + body.password, + ); + } @Post('login') - login() {} + login(@Body() body: OperadorLoginDto) { + return this.operadorService.login(body.operador, body.password); + } @Get('operador') - operador() {} + operador(@Query() query: OperadorDto) { + return this.operadorService.findByOperador(query.operador); + } @Get('operadores') - operadores() {} + operadores(@Query() query: OperadorOperadoresDto) { + return this.operadorService.findAll(query); + } @Get('reporte') reporte() {} @Put() - update() {} + update(@Body() body: OperadorUpdateDto) { + return this.operadorService.update(body); + } } diff --git a/src/operador/operador.module.ts b/src/operador/operador.module.ts index b54e8dd..ef5df32 100644 --- a/src/operador/operador.module.ts +++ b/src/operador/operador.module.ts @@ -3,9 +3,15 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { OperadorController } from './operador.controller'; import { OperadorService } from './operador.service'; import { Operador } from './entity/operador.entity'; +import { InstitucionModule } from 'src/institucion/institucion.module'; +import { TipoUsuarioModule } from 'src/tipo-usuario/tipo-usuario.module'; @Module({ - imports: [TypeOrmModule.forFeature([Operador])], + imports: [ + TypeOrmModule.forFeature([Operador]), + InstitucionModule, + TipoUsuarioModule, + ], controllers: [OperadorController], providers: [OperadorService], }) diff --git a/src/operador/operador.service.ts b/src/operador/operador.service.ts index 2a86d67..c39acfc 100644 --- a/src/operador/operador.service.ts +++ b/src/operador/operador.service.ts @@ -1,11 +1,94 @@ -import { Injectable } from '@nestjs/common'; +import { + ConflictException, + Injectable, + NotFoundException, + UnauthorizedException, +} from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { Operador } from './entity/operador.entity'; +import { InstitucionService } from 'src/institucion/institucion.service'; +import { TipoUsuarioService } from 'src/tipo-usuario/tipo-usuario.service'; @Injectable() export class OperadorService { constructor( @InjectRepository(Operador) private repository: Repository, + private isnstitucionService: InstitucionService, + private tipoUsuarioService: TipoUsuarioService, ) {} + + async create(id_institucion: number, operador: string, password: string) { + const institucion = await this.isnstitucionService.findById(id_institucion); + const tipoUsuario = await this.tipoUsuarioService.findById(4); + + return this.repository + .findOne({ operador, institucion }) + .then((existeOperador) => { + if (existeOperador) + throw new ConflictException( + 'Ya existe un operador con ese nombre, intente de nuevo.', + ); + /* Encriptar password */ + return this.repository.save( + this.repository.create({ + institucion, + operador, + password, + tipoUsuario, + }), + ); + }) + .then((_) => ({ message: 'Se creo correctamente al operador.' })); + } + + async findAll(filtros: { + pagina: string; + id_institucion?: string; + operador?: string; + }) { + const busqueda: any = {}; + const institucion = filtros.id_institucion + ? await this.isnstitucionService.findById(Number(filtros.id_institucion)) + : null; + + if (filtros.operador) busqueda.operador = filtros.operador; + if (filtros.id_institucion) busqueda.institucion = institucion; + return this.repository.find(busqueda); + } + + findById(id_operador: number) { + return this.repository.findOne({ id_operador }).then((operador) => { + if (!operador) throw new NotFoundException('No existe este operador'); + return operador; + }); + } + + findByOperador(operador: string) { + return this.repository.findOne({ operador }).then((operador) => { + if (!operador) throw new NotFoundException('No existe este operador'); + return operador; + }); + } + + login(operador: string, password: string) { + return this.findByOperador(operador).then((operador) => { + if (operador.password !== password) + throw new UnauthorizedException('Contraseña incorrecta.'); + /* Crear JWT y regresarlo */ + return operador; + }); + } + + async update(attrs: Partial) { + return this.findById(attrs.id_operador) + .then((operador) => { + /* encriptar password */ + Object.assign(operador, attrs); + return this.repository.save(operador); + }) + .then((_) => ({ + message: 'Se actualizo correctamente la información del operador.', + })); + } } diff --git a/src/usuario/usuario.service.ts b/src/usuario/usuario.service.ts index bb7604f..179f3f3 100644 --- a/src/usuario/usuario.service.ts +++ b/src/usuario/usuario.service.ts @@ -51,7 +51,7 @@ export class UsuarioService { login(usuario: string, password: string) { return this.findByUsuario(usuario).then((usuario) => { if (password !== usuario.password) - throw new UnauthorizedException('Contraseña incorrecta'); + throw new UnauthorizedException('Contraseña incorrecta.'); /* Crear JWT y regresarlo */ return usuario; }); @@ -71,6 +71,7 @@ export class UsuarioService { async update(attrs: Partial) { return this.findById(attrs.id_usuario) .then((usuario) => { + /* crear y encriptar password en caso de cambio de password */ Object.assign(usuario, attrs); return this.repository.save(usuario); })