11import { isEmpty , isNil } from 'lodash-es' ;
22import { EmailTemplate , EmailRecord } from '../models/index.js' ;
3- import { IRegisterTemplateParams , ISendEmailParams } from '../interfaces/index.js' ;
3+ import {
4+ IRegisterTemplateParams ,
5+ ISendEmailParams ,
6+ IUpdateTemplateParams ,
7+ } from '../interfaces/index.js' ;
48import handlebars from 'handlebars' ;
59import { EmailProvider } from '../email-provider/index.js' ;
610import { CreateEmailTemplate } from '../email-provider/interfaces/CreateEmailTemplate.js' ;
@@ -12,6 +16,7 @@ import { ConfigController } from '@conduitplatform/module-tools';
1216import { Config } from '../config/index.js' ;
1317import { status } from '@grpc/grpc-js' ;
1418import { storeEmail } from '../utils/index.js' ;
19+ import { getHandleBarsValues } from '../email-provider/utils/index.js' ;
1520
1621export class EmailService {
1722 constructor (
@@ -38,10 +43,6 @@ export class EmailService {
3843 ) ;
3944 }
4045
41- updateTemplate ( data : UpdateEmailTemplate ) {
42- return this . emailer . _transport ?. updateTemplate ( data ) ;
43- }
44-
4546 deleteExternalTemplate ( id : string ) {
4647 return this . emailer . _transport ?. deleteTemplate ( id ) ;
4748 }
@@ -61,6 +62,56 @@ export class EmailService {
6162 } ) ;
6263 }
6364
65+ async updateTemplate ( id : string , params : IUpdateTemplateParams ) {
66+ const templateDocument = await EmailTemplate . getInstance ( ) . findOne ( {
67+ _id : id ,
68+ } ) ;
69+ if ( isNil ( templateDocument ) ) {
70+ throw new GrpcError ( status . NOT_FOUND , 'Template does not exist' ) ;
71+ }
72+
73+ [ 'name' , 'subject' , 'body' , 'sender' ] . forEach ( key => {
74+ if ( params [ key as keyof IUpdateTemplateParams ] ) {
75+ // @ts -ignore
76+ templateDocument [ key ] = params [ key ] ;
77+ }
78+ } ) ;
79+
80+ templateDocument . variables = Object . keys ( getHandleBarsValues ( params . body ) ) . concat (
81+ Object . keys ( getHandleBarsValues ( params . subject ) ) ,
82+ ) ;
83+ if ( templateDocument . variables ) {
84+ templateDocument . variables = templateDocument . variables . filter (
85+ ( value , index ) => templateDocument . variables ! . indexOf ( value ) === index ,
86+ ) ;
87+ }
88+
89+ const updatedTemplate = await EmailTemplate . getInstance ( )
90+ . findByIdAndUpdate ( id , templateDocument )
91+ . catch ( ( e : Error ) => {
92+ throw new GrpcError ( status . INTERNAL , e . message ) ;
93+ } ) ;
94+
95+ if ( templateDocument . externalManaged ) {
96+ const template = await this . getExternalTemplate ( updatedTemplate ! . externalId ! ) ;
97+ let versionId = undefined ;
98+ if ( ! isNil ( template ?. versions [ 0 ] . id ) ) {
99+ versionId = template ?. versions [ 0 ] . id ;
100+ }
101+ const data = {
102+ id : updatedTemplate ! . externalId ! ,
103+ subject : updatedTemplate ! . subject ,
104+ body : updatedTemplate ! . body ,
105+ versionId : versionId ,
106+ } ;
107+ await this . emailer . _transport ?. updateTemplate ( data ) . catch ( ( e : Error ) => {
108+ throw new GrpcError ( status . INTERNAL , e . message ) ;
109+ } ) ;
110+ }
111+
112+ return { template : updatedTemplate } ;
113+ }
114+
64115 async sendEmail (
65116 template : string | undefined ,
66117 params : ISendEmailParams ,
0 commit comments