Files
api-AT/src/bitacora_mesa/bitacora_mesa.service.ts
T

164 lines
4.7 KiB
TypeScript
Raw Normal View History

2026-01-23 16:46:55 -06:00
import { Injectable, NotFoundException } from '@nestjs/common';
2026-01-21 15:13:21 -06:00
import { CreateBitacoraMesaDto } from './dto/create-bitacora_mesa.dto';
2026-01-21 17:10:49 -06:00
import { InjectRepository } from '@nestjs/typeorm';
import { BitacoraMesa } from './entities/bitacora_mesa.entity';
2026-02-17 19:21:29 -06:00
import { Repository, Between } from 'typeorm';
2026-02-05 18:53:14 -06:00
import { BadRequestException } from '@nestjs/common';
2026-01-21 15:13:21 -06:00
@Injectable()
export class BitacoraMesaService {
2026-01-21 17:10:49 -06:00
constructor(
@InjectRepository(BitacoraMesa)
private readonly bitacoraMesaRepository: Repository<BitacoraMesa>,
2026-02-17 19:21:29 -06:00
) { }
2026-01-28 12:51:55 -06:00
2026-02-17 19:21:29 -06:00
async assignment(dto: CreateBitacoraMesaDto) {
2026-02-24 17:37:39 -06:00
2026-02-17 19:21:29 -06:00
const alumnoConMesa = await this.bitacoraMesaRepository
.createQueryBuilder('bitacora')
.where('bitacora.id_alumno_inscrito = :id_alumno_inscrito', {
id_alumno_inscrito: dto.id_alumno_inscrito,
})
.andWhere(
`TIMESTAMPDIFF(
2026-02-05 18:53:14 -06:00
SECOND,
NOW(),
DATE_ADD(bitacora.tiempo_entrada, INTERVAL bitacora.tiempo_asignado MINUTE)
) > 0`,
2026-02-17 19:21:29 -06:00
)
.getOne();
2026-01-28 12:51:55 -06:00
2026-02-17 19:21:29 -06:00
if (alumnoConMesa) {
throw new BadRequestException(
'El alumno ya tiene una mesa asignada',
);
}
2026-01-21 15:13:21 -06:00
2026-02-05 18:53:14 -06:00
2026-02-17 19:21:29 -06:00
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 },
});
2026-02-05 18:53:14 -06:00
2026-02-17 19:21:29 -06:00
return this.bitacoraMesaRepository.save(bitacora);
}
2026-02-05 18:53:14 -06:00
2026-01-21 15:13:21 -06:00
findAll() {
2026-01-23 16:46:55 -06:00
return this.bitacoraMesaRepository.find({
relations: ['mesa', 'alumno_inscrito'],
take: 100,
2026-02-19 15:24:53 -06:00
order: { id_bitacora_mesa: "DESC" }
2026-01-23 16:46:55 -06:00
});
2026-01-21 15:13:21 -06:00
}
findOne(id: number) {
return `This action returns a #${id} bitacoraMesa`;
}
2026-01-23 16:46:55 -06:00
async findByCuenta(id_cuenta: number) {
2026-01-27 12:26:22 -06:00
const student = await this.bitacoraMesaRepository
2026-01-23 20:09:52 -06:00
.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 })
2026-01-28 12:51:55 -06:00
.andWhere(
`TIMESTAMPDIFF(SECOND,NOW(),DATE_ADD(bitacora.tiempo_entrada, INTERVAL bitacora.tiempo_asignado MINUTE)) > 0`,
)
2026-01-23 20:09:52 -06:00
.orderBy('bitacora.tiempo_entrada', 'DESC')
.getOne();
2026-01-27 12:26:22 -06:00
if (!student) {
2026-02-19 15:24:53 -06:00
throw new NotFoundException('Alumno sin mesa');
2026-01-23 16:46:55 -06:00
}
2026-01-27 12:26:22 -06:00
return student;
2026-01-23 16:46:55 -06:00
}
2026-01-28 12:51:55 -06:00
2026-02-17 19:21:29 -06:00
async findByTable(id_mesa: number) {
2026-01-29 11:13:00 -06:00
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;
}
2026-02-17 19:21:29 -06:00
async findByDate(date) {
const start = new Date(`${date}T00:00:00`);
const end = new Date(`${date}T23:59:59`);
2026-01-28 12:51:55 -06:00
2026-02-17 19:21:29 -06:00
return this.bitacoraMesaRepository.find({
where: {
tiempo_entrada: Between(start, end),
},
})
2026-01-28 12:51:55 -06:00
2026-02-19 10:49:09 -06:00
}
2026-01-28 12:51:55 -06:00
2026-02-17 19:21:29 -06:00
async cancelation(id_bitacora_mesa: number, nuevoTiempo: number) {
2026-02-19 10:49:09 -06:00
const bitacora = await this.bitacoraMesaRepository.findOne({
where: { id_bitacora_mesa },
});
if (!bitacora) {
throw new NotFoundException('Mesa no encontrada');
}
bitacora.tiempo_asignado -= nuevoTiempo;
2026-01-28 12:51:55 -06:00
2026-02-19 10:49:09 -06:00
await this.bitacoraMesaRepository.save(bitacora);
return { message: 'Tiempo cancelado' };
2026-01-28 12:51:55 -06:00
}
2026-02-17 19:21:29 -06:00
2026-02-19 10:49:09 -06:00
async mesasPorFecha(fecha: string) {
const inicio = new Date(`${fecha}T00:00:00`);
const fin = new Date(`${fecha}T23:59:59`);
2026-02-17 19:21:29 -06:00
2026-02-19 10:49:09 -06:00
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
),
}));
}
2026-02-17 19:21:29 -06:00
2026-01-21 15:13:21 -06:00
}