import { Injectable } from '@nestjs/common'; import { EntityServiceImpl } from 'src/module/meta/service/entity-service-impl.service'; import { Repository } from 'typeorm'; import { InjectRepository } from '@nestjs/typeorm'; import { ListMasterItems } from '../../listmaster/entity/list-master-items.entity'; import { CommTemplate } from '../entity/comm-template.entity'; @Injectable() export class ActionTemplateMappingService extends EntityServiceImpl { constructor(@InjectRepository(ListMasterItems) private listMasterItemsRepo: Repository, @InjectRepository(CommTemplate) private commTemplateRepository: Repository, ) { super(); } async getTemplateByMode( stage_id: number, action_id: number, loggedInUser, mode?: number, ) { let stageAction = [] as any; if (action_id != 0) { const stageActionMappingRepo = this.reflectionHelper.getRepoService('StageActionMapping'); stageAction = await stageActionMappingRepo.find({ where: { action_id: action_id, }, }); } else { // generic action // get template by mode let modeId = mode; if (!modeId) { const rows = await this.listMasterItemsRepo.find({ where: { listtype: 'MOD', code: 'email', enterprise_id: loggedInUser.enterprise_id, }, }); modeId = rows?.[0]?.id ?? 0; } const commTemplates = await this.commTemplateRepository .createQueryBuilder('ct') .select([ 'ct.code AS value', 'ct.name AS label', 'ct.id AS id', ]) .where('ct.mode = :mode', { mode: String(modeId) }) .andWhere('ct.enterprise_id = :entId', { entId: Number(loggedInUser.enterprise_id), }) .andWhere('ct.level_type = :levelType', { levelType: loggedInUser.level_type, }) .andWhere('ct.level_id = :levelId', { levelId: String(loggedInUser.level_id), }) .andWhere((qb) => { const subQuery = qb .subQuery() .select('lmi.id') .from('frm_list_master_items', 'lmi') .where('lmi.code = :active', { active: 'STATUS_ACTIVE' }) .andWhere('lmi.enterprise_id = :entId', { entId: Number(loggedInUser.enterprise_id), }) .getQuery(); // ⭐ FIX: CAST ct.status TO INT return `(ct.status)::int IN ${subQuery}`; }) .getRawMany(); return commTemplates; } if (!stageAction?.length) return []; const stgActMappingId = stageAction[0].id; if (!mode) { const actionRepo = this.reflectionHelper.getRepoService('ActionEntity'); const getModeFromAction = await actionRepo.findOne({ where: { id: action_id, }, }); mode = getModeFromAction?.mode; } const actionResourceMappingRepo = this.reflectionHelper.getRepoService('ActionResourcesMapping'); const templates = await actionResourceMappingRepo.find({ where: { stg_act_mapping_id: stgActMappingId, }, }); const templateCodes = templates.map((t) => t.template_code); if (!templateCodes.length) return []; const commTemplateRepo = this.reflectionHelper.getRepoService('CommTemplate'); return await commTemplateRepo .createQueryBuilder('ct') .select([ 'ct.code AS value', 'ct.name AS label', 'ct.id AS id', ]) .innerJoin( 'frm_list_master_items', 'fmi', `fmi.id = ct.status::bigint AND fmi.code = 'STATUS_ACTIVE' AND fmi.enterprise_id::text = :entId`, { entId: String(loggedInUser.enterprise_id) } ) .where('ct.code IN (:...codes)', { codes: templateCodes }) .andWhere('ct.mode::text = :mode', { mode: String(mode) }) .andWhere('ct.level_type = :levelType', { levelType: loggedInUser.level_type, }) .andWhere('ct.level_id::text = :levelId', { levelId: String(loggedInUser.level_id), }) .getRawMany(); } }