optimizacion modulo funcion existeModulo
This commit is contained in:
parent
2186becc1e
commit
c5bd685683
@ -5,6 +5,7 @@ import {
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { Institucion } from 'src/institucion/entity/institucion.entity';
|
||||
import { Modulo } from './entity/modulo.entity';
|
||||
import { InstitucionService } from '../institucion/institucion.service';
|
||||
|
||||
@ -18,21 +19,32 @@ export class ModuloService {
|
||||
async create(id_institucion: number, modulo: string) {
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
|
||||
return this.existeModulo(institucion, modulo)
|
||||
.then(() =>
|
||||
this.repository.save(
|
||||
this.repository.create({
|
||||
institucion,
|
||||
modulo,
|
||||
}),
|
||||
),
|
||||
)
|
||||
.then((_) => ({ message: 'Se creo correctamente el módulo.' }));
|
||||
}
|
||||
|
||||
async existeModulo(id_institucion: number | Institucion, modulo: string) {
|
||||
const institucion =
|
||||
typeof id_institucion === 'number'
|
||||
? await this.institucionService.findById(id_institucion)
|
||||
: id_institucion;
|
||||
|
||||
return this.repository
|
||||
.findOne({ modulo, institucion })
|
||||
.findOne({ institucion, modulo })
|
||||
.then((existeModulo) => {
|
||||
if (existeModulo)
|
||||
throw new ConflictException(
|
||||
'Ya existe un módulo con este nombre, intente con otro nombre.',
|
||||
);
|
||||
return this.repository.save(
|
||||
this.repository.create({
|
||||
institucion,
|
||||
modulo,
|
||||
}),
|
||||
);
|
||||
})
|
||||
.then((_) => ({ message: 'Se creo correctamente el módulo.' }));
|
||||
});
|
||||
}
|
||||
|
||||
findAll() {
|
||||
@ -56,17 +68,7 @@ export class ModuloService {
|
||||
return this.findById(attrs.id_modulo)
|
||||
.then(async (modulo) => {
|
||||
if (attrs.modulo)
|
||||
await this.repository
|
||||
.findOne({
|
||||
modulo: attrs.modulo,
|
||||
institucion: modulo.institucion,
|
||||
})
|
||||
.then((existeModulo) => {
|
||||
if (existeModulo)
|
||||
throw new ConflictException(
|
||||
'Ya existe un módulo con este nombre, intente con otro nombre.',
|
||||
);
|
||||
});
|
||||
await this.existeModulo(modulo.institucion, attrs.modulo);
|
||||
Object.assign(modulo, attrs);
|
||||
return this.repository.save(modulo);
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user