erp-construccion-backend/dist/modules/hse/services/capacitacion.service.js

108 lines
3.4 KiB
JavaScript

"use strict";
/**
* CapacitacionService - Servicio para catálogo de capacitaciones HSE
*
* Gestión de capacitaciones con CRUD y filtros.
*
* @module HSE
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CapacitacionService = void 0;
class CapacitacionService {
repository;
constructor(repository) {
this.repository = repository;
}
async findAll(ctx, filters = {}, page = 1, limit = 20) {
const skip = (page - 1) * limit;
const queryBuilder = this.repository
.createQueryBuilder('capacitacion')
.where('capacitacion.tenant_id = :tenantId', { tenantId: ctx.tenantId });
if (filters.tipo) {
queryBuilder.andWhere('capacitacion.tipo = :tipo', { tipo: filters.tipo });
}
if (filters.activo !== undefined) {
queryBuilder.andWhere('capacitacion.activo = :activo', { activo: filters.activo });
}
if (filters.search) {
queryBuilder.andWhere('(capacitacion.codigo ILIKE :search OR capacitacion.nombre ILIKE :search)', { search: `%${filters.search}%` });
}
queryBuilder
.orderBy('capacitacion.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,
},
});
}
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(`Capacitacion with codigo ${dto.codigo} already exists`);
}
const capacitacion = this.repository.create({
tenantId: ctx.tenantId,
codigo: dto.codigo,
nombre: dto.nombre,
descripcion: dto.descripcion,
tipo: dto.tipo,
duracionHoras: dto.duracionHoras || 1,
vigenciaMeses: dto.vigenciaMeses,
requiereEvaluacion: dto.requiereEvaluacion || false,
calificacionMinima: dto.calificacionMinima,
activo: true,
});
return this.repository.save(capacitacion);
}
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 getByTipo(ctx, tipo) {
return this.repository.find({
where: {
tenantId: ctx.tenantId,
tipo,
activo: true,
},
order: { nombre: 'ASC' },
});
}
}
exports.CapacitacionService = CapacitacionService;
//# sourceMappingURL=capacitacion.service.js.map