operador y usuario a medias
This commit is contained in:
parent
73129b313f
commit
c52e5b7850
12
src/operador/dto/operador-create.dto.ts
Normal file
12
src/operador/dto/operador-create.dto.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import { IsInt, IsString } from 'class-validator';
|
||||
|
||||
export class OperadorCreateDto {
|
||||
@IsInt()
|
||||
id_institucion: number;
|
||||
|
||||
@IsString()
|
||||
operador: string;
|
||||
|
||||
@IsString()
|
||||
password: string;
|
||||
}
|
9
src/operador/dto/operador-login.dto.ts
Normal file
9
src/operador/dto/operador-login.dto.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { IsNumberString, IsString } from 'class-validator';
|
||||
|
||||
export class OperadorLoginDto {
|
||||
@IsNumberString()
|
||||
operador: string;
|
||||
|
||||
@IsString()
|
||||
password: string;
|
||||
}
|
14
src/operador/dto/operador-operadores.dto.ts
Normal file
14
src/operador/dto/operador-operadores.dto.ts
Normal file
@ -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;
|
||||
}
|
18
src/operador/dto/operador-update.dto.ts
Normal file
18
src/operador/dto/operador-update.dto.ts
Normal file
@ -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;
|
||||
}
|
6
src/operador/dto/operador.dto.ts
Normal file
6
src/operador/dto/operador.dto.ts
Normal file
@ -0,0 +1,6 @@
|
||||
import { IsString } from 'class-validator';
|
||||
|
||||
export class OperadorDto {
|
||||
@IsString()
|
||||
operador: string;
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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],
|
||||
})
|
||||
|
@ -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<Operador>,
|
||||
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<Operador>) {
|
||||
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.',
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
@ -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<Usuario>) {
|
||||
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);
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user