import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository, ILike } from 'typeorm'; import { StructureEntity } from '../entities/structure.entity'; import { CreateStructureDto, UpdateStructureDto, StructureFiltersDto, } from '../dto/structure.dto'; @Injectable() export class StructuresService { constructor( @InjectRepository(StructureEntity) private readonly structureRepository: Repository, ) {} async findAll(tenantId: string, filters?: StructureFiltersDto): Promise { const where: any = { tenantId }; if (filters?.type) { where.type = filters.type; } if (filters?.isActive !== undefined) { where.isActive = filters.isActive; } if (filters?.search) { where.name = ILike(`%${filters.search}%`); } return this.structureRepository.find({ where, order: { createdAt: 'DESC' }, }); } async findOne(tenantId: string, id: string): Promise { const structure = await this.structureRepository.findOne({ where: { id, tenantId }, }); if (!structure) { throw new NotFoundException(`Structure with ID ${id} not found`); } return structure; } async create( tenantId: string, userId: string, dto: CreateStructureDto, ): Promise { const structure = this.structureRepository.create({ ...dto, tenantId, createdBy: userId, }); return this.structureRepository.save(structure); } async update( tenantId: string, id: string, dto: UpdateStructureDto, ): Promise { const structure = await this.findOne(tenantId, id); Object.assign(structure, dto); return this.structureRepository.save(structure); } async remove(tenantId: string, id: string): Promise { const structure = await this.findOne(tenantId, id); await this.structureRepository.remove(structure); } }