added modified programs
This commit is contained in:
@@ -1,24 +1,29 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { CreateProgramaEquipoDto } from './dto/create-programa_equipo.dto';
|
||||
import { UpdateProgramaEquipoDto } from './dto/update-programa_equipo.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { ProgramaEquipo } from './entities/programa_equipo.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { In, Repository } from 'typeorm';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { Equipo } from 'src/equipo/entities/equipo.entity';
|
||||
|
||||
@Injectable()
|
||||
export class ProgramaEquipoService {
|
||||
constructor(
|
||||
@InjectRepository(ProgramaEquipo)
|
||||
private readonly ProgramaEquipoRepository: Repository<ProgramaEquipo>,
|
||||
private readonly programaEquipoRepository: Repository<ProgramaEquipo>,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
findAllProgramByNumber(id_equipo: number) {
|
||||
return this.ProgramaEquipoRepository.find({ where: { id_equipo } });
|
||||
return this.programaEquipoRepository.find({ where: { id_equipo } });
|
||||
}
|
||||
|
||||
findAllProgramByUbication(ubicacion: string) {
|
||||
return this.programaEquipoRepository.find();
|
||||
}
|
||||
|
||||
async findOne(ubicacion: string) {
|
||||
const equipo = await this.ProgramaEquipoRepository.find({
|
||||
where: { equipo:{ubicacion} },
|
||||
const equipo = await this.programaEquipoRepository.find({
|
||||
where: { equipo: { ubicacion } },
|
||||
});
|
||||
|
||||
if (equipo.length === 0) {
|
||||
@@ -28,7 +33,104 @@ export class ProgramaEquipoService {
|
||||
return equipo;
|
||||
}
|
||||
|
||||
update(id: number, updateProgramaEquipoDto: UpdateProgramaEquipoDto) {
|
||||
return `This action updates a #${id} programaEquipo`;
|
||||
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',
|
||||
};
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user