2022-06-09 04:37:29 +00:00
|
|
|
import {
|
|
|
|
ConflictException,
|
2022-08-06 06:01:12 +00:00
|
|
|
ForbiddenException,
|
2022-06-09 04:37:29 +00:00
|
|
|
forwardRef,
|
|
|
|
Inject,
|
|
|
|
Injectable,
|
|
|
|
} from '@nestjs/common';
|
2022-06-07 21:02:09 +00:00
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
import { Repository } from 'typeorm';
|
2022-07-08 04:58:21 +00:00
|
|
|
import { Equipo } from '../equipo/entity/equipo.entity';
|
2022-06-07 21:02:09 +00:00
|
|
|
import { EquipoPrograma } from './entity/equipo-programa.entity';
|
2022-07-08 04:58:21 +00:00
|
|
|
import { Programa } from '../institucion-programa/entity/programa.entity';
|
2022-06-08 00:55:02 +00:00
|
|
|
import { EquipoService } from '../equipo/equipo.service';
|
|
|
|
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
2022-08-15 19:29:53 +00:00
|
|
|
import { Operador } from '../operador/entity/operador.entity';
|
2022-06-07 21:02:09 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class EquipoProgramaService {
|
|
|
|
constructor(
|
|
|
|
@InjectRepository(EquipoPrograma)
|
2022-06-08 00:55:02 +00:00
|
|
|
private repository: Repository<EquipoPrograma>,
|
2022-06-09 04:37:29 +00:00
|
|
|
@Inject(forwardRef(() => EquipoService))
|
2022-06-08 00:55:02 +00:00
|
|
|
private equipoService: EquipoService,
|
|
|
|
private institucionProgramaService: InstitucionProgramaService,
|
2022-06-07 21:02:09 +00:00
|
|
|
) {}
|
2022-06-08 00:55:02 +00:00
|
|
|
|
2022-08-06 06:01:12 +00:00
|
|
|
async create(
|
|
|
|
// operador: Operador,
|
|
|
|
id_equipo: number | Equipo,
|
|
|
|
id_programa: number | Programa,
|
|
|
|
) {
|
|
|
|
const sinPrograma = await this.institucionProgramaService.findProgramaById(
|
|
|
|
1,
|
|
|
|
);
|
2022-06-08 03:47:46 +00:00
|
|
|
const equipo =
|
|
|
|
typeof id_equipo === 'number'
|
|
|
|
? await this.equipoService.findById(id_equipo)
|
|
|
|
: id_equipo;
|
|
|
|
const programa =
|
|
|
|
typeof id_programa === 'number'
|
|
|
|
? await this.institucionProgramaService.findProgramaById(id_programa)
|
|
|
|
: id_programa;
|
2022-06-08 00:55:02 +00:00
|
|
|
|
2022-07-25 03:35:00 +00:00
|
|
|
return this.findEquipoProgramaByEquipoPrograma(equipo, programa)
|
2022-06-09 04:37:29 +00:00
|
|
|
.then((_) =>
|
2022-08-06 06:01:12 +00:00
|
|
|
this.findEquipoProgramaByEquipoPrograma(equipo, sinPrograma, false),
|
2022-06-08 03:47:46 +00:00
|
|
|
)
|
2022-08-06 06:01:12 +00:00
|
|
|
.then(async (existeEquipoSinPrograma) => {
|
|
|
|
if (existeEquipoSinPrograma)
|
|
|
|
await this.repository.remove(existeEquipoSinPrograma);
|
|
|
|
return this.repository.save(
|
|
|
|
this.repository.create({ equipo, programa }),
|
|
|
|
);
|
|
|
|
})
|
2022-06-08 03:47:46 +00:00
|
|
|
.then((equipoPrograma) => ({
|
2022-08-06 06:01:12 +00:00
|
|
|
message: `Se asignó el software: ${equipoPrograma.programa.programa}, al equipo con número de inventario: ${equipo.numero_inventario}.`,
|
2022-06-08 03:47:46 +00:00
|
|
|
equipoPrograma,
|
|
|
|
}));
|
2022-06-08 00:55:02 +00:00
|
|
|
}
|
|
|
|
|
2022-08-06 06:01:12 +00:00
|
|
|
async delete(operador: Operador, id_equipo_programa: number) {
|
|
|
|
const equipoPrograma = await this.findById(id_equipo_programa);
|
2022-06-09 04:37:29 +00:00
|
|
|
|
2022-08-06 06:01:12 +00:00
|
|
|
if (
|
|
|
|
operador.institucion.id_institucion !=
|
|
|
|
equipoPrograma.equipo.carrito.modulo.institucion.id_institucion
|
|
|
|
)
|
|
|
|
throw new ForbiddenException(
|
|
|
|
'No puedes eliminar el software de este equipo porque no pertenece a tu institución.',
|
|
|
|
);
|
|
|
|
return this.repository
|
|
|
|
.count({ equipo: equipoPrograma.equipo })
|
|
|
|
.then(async (n) => {
|
2022-06-09 04:37:29 +00:00
|
|
|
if (n === 1) {
|
|
|
|
if (equipoPrograma.programa.id_programa === 1)
|
|
|
|
throw new ConflictException('No se puede eliminar esta opción.');
|
2022-08-06 06:01:12 +00:00
|
|
|
await this.create(equipoPrograma.equipo, 1);
|
2022-06-09 04:37:29 +00:00
|
|
|
}
|
2022-08-06 06:01:12 +00:00
|
|
|
return this.repository.remove(equipoPrograma);
|
|
|
|
})
|
|
|
|
.then((_) => ({
|
|
|
|
message: 'Se eliminó correctamente el software de este equipo.',
|
|
|
|
}));
|
2022-06-08 00:55:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 03:35:00 +00:00
|
|
|
findById(id_equipo_programa: number) {
|
|
|
|
return this.repository
|
|
|
|
.findOne({
|
2022-08-06 06:01:12 +00:00
|
|
|
join: {
|
|
|
|
alias: 'ep',
|
|
|
|
innerJoinAndSelect: {
|
|
|
|
e: 'ep.equipo',
|
|
|
|
p: 'ep.programa',
|
|
|
|
c: 'e.carrito',
|
|
|
|
m: 'c.modulo',
|
|
|
|
i: 'm.institucion',
|
|
|
|
},
|
|
|
|
},
|
2022-07-25 03:35:00 +00:00
|
|
|
where: { id_equipo_programa },
|
|
|
|
})
|
|
|
|
.then((equipoPrograma) => {
|
|
|
|
if (!equipoPrograma)
|
2022-08-06 06:01:12 +00:00
|
|
|
throw new ConflictException('No existe este id equipo software.');
|
2022-07-25 03:35:00 +00:00
|
|
|
return equipoPrograma;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
findEquipoProgramaByEquipoPrograma(
|
2022-06-08 03:47:46 +00:00
|
|
|
equipo: Equipo,
|
|
|
|
programa: Programa,
|
|
|
|
validarExistencia = true,
|
|
|
|
) {
|
2022-06-08 00:55:02 +00:00
|
|
|
return this.repository
|
|
|
|
.findOne({ equipo, programa })
|
|
|
|
.then((equipoPrograma) => {
|
2022-06-08 03:47:46 +00:00
|
|
|
if (validarExistencia && equipoPrograma)
|
2022-06-08 00:55:02 +00:00
|
|
|
throw new ConflictException(
|
2022-08-06 06:01:12 +00:00
|
|
|
'Este software ya fue asignado a este equipo.',
|
2022-06-08 00:55:02 +00:00
|
|
|
);
|
2022-06-08 03:47:46 +00:00
|
|
|
return equipoPrograma;
|
2022-06-08 00:55:02 +00:00
|
|
|
});
|
|
|
|
}
|
2022-06-07 21:02:09 +00:00
|
|
|
}
|