164 lines
4.7 KiB
TypeScript
164 lines
4.7 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { CreateBitacoraMesaDto } from './dto/create-bitacora_mesa.dto';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { BitacoraMesa } from './entities/bitacora_mesa.entity';
|
|
import { Repository, Between } from 'typeorm';
|
|
import { BadRequestException } from '@nestjs/common';
|
|
|
|
|
|
@Injectable()
|
|
export class BitacoraMesaService {
|
|
constructor(
|
|
@InjectRepository(BitacoraMesa)
|
|
private readonly bitacoraMesaRepository: Repository<BitacoraMesa>,
|
|
) { }
|
|
|
|
async assignment(dto: CreateBitacoraMesaDto) {
|
|
|
|
const alumnoConMesa = await this.bitacoraMesaRepository
|
|
.createQueryBuilder('bitacora')
|
|
.where('bitacora.id_alumno_inscrito = :id_alumno_inscrito', {
|
|
id_alumno_inscrito: dto.id_alumno_inscrito,
|
|
})
|
|
.andWhere(
|
|
`TIMESTAMPDIFF(
|
|
SECOND,
|
|
NOW(),
|
|
DATE_ADD(bitacora.tiempo_entrada, INTERVAL bitacora.tiempo_asignado MINUTE)
|
|
) > 0`,
|
|
)
|
|
.getOne();
|
|
|
|
if (alumnoConMesa) {
|
|
throw new BadRequestException(
|
|
'El alumno ya tiene una mesa asignada',
|
|
);
|
|
}
|
|
|
|
|
|
const bitacora = this.bitacoraMesaRepository.create({
|
|
tiempo_entrada: new Date(),
|
|
tiempo_asignado: dto.tiempo_asignado,
|
|
mesa: { id_mesa: dto.id_mesa },
|
|
alumno_inscrito: { id_alumno_inscrito: dto.id_alumno_inscrito },
|
|
});
|
|
|
|
return this.bitacoraMesaRepository.save(bitacora);
|
|
}
|
|
|
|
findAll() {
|
|
return this.bitacoraMesaRepository.find({
|
|
relations: ['mesa', 'alumno_inscrito'],
|
|
take: 100,
|
|
order: { id_bitacora_mesa: "DESC" }
|
|
});
|
|
}
|
|
|
|
findOne(id: number) {
|
|
return `This action returns a #${id} bitacoraMesa`;
|
|
}
|
|
|
|
async findByCuenta(id_cuenta: number) {
|
|
const student = await this.bitacoraMesaRepository
|
|
.createQueryBuilder('bitacora')
|
|
.leftJoinAndSelect('bitacora.alumno_inscrito', 'alumnoInscrito')
|
|
.leftJoinAndSelect('alumnoInscrito.alumno', 'alumno')
|
|
.leftJoinAndSelect('alumno.carrera', 'carrera')
|
|
.leftJoinAndSelect('bitacora.mesa', 'mesa')
|
|
.where('alumno.id_cuenta = :id_cuenta', { id_cuenta })
|
|
|
|
.andWhere(
|
|
`TIMESTAMPDIFF(SECOND,NOW(),DATE_ADD(bitacora.tiempo_entrada, INTERVAL bitacora.tiempo_asignado MINUTE)) > 0`,
|
|
)
|
|
|
|
.orderBy('bitacora.tiempo_entrada', 'DESC')
|
|
.getOne();
|
|
|
|
if (!student) {
|
|
throw new NotFoundException('Alumno sin mesa');
|
|
}
|
|
|
|
return student;
|
|
}
|
|
|
|
async findByTable(id_mesa: number) {
|
|
const student = await this.bitacoraMesaRepository
|
|
.createQueryBuilder('bitacora')
|
|
.leftJoinAndSelect('bitacora.alumno_inscrito', 'alumnoInscrito')
|
|
.leftJoinAndSelect('alumnoInscrito.alumno', 'alumno')
|
|
.leftJoinAndSelect('alumno.carrera', 'carrera')
|
|
.leftJoinAndSelect('bitacora.mesa', 'mesa')
|
|
.where('mesa.id_mesa = :id_mesa', { id_mesa })
|
|
|
|
.andWhere(
|
|
`TIMESTAMPDIFF(SECOND,NOW(),DATE_ADD(bitacora.tiempo_entrada, INTERVAL bitacora.tiempo_asignado MINUTE)) > 0`,
|
|
)
|
|
|
|
.orderBy('bitacora.tiempo_entrada', 'DESC')
|
|
.getOne();
|
|
|
|
if (!student) {
|
|
throw new NotFoundException('Mesa no asignada');
|
|
}
|
|
|
|
return student;
|
|
}
|
|
|
|
async findByDate(date) {
|
|
const start = new Date(`${date}T00:00:00`);
|
|
const end = new Date(`${date}T23:59:59`);
|
|
|
|
return this.bitacoraMesaRepository.find({
|
|
where: {
|
|
tiempo_entrada: Between(start, end),
|
|
},
|
|
})
|
|
|
|
}
|
|
|
|
async cancelation(id_bitacora_mesa: number, nuevoTiempo: number) {
|
|
const bitacora = await this.bitacoraMesaRepository.findOne({
|
|
where: { id_bitacora_mesa },
|
|
});
|
|
|
|
if (!bitacora) {
|
|
throw new NotFoundException('Mesa no encontrada');
|
|
}
|
|
|
|
bitacora.tiempo_asignado -= nuevoTiempo;
|
|
|
|
await this.bitacoraMesaRepository.save(bitacora);
|
|
|
|
return { message: 'Tiempo cancelado' };
|
|
}
|
|
|
|
async mesasPorFecha(fecha: string) {
|
|
const inicio = new Date(`${fecha}T00:00:00`);
|
|
const fin = new Date(`${fecha}T23:59:59`);
|
|
|
|
const registros = await this.bitacoraMesaRepository
|
|
.createQueryBuilder('bitacora')
|
|
.innerJoinAndSelect('bitacora.mesa', 'mesa')
|
|
.innerJoinAndSelect('bitacora.alumno_inscrito', 'alumnoInscrito')
|
|
.innerJoinAndSelect('alumnoInscrito.alumno', 'alumno')
|
|
.where('bitacora.tiempo_entrada BETWEEN :inicio AND :fin', {
|
|
inicio,
|
|
fin,
|
|
})
|
|
.orderBy('bitacora.tiempo_entrada', 'DESC')
|
|
.getMany();
|
|
|
|
return registros.map(reg => ({
|
|
no_mesa: reg.mesa?.id_mesa,
|
|
no_cuenta: reg.alumno_inscrito?.alumno?.id_cuenta,
|
|
hora_entrada: reg.tiempo_entrada,
|
|
tiempo_asignado: reg.tiempo_asignado,
|
|
hora_salida: new Date(
|
|
new Date(reg.tiempo_entrada).getTime() +
|
|
reg.tiempo_asignado * 60000
|
|
),
|
|
}));
|
|
}
|
|
|
|
}
|