Files
pcpuma_unam_api/src/modulo/modulo.service.ts
T

109 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-04-16 13:05:27 -05:00
import {
ConflictException,
Injectable,
NotFoundException,
} from '@nestjs/common';
2022-04-04 20:02:54 -05:00
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
2022-05-04 11:22:17 -05:00
import { Institucion } from '../institucion/entity/institucion.entity';
2022-04-16 14:37:17 -05:00
import { Modulo } from './entity/modulo.entity';
2022-04-17 21:55:00 -05:00
import { InstitucionService } from '../institucion/institucion.service';
2022-04-04 20:02:54 -05:00
@Injectable()
export class ModuloService {
constructor(
2022-04-18 16:55:19 -05:00
@InjectRepository(Modulo) private repository: Repository<Modulo>,
private institucionService: InstitucionService,
2022-04-04 20:02:54 -05:00
) {}
async create(id_institucion: number, modulo: string) {
const institucion = await this.institucionService.findById(id_institucion);
2022-05-02 15:50:00 -05:00
return this.existeModulo(institucion, modulo)
.then(() =>
this.repository.save(
this.repository.create({
institucion,
modulo,
}),
),
)
2022-06-23 23:04:47 -05:00
.then((_) => ({
message: 'Se creó correctamente un nuevo módulo.',
}));
2022-05-02 15:50:00 -05:00
}
async existeModulo(id_institucion: number | Institucion, modulo: string) {
const institucion =
typeof id_institucion === 'number'
? await this.institucionService.findById(id_institucion)
: id_institucion;
2022-04-18 16:55:19 -05:00
return this.repository
2022-05-02 15:50:00 -05:00
.findOne({ institucion, modulo })
.then((existeModulo) => {
if (existeModulo)
throw new ConflictException(
2022-06-23 23:04:47 -05:00
'Ya existe un módulo en esta institución con este nombre, intente con otro nombre.',
);
2022-05-02 15:50:00 -05:00
});
2022-04-04 20:02:54 -05:00
}
2022-04-16 13:05:27 -05:00
2022-07-19 08:25:58 -05:00
findAllByIdInstitucion(id_institucion: number, activos = false) {
2022-04-16 14:37:17 -05:00
return this.institucionService
.findById(id_institucion)
2022-07-19 08:25:58 -05:00
.then((institucion) => {
const query = this.repository
2022-06-24 19:15:49 -05:00
.createQueryBuilder('m')
.innerJoinAndSelect(
'm.institucion',
'i',
'i.id_institucion = :id_institucion',
{ id_institucion: institucion.id_institucion },
)
.orderBy('i.institucion')
2022-07-19 08:25:58 -05:00
.orderBy('m.modulo');
if (activos) query.andWhere('m.activo = 1');
return query.getMany();
});
2022-04-16 14:37:17 -05:00
}
findById(id_modulo: number) {
2022-04-18 16:55:19 -05:00
return this.repository.findOne({ id_modulo }).then((modulo) => {
2022-06-26 12:47:00 -05:00
if (!modulo) throw new NotFoundException('No existe este id módulo.');
2022-04-16 14:37:17 -05:00
return modulo;
});
}
2022-06-06 15:00:36 -05:00
async findModulo(
id_institucion: number | Institucion,
modulo: string,
validarNoExiste = true,
) {
const institucion =
typeof id_institucion === 'number'
? await this.institucionService.findById(id_institucion)
: id_institucion;
return this.repository.findOne({ institucion, modulo }).then((modulo) => {
if (validarNoExiste && !modulo)
throw new ConflictException('No existe este módulo.');
return modulo;
});
}
2022-04-21 22:08:59 -05:00
update(attrs: Partial<Modulo>) {
return this.findById(attrs.id_modulo)
.then(async (modulo) => {
if (attrs.modulo)
2022-05-02 15:50:00 -05:00
await this.existeModulo(modulo.institucion, attrs.modulo);
2022-04-16 13:05:27 -05:00
Object.assign(modulo, attrs);
2022-04-18 16:55:19 -05:00
return this.repository.save(modulo);
2022-04-16 13:07:55 -05:00
})
2022-04-18 16:55:19 -05:00
.then((_) => ({
2022-06-23 23:04:47 -05:00
message: 'Se guardaron los cambios correctamente.',
2022-04-16 13:07:55 -05:00
}));
2022-04-16 13:05:27 -05:00
}
2022-04-04 20:02:54 -05:00
}