prestamo mejorado
This commit is contained in:
parent
1fa6ad02b6
commit
b34eb420e3
@ -57,10 +57,8 @@ export class InstitucionDiaService {
|
||||
async hoy(id_institucion: number) {
|
||||
const ahora = moment();
|
||||
|
||||
if (ahora.weekday() === 0 || ahora.weekday() === 6)
|
||||
throw new ConflictException(
|
||||
'No se puede pedir equipo de cómputo los sabados y domingos.',
|
||||
);
|
||||
if (ahora.weekday() === 0)
|
||||
throw new ConflictException('No hay servicio los días domingo.');
|
||||
return this.institucionService
|
||||
.findInfoInstitucionById(id_institucion)
|
||||
.then((institucion) =>
|
||||
|
@ -155,7 +155,7 @@ export class PrestamoController {
|
||||
const usuario: Usuario = req.user.usuario;
|
||||
|
||||
this.validarUsuarioService.validarUsuario(usuario);
|
||||
return this.prestamoService.cancelarUsuario(body.id_prestamo);
|
||||
return this.prestamoService.cancelarUsuario(usuario, body.id_prestamo);
|
||||
}
|
||||
|
||||
@Serealize(EquipoMinOutputDto)
|
||||
|
@ -37,6 +37,7 @@ import { ModuloService } from '../modulo/modulo.service';
|
||||
import { MultaService } from '../multa/multa.service';
|
||||
import { OperadorService } from '../operador/operador.service';
|
||||
import { TipoUsuarioService } from '../tipo-usuario/tipo-usuario.service';
|
||||
import { UsuarioService } from '../usuario/usuario.service';
|
||||
|
||||
@Injectable()
|
||||
export class PrestamoService {
|
||||
@ -60,6 +61,7 @@ export class PrestamoService {
|
||||
private multaService: MultaService,
|
||||
private operadorService: OperadorService,
|
||||
private tipoUsuarioService: TipoUsuarioService,
|
||||
private usuarioService: UsuarioService,
|
||||
) {}
|
||||
|
||||
async cancelarOperador(
|
||||
@ -70,7 +72,8 @@ export class PrestamoService {
|
||||
const ahora = moment();
|
||||
const prestamo = await this.findInfoPrestamoById(id_prestamo);
|
||||
|
||||
this.validacionBasicaPrestamo(prestamo, operadorRegreso);
|
||||
this.validacionBasicaPrestamo(prestamo);
|
||||
this.validacionOperadorPrestamo(prestamo, operadorRegreso);
|
||||
prestamo.activo = false;
|
||||
prestamo.fecha_entrega = ahora.toDate();
|
||||
prestamo.cancelado_operador = true;
|
||||
@ -89,7 +92,7 @@ export class PrestamoService {
|
||||
});
|
||||
}
|
||||
|
||||
async cancelarUsuario(id_prestamo: number) {
|
||||
async cancelarUsuario(usuario: Usuario, id_prestamo: number) {
|
||||
const ahora = moment();
|
||||
const prestamo = await this.findInfoPrestamoById(id_prestamo);
|
||||
|
||||
@ -98,6 +101,10 @@ export class PrestamoService {
|
||||
throw new ConflictException(
|
||||
'No puedes cancelar este préstamo una vez se te entregó el equipo.',
|
||||
);
|
||||
if (usuario.id_usuario != prestamo.usuario.id_usuario)
|
||||
throw new ConflictException(
|
||||
'No puedes cancelar este préstamo porque no te pretenece.',
|
||||
);
|
||||
prestamo.activo = false;
|
||||
prestamo.fecha_entrega = ahora.toDate();
|
||||
prestamo.cancelado_usuario = true;
|
||||
@ -213,64 +220,32 @@ export class PrestamoService {
|
||||
});
|
||||
}
|
||||
|
||||
viewToPrestamo(infoPrestamo: InformacionPrestamoView) {
|
||||
return this.repository.create({
|
||||
id_prestamo: infoPrestamo.id_prestamo,
|
||||
activo: infoPrestamo.activo === 1,
|
||||
cancelado_operador: infoPrestamo.cancelado_operador === 1,
|
||||
cancelado_usuario: infoPrestamo.cancelado_usuario === 1,
|
||||
fecha_entrega: infoPrestamo.fecha_entrega,
|
||||
fecha_inicio: infoPrestamo.fecha_inicio,
|
||||
hora_fin: infoPrestamo.hora_fin,
|
||||
hora_inicio: infoPrestamo.hora_inicio,
|
||||
hora_max_recoger: infoPrestamo.hora_max_recoger,
|
||||
equipo: {
|
||||
id_equipo: infoPrestamo.id_equipo,
|
||||
equipo: infoPrestamo.equipo,
|
||||
numero_inventario: infoPrestamo.numero_inventario,
|
||||
carrito: {
|
||||
id_carrito: infoPrestamo.id_carrito,
|
||||
carrito: infoPrestamo.carrito,
|
||||
modulo: {
|
||||
id_modulo: infoPrestamo.id_modulo,
|
||||
institucion: { id_institucion: infoPrestamo.id_institucion },
|
||||
},
|
||||
tipoCarrito: {
|
||||
id_tipo_carrito: infoPrestamo.id_tipo_carrito,
|
||||
tipo_carrito: infoPrestamo.tipo_carrito,
|
||||
},
|
||||
},
|
||||
status: { id_status: infoPrestamo.id_status },
|
||||
},
|
||||
operadorEntrega: { id_operador: infoPrestamo.id_operador_entrega },
|
||||
operadorRegreso: { id_operador: infoPrestamo.id_operador_regreso },
|
||||
usuario: { id_usuario: infoPrestamo.id_usuario },
|
||||
});
|
||||
}
|
||||
|
||||
async desactivarPrestamos() {
|
||||
const ahora = moment();
|
||||
|
||||
return this.findInfoPrestamoDesactivables().then(async (prestamos) => {
|
||||
for (let i = 0; i < prestamos.length; i++)
|
||||
if (ahora.diff(moment(prestamos[i].hora_max_recoger)) > 0) {
|
||||
prestamos[i].activo = false;
|
||||
prestamos[i].cancelado_operador = true;
|
||||
prestamos[i].equipo.u = null;
|
||||
prestamos[i].equipo.status.id_status = 1;
|
||||
await this.repository
|
||||
.save(prestamos[i])
|
||||
.then((_) => this.equipoService.updateStatus(prestamos[i].equipo))
|
||||
.then((_) => {
|
||||
this.appGateway.actualizarOperador(
|
||||
prestamos[i].equipo.carrito.modulo.institucion.id_institucion,
|
||||
);
|
||||
this.appGateway.actualizarUsuario(
|
||||
prestamos[i].usuario.id_usuario,
|
||||
);
|
||||
});
|
||||
return this.informacionPrestamoView
|
||||
.find({ where: { activo: 1, id_status: 2 } })
|
||||
.then(async (infoPrestamo) => {
|
||||
for (let i = 0; i < infoPrestamo.length; i++) {
|
||||
const prestamo = this.viewToPrestamo(infoPrestamo[i]);
|
||||
|
||||
if (ahora.diff(moment(prestamo.hora_max_recoger)) > 0) {
|
||||
prestamo.activo = false;
|
||||
prestamo.cancelado_operador = true;
|
||||
prestamo.equipo.u = null;
|
||||
prestamo.equipo.status.id_status = 1;
|
||||
await this.repository
|
||||
.save(prestamo)
|
||||
.then((_) => this.equipoService.updateStatus(prestamo.equipo))
|
||||
.then((_) => {
|
||||
this.appGateway.actualizarOperador(
|
||||
prestamo.equipo.carrito.modulo.institucion.id_institucion,
|
||||
);
|
||||
this.appGateway.actualizarUsuario(prestamo.usuario.id_usuario);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async entregar(operadorEntrega: Operador, id_prestamo: number) {
|
||||
@ -289,7 +264,8 @@ export class PrestamoService {
|
||||
? moment(`${ahoraStr} ${institucionDia.hora_fin}`)
|
||||
: null;
|
||||
|
||||
this.validacionBasicaPrestamo(prestamo, operadorEntrega);
|
||||
this.validacionBasicaPrestamo(prestamo);
|
||||
this.validacionOperadorPrestamo(prestamo, operadorEntrega);
|
||||
if (prestamo.equipo.status.id_status === 3)
|
||||
throw new ConflictException(
|
||||
'Ya se entregó el equipo de cómputo al usuario.',
|
||||
@ -314,14 +290,61 @@ export class PrestamoService {
|
||||
});
|
||||
}
|
||||
|
||||
findAllByIdUsuario(id_usuario: number, pagina: number) {
|
||||
async findAllByIdEquipo(id_equipo: number, pagina: number) {
|
||||
return this.fullInformacionPrestamoView
|
||||
.findAndCount({
|
||||
where: { id_usuario },
|
||||
where: { id_equipo },
|
||||
order: { id_prestamo: 'DESC' },
|
||||
skip: (pagina - 1) * 25,
|
||||
take: 25,
|
||||
})
|
||||
.then((infoPrestamos) => {
|
||||
const prestamos: Prestamo[] = [];
|
||||
for (let i = 0; i < infoPrestamos[0].length; i++)
|
||||
prestamos.push(
|
||||
this.repository.create({
|
||||
id_prestamo: infoPrestamos[0][i].id_prestamo,
|
||||
activo: infoPrestamos[0][i].activo === 1,
|
||||
cancelado_operador: infoPrestamos[0][i].cancelado_operador === 1,
|
||||
cancelado_usuario: infoPrestamos[0][i].cancelado_usuario === 1,
|
||||
fecha_entrega: infoPrestamos[0][i].fecha_entrega,
|
||||
fecha_inicio: infoPrestamos[0][i].fecha_inicio,
|
||||
hora_fin: infoPrestamos[0][i].hora_fin,
|
||||
hora_inicio: infoPrestamos[0][i].hora_inicio,
|
||||
hora_max_recoger: infoPrestamos[0][i].hora_max_recoger,
|
||||
usuario: {
|
||||
id_usuario: infoPrestamos[0][i].id_usuario,
|
||||
nombre: infoPrestamos[0][i].nombre,
|
||||
usuario: infoPrestamos[0][i].usuario,
|
||||
tipoUsuario: {
|
||||
id_tipo_usuario: infoPrestamos[0][i].id_tipo_usuario,
|
||||
},
|
||||
},
|
||||
operadorEntrega: {
|
||||
id_operador: infoPrestamos[0][i].id_operador_entrega,
|
||||
operador: infoPrestamos[0][i].operador_entrega,
|
||||
},
|
||||
operadorRegreso: {
|
||||
id_operador: infoPrestamos[0][i].id_operador_regreso,
|
||||
operador: infoPrestamos[0][i].operador_regreso,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return [prestamos, infoPrestamos[1]];
|
||||
});
|
||||
}
|
||||
|
||||
findAllByIdUsuario(id_usuario: number, pagina: number) {
|
||||
return this.usuarioService
|
||||
.findById(id_usuario)
|
||||
.then((usuario) =>
|
||||
this.fullInformacionPrestamoView.findAndCount({
|
||||
where: { id_usuario: usuario.id_usuario },
|
||||
order: { id_prestamo: 'DESC' },
|
||||
skip: (pagina - 1) * 25,
|
||||
take: 25,
|
||||
}),
|
||||
)
|
||||
.then((infoPrestamos) => {
|
||||
const prestamos: Prestamo[] = [];
|
||||
|
||||
@ -372,50 +395,6 @@ export class PrestamoService {
|
||||
});
|
||||
}
|
||||
|
||||
async findAllByIdEquipo(id_equipo: number, pagina: number) {
|
||||
return this.fullInformacionPrestamoView
|
||||
.findAndCount({
|
||||
where: { id_equipo },
|
||||
order: { id_prestamo: 'DESC' },
|
||||
skip: (pagina - 1) * 25,
|
||||
take: 25,
|
||||
})
|
||||
.then((infoPrestamos) => {
|
||||
const prestamos: Prestamo[] = [];
|
||||
for (let i = 0; i < infoPrestamos[0].length; i++)
|
||||
prestamos.push(
|
||||
this.repository.create({
|
||||
id_prestamo: infoPrestamos[0][i].id_prestamo,
|
||||
activo: infoPrestamos[0][i].activo === 1,
|
||||
cancelado_operador: infoPrestamos[0][i].cancelado_operador === 1,
|
||||
cancelado_usuario: infoPrestamos[0][i].cancelado_usuario === 1,
|
||||
fecha_entrega: infoPrestamos[0][i].fecha_entrega,
|
||||
fecha_inicio: infoPrestamos[0][i].fecha_inicio,
|
||||
hora_fin: infoPrestamos[0][i].hora_fin,
|
||||
hora_inicio: infoPrestamos[0][i].hora_inicio,
|
||||
hora_max_recoger: infoPrestamos[0][i].hora_max_recoger,
|
||||
usuario: {
|
||||
id_usuario: infoPrestamos[0][i].id_usuario,
|
||||
nombre: infoPrestamos[0][i].nombre,
|
||||
usuario: infoPrestamos[0][i].usuario,
|
||||
tipoUsuario: {
|
||||
id_tipo_usuario: infoPrestamos[0][i].id_tipo_usuario,
|
||||
},
|
||||
},
|
||||
operadorEntrega: {
|
||||
id_operador: infoPrestamos[0][i].id_operador_entrega,
|
||||
operador: infoPrestamos[0][i].operador_entrega,
|
||||
},
|
||||
operadorRegreso: {
|
||||
id_operador: infoPrestamos[0][i].id_operador_regreso,
|
||||
operador: infoPrestamos[0][i].operador_regreso,
|
||||
},
|
||||
}),
|
||||
);
|
||||
return [prestamos, infoPrestamos[1]];
|
||||
});
|
||||
}
|
||||
|
||||
findById(id_prestamo: number) {
|
||||
return this.repository
|
||||
.createQueryBuilder('p')
|
||||
@ -500,9 +479,10 @@ export class PrestamoService {
|
||||
options.skip = (parseInt(filtros.pagina) - 1) * 25;
|
||||
}
|
||||
if (filtros.activo) {
|
||||
if (typeof filtros.activo === 'boolean')
|
||||
if (typeof filtros.activo === 'boolean') {
|
||||
busqueda.activo = filtros.activo ? 1 : 0;
|
||||
else busqueda.activo = filtros.activo === 'true' ? 1 : 0;
|
||||
options.order.id_prestamo = 'ASC';
|
||||
} else busqueda.activo = filtros.activo === 'true' ? 1 : 0;
|
||||
}
|
||||
if (filtros.cancelado_operador)
|
||||
busqueda.cancelado_operador =
|
||||
@ -598,19 +578,7 @@ export class PrestamoService {
|
||||
});
|
||||
}
|
||||
|
||||
findInfoPrestamoDesactivables() {
|
||||
return this.informacionPrestamoView
|
||||
.find({ where: { activo: 1, id_status: 2 } })
|
||||
.then((infoPrestamo) => {
|
||||
const prstamos: Prestamo[] = [];
|
||||
|
||||
for (let i = 0; i < infoPrestamo.length; i++)
|
||||
this.viewToPrestamo(infoPrestamo[i]);
|
||||
return prstamos;
|
||||
});
|
||||
}
|
||||
|
||||
async findInfoPrestamoByNumeroInventario(
|
||||
findInfoPrestamoByNumeroInventario(
|
||||
institucion: Institucion,
|
||||
numero_inventario: string,
|
||||
) {
|
||||
@ -630,6 +598,10 @@ export class PrestamoService {
|
||||
});
|
||||
}
|
||||
|
||||
private mensajeNoPoderPedir() {
|
||||
return 'No puedes pedir equipos de cómputo porque:';
|
||||
}
|
||||
|
||||
prestamoInfoById(id_prestamo: number) {
|
||||
return this.prestamoInformacionView
|
||||
.findOne({ where: { id_prestamo } })
|
||||
@ -766,10 +738,6 @@ export class PrestamoService {
|
||||
});
|
||||
}
|
||||
|
||||
private mensajeNoPoderPedir() {
|
||||
return 'No puedes pedir equipos de cómputo porque:';
|
||||
}
|
||||
|
||||
async regresar(
|
||||
prestamo: Prestamo,
|
||||
operadorRegreso: Operador,
|
||||
@ -784,7 +752,8 @@ export class PrestamoService {
|
||||
tardanza / operadorRegreso.institucion.tiempo_entrega,
|
||||
);
|
||||
|
||||
this.validacionBasicaPrestamo(prestamo, operadorRegreso);
|
||||
this.validacionBasicaPrestamo(prestamo);
|
||||
this.validacionOperadorPrestamo(prestamo, operadorRegreso);
|
||||
if (prestamo.equipo.status.id_status === 2)
|
||||
throw new ConflictException(
|
||||
'Aún no se ha entregado el equipo de cómputo al usuario.',
|
||||
@ -885,7 +854,7 @@ export class PrestamoService {
|
||||
);
|
||||
}
|
||||
|
||||
validacionBasicaPrestamo(prestamo: Prestamo, operador?: Operador) {
|
||||
validacionBasicaPrestamo(prestamo: Prestamo) {
|
||||
if (prestamo.cancelado_usuario)
|
||||
throw new ConflictException(
|
||||
'Este préstamo fue cancelado por el usuario.',
|
||||
@ -896,14 +865,6 @@ export class PrestamoService {
|
||||
);
|
||||
if (!prestamo.activo)
|
||||
throw new ConflictException('Este préstamo ya no se encuentra activo.');
|
||||
if (
|
||||
operador &&
|
||||
operador.institucion.id_institucion !=
|
||||
prestamo.equipo.carrito.modulo.institucion.id_institucion
|
||||
)
|
||||
throw new ConflictException(
|
||||
'Este préstamo no pertenece a esta institución.',
|
||||
);
|
||||
}
|
||||
|
||||
validarInstitucionUsuario(
|
||||
@ -926,7 +887,52 @@ export class PrestamoService {
|
||||
}
|
||||
}
|
||||
throw new BadRequestException(
|
||||
`${this.mensajeNoPoderPedir()} no perteneces a esta institución.`,
|
||||
`${this.mensajeNoPoderPedir()} no perteneces a la institución del módulo seleccionado.`,
|
||||
);
|
||||
}
|
||||
|
||||
validacionOperadorPrestamo(prestamo: Prestamo, operador: Operador) {
|
||||
if (
|
||||
operador.institucion.id_institucion !=
|
||||
prestamo.equipo.carrito.modulo.institucion.id_institucion
|
||||
)
|
||||
throw new ConflictException(
|
||||
'Este préstamo no pertenece a esta institución.',
|
||||
);
|
||||
}
|
||||
|
||||
private viewToPrestamo(infoPrestamo: InformacionPrestamoView) {
|
||||
return this.repository.create({
|
||||
id_prestamo: infoPrestamo.id_prestamo,
|
||||
activo: infoPrestamo.activo === 1,
|
||||
cancelado_operador: infoPrestamo.cancelado_operador === 1,
|
||||
cancelado_usuario: infoPrestamo.cancelado_usuario === 1,
|
||||
fecha_entrega: infoPrestamo.fecha_entrega,
|
||||
fecha_inicio: infoPrestamo.fecha_inicio,
|
||||
hora_fin: infoPrestamo.hora_fin,
|
||||
hora_inicio: infoPrestamo.hora_inicio,
|
||||
hora_max_recoger: infoPrestamo.hora_max_recoger,
|
||||
equipo: {
|
||||
id_equipo: infoPrestamo.id_equipo,
|
||||
equipo: infoPrestamo.equipo,
|
||||
numero_inventario: infoPrestamo.numero_inventario,
|
||||
carrito: {
|
||||
id_carrito: infoPrestamo.id_carrito,
|
||||
carrito: infoPrestamo.carrito,
|
||||
modulo: {
|
||||
id_modulo: infoPrestamo.id_modulo,
|
||||
institucion: { id_institucion: infoPrestamo.id_institucion },
|
||||
},
|
||||
tipoCarrito: {
|
||||
id_tipo_carrito: infoPrestamo.id_tipo_carrito,
|
||||
tipo_carrito: infoPrestamo.tipo_carrito,
|
||||
},
|
||||
},
|
||||
status: { id_status: infoPrestamo.id_status },
|
||||
},
|
||||
operadorEntrega: { id_operador: infoPrestamo.id_operador_entrega },
|
||||
operadorRegreso: { id_operador: infoPrestamo.id_operador_regreso },
|
||||
usuario: { id_usuario: infoPrestamo.id_usuario },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user