2025-10-03 16:34:39 -06:00
|
|
|
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
import { ProgramaEquipo } from './entities/programa_equipo.entity';
|
2026-01-28 19:05:51 -06:00
|
|
|
import { In, Repository } from 'typeorm';
|
|
|
|
|
import { DataSource } from 'typeorm';
|
|
|
|
|
import { Equipo } from 'src/equipo/entities/equipo.entity';
|
2025-10-03 16:34:39 -06:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class ProgramaEquipoService {
|
|
|
|
|
constructor(
|
|
|
|
|
@InjectRepository(ProgramaEquipo)
|
2026-01-28 19:05:51 -06:00
|
|
|
private readonly programaEquipoRepository: Repository<ProgramaEquipo>,
|
|
|
|
|
private readonly dataSource: DataSource,
|
2025-10-03 16:34:39 -06:00
|
|
|
) {}
|
|
|
|
|
|
2025-10-08 15:45:21 -06:00
|
|
|
findAllProgramByNumber(id_equipo: number) {
|
2026-01-28 19:05:51 -06:00
|
|
|
return this.programaEquipoRepository.find({ where: { id_equipo } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findAllProgramByUbication(ubicacion: string) {
|
|
|
|
|
return this.programaEquipoRepository.find();
|
2025-10-03 16:34:39 -06:00
|
|
|
}
|
|
|
|
|
|
2026-01-27 18:33:43 -06:00
|
|
|
async findOne(ubicacion: string) {
|
2026-01-28 19:05:51 -06:00
|
|
|
const equipo = await this.programaEquipoRepository.find({
|
|
|
|
|
where: { equipo: { ubicacion } },
|
2025-10-03 16:34:39 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (equipo.length === 0) {
|
|
|
|
|
throw new NotFoundException(`machine without programs`);
|
|
|
|
|
}
|
2025-10-08 15:45:21 -06:00
|
|
|
|
2025-10-03 16:34:39 -06:00
|
|
|
return equipo;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-28 19:05:51 -06:00
|
|
|
async updateMachine(id_equipo: number, programasSeleccionados: number[]) {
|
|
|
|
|
const actuales = await this.programaEquipoRepository.find({
|
|
|
|
|
where: { id_equipo },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const actualesIds = actuales.map((p) => p.id_programa);
|
|
|
|
|
|
|
|
|
|
const aCrear = programasSeleccionados.filter(
|
|
|
|
|
(id) => !actualesIds.includes(id),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const aEliminar = actualesIds.filter(
|
|
|
|
|
(id) => !programasSeleccionados.includes(id),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (aCrear.length > 0) {
|
|
|
|
|
const nuevos = aCrear.map((id_programa) =>
|
|
|
|
|
this.programaEquipoRepository.create({
|
|
|
|
|
id_equipo,
|
|
|
|
|
id_programa,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
await this.programaEquipoRepository.save(nuevos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (aEliminar.length > 0) {
|
|
|
|
|
await this.programaEquipoRepository.delete({
|
|
|
|
|
id_equipo,
|
|
|
|
|
id_programa: In(aEliminar),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
message: 'Programas del equipo actualizados correctamente',
|
|
|
|
|
};
|
2025-10-03 16:34:39 -06:00
|
|
|
}
|
2026-01-28 19:05:51 -06:00
|
|
|
|
|
|
|
|
async updateBySala(
|
|
|
|
|
id_area_ubicacion: number,
|
|
|
|
|
programasSeleccionados: number[],
|
|
|
|
|
) {
|
|
|
|
|
const queryRunner = this.dataSource.createQueryRunner();
|
|
|
|
|
await queryRunner.connect();
|
|
|
|
|
await queryRunner.startTransaction();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const equipos = await queryRunner.manager.find(Equipo, {
|
|
|
|
|
where: { id_area_ubicacion },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (equipos.length === 0) {
|
|
|
|
|
throw new Error('No hay equipos en la sala');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const equipo of equipos) {
|
|
|
|
|
const actuales = await queryRunner.manager.find(ProgramaEquipo, {
|
|
|
|
|
where: { id_equipo: equipo.id_equipo },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const actualesIds = actuales.map(p => p.id_programa);
|
|
|
|
|
|
|
|
|
|
const aCrear = programasSeleccionados.filter(
|
|
|
|
|
id => !actualesIds.includes(id),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const aEliminar = actualesIds.filter(
|
|
|
|
|
id => !programasSeleccionados.includes(id),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (aCrear.length > 0) {
|
|
|
|
|
const nuevos = aCrear.map(id_programa =>
|
|
|
|
|
queryRunner.manager.create(ProgramaEquipo, {
|
|
|
|
|
id_equipo: equipo.id_equipo,
|
|
|
|
|
id_programa,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
await queryRunner.manager.save(nuevos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (aEliminar.length > 0) {
|
|
|
|
|
await queryRunner.manager.delete(ProgramaEquipo, {
|
|
|
|
|
id_equipo: equipo.id_equipo,
|
|
|
|
|
id_programa: In(aEliminar),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await queryRunner.commitTransaction();
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
message: 'Programas actualizados correctamente en la sala',
|
|
|
|
|
equiposActualizados: equipos.length,
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
await queryRunner.rollbackTransaction();
|
|
|
|
|
throw error;
|
|
|
|
|
} finally {
|
|
|
|
|
await queryRunner.release();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-03 16:34:39 -06:00
|
|
|
}
|