import { BadRequestException, Inject, Injectable } from '@nestjs/common'; import { CommTemplateRepository } from '../repository/comm-template.repository'; import { EntityServiceImpl } from 'src/module/meta/service/entity-service-impl.service'; import { UserData } from 'src/module/user/entity/user.entity'; import axios from 'axios'; import { MediaDataService } from '../../meta/service/media-data.service'; import { FieldMapperService } from '../../mapper/service/field-mapper.service'; @Injectable() export class CommTemplateService extends EntityServiceImpl { constructor(private readonly commTemplateRepository: CommTemplateRepository, private readonly mediaService: MediaDataService, @Inject('FieldMapperService') private readonly fieldMapperService: FieldMapperService, ) { super(); } async createEntity(entityData: any, loggedInUser: any): Promise { const { attachments, ...templateData } = entityData; const tempData = await super.createEntity( { ...templateData, flag: templateData.flag || 'default' }, loggedInUser, ); // attachment mapper create and update if (attachments && attachments.length > 0) { const attachmentEntities = attachments.map((attachment) => ({ entity_type: templateData.entity_type, template_id: tempData.id, media_id: attachment, organization_id: loggedInUser.organization_id, appcode: loggedInUser.appcode, level_id: loggedInUser.level_id, mapped_entity_type: 'LEAD', level_type: loggedInUser.level_type, })); await this.commTemplateRepository.save(attachmentEntities); } return tempData; } async updateEntity(entityData: any, loggedInUser: UserData): Promise { const { attachments, ...templateData } = entityData; if ( loggedInUser.level_type === 'SCH' && templateData.level_type === 'ORG' ) { const { id, level_type, level_id, ...newTemplateData } = entityData; const create = await this.createEntity( { ...newTemplateData, level_type: loggedInUser.level_type, level_id: loggedInUser.level_id, flag: 'edited', }, loggedInUser, ); return create; } const tempData = await super.updateEntity(templateData, loggedInUser); // delete existing mapper await this.commTemplateRepository.delete({ template_id: tempData.id, }); // attachment mapper create and update if (attachments && attachments.length > 0) { const attachmentEntities = attachments.map((attachment) => ({ entity_type: templateData.entity_type, template_id: tempData.id, media_id: attachment, organization_id: loggedInUser.organization_id, enterprise_id: loggedInUser.enterprise_id, appcode: loggedInUser.appcode, level_id: loggedInUser.level_id, level_type: loggedInUser.level_type, })); await this.commTemplateRepository.save(attachmentEntities); } return tempData; } async getEntityData( entityType: string, id: number, loggedInUser, ): Promise { //TODO: check if current level_id and levl_type has that code if not go to parent level const getTemplate = await this.commTemplateRepository.getTemplateByCode( entityType, id, loggedInUser, ); return getTemplate; } async getAllCommTemplate( entity_type: string, action_id?: number, loggedInUser?, ) { const commTemplateData = await this.commTemplateRepository.getAllCommTemplate( entity_type, action_id, loggedInUser, ); const response = commTemplateData.map((item) => ({ value: item.code, label: item.name, mode: item.mode, template: item.is_template, })); return response; } async getResolvedTemplateMsg(payload: any, loggedInUser: UserData) { let commTemplate = await this.commTemplateRepository.findByCodeAndLevelIdAndLevelTypeAndAppCode( payload.template_code, payload.level_id, payload.level_type, payload.appcode, ); let variables; if (commTemplate && !commTemplate.template_id) { if (commTemplate.mapper_id) { variables = await this.fieldMapperService.resolveData( commTemplate.mapper_id, 'LOOKUP', payload.entity_type, payload.entity_id, loggedInUser, undefined, undefined, payload.overwrite || false, payload.entity_data || null, ); } if (!commTemplate.template_id && !commTemplate.markup_id) { let richText = commTemplate.rich_text; if (!richText) { if (commTemplate.markup_id) { let url = await this.mediaService.getMediaDownloadUrl(commTemplate.markup_id, {}, 60000); if (url) { let response = await axios.get(url.signedUrl, { responseType: 'text' }); richText = response.data; } } } richText = richText.replace(/\{\{(\w+)\}\}/g, (match, key) => { const value = variables[key]; return value !== undefined ? String(value) : match; }); return { rich_text: richText, subject: commTemplate.subject, }; } } else { throw new BadRequestException('Editing is disabled for external templates'); } } }