diff --git a/src/institucion-programa/institucion-programa.service.ts b/src/institucion-programa/institucion-programa.service.ts index 61f1973..7fff20d 100644 --- a/src/institucion-programa/institucion-programa.service.ts +++ b/src/institucion-programa/institucion-programa.service.ts @@ -21,19 +21,22 @@ export class InstitucionProgramaService { ) {} create(programa: string) { + // Busco un programa con ese nombre return this.programaRepository .findOne({ where: { programa } }) .then((existePrograma) => { + // Saco error si existe if (existePrograma) throw new ConflictException('Ya existe este programa.'); + // Creo y guardo el registro return this.programaRepository.save( this.programaRepository.create({ programa }), ); }) .then(async (programa) => { - const instituciones = - await this.institucionService.findAll(); + const instituciones = await this.institucionService.findAll(); + // Le asigno el nuevo programa a todas las instituciones for (let i = 0; i < instituciones.length; i++) await this.institucionProgramaRepository.save( this.institucionProgramaRepository.create({ @@ -111,6 +114,8 @@ export class InstitucionProgramaService { update(admin: Operador, attrs: Partial) { return this.findById(attrs.id_institucion_programa) .then((institucionPrograma) => { + // Valido que la institucionPrograma le pertenezca al operador que + // realiza esta acción if ( admin.institucion.id_institucion != institucionPrograma.institucion.id_institucion @@ -118,11 +123,11 @@ export class InstitucionProgramaService { throw new ConflictException( 'No puedes actualizar la información de este software porque no le pertenece a tu institución.', ); + // Asigno valores enviados al objeto Object.assign(institucionPrograma, attrs); + // Guardar return this.institucionProgramaRepository.save(institucionPrograma); }) - .then((_) => ({ - message: 'Se guardaron los cambios correctamente.', - })); + .then((_) => ({ message: 'Se guardaron los cambios correctamente.' })); } } diff --git a/src/institucion-tipo-carrito/institucion-tipo-carrito.service.ts b/src/institucion-tipo-carrito/institucion-tipo-carrito.service.ts index 90b7f1f..cd2fad2 100644 --- a/src/institucion-tipo-carrito/institucion-tipo-carrito.service.ts +++ b/src/institucion-tipo-carrito/institucion-tipo-carrito.service.ts @@ -21,9 +21,11 @@ export class InstitucionTipoCarritoService { ) {} create(letra: string, tipo_carrito: string) { + // Busco un tipo de carrito con ese nombre o letra return this.tipoCarritoRepository .findOne({ where: [{ letra }, { tipo_carrito }] }) .then((existeTipoCarrito) => { + // Saco error si existe if (existeTipoCarrito) { if (letra === existeTipoCarrito.letra) throw new ConflictException( @@ -34,14 +36,15 @@ export class InstitucionTipoCarritoService { 'Ya existe un tipo carrito con este nombre, intente con otro nombre.', ); } + // Creo y guardo el registros return this.tipoCarritoRepository.save( this.tipoCarritoRepository.create({ letra, tipo_carrito }), ); }) .then(async (tipoCarrito) => { - const instituciones = - await this.institucionService.findAll(); + const instituciones = await this.institucionService.findAll(); + // Le asigno el nuevo tipo de carrito a todas las instituciones for (let i = 0; i < instituciones.length; i++) await this.institucionTipoCarritoRepository.save( this.institucionTipoCarritoRepository.create({ @@ -117,6 +120,8 @@ export class InstitucionTipoCarritoService { attrs.id_institucion_tipo_carrito, ) .then((institucionTipoCarrito) => { + // Valido que la institucionTipoCarrito le pertenezca al operador que + // realiza esta acción if ( admin.institucion.id_institucion != institucionTipoCarrito.institucion.id_institucion @@ -124,13 +129,13 @@ export class InstitucionTipoCarritoService { throw new ConflictException( 'No puedes actualizar la información de este tipo de carrito porque no le pertenece a tu institución.', ); + // Asigno valores enviados al objeto Object.assign(institucionTipoCarrito, attrs); + // Guardar return this.institucionTipoCarritoRepository.save( institucionTipoCarrito, ); }) - .then((_) => ({ - message: 'Se guardaron los cambios correctamente.', - })); + .then((_) => ({ message: 'Se guardaron los cambios correctamente.' })); } } diff --git a/src/institucion-tipo-entrada/institucion-tipo-entrada.service.ts b/src/institucion-tipo-entrada/institucion-tipo-entrada.service.ts index 60f97e8..8b5ec71 100644 --- a/src/institucion-tipo-entrada/institucion-tipo-entrada.service.ts +++ b/src/institucion-tipo-entrada/institucion-tipo-entrada.service.ts @@ -21,20 +21,22 @@ export class InstitucionTipoEntradaService { ) {} async create(tipo_entrada: string) { + // Busco un tipo de entrada con ese nombre return this.tipoEntradaRepository .findOne({ where: { tipo_entrada } }) .then((existeTipoEntrada) => { + // Saco error si existe if (existeTipoEntrada) throw new ConflictException('Ya existe este tipo de entrada.'); + // Creo y guardo el registro return this.tipoEntradaRepository.save( - this.tipoEntradaRepository.create({ - tipo_entrada, - }), + this.tipoEntradaRepository.create({ tipo_entrada }), ); }) .then(async (tipoEntrada) => { const instituciones = await this.institucionService.findAll(); + // Le asigno el nuevo tipo de entrada a todas las instituciones for (let i = 0; i < instituciones.length; i++) await this.institucionTipoEntradaRepository.save( this.institucionTipoEntradaRepository.create({ @@ -42,9 +44,7 @@ export class InstitucionTipoEntradaService { tipoEntrada, }), ); - return { - message: 'Se creó correctamente un nuevo tipo de entrada.', - }; + return { message: 'Se creó correctamente un nuevo tipo de entrada.' }; }); } @@ -110,6 +110,8 @@ export class InstitucionTipoEntradaService { update(admin: Operador, attrs: Partial) { return this.findById(attrs.id_institucion_tipo_entrada) .then((institucionTipoEntrada) => { + // Valido que la institucionTipoEntrada le pertenezca al operador que + // realiza esta acción if ( admin.institucion.id_institucion != institucionTipoEntrada.institucion.id_institucion @@ -117,7 +119,9 @@ export class InstitucionTipoEntradaService { throw new ConflictException( 'No puedes actualizar la información de este tipo de carrito porque no le pertenece a tu institución.', ); + // Asigno valores enviados al objeto Object.assign(institucionTipoEntrada, attrs); + // Guardar return this.institucionTipoEntradaRepository.save( institucionTipoEntrada, );