import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { CommTemplate } from '../entity/comm-template.entity'; import { TemplateAttach } from '../entity/template-attach-mapper.entity'; import { MediaDataService } from 'src/module/meta/service/media-data.service'; import { ReflectionHelper } from '../../../utils/service/reflection-helper.service'; @Injectable() export class CommTemplateRepository { constructor( @InjectRepository(CommTemplate) private readonly commTemplateRepository: Repository, @InjectRepository(TemplateAttach) private readonly templateAttachEntity: Repository, private readonly mediaDataService: MediaDataService, private readonly reflectionHelper: ReflectionHelper, ) { } async getAllCommTemplate( entity_type: string, action_id?: number, loggedInUser?, ) { const { organization_id, level_id, level_type } = loggedInUser; let is_template: number | undefined; // 1️⃣ Only check action_id if provided if (action_id) { const actionRepo = this.reflectionHelper.getRepoService('ActionEntity'); const actionResult = await actionRepo.findOne({ where: { id: action_id, organization_id: organization_id, }, }); if (!actionResult) { throw new Error('Invalid action_id'); } is_template = actionResult.is_template; await this.commTemplateRepository.find({ where: {}, }); await this.commTemplateRepository .createQueryBuilder() .update() .set({ is_template: Boolean(is_template) }) .where("mapped_entity_type = :entityType", { entityType: entity_type }) .andWhere("organization_id = :orgId", { orgId: organization_id }) .andWhere("level_id = :levelId", { levelId: level_id }) .andWhere("level_type = :levelType", { levelType: level_type }) .execute(); } const whereCondition: any = { // mapped_entity_type: entity_type, organization_id, level_id, level_type, }; if (typeof is_template !== 'undefined') { whereCondition.is_template = is_template; } return this.commTemplateRepository.find({ select: ['name', 'code', 'mode', 'is_template'], where: whereCondition, }); } async save(commTemplate: any): Promise { // // delete existing mapper // for (const attachment of commTemplate || []) { // await this.templateAttachEntity.delete({ // entity_type: attachment.entity_type, // template_id: attachment.template_id, // }); // } return this.templateAttachEntity.save(commTemplate); } async delete(id: any): Promise { // delete existing mapper return await this.templateAttachEntity.delete(id); } async getTemplateByCode( entity_type: string, id: number, loggedInUser, ): Promise { const template = await this.commTemplateRepository.findOne({ where: { entity_type, id }, }); if (!template) { throw new NotFoundException(`Template with id ${id} not found`); } if (template.format_type === 'markup') { const media = await this.mediaDataService.getMediaDownloadUrl( template.markup_id, loggedInUser, ); template.markup_id = media as any; } if (template.format_type === 'richtext') { return template; } // Get attachments for this template const attachments = await this.templateAttachEntity.find({ select: ['media_id'], where: { entity_type, template_id: template.id, }, }); // If there are attachments, return array of media JSONs let attachmentsMedia: any[] = []; if (attachments.length > 0) { attachmentsMedia = await Promise.all( attachments.map(async (att) => { // Fetch media details for each media_id const media = await this.mediaDataService.getMediaDownloadUrl( att.media_id, loggedInUser, ); return media; // return full media info (e.g., { id, url, ... }) }), ); } // Return template with attachments array return { ...template, attachments: attachmentsMedia, // [] or array of media JSONs }; } async findByCodeAndLevelIdAndLevelTypeAndAppCode(code: string, level_id: string, level_type: string, appcode: string) { return await this.commTemplateRepository.findOne({ where: { code, level_id, level_type, appcode }, }); } }