67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import {
|
|
ConflictException,
|
|
Injectable,
|
|
NotFoundException,
|
|
} from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { TipoUsuario } from './entity/tipo-usuario.entity';
|
|
|
|
@Injectable()
|
|
export class TipoUsuarioService {
|
|
constructor(
|
|
@InjectRepository(TipoUsuario) private repository: Repository<TipoUsuario>,
|
|
) {}
|
|
|
|
create(tipo_usuario: string) {
|
|
// Buscamos un tipo de usuario con ese nombre
|
|
return this.repository
|
|
.findOne({ where: { tipo_usuario } })
|
|
.then((existeTipoUsuario) => {
|
|
// Saco error si existe
|
|
if (existeTipoUsuario)
|
|
throw new ConflictException('Ya existe este tipo usuario');
|
|
// Creamos y guardamos un registro
|
|
return this.repository.save(this.repository.create({ tipo_usuario }));
|
|
})
|
|
.then((_) => ({
|
|
message: 'Se creó correctamente un nuevo tipo de usuario.',
|
|
}));
|
|
}
|
|
|
|
crearTipoUsuario(id_tipo_usuario: number) {
|
|
return this.repository.create({ id_tipo_usuario });
|
|
}
|
|
|
|
findAll(informacion?: string) {
|
|
const query = this.repository
|
|
.createQueryBuilder('tu')
|
|
.where('tu.id_tipo_usuario != 1 AND tu.id_tipo_usuario != 2')
|
|
.orderBy('tu.tipo_usuario');
|
|
|
|
if (informacion && informacion === 'operador')
|
|
query.andWhere('tu.id_tipo_usuario > 4');
|
|
return query.getMany();
|
|
}
|
|
|
|
findById(id_tipo_usuario: number) {
|
|
return this.repository
|
|
.findOne({ where: { id_tipo_usuario } })
|
|
.then((tipoUsuario) => {
|
|
if (!tipoUsuario)
|
|
throw new NotFoundException('No existe este id tipo usuario.');
|
|
return tipoUsuario;
|
|
});
|
|
}
|
|
|
|
findByTipoUsuario(tipo_usuario: string, validarNoExiste = true) {
|
|
return this.repository
|
|
.findOne({ where: { tipo_usuario } })
|
|
.then((tipoUsuario) => {
|
|
if (validarNoExiste && !tipoUsuario)
|
|
throw new NotFoundException('No existe este tipo usuario');
|
|
return tipoUsuario;
|
|
});
|
|
}
|
|
}
|