usuario service final
This commit is contained in:
@@ -110,7 +110,7 @@ export class CarritoController {
|
||||
const operador: Operador = req.user.operador;
|
||||
|
||||
this.validarUsuarioService.validarAdminOperador(operador);
|
||||
return this.carritoService.findFullInfoAll(operador, query);
|
||||
return this.carritoService.findAll(operador, query);
|
||||
}
|
||||
|
||||
@Serealize(MessageOutputDto)
|
||||
|
||||
@@ -92,7 +92,7 @@ export class CarritoService {
|
||||
.then((infoCarrito) => this.viewToCarrito(infoCarrito));
|
||||
}
|
||||
|
||||
async findFullInfoAll(
|
||||
async findAll(
|
||||
operador: Operador,
|
||||
filtros: {
|
||||
pagina?: string;
|
||||
|
||||
@@ -32,51 +32,46 @@ export class OperadorService {
|
||||
nombre: string,
|
||||
correo: string,
|
||||
password?: string,
|
||||
) {
|
||||
): Promise<{ message: string }> {
|
||||
const institucion =
|
||||
typeof id_institucion === 'number'
|
||||
? await this.institucionService.findById(id_institucion)
|
||||
: id_institucion;
|
||||
const tipoUsuario = await this.tipoUsuarioService.findById(id_tipo_usuario);
|
||||
|
||||
// Solo se puede crear admins y operadores
|
||||
if (tipoUsuario.id_tipo_usuario < 3 || tipoUsuario.id_tipo_usuario > 4)
|
||||
throw new ConflictException(
|
||||
'No se puede asignar un tipo de usuario distinto a admin y operador',
|
||||
);
|
||||
// Ver si existe un admin con ese usaurio
|
||||
// Buscamos un registro de admin con ese nombre
|
||||
return this.findAdmin(operador)
|
||||
.then((existeAdmin) => {
|
||||
// Error si lo hay
|
||||
// Sacamos error si existe
|
||||
if (existeAdmin)
|
||||
throw new ConflictException(
|
||||
'Ya existe un admin con ese nombre, intenta de nuevo con otro.',
|
||||
);
|
||||
// Ver que haya un operador en esta institución con este usuario
|
||||
// Buscamos un registro de operador con ese nombre
|
||||
return this.findOperador(institucion, operador);
|
||||
})
|
||||
.then(async (existeOperador) => {
|
||||
// Error si lo hay
|
||||
// Sacamos error si existe
|
||||
if (existeOperador)
|
||||
throw new ConflictException(
|
||||
'Ya existe un operador en esta institución con ese nombre, intenta de nuevo con otro.',
|
||||
);
|
||||
// Crear password si no se mandó
|
||||
// Generamos un password por si no se mando uno
|
||||
if (!password) password = this.bcryptService.generarPassword();
|
||||
// Crear registro
|
||||
// Creamos y guardamos un registro
|
||||
return this.repository.save(
|
||||
this.repository.create({
|
||||
correo,
|
||||
institucion,
|
||||
nombre,
|
||||
operador,
|
||||
password: this.bcryptService.encriptar(password),
|
||||
institucion,
|
||||
tipoUsuario,
|
||||
}),
|
||||
);
|
||||
})
|
||||
.then((operador) =>
|
||||
// Correo con credenciales
|
||||
// Enviamos un correo con credenciales
|
||||
this.nodemailerService.sendEmail({
|
||||
email: operador.correo,
|
||||
subject: 'Credenciales Pc Puma',
|
||||
@@ -97,18 +92,9 @@ export class OperadorService {
|
||||
}));
|
||||
}
|
||||
|
||||
findAdmin(admin: string) {
|
||||
findAdmin(admin: string): Promise<Operador> {
|
||||
return this.repository
|
||||
.findOne({
|
||||
select: [
|
||||
'id_operador',
|
||||
'activo',
|
||||
'nombre',
|
||||
'operador',
|
||||
'password',
|
||||
'id_institucion',
|
||||
'id_tipo_usuario',
|
||||
],
|
||||
where: {
|
||||
operador: admin,
|
||||
tipoUsuario: { id_tipo_usuario: Between(2, 3) },
|
||||
@@ -122,7 +108,7 @@ export class OperadorService {
|
||||
id_institucion?: string;
|
||||
id_tipo_usuario?: string;
|
||||
operador?: string;
|
||||
}) {
|
||||
}): Promise<[Operador[], number]> {
|
||||
const institucion = filtros.id_institucion
|
||||
? await this.institucionService.findById(parseInt(filtros.id_institucion))
|
||||
: null;
|
||||
@@ -169,31 +155,22 @@ export class OperadorService {
|
||||
});
|
||||
}
|
||||
|
||||
findAllByInstitucion(institucion: Institucion) {
|
||||
findAllByInstitucion(institucion: Institucion): Promise<Operador[]> {
|
||||
return this.repository.find({
|
||||
select: ['id_operador', 'operador'],
|
||||
where: { institucion },
|
||||
});
|
||||
}
|
||||
|
||||
findOperador(institucion: Institucion, operador: string) {
|
||||
findOperador(institucion: Institucion, operador: string): Promise<Operador> {
|
||||
return this.repository
|
||||
.findOne({
|
||||
select: [
|
||||
'id_operador',
|
||||
'activo',
|
||||
'nombre',
|
||||
'operador',
|
||||
'password',
|
||||
'id_institucion',
|
||||
'id_tipo_usuario',
|
||||
],
|
||||
where: { operador, institucion, tipoUsuario: { id_tipo_usuario: 4 } },
|
||||
})
|
||||
.then((operador) => this.llenarIds(operador));
|
||||
}
|
||||
|
||||
findById(id_operador: number) {
|
||||
findById(id_operador: number): Promise<Operador> {
|
||||
return this.repository
|
||||
.findOne({
|
||||
select: [
|
||||
@@ -212,7 +189,7 @@ export class OperadorService {
|
||||
});
|
||||
}
|
||||
|
||||
llenarIds(operador: Operador) {
|
||||
llenarIds(operador: Operador): Operador {
|
||||
if (operador) {
|
||||
operador.tipoUsuario = this.tipoUsuarioService.crearTipoUsuario(
|
||||
operador.id_tipo_usuario,
|
||||
@@ -225,7 +202,11 @@ export class OperadorService {
|
||||
return operador;
|
||||
}
|
||||
|
||||
passwordReset(admin: Operador, id_operador: number, password?: string) {
|
||||
passwordReset(
|
||||
admin: Operador,
|
||||
id_operador: number,
|
||||
password?: string,
|
||||
): Promise<{ message: string }> {
|
||||
return this.findById(id_operador)
|
||||
.then((operador) => {
|
||||
this.validarUpdate(admin, operador);
|
||||
@@ -256,11 +237,14 @@ export class OperadorService {
|
||||
.then((_) => ({ message: 'Se cambió correctamente la constraseña.' }));
|
||||
}
|
||||
|
||||
sistema() {
|
||||
sistema(): Operador {
|
||||
return this.repository.create({ id_operador: 1 });
|
||||
}
|
||||
|
||||
update(admin: Operador, attrs: Partial<Operador>) {
|
||||
update(
|
||||
admin: Operador,
|
||||
attrs: Partial<Operador>,
|
||||
): Promise<{ message: string }> {
|
||||
return this.findById(attrs.id_operador)
|
||||
.then((operador) => {
|
||||
this.validarUpdate(admin, operador);
|
||||
@@ -272,7 +256,7 @@ export class OperadorService {
|
||||
.then((_) => ({ message: 'Se guardaron los cambios correctamente.' }));
|
||||
}
|
||||
|
||||
validarUpdate(admin: Operador, operador: Operador) {
|
||||
validarUpdate(admin: Operador, operador: Operador): void {
|
||||
// Validamos que el super admin solo puedar modificar admins
|
||||
if (
|
||||
admin.tipoUsuario.id_tipo_usuario === 2 &&
|
||||
|
||||
@@ -180,6 +180,6 @@ export class UsuarioController {
|
||||
const operador: Operador = req.user.operador;
|
||||
|
||||
this.validarUsuarioService.validarOperador(operador);
|
||||
return this.usuarioService.findAll(query);
|
||||
return this.usuarioService.findAll(operador, query);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,16 +57,19 @@ export class UsuarioService {
|
||||
);
|
||||
}
|
||||
|
||||
async findAll(filtros: {
|
||||
pagina: string;
|
||||
activo?: string;
|
||||
id_carrera?: string;
|
||||
id_institucion?: string;
|
||||
id_tipo_usuario?: string;
|
||||
nombre?: string;
|
||||
password?: string;
|
||||
usuario?: string;
|
||||
}): Promise<[Usuario[], number]> {
|
||||
async findAll(
|
||||
operador: Operador,
|
||||
filtros: {
|
||||
pagina: string;
|
||||
activo?: string;
|
||||
id_carrera?: string;
|
||||
id_institucion?: string;
|
||||
id_tipo_usuario?: string;
|
||||
nombre?: string;
|
||||
password?: string;
|
||||
usuario?: string;
|
||||
},
|
||||
): Promise<[Usuario[], number]> {
|
||||
const carrera = filtros.id_carrera
|
||||
? await this.institucionCarreraService.findCarreraByIdCarrera(
|
||||
parseInt(filtros.id_carrera),
|
||||
@@ -91,6 +94,14 @@ export class UsuarioService {
|
||||
.take(25)
|
||||
.skip((parseInt(filtros.pagina) - 1) * 25);
|
||||
|
||||
if (operador.tipoUsuario.id_tipo_usuario > 2)
|
||||
query.andWhere('i.id_institucion = :id_institucion', {
|
||||
id_institucion: operador.institucion.id_institucion,
|
||||
});
|
||||
else if (institucion)
|
||||
query.andWhere('i.id_institucion = :id_institucion', {
|
||||
id_institucion: institucion.id_institucion,
|
||||
});
|
||||
if (
|
||||
filtros.password &&
|
||||
(filtros.password === 'true' || filtros.password === 'false')
|
||||
@@ -114,10 +125,6 @@ export class UsuarioService {
|
||||
query.andWhere('c.id_carrera = :id_carrera', {
|
||||
id_carrera: carrera.id_carrera,
|
||||
});
|
||||
if (institucion)
|
||||
query.andWhere('i.id_institucion = :id_institucion', {
|
||||
id_institucion: institucion.id_institucion,
|
||||
});
|
||||
if (tipoUsuario)
|
||||
query.andWhere('tu.id_tipo_usuario = :id_tipo_usuario', {
|
||||
id_tipo_usuario: tipoUsuario.id_tipo_usuario,
|
||||
|
||||
Reference in New Issue
Block a user