136 lines
4.6 KiB
TypeScript
136 lines
4.6 KiB
TypeScript
import {
|
|
ConflictException,
|
|
ForbiddenException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { CarreraPrograma } from './entity/carrera-programa.entity';
|
|
import { Institucion } from '../institucion/entity/institucion.entity';
|
|
import { Operador } from '../operador/entity/operador.entity';
|
|
import { Usuario } from '../usuario/entity/usuario.entity';
|
|
import { InstitucionService } from '../institucion/institucion.service';
|
|
import { InstitucionCarreraService } from '../institucion-carrera/institucion-carrera.service';
|
|
import { InstitucionProgramaService } from '../institucion-programa/institucion-programa.service';
|
|
import { InstitucionUsuarioService } from '../institucion-usuario/institucion-usuario.service';
|
|
|
|
@Injectable()
|
|
export class CarreraProgramaService {
|
|
constructor(
|
|
@InjectRepository(CarreraPrograma)
|
|
private repository: Repository<CarreraPrograma>,
|
|
private institucionService: InstitucionService,
|
|
private institucionCarreraService: InstitucionCarreraService,
|
|
private institucionProgramaService: InstitucionProgramaService,
|
|
private institucionUsuarioService: InstitucionUsuarioService,
|
|
) {}
|
|
|
|
async create(
|
|
admin: Operador,
|
|
id_institucion_carrera: number,
|
|
id_programa: number,
|
|
): Promise<{ message: string }> {
|
|
const institucionCarrera = await this.institucionCarreraService.findById(
|
|
id_institucion_carrera,
|
|
);
|
|
const programa = await this.institucionProgramaService.findProgramaById(
|
|
id_programa,
|
|
);
|
|
|
|
// Validamos que la institución carrera le pertenezca al admin
|
|
if (
|
|
admin.institucion.id_institucion !=
|
|
institucionCarrera.institucion.id_institucion
|
|
)
|
|
throw new ForbiddenException(
|
|
'Esta carrera no pertenece a tu institución.',
|
|
);
|
|
// Busco un registro con los datos mandados del client
|
|
return this.repository
|
|
.findOne({
|
|
select: ['id_carrera_programa'],
|
|
where: { institucionCarrera, programa },
|
|
})
|
|
.then((existeCarretaPrograma) => {
|
|
// Si encuentro uno saco error
|
|
if (existeCarretaPrograma)
|
|
throw new ConflictException(
|
|
'Ya se asignó este programa a esta carrera.',
|
|
);
|
|
// Creo y guardo registro
|
|
return this.repository.save(
|
|
this.repository.create({ institucionCarrera, programa }),
|
|
);
|
|
})
|
|
.then((_) => ({
|
|
message: 'Se asignó correctamente el programa a la carrera.',
|
|
}));
|
|
}
|
|
|
|
delete(
|
|
admin: Operador,
|
|
id_carrera_programa: number,
|
|
): Promise<{ message: string }> {
|
|
return this.findById(id_carrera_programa)
|
|
.then((carreraPrograma) => {
|
|
// Validamos que la carrera programa pertenezca a la institución del admin
|
|
if (
|
|
admin.institucion.id_institucion !=
|
|
carreraPrograma.institucionCarrera.institucion.id_institucion
|
|
)
|
|
throw new ForbiddenException(
|
|
'No puedes eliminar esta asociación porque no pertenece a tu institución.',
|
|
);
|
|
// Eliminamos el registro
|
|
return this.repository.remove(carreraPrograma);
|
|
})
|
|
.then((_) => ({ message: 'Se eliminó correctamente esta asociación.' }));
|
|
}
|
|
|
|
findAllByInstitucion(institucion: Institucion): Promise<CarreraPrograma[]> {
|
|
return this.repository.find({
|
|
join: {
|
|
alias: 'cp',
|
|
innerJoinAndSelect: {
|
|
ic: 'cp.institucionCarrera',
|
|
c: 'ic.carrera',
|
|
i: 'ic.institucion',
|
|
},
|
|
},
|
|
where: { institucionCarrera: { institucion } },
|
|
});
|
|
}
|
|
|
|
findById(id_carrera_programa: number): Promise<CarreraPrograma> {
|
|
return this.repository
|
|
.findOne({
|
|
select: ['id_carrera_programa'],
|
|
where: { id_carrera_programa },
|
|
})
|
|
.then((carreraPrograma) => {
|
|
if (!carreraPrograma)
|
|
throw new NotFoundException('No existe este id carrera programa.');
|
|
return carreraPrograma;
|
|
});
|
|
}
|
|
|
|
async findAllOfUsuario(
|
|
usuario: Usuario,
|
|
id_institucion: number,
|
|
): Promise<CarreraPrograma[]> {
|
|
const institucion = await this.institucionService.findById(id_institucion);
|
|
|
|
return this.institucionUsuarioService
|
|
.findByIdUsuarioIdInstitucion(usuario, institucion)
|
|
.then((institucionUsuario) => {
|
|
// Validamos que el usuario pertenezca a la institución de la información que solicita
|
|
if (!institucionUsuario)
|
|
throw new ForbiddenException(
|
|
'No puedes acceder a esta información porque no perteneces a esta institución.',
|
|
);
|
|
return this.findAllByInstitucion(institucion);
|
|
});
|
|
}
|
|
}
|