117 lines
3.5 KiB
JavaScript
117 lines
3.5 KiB
JavaScript
"use strict";
|
|
/**
|
|
* ManzanaService - Gestión de manzanas (bloques)
|
|
*
|
|
* CRUD de manzanas con soporte multi-tenant.
|
|
*
|
|
* @module Construction
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ManzanaService = void 0;
|
|
const typeorm_1 = require("typeorm");
|
|
class ManzanaService {
|
|
repository;
|
|
constructor(repository) {
|
|
this.repository = repository;
|
|
}
|
|
/**
|
|
* Listar manzanas
|
|
*/
|
|
async findAll(options) {
|
|
const { tenantId, etapaId, page = 1, limit = 20, search } = options;
|
|
const query = this.repository
|
|
.createQueryBuilder('m')
|
|
.where('m.tenant_id = :tenantId', { tenantId })
|
|
.andWhere('m.deleted_at IS NULL');
|
|
if (etapaId) {
|
|
query.andWhere('m.etapa_id = :etapaId', { etapaId });
|
|
}
|
|
if (search) {
|
|
query.andWhere('(m.code ILIKE :search OR m.name ILIKE :search)', { search: `%${search}%` });
|
|
}
|
|
const total = await query.getCount();
|
|
const items = await query
|
|
.skip((page - 1) * limit)
|
|
.take(limit)
|
|
.orderBy('m.code', 'ASC')
|
|
.getMany();
|
|
return { items, total };
|
|
}
|
|
/**
|
|
* Obtener manzana por ID
|
|
*/
|
|
async findById(id, tenantId) {
|
|
return this.repository.findOne({
|
|
where: { id, tenantId, deletedAt: (0, typeorm_1.IsNull)() },
|
|
relations: ['lotes'],
|
|
});
|
|
}
|
|
/**
|
|
* Obtener manzana por código dentro de una etapa
|
|
*/
|
|
async findByCode(code, etapaId, tenantId) {
|
|
return this.repository.findOne({
|
|
where: { code, etapaId, tenantId, deletedAt: (0, typeorm_1.IsNull)() },
|
|
});
|
|
}
|
|
/**
|
|
* Crear manzana
|
|
*/
|
|
async create(tenantId, dto, createdBy) {
|
|
const existing = await this.findByCode(dto.code, dto.etapaId, tenantId);
|
|
if (existing) {
|
|
throw new Error('Block code already exists in this stage');
|
|
}
|
|
return this.repository.save(this.repository.create({
|
|
tenantId,
|
|
...dto,
|
|
createdBy,
|
|
}));
|
|
}
|
|
/**
|
|
* Actualizar manzana
|
|
*/
|
|
async update(id, tenantId, dto, updatedBy) {
|
|
const manzana = await this.findById(id, tenantId);
|
|
if (!manzana) {
|
|
throw new Error('Block not found');
|
|
}
|
|
// Verificar código único si se está cambiando
|
|
if (dto.code && dto.code !== manzana.code) {
|
|
const existing = await this.findByCode(dto.code, manzana.etapaId, tenantId);
|
|
if (existing) {
|
|
throw new Error('Block code already exists in this stage');
|
|
}
|
|
}
|
|
await this.repository.update(id, {
|
|
...dto,
|
|
updatedBy,
|
|
updatedAt: new Date(),
|
|
});
|
|
return this.findById(id, tenantId);
|
|
}
|
|
/**
|
|
* Eliminar manzana (soft delete)
|
|
*/
|
|
async delete(id, tenantId, _deletedBy) {
|
|
const manzana = await this.findById(id, tenantId);
|
|
if (!manzana) {
|
|
throw new Error('Block not found');
|
|
}
|
|
// TODO: Verificar si tiene lotes antes de eliminar
|
|
await this.repository.update(id, {
|
|
deletedAt: new Date(),
|
|
});
|
|
}
|
|
/**
|
|
* Obtener manzanas por etapa
|
|
*/
|
|
async findByEtapa(etapaId, tenantId) {
|
|
return this.repository.find({
|
|
where: { etapaId, tenantId, deletedAt: (0, typeorm_1.IsNull)() },
|
|
order: { code: 'ASC' },
|
|
});
|
|
}
|
|
}
|
|
exports.ManzanaService = ManzanaService;
|
|
//# sourceMappingURL=manzana.service.js.map
|