117 lines
3.6 KiB
TypeScript
117 lines
3.6 KiB
TypeScript
import {
|
|
ConflictException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { InstitucionTipoCarrito } from './entity/institucion-tipo-carrito.entity';
|
|
import { TipoCarrito } from './entity/tipo-carrito.entity';
|
|
import { InstitucionService } from 'src/institucion/institucion.service';
|
|
|
|
@Injectable()
|
|
export class InstitucionTipoCarritoService {
|
|
constructor(
|
|
@InjectRepository(TipoCarrito)
|
|
private repositoryTipoCarrito: Repository<TipoCarrito>,
|
|
@InjectRepository(InstitucionTipoCarrito)
|
|
private repositoryInstitucionTipoCarrito: Repository<InstitucionTipoCarrito>,
|
|
private institucionService: InstitucionService,
|
|
) {}
|
|
|
|
create(letra: string, tipo_carrito: string) {
|
|
/** Checar que no este usando la letra ni el nombre */
|
|
return this.repositoryTipoCarrito
|
|
.findOne({ letra, tipo_carrito })
|
|
.then((existeTipoCarrito) => {
|
|
if (existeTipoCarrito) {
|
|
if (letra === existeTipoCarrito.letra)
|
|
throw new ConflictException(
|
|
'Ya existe un tipo carrito con esta letra, intente con otra letra.',
|
|
);
|
|
else
|
|
throw new ConflictException(
|
|
'Ya existe un tipo carrito con este nombre, intente con otro nombre.',
|
|
);
|
|
}
|
|
return this.repositoryTipoCarrito.save(
|
|
this.repositoryTipoCarrito.create({ letra, tipo_carrito }),
|
|
);
|
|
})
|
|
.then(async (tipoCarrito) => {
|
|
const instituciones = await this.institucionService.findAll();
|
|
|
|
for (let i = 0; i < instituciones.length; i++)
|
|
await this.repositoryInstitucionTipoCarrito.save(
|
|
this.repositoryInstitucionTipoCarrito.create({
|
|
tipoCarrito,
|
|
institucion: instituciones[i],
|
|
}),
|
|
);
|
|
return { message: 'Se creo correctamente el tipo carrito.' };
|
|
});
|
|
}
|
|
|
|
findAll() {
|
|
return this.repositoryTipoCarrito.find();
|
|
}
|
|
|
|
findAllByIdInstitucion(id_institucion: number) {
|
|
return this.institucionService
|
|
.findById(id_institucion)
|
|
.then((institucion) =>
|
|
this.repositoryInstitucionTipoCarrito.find({ institucion }),
|
|
);
|
|
}
|
|
|
|
findAllByIdInstitucionMostar(id_institucion: number) {
|
|
return this.institucionService
|
|
.findById(id_institucion)
|
|
.then((institucion) =>
|
|
this.repositoryInstitucionTipoCarrito.find({
|
|
institucion,
|
|
mostrar: true,
|
|
}),
|
|
);
|
|
}
|
|
|
|
findById(id_institucion_tipo_carrito) {
|
|
return this.repositoryInstitucionTipoCarrito
|
|
.findOne({
|
|
id_institucion_tipo_carrito,
|
|
})
|
|
.then((institucionTipoCarrito) => {
|
|
if (!institucionTipoCarrito)
|
|
throw new NotFoundException(
|
|
'No existe esta institucion tipo carrito.',
|
|
);
|
|
return institucionTipoCarrito;
|
|
});
|
|
}
|
|
|
|
findByIdTipoCarito(id_tipo_carrito: number) {
|
|
return this.repositoryTipoCarrito
|
|
.findOne({ id_tipo_carrito })
|
|
.then((tipoCarrito) => {
|
|
if (!tipoCarrito)
|
|
throw new NotFoundException(
|
|
'No existe este tipo carrito.',
|
|
);
|
|
return tipoCarrito;
|
|
})
|
|
}
|
|
|
|
update(attrs: Partial<InstitucionTipoCarrito>) {
|
|
return this.findById(attrs.id_institucion_tipo_carrito)
|
|
.then((institucionTipoCarrito) => {
|
|
Object.assign(institucionTipoCarrito, attrs);
|
|
return this.repositoryInstitucionTipoCarrito.save(
|
|
institucionTipoCarrito,
|
|
);
|
|
})
|
|
.then((_) => ({
|
|
message: 'Se actualió correctamente la institucion tipo carrtio.',
|
|
}));
|
|
}
|
|
}
|