HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux ip-172-31-4-197 6.8.0-1036-aws #38~22.04.1-Ubuntu SMP Fri Aug 22 15:44:33 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/api-parametros/src/app/select/services/select.service.ts
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { CreateParametroDto } from '../../parametros/dto/create-parametro.dto';
import { Parametro } from '../../parametros/schemas/parametro.schema';
import { Proyecto } from '../../proyectos/schemas/proyecto.schema';
import { ResApiInterface } from '../../../shared/interfaces/res-api.interface';
import { handleValidationError } from '../../../shared/utils/error-handler';
import { handleMessageSucces } from '../../../shared/utils/message-handler';

@Injectable()
export class SelectService {
  constructor(
    @InjectModel(Proyecto.name) private proyectoModel: Model<Proyecto>,
    @InjectModel(Parametro.name)
    private readonly parametroModel: Model<Parametro>,
  ) {}
  async findSelectProyectos() {
    try {
      const totalRecordsPromise = await this.proyectoModel.countDocuments({
        deletedAt: { $in: null },
      });

      const proyectosPromise = this.proyectoModel
        .find({ deletedAt: { $in: null } }, 'nombre')
        .sort({ nombre: 1 })
        .exec();

      const [proyectos, totalRecords] = await Promise.all([
        proyectosPromise,
        totalRecordsPromise,
      ]);

      return handleMessageSucces(proyectos, 'findAll', totalRecords);
    } catch (error) {
      throw handleValidationError(error);
    }
  }

  async findSelectParametrosByTipoParametro(
    idTipoParametro: string,
  ): Promise<ResApiInterface<CreateParametroDto[]>> {
    try {
      const parametrosPromise = this.parametroModel
        .find(
          { deletedAt: { $in: null }, idTipoParametro: idTipoParametro },
          '_id codigo nombre icon.codigo posicion showMenu',
        )
        .sort({ nombre: 1 })
        .exec();

      const [parametros] = await Promise.all([parametrosPromise]);

      return handleMessageSucces(parametros, 'findAll');
    } catch (error) {
      throw handleValidationError(error);
    }
  }

  async findSelectParametrosByTipoParametroPadre(
    idTipoParametro: string,
    idPadre: string,
  ): Promise<ResApiInterface<CreateParametroDto[]>> {
    try {
      const parametrosPromise = this.parametroModel
        .find(
          {
            deletedAt: null,
            idTipoParametro: idTipoParametro,
            'padre._id': idPadre,
          },
          '_id codigo nombre icon.codigo posicion showMenu',
        )
        .sort({ nombre: 1 })
        .exec();

      const [parametros] = await Promise.all([parametrosPromise]);

      return handleMessageSucces(parametros, 'findAll');
    } catch (error) {
      throw handleValidationError(error);
    }
  }

  async findPadresByTipoParametro(
    idTipoParametro: string,
  ): Promise<ResApiInterface<any>> {
    try {
      // Paso 1: Obtener los parámetros
      const parametros = await this.parametroModel.find(
        { idTipoParametro },
        'padre',
      );

      // Paso 2: Utilizar un Set para almacenar los IDs únicos y un Map para almacenar los objetos
      const padresMap = new Map();

      parametros.forEach((param: any) => {
        if (param.padre) {
          const padre = {
            nombre: param.padre.nombre,
            _id: param.padre._id,
          };
          padresMap.set(padre._id, padre);
        }
      });

      // Paso 3: Convertir el Map a un array de objetos únicos
      const uniqueParents = Array.from(padresMap.values());

      uniqueParents.sort((a, b) => a.nombre.localeCompare(b.nombre));

      // Paso 4: Devolver los resultados
      return handleMessageSucces(uniqueParents, 'findAll');
    } catch (error) {
      throw handleValidationError(error);
    }
  }
}