validación de días y horas listo para pedir equpo
This commit is contained in:
@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { InstitucionDia } from './entity/institucion-dia.entity';
|
||||
import { Institucion } from '../institucion/entity/institucion.entity';
|
||||
import { InstitucionService } from '../institucion/institucion.service';
|
||||
@Injectable()
|
||||
export class InstitucionDiaService {
|
||||
@@ -26,6 +27,10 @@ export class InstitucionDiaService {
|
||||
});
|
||||
}
|
||||
|
||||
findDia(institucion: Institucion, id_dia: number) {
|
||||
return this.repository.findOne({ institucion, dia: { id_dia } });
|
||||
}
|
||||
|
||||
update(attrs: Partial<InstitucionDia>) {
|
||||
return this.findById(attrs.id_institucion_dia)
|
||||
.then((institucionDia) => {
|
||||
|
||||
@@ -3,10 +3,9 @@ import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { PrestamoController } from './prestamo.controller';
|
||||
import { PrestamoService } from './prestamo.service';
|
||||
import { Prestamo } from './entity/prestamo.entity';
|
||||
import { CarritoModule } from '../carrito/carrito.module';
|
||||
import { EquipoModule } from '../equipo/equipo.module';
|
||||
import { InstitucionModule } from '../institucion/institucion.module';
|
||||
import { InstitucionCarreraModule } from '../institucion-carrera/institucion-carrera.module';
|
||||
import { InstitucionDiaModule } from '../institucion-dia/institucion-dia.module';
|
||||
import { InstitucionProgramaModule } from '../institucion-programa/institucion-programa.module';
|
||||
import { InstitucionTipoCarritoModule } from '../institucion-tipo-carrito/institucion-tipo-carrito.module';
|
||||
import { InstitucionTipoEntradaModule } from '../institucion-tipo-entrada/institucion-tipo-entrada.module';
|
||||
@@ -17,10 +16,9 @@ import { TipoUsuarioModule } from '../tipo-usuario/tipo-usuario.module';
|
||||
import { UsuarioModule } from '../usuario/usuario.module';
|
||||
@Module({
|
||||
imports: [
|
||||
CarritoModule,
|
||||
EquipoModule,
|
||||
InstitucionModule,
|
||||
InstitucionCarreraModule,
|
||||
InstitucionDiaModule,
|
||||
InstitucionProgramaModule,
|
||||
InstitucionTipoCarritoModule,
|
||||
InstitucionTipoEntradaModule,
|
||||
|
||||
@@ -6,33 +6,31 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { FindOperator, Like, Repository } from 'typeorm';
|
||||
import { Institucion } from '../institucion/entity/institucion.entity';
|
||||
import { Modulo } from '../modulo/entity/modulo.entity';
|
||||
import { Operador } from '../operador/entity/operador.entity';
|
||||
import { Prestamo } from './entity/prestamo.entity';
|
||||
import { TipoCarrito } from '../institucion-tipo-carrito/entity/tipo-carrito.entity';
|
||||
import { TipoUsuario } from '../tipo-usuario/entity/tipo-usuario.entity';
|
||||
import { CarritoService } from '../carrito/carrito.service';
|
||||
import { EquipoService } from '../equipo/equipo.service';
|
||||
import { InstitucionService } from '../institucion/institucion.service';
|
||||
import { ModuloService } from '../modulo/modulo.service';
|
||||
import { InstitucionCarreraService } from '../institucion-carrera/institucion-carrera.service';
|
||||
import { InstitucionDiaService } from '../institucion-dia/institucion-dia.service';
|
||||
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
||||
import { InstitucionTipoCarritoService } from '../institucion-tipo-carrito/institucion-tipo-carrito.service';
|
||||
import { InstitucionTipoEntradaService } from '../institucion-tipo-entrada/institucion-tipo-entrada.service';
|
||||
import { ModuloService } from '../modulo/modulo.service';
|
||||
import { OperadorService } from '../operador/operador.service';
|
||||
import { StatusService } from '../status/status.service';
|
||||
import { TipoUsuarioService } from '../tipo-usuario/tipo-usuario.service';
|
||||
import { UsuarioService } from '../usuario/usuario.service';
|
||||
import { Institucion } from 'src/institucion/entity/institucion.entity';
|
||||
|
||||
@Injectable()
|
||||
export class PrestamoService {
|
||||
constructor(
|
||||
@InjectRepository(Prestamo) private repository: Repository<Prestamo>,
|
||||
private carritoService: CarritoService,
|
||||
private equipoService: EquipoService,
|
||||
private institucionService: InstitucionService,
|
||||
private institucionCarreraService: InstitucionCarreraService,
|
||||
private institucionDiaService: InstitucionDiaService,
|
||||
private institucionProgramaService: InstitucionProgramaService,
|
||||
private institucionTipoCarritoService: InstitucionTipoCarritoService,
|
||||
private institucionTipoEntradaService: InstitucionTipoEntradaService,
|
||||
@@ -104,7 +102,7 @@ export class PrestamoService {
|
||||
id_programa?: number,
|
||||
id_tipo_entrada?: number,
|
||||
) {
|
||||
const ahora = moment();
|
||||
const ahora = moment().add(1, 'd');
|
||||
const usuario = await this.usuarioService.findById(id_usuario, true, true);
|
||||
const modulo = await this.moduloService.findById(id_modulo);
|
||||
const tipoCarrito =
|
||||
@@ -120,6 +118,31 @@ export class PrestamoService {
|
||||
)
|
||||
: null;
|
||||
|
||||
// if (ahora.weekday() === 0 || ahora.weekday() === 6)
|
||||
// throw new ConflictException(
|
||||
// 'No se puede pedir equipos de cómputo los días sabados ni domingos.',
|
||||
// );
|
||||
// else {
|
||||
// const institucionDia = await this.institucionDiaService.findDia(
|
||||
// modulo.institucion,
|
||||
// ahora.weekday(),
|
||||
// );
|
||||
// const horaMax = moment(
|
||||
// `${ahora.format('YYYY-MM-DD')} ${institucionDia.hora_fin}`,
|
||||
// );
|
||||
// const horaMin = moment(
|
||||
// `${ahora.format('YYYY-MM-DD')} ${institucionDia.hora_inicio}`,
|
||||
// );
|
||||
|
||||
// if (!institucionDia.activo)
|
||||
// throw new ConflictException(
|
||||
// 'El día de hoy no se esta realizando préstamo de equipos.',
|
||||
// );
|
||||
// if (ahora > horaMax)
|
||||
// throw new Error('Ya no se puede realizar un préstamo el día de hoy.');
|
||||
// if (ahora < horaMin)
|
||||
// throw new Error('Aún es muy temprano para realizar un préstamo.');
|
||||
// }
|
||||
return this.repository
|
||||
.findOne({ activo: true, usuario })
|
||||
.then((existePrestamo) => {
|
||||
@@ -195,35 +218,6 @@ export class PrestamoService {
|
||||
}));
|
||||
}
|
||||
|
||||
async regresar(id_prestamo: number, id_operador: number) {
|
||||
const ahora = moment();
|
||||
const operadorRegreso = await this.operadorService.findById(id_operador);
|
||||
const prestamo = await this.findById(id_prestamo);
|
||||
|
||||
if (prestamo.cancelado_usuario)
|
||||
throw new ConflictException(
|
||||
'Este préstamo ya fue cancelado por el usuario.',
|
||||
);
|
||||
if (prestamo.cancelado_operador)
|
||||
throw new ConflictException(
|
||||
'Este préstamo ya fue cancelado por un operador.',
|
||||
);
|
||||
if (!prestamo.activo)
|
||||
throw new ConflictException('Este préstamo ya no se encuentra activo.');
|
||||
if (prestamo.equipo.status.id_status === 2)
|
||||
throw new ConflictException('Aun no se entrega el equipo al usuario.');
|
||||
prestamo.activo = false;
|
||||
prestamo.fecha_entrega = ahora.toDate();
|
||||
prestamo.operadorRegreso = operadorRegreso;
|
||||
prestamo.equipo.status = await this.statusService.findById(1);
|
||||
return this.equipoService
|
||||
.update(prestamo.equipo)
|
||||
.then((_) => this.repository.save(prestamo))
|
||||
.then((_) => ({
|
||||
message: 'Se regresó el equipo de cómputo correctamente.',
|
||||
}));
|
||||
}
|
||||
|
||||
async findAll(filtros: {
|
||||
pagina: string;
|
||||
activo?: string | boolean;
|
||||
@@ -391,4 +385,33 @@ export class PrestamoService {
|
||||
return prestamo;
|
||||
});
|
||||
}
|
||||
|
||||
async regresar(id_prestamo: number, id_operador: number) {
|
||||
const ahora = moment();
|
||||
const operadorRegreso = await this.operadorService.findById(id_operador);
|
||||
const prestamo = await this.findById(id_prestamo);
|
||||
|
||||
if (prestamo.cancelado_usuario)
|
||||
throw new ConflictException(
|
||||
'Este préstamo ya fue cancelado por el usuario.',
|
||||
);
|
||||
if (prestamo.cancelado_operador)
|
||||
throw new ConflictException(
|
||||
'Este préstamo ya fue cancelado por un operador.',
|
||||
);
|
||||
if (!prestamo.activo)
|
||||
throw new ConflictException('Este préstamo ya no se encuentra activo.');
|
||||
if (prestamo.equipo.status.id_status === 2)
|
||||
throw new ConflictException('Aun no se entrega el equipo al usuario.');
|
||||
prestamo.activo = false;
|
||||
prestamo.fecha_entrega = ahora.toDate();
|
||||
prestamo.operadorRegreso = operadorRegreso;
|
||||
prestamo.equipo.status = await this.statusService.findById(1);
|
||||
return this.equipoService
|
||||
.update(prestamo.equipo)
|
||||
.then((_) => this.repository.save(prestamo))
|
||||
.then((_) => ({
|
||||
message: 'Se regresó el equipo de cómputo correctamente.',
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user