136 lines
4.6 KiB
TypeScript
136 lines
4.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 { Operador } from 'src/operador/entity/operador.entity';
|
|
import { TipoCarrito } from './entity/tipo-carrito.entity';
|
|
import { InstitucionService } from '../institucion/institucion.service';
|
|
|
|
@Injectable()
|
|
export class InstitucionTipoCarritoService {
|
|
constructor(
|
|
@InjectRepository(TipoCarrito)
|
|
private tipoCarritoRepository: Repository<TipoCarrito>,
|
|
@InjectRepository(InstitucionTipoCarrito)
|
|
private institucionTipoCarritoRepository: Repository<InstitucionTipoCarrito>,
|
|
private institucionService: InstitucionService,
|
|
) {}
|
|
|
|
create(letra: string, tipo_carrito: string) {
|
|
return this.tipoCarritoRepository
|
|
.findOne({ where: [{ 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.tipoCarritoRepository.save(
|
|
this.tipoCarritoRepository.create({ letra, tipo_carrito }),
|
|
);
|
|
})
|
|
.then(async (tipoCarrito) => {
|
|
const instituciones = await this.institucionService.findAll();
|
|
|
|
for (let i = 0; i < instituciones.length; i++)
|
|
await this.institucionTipoCarritoRepository.save(
|
|
this.institucionTipoCarritoRepository.create({
|
|
institucion: instituciones[i],
|
|
tipoCarrito,
|
|
}),
|
|
);
|
|
return { message: 'Se creó correctamente un nuevo tipo de carrito.' };
|
|
});
|
|
}
|
|
|
|
findAll() {
|
|
return this.tipoCarritoRepository.find({ order: { tipo_carrito: 'ASC' } });
|
|
}
|
|
|
|
async findAllByIdInstitucion(id_institucion: number, mostrar = false) {
|
|
const institucion = await this.institucionService.findById(id_institucion);
|
|
const query = this.institucionTipoCarritoRepository
|
|
.createQueryBuilder('itc')
|
|
.innerJoinAndSelect(
|
|
'itc.institucion',
|
|
'i',
|
|
'i.id_institucion = :id_institucion',
|
|
{ id_institucion: institucion.id_institucion },
|
|
)
|
|
.innerJoinAndSelect('itc.tipoCarrito', 'tc')
|
|
.orderBy('tc.tipo_carrito');
|
|
|
|
if (mostrar) query.andWhere('itc.mostrar = 1');
|
|
return query.getMany();
|
|
}
|
|
|
|
findInstitucionTipoCarritoById(id_institucion_tipo_carrito) {
|
|
return this.institucionTipoCarritoRepository
|
|
.findOne({
|
|
join: {
|
|
alias: 'itc',
|
|
innerJoinAndSelect: { i: 'itc.institucion', tc: 'itc.tipoCarrito' },
|
|
},
|
|
where: { id_institucion_tipo_carrito },
|
|
})
|
|
.then((institucionTipoCarrito) => {
|
|
if (!institucionTipoCarrito)
|
|
throw new NotFoundException(
|
|
'No existe este id institución tipo carrito.',
|
|
);
|
|
return institucionTipoCarrito;
|
|
});
|
|
}
|
|
|
|
findTipoCarritoById(id_tipo_carrito: number) {
|
|
return this.tipoCarritoRepository
|
|
.findOne({ id_tipo_carrito })
|
|
.then((tipoCarrito) => {
|
|
if (!tipoCarrito)
|
|
throw new NotFoundException('No existe este id tipo carrito.');
|
|
return tipoCarrito;
|
|
});
|
|
}
|
|
|
|
findTipoCarritoByTipoCarrito(tipo_carrito: string, validarNoExiste = true) {
|
|
return this.tipoCarritoRepository
|
|
.findOne({ tipo_carrito })
|
|
.then((tipoCarrito) => {
|
|
if (validarNoExiste && !tipoCarrito)
|
|
throw new NotFoundException('No existe este tipo de carrito.');
|
|
return tipoCarrito;
|
|
});
|
|
}
|
|
|
|
update(admin: Operador, attrs: Partial<InstitucionTipoCarrito>) {
|
|
return this.findInstitucionTipoCarritoById(
|
|
attrs.id_institucion_tipo_carrito,
|
|
)
|
|
.then((institucionTipoCarrito) => {
|
|
if (
|
|
admin.institucion.id_institucion !=
|
|
institucionTipoCarrito.institucion.id_institucion
|
|
)
|
|
throw new ConflictException(
|
|
'No puedes actualizar la información de este tipo de carrito porque no le corresponde a tu institución.',
|
|
);
|
|
Object.assign(institucionTipoCarrito, attrs);
|
|
return this.institucionTipoCarritoRepository.save(
|
|
institucionTipoCarrito,
|
|
);
|
|
})
|
|
.then((_) => ({
|
|
message: 'Se guardaron los cambios correctamente.',
|
|
}));
|
|
}
|
|
}
|