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