erp-construccion-backend/dist/modules/hr/services/puesto.service.js

104 lines
3.2 KiB
JavaScript

"use strict";
/**
* PuestoService - Servicio para catálogo de puestos
*
* Gestión de puestos de trabajo con CRUD básico.
*
* @module HR
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.PuestoService = void 0;
class PuestoService {
repository;
constructor(repository) {
this.repository = repository;
}
async findAll(ctx, filters = {}, page = 1, limit = 20) {
const skip = (page - 1) * limit;
const queryBuilder = this.repository
.createQueryBuilder('puesto')
.where('puesto.tenant_id = :tenantId', { tenantId: ctx.tenantId });
if (filters.activo !== undefined) {
queryBuilder.andWhere('puesto.activo = :activo', { activo: filters.activo });
}
if (filters.nivelRiesgo) {
queryBuilder.andWhere('puesto.nivel_riesgo = :nivelRiesgo', { nivelRiesgo: filters.nivelRiesgo });
}
if (filters.search) {
queryBuilder.andWhere('(puesto.codigo ILIKE :search OR puesto.nombre ILIKE :search)', { search: `%${filters.search}%` });
}
queryBuilder
.orderBy('puesto.nombre', 'ASC')
.skip(skip)
.take(limit);
const [data, total] = await queryBuilder.getManyAndCount();
return {
data,
meta: {
total,
page,
limit,
totalPages: Math.ceil(total / limit),
},
};
}
async findById(ctx, id) {
return this.repository.findOne({
where: {
id,
tenantId: ctx.tenantId,
},
relations: ['empleados'],
});
}
async findByCodigo(ctx, codigo) {
return this.repository.findOne({
where: {
codigo,
tenantId: ctx.tenantId,
},
});
}
async create(ctx, dto) {
const existing = await this.findByCodigo(ctx, dto.codigo);
if (existing) {
throw new Error(`Puesto with codigo ${dto.codigo} already exists`);
}
const puesto = this.repository.create({
tenantId: ctx.tenantId,
codigo: dto.codigo,
nombre: dto.nombre,
descripcion: dto.descripcion,
nivelRiesgo: dto.nivelRiesgo,
requiereCapacitacionEspecial: dto.requiereCapacitacionEspecial || false,
activo: true,
});
return this.repository.save(puesto);
}
async update(ctx, id, dto) {
const existing = await this.findById(ctx, id);
if (!existing) {
return null;
}
const updated = this.repository.merge(existing, dto);
return this.repository.save(updated);
}
async toggleActive(ctx, id) {
const existing = await this.findById(ctx, id);
if (!existing) {
return null;
}
existing.activo = !existing.activo;
return this.repository.save(existing);
}
async getActiveCount(ctx) {
return this.repository.count({
where: {
tenantId: ctx.tenantId,
activo: true,
},
});
}
}
exports.PuestoService = PuestoService;
//# sourceMappingURL=puesto.service.js.map