170 lines
6.4 KiB
TypeScript
170 lines
6.4 KiB
TypeScript
import {
|
|
ConflictException,
|
|
ForbiddenException,
|
|
forwardRef,
|
|
Inject,
|
|
Injectable,
|
|
} from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { Equipo } from '../equipo/entity/equipo.entity';
|
|
import { EquipoPrograma } from './entity/equipo-programa.entity';
|
|
import { Operador } from '../operador/entity/operador.entity';
|
|
import { Programa } from '../institucion-programa/entity/programa.entity';
|
|
import { FullInformacionEquipoProgramaView } from './entity/views/full-informacion-equipo-programa.view';
|
|
import { InformacionEquipoProgramaView } from './entity/views/informacion-equipo-programa.view';
|
|
import { EquipoService } from '../equipo/equipo.service';
|
|
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
|
|
|
@Injectable()
|
|
export class EquipoProgramaService {
|
|
constructor(
|
|
@InjectRepository(EquipoPrograma)
|
|
private repository: Repository<EquipoPrograma>,
|
|
@InjectRepository(FullInformacionEquipoProgramaView)
|
|
private fullInformacionEquipoProgramaView: Repository<FullInformacionEquipoProgramaView>,
|
|
@InjectRepository(InformacionEquipoProgramaView)
|
|
private informacionEquipoProgramaView: Repository<InformacionEquipoProgramaView>,
|
|
@Inject(forwardRef(() => EquipoService))
|
|
private equipoService: EquipoService,
|
|
private institucionProgramaService: InstitucionProgramaService,
|
|
) {}
|
|
|
|
async asignar(operador: Operador, id_equipo: number, id_programa: number) {
|
|
const equipo = await this.equipoService.findById(id_equipo);
|
|
const programa = await this.institucionProgramaService.findProgramaById(
|
|
id_programa,
|
|
);
|
|
|
|
// valido que el programa seleccionado no sea "Sin programa"
|
|
if (programa.id_programa === 1)
|
|
throw new ConflictException(
|
|
'No se puede asignar manualmente este programa.',
|
|
);
|
|
// Valida que el equipo pertenezca a la institución del operador
|
|
if (
|
|
operador.tipoUsuario.id_tipo_usuario > 2 &&
|
|
operador.institucion.id_institucion !=
|
|
equipo.carrito.modulo.institucion.id_institucion
|
|
)
|
|
throw new ForbiddenException(
|
|
'No puedes modificar la información este equipo porque no pertenece a tu institución.',
|
|
);
|
|
// Verifica que no eixsta un registro con estos ids
|
|
return this.findByEquipoPrograma(equipo, programa).then(
|
|
(existeEquipoPrograma) => {
|
|
// Error si existe
|
|
if (existeEquipoPrograma)
|
|
throw new ConflictException(
|
|
'Este software ya fue asignado a este equipo.',
|
|
);
|
|
return this.create(equipo, programa);
|
|
},
|
|
);
|
|
}
|
|
|
|
create(equipo: Equipo, programa: Programa) {
|
|
// Ver si tiene programa "Sin programa"
|
|
return this.findByEquipoPrograma(equipo, { id_programa: 1 })
|
|
.then(async (existeEquipoSinPrograma) => {
|
|
// Si tiene lo elimino
|
|
if (existeEquipoSinPrograma)
|
|
await this.repository.remove(existeEquipoSinPrograma);
|
|
// Creo el registro
|
|
return this.repository.save(
|
|
this.repository.create({ equipo, programa }),
|
|
);
|
|
})
|
|
.then((equipoPrograma) => ({
|
|
message: `Se asignó el software: ${equipoPrograma.programa.programa}, al equipo con número de inventario: ${equipo.numero_inventario}.`,
|
|
equipoPrograma,
|
|
}));
|
|
}
|
|
|
|
createSinPrograma(equipo: Equipo) {
|
|
return this.repository.save(
|
|
this.repository.create({ equipo, programa: { id_programa: 1 } }),
|
|
);
|
|
}
|
|
|
|
async delete(operador: Operador, id_equipo_programa: number) {
|
|
const equipoPrograma = await this.findById(id_equipo_programa);
|
|
|
|
// No se puede eliminar "Sin programa" manualmente
|
|
if (equipoPrograma.programa.id_programa === 1)
|
|
throw new ConflictException('No se puede eliminar esta opción.');
|
|
// Valida que el equipo pertenezca a la institución del operador
|
|
if (
|
|
operador.institucion.id_institucion !=
|
|
equipoPrograma.equipo.carrito.modulo.institucion.id_institucion
|
|
)
|
|
throw new ForbiddenException(
|
|
'No puedes modificar la información este equipo porque no pertenece a tu institución.',
|
|
);
|
|
// Ver cuantos programas tiene el equipo
|
|
return this.repository
|
|
.count({ where: { equipo: equipoPrograma.equipo } })
|
|
.then(async (n) => {
|
|
// Asignar "Sin programa" si es que solo tiene 1, el que se va a eliminar
|
|
if (n === 1) await this.createSinPrograma(equipoPrograma.equipo);
|
|
// Eliminar registro
|
|
return this.repository.remove(equipoPrograma);
|
|
})
|
|
.then((_) => ({
|
|
message: 'Se eliminó correctamente el software de este equipo.',
|
|
}));
|
|
}
|
|
|
|
findFullInfoAllByIdEquipo(id_equipo: number) {
|
|
return this.fullInformacionEquipoProgramaView
|
|
.find({ where: { id_equipo } })
|
|
.then((infoEquipoProgramas) => {
|
|
const equipoProgramas: EquipoPrograma[] = [];
|
|
|
|
for (let i = 0; i < infoEquipoProgramas.length; i++)
|
|
equipoProgramas.push(
|
|
this.repository.create({
|
|
id_equipo_programa: infoEquipoProgramas[i].id_equipo_programa,
|
|
programa: {
|
|
id_programa: infoEquipoProgramas[i].id_programa,
|
|
programa: infoEquipoProgramas[i].programa,
|
|
},
|
|
}),
|
|
);
|
|
return equipoProgramas;
|
|
});
|
|
}
|
|
|
|
findById(id_equipo_programa: number) {
|
|
return this.informacionEquipoProgramaView
|
|
.findOne({ where: { id_equipo_programa } })
|
|
.then((infoEquipoPrograma) => {
|
|
if (!infoEquipoPrograma)
|
|
throw new ConflictException('No existe este id equipo programa.');
|
|
return this.repository.create({
|
|
id_equipo_programa: infoEquipoPrograma.id_equipo_programa,
|
|
equipo: {
|
|
id_equipo: infoEquipoPrograma.id_equipo,
|
|
carrito: {
|
|
id_carrito: infoEquipoPrograma.id_carrito,
|
|
modulo: {
|
|
id_modulo: infoEquipoPrograma.id_modulo,
|
|
institucion: {
|
|
id_institucion: infoEquipoPrograma.id_institucion,
|
|
},
|
|
},
|
|
tipoCarrito: {
|
|
id_tipo_carrito: infoEquipoPrograma.id_tipo_carrito,
|
|
},
|
|
},
|
|
},
|
|
programa: { id_programa: infoEquipoPrograma.id_programa },
|
|
});
|
|
});
|
|
}
|
|
|
|
findByEquipoPrograma(equipo: Equipo, programa: Programa | Partial<Programa>) {
|
|
return this.repository.findOne({ where: { equipo, programa } });
|
|
}
|
|
}
|