155 lines
4.8 KiB
TypeScript
155 lines
4.8 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import { CreateBitacoraDto, EquiposPorEquipoDto } from './dto/create-bitacora.dto';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Bitacora } from './entities/bitacora.entity';
|
|
import { Repository } from 'typeorm';
|
|
|
|
@Injectable()
|
|
export class BitacoraService {
|
|
constructor(
|
|
@InjectRepository(Bitacora)
|
|
private readonly bitacoraRepository: Repository<Bitacora>,
|
|
) { }
|
|
|
|
assignment(dto: CreateBitacoraDto) {
|
|
const bitacora = this.bitacoraRepository.create({
|
|
tiempo_entrada: new Date(),
|
|
tiempo_asignado: dto.tiempo_asignado,
|
|
ubicacion: dto.ubicacion,
|
|
equipo: { id_equipo: dto.id_equipo },
|
|
alumno_inscrito: { id_alumno_inscrito: dto.id_alumno_inscrito },
|
|
});
|
|
|
|
return this.bitacoraRepository.save(bitacora);
|
|
}
|
|
|
|
findAll() {
|
|
return this.bitacoraRepository.find({
|
|
take: 100,
|
|
order: { tiempo_entrada: 'DESC' },
|
|
relations: ['alumno_inscrito', 'equipo'],
|
|
});
|
|
}
|
|
|
|
async findOneByAcount(id_cuenta: number) {
|
|
const student = await this.bitacoraRepository
|
|
.createQueryBuilder('bitacora')
|
|
.leftJoinAndSelect('bitacora.alumno_inscrito', 'alumnoInscrito')
|
|
.leftJoinAndSelect('alumnoInscrito.alumno', 'alumno')
|
|
.leftJoinAndSelect('alumno.carrera', 'carrera')
|
|
.leftJoinAndSelect('bitacora.equipo', 'equipo')
|
|
.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('No cuenta con tiempo asignado');
|
|
}
|
|
|
|
return student;
|
|
}
|
|
|
|
async findOneByMachine(ubicacion: number) {
|
|
const student = await this.bitacoraRepository
|
|
.createQueryBuilder('bitacora')
|
|
.leftJoinAndSelect('bitacora.alumno_inscrito', 'alumnoInscrito')
|
|
.leftJoinAndSelect('alumnoInscrito.alumno', 'alumno')
|
|
.leftJoinAndSelect('alumno.carrera', 'carrera')
|
|
.leftJoinAndSelect('bitacora.equipo', 'equipo')
|
|
.where('equipo.ubicacion = :ubicacion', { ubicacion })
|
|
|
|
.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('Equipo no asignado');
|
|
}
|
|
|
|
return student;
|
|
}
|
|
|
|
async cancelation(id_bitacora: number, nuevoTiempo: number) {
|
|
const bitacora = await this.bitacoraRepository.findOne({
|
|
where: { id_bitacora },
|
|
});
|
|
|
|
if (!bitacora) {
|
|
throw new NotFoundException('alumno sin equipo asignado');
|
|
}
|
|
|
|
bitacora.tiempo_asignado -= nuevoTiempo;
|
|
|
|
await this.bitacoraRepository.save(bitacora);
|
|
|
|
return { message: 'Tiempo cancelado' };
|
|
}
|
|
|
|
async obtenerEquiposPorAlumnoYFecha(numeroCuenta: string, fecha: string) {
|
|
|
|
const fechaInicio = new Date(`${fecha}T00:00:00`);
|
|
const fechaFin = new Date(`${fecha}T23:59:59`);
|
|
|
|
return await this.bitacoraRepository
|
|
.createQueryBuilder('bitacora')
|
|
.innerJoinAndSelect('bitacora.equipo', 'equipo')
|
|
.innerJoin('bitacora.alumno_inscrito', 'alumnoInscrito')
|
|
.innerJoin('alumnoInscrito.alumno', 'alumno')
|
|
.where('alumno.id_cuenta = :numeroCuenta', { numeroCuenta })
|
|
.andWhere('bitacora.tiempo_entrada BETWEEN :inicio AND :fin', {
|
|
inicio: fechaInicio,
|
|
fin: fechaFin,
|
|
})
|
|
.select([
|
|
'bitacora.tiempo_entrada',
|
|
'bitacora.tiempo_asignado',
|
|
'equipo.ubicacion',
|
|
'equipo.nombre_equipo'
|
|
])
|
|
.getMany();
|
|
}
|
|
|
|
async equiposPorEquipo(dto: EquiposPorEquipoDto) {
|
|
const { id_equipo, fecha } = dto;
|
|
|
|
const inicio = new Date(`${fecha}T00:00:00`);
|
|
const fin = new Date(`${fecha}T23:59:59`);
|
|
|
|
const registros = await this.bitacoraRepository
|
|
.createQueryBuilder('bitacora')
|
|
.innerJoin('bitacora.equipo', 'equipo')
|
|
.innerJoin('bitacora.alumno_inscrito', 'alumnoInscrito')
|
|
.innerJoin('alumnoInscrito.alumno', 'alumno')
|
|
.where('equipo.id_equipo = :id_equipo', { id_equipo })
|
|
.andWhere('bitacora.tiempo_entrada BETWEEN :inicio AND :fin', {
|
|
inicio,
|
|
fin,
|
|
})
|
|
.select([
|
|
'bitacora.tiempo_entrada AS tiempo_entrada',
|
|
'bitacora.tiempo_asignado AS tiempo_asignado',
|
|
'alumno.id_cuenta AS id_cuenta'
|
|
])
|
|
.orderBy('bitacora.tiempo_entrada', 'DESC')
|
|
.getRawMany();
|
|
|
|
return registros.map(reg => ({
|
|
hora_entrada: reg.tiempo_entrada,
|
|
min_utilizados: reg.tiempo_asignado,
|
|
hora_salida: new Date(
|
|
new Date(reg.tiempo_entrada).getTime() +
|
|
reg.tiempo_asignado * 60000
|
|
),
|
|
cuenta_asociada: reg.id_cuenta
|
|
}));
|
|
}
|
|
|
|
}
|