@b2y/email-service 1.0.9 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Config.js +44 -39
- package/Logger.js +5 -5
- package/README.md +18 -18
- package/constants/AppConstants.js +8 -0
- package/constants/StatusMessageConstants.js +39 -38
- package/enum/EmailProvider.js +8 -7
- package/index.js +82 -77
- package/model/Connect.js +23 -23
- package/model/EmailTemplate.js +51 -51
- package/package.json +55 -53
- package/providers/AmazonSESProvider.js +83 -76
- package/providers/BaseProvider.js +85 -73
- package/providers/MailgunProvider.js +85 -0
- package/providers/NodeMailerProvider.js +52 -51
- package/providers/PostmarkProvider.js +61 -61
- package/providers/SendgridProvider.js +60 -60
- package/service/EmailService.js +101 -98
- package/utils/TemplateEngine.js +98 -98
|
@@ -1,62 +1,62 @@
|
|
|
1
|
-
// providers/PostmarkProvider.js
|
|
2
|
-
const postmark = require('postmark');
|
|
3
|
-
const BaseProvider = require('./BaseProvider');
|
|
4
|
-
const Config = require('../Config');
|
|
5
|
-
const logger = require('../Logger');
|
|
6
|
-
const StatusMessage = require('../constants/StatusMessageConstants');
|
|
7
|
-
const { EmailProvider } = require('../enum/EmailProvider');
|
|
8
|
-
|
|
9
|
-
class PostmarkProvider extends BaseProvider {
|
|
10
|
-
constructor() {
|
|
11
|
-
super();
|
|
12
|
-
this.client = new postmark.Client(Config.postmarkApiKey);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
async sendEmail(options) {
|
|
16
|
-
try {
|
|
17
|
-
this.validateEmailOptions(options);
|
|
18
|
-
const mailOptions = {
|
|
19
|
-
From: options.from || Config.postmarkFromEmail,
|
|
20
|
-
To: this.formatRecipients(options.to).join(','),
|
|
21
|
-
Subject: options.subject,
|
|
22
|
-
HtmlBody: options.html,
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
if (options.cc && options.cc.length > 0) {
|
|
26
|
-
mailOptions.Cc = this.formatRecipients(options.cc).join(',');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (options.bcc && options.bcc.length > 0) {
|
|
30
|
-
mailOptions.Bcc = this.formatRecipients(options.bcc).join(',');
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (options.attachments && options.attachments.length > 0) {
|
|
34
|
-
mailOptions.Attachments = options.attachments.map(att => ({
|
|
35
|
-
Name: att.filename,
|
|
36
|
-
Content: att.content,
|
|
37
|
-
ContentType: att.contentType
|
|
38
|
-
}));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const result = await this.client.sendEmail(mailOptions);
|
|
42
|
-
|
|
43
|
-
logger.info('Email sent successfully via Postmark', {
|
|
44
|
-
messageId: result.MessageID,
|
|
45
|
-
recipients: { to: options.to, cc: options.cc, bcc: options.bcc },
|
|
46
|
-
tenantId: options.tenantId
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
success: true,
|
|
51
|
-
messageId: result.MessageID,
|
|
52
|
-
provider: EmailProvider.POSTMARK,
|
|
53
|
-
tenantId: options.tenantId
|
|
54
|
-
};
|
|
55
|
-
} catch (error) {
|
|
56
|
-
logger.error(`Postmark failed to send email: ${error.message}`);
|
|
57
|
-
throw new Error(`${StatusMessage.POSTMARK_SEND_ERROR}: ${error.message}`);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
1
|
+
// providers/PostmarkProvider.js
|
|
2
|
+
const postmark = require('postmark');
|
|
3
|
+
const BaseProvider = require('./BaseProvider');
|
|
4
|
+
const Config = require('../Config');
|
|
5
|
+
const logger = require('../Logger');
|
|
6
|
+
const StatusMessage = require('../constants/StatusMessageConstants');
|
|
7
|
+
const { EmailProvider } = require('../enum/EmailProvider');
|
|
8
|
+
|
|
9
|
+
class PostmarkProvider extends BaseProvider {
|
|
10
|
+
constructor() {
|
|
11
|
+
super();
|
|
12
|
+
this.client = new postmark.Client(Config.postmarkApiKey);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async sendEmail(options) {
|
|
16
|
+
try {
|
|
17
|
+
this.validateEmailOptions(options);
|
|
18
|
+
const mailOptions = {
|
|
19
|
+
From: options.from || Config.postmarkFromEmail,
|
|
20
|
+
To: this.formatRecipients(options.to).join(','),
|
|
21
|
+
Subject: options.subject,
|
|
22
|
+
HtmlBody: options.html,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
if (options.cc && options.cc.length > 0) {
|
|
26
|
+
mailOptions.Cc = this.formatRecipients(options.cc).join(',');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (options.bcc && options.bcc.length > 0) {
|
|
30
|
+
mailOptions.Bcc = this.formatRecipients(options.bcc).join(',');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (options.attachments && options.attachments.length > 0) {
|
|
34
|
+
mailOptions.Attachments = options.attachments.map(att => ({
|
|
35
|
+
Name: att.filename,
|
|
36
|
+
Content: att.content,
|
|
37
|
+
ContentType: att.contentType
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const result = await this.client.sendEmail(mailOptions);
|
|
42
|
+
|
|
43
|
+
logger.info('Email sent successfully via Postmark', {
|
|
44
|
+
messageId: result.MessageID,
|
|
45
|
+
recipients: { to: options.to, cc: options.cc, bcc: options.bcc },
|
|
46
|
+
tenantId: options.tenantId
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
success: true,
|
|
51
|
+
messageId: result.MessageID,
|
|
52
|
+
provider: EmailProvider.POSTMARK,
|
|
53
|
+
tenantId: options.tenantId
|
|
54
|
+
};
|
|
55
|
+
} catch (error) {
|
|
56
|
+
logger.error(`Postmark failed to send email: ${error.message}`);
|
|
57
|
+
throw new Error(`${StatusMessage.POSTMARK_SEND_ERROR}: ${error.message}`);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
62
|
module.exports = PostmarkProvider;
|
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
// providers/SendgridProvider.js
|
|
2
|
-
const sgMail = require('@sendgrid/mail');
|
|
3
|
-
const BaseProvider = require('./BaseProvider');
|
|
4
|
-
const Config = require('../Config');
|
|
5
|
-
const logger = require('../Logger');
|
|
6
|
-
const { EmailProvider } = require('../enum/EmailProvider');
|
|
7
|
-
|
|
8
|
-
class SendgridProvider extends BaseProvider {
|
|
9
|
-
constructor() {
|
|
10
|
-
super();
|
|
11
|
-
sgMail.setApiKey(Config.sendGridApiKey);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
async sendEmail({ tenantId, to, cc, bcc, subject, html, from, attachments }) {
|
|
15
|
-
try {
|
|
16
|
-
this.validateEmailOptions({ to, subject, html, from });
|
|
17
|
-
|
|
18
|
-
const mailOptions = {
|
|
19
|
-
to: this.formatRecipients(to),
|
|
20
|
-
from: Config.emailUser,
|
|
21
|
-
subject,
|
|
22
|
-
html,
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
if (cc && cc.length > 0) {
|
|
26
|
-
mailOptions.cc = this.formatRecipients(cc);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (bcc && bcc.length > 0) {
|
|
30
|
-
mailOptions.bcc = this.formatRecipients(bcc);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (attachments && attachments.length > 0) {
|
|
34
|
-
mailOptions.attachments = attachments;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const result = await sgMail.send(mailOptions);
|
|
38
|
-
|
|
39
|
-
logger.info('Email sent successfully via SendGrid', {
|
|
40
|
-
messageId: result[0].headers['x-message-id'],
|
|
41
|
-
recipients: { to, cc, bcc },
|
|
42
|
-
tenantId
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
return {
|
|
46
|
-
success: true,
|
|
47
|
-
messageId: result[0].headers['x-message-id'],
|
|
48
|
-
provider: EmailProvider.SENDGRID,
|
|
49
|
-
tenantId
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
} catch (error) {
|
|
53
|
-
logger.error(`SendGrid failed to send email : ${error.message}`);
|
|
54
|
-
throw new Error(`${StatusMessage.SENDGRID_SEND_ERROR}: ${error.message}`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
module.exports = SendgridProvider;
|
|
60
|
-
|
|
1
|
+
// providers/SendgridProvider.js
|
|
2
|
+
const sgMail = require('@sendgrid/mail');
|
|
3
|
+
const BaseProvider = require('./BaseProvider');
|
|
4
|
+
const Config = require('../Config');
|
|
5
|
+
const logger = require('../Logger');
|
|
6
|
+
const { EmailProvider } = require('../enum/EmailProvider');
|
|
7
|
+
|
|
8
|
+
class SendgridProvider extends BaseProvider {
|
|
9
|
+
constructor() {
|
|
10
|
+
super();
|
|
11
|
+
sgMail.setApiKey(Config.sendGridApiKey);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async sendEmail({ tenantId, to, cc, bcc, subject, html, from, attachments }) {
|
|
15
|
+
try {
|
|
16
|
+
this.validateEmailOptions({ to, subject, html, from });
|
|
17
|
+
|
|
18
|
+
const mailOptions = {
|
|
19
|
+
to: this.formatRecipients(to),
|
|
20
|
+
from: Config.emailUser,
|
|
21
|
+
subject,
|
|
22
|
+
html,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
if (cc && cc.length > 0) {
|
|
26
|
+
mailOptions.cc = this.formatRecipients(cc);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (bcc && bcc.length > 0) {
|
|
30
|
+
mailOptions.bcc = this.formatRecipients(bcc);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (attachments && attachments.length > 0) {
|
|
34
|
+
mailOptions.attachments = attachments;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const result = await sgMail.send(mailOptions);
|
|
38
|
+
|
|
39
|
+
logger.info('Email sent successfully via SendGrid', {
|
|
40
|
+
messageId: result[0].headers['x-message-id'],
|
|
41
|
+
recipients: { to, cc, bcc },
|
|
42
|
+
tenantId
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
success: true,
|
|
47
|
+
messageId: result[0].headers['x-message-id'],
|
|
48
|
+
provider: EmailProvider.SENDGRID,
|
|
49
|
+
tenantId
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
} catch (error) {
|
|
53
|
+
logger.error(`SendGrid failed to send email : ${error.message}`);
|
|
54
|
+
throw new Error(`${StatusMessage.SENDGRID_SEND_ERROR}: ${error.message}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
module.exports = SendgridProvider;
|
|
60
|
+
|
package/service/EmailService.js
CHANGED
|
@@ -1,99 +1,102 @@
|
|
|
1
|
-
const logger = require('../Logger');
|
|
2
|
-
const TemplateEngine = require('../utils/TemplateEngine');
|
|
3
|
-
const Config = require('../Config');
|
|
4
|
-
const StatusMessage = require('../constants/StatusMessageConstants');
|
|
5
|
-
class EmailService {
|
|
6
|
-
constructor(provider, dbClient) {
|
|
7
|
-
if (!provider) throw new Error(StatusMessage.PROVIDER_REQUIRED);
|
|
8
|
-
if (!dbClient) throw new Error(StatusMessage.DB_CLIENT_REQUIRED);
|
|
9
|
-
|
|
10
|
-
this.provider = provider;
|
|
11
|
-
this.EmailTemplate = dbClient.EmailTemplate;
|
|
12
|
-
this.templateEngine = new TemplateEngine(dbClient);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
async prepareEmailContent(templateName, placeholders = {}, tenantId, langCode) {
|
|
16
|
-
try {
|
|
17
|
-
return await this.templateEngine.compileTemplate(
|
|
18
|
-
templateName,
|
|
19
|
-
placeholders,
|
|
20
|
-
tenantId,
|
|
21
|
-
langCode
|
|
22
|
-
);
|
|
23
|
-
} catch (error) {
|
|
24
|
-
logger.error(`Error preparing email content: ${error.message}`);
|
|
25
|
-
throw error;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
replacePlaceholders(content, placeholders = {}) {
|
|
30
|
-
return this.templateEngine.replacePlaceholders(content, placeholders);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async sendEmail(options) {
|
|
34
|
-
const {
|
|
35
|
-
tenantId,
|
|
36
|
-
langCode,
|
|
37
|
-
templateName,
|
|
38
|
-
placeholders = {},
|
|
39
|
-
recipients,
|
|
40
|
-
subject,
|
|
41
|
-
attachments = [],
|
|
42
|
-
from = Config.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (!
|
|
49
|
-
if (!
|
|
50
|
-
if (!
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
let
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
1
|
+
const logger = require('../Logger');
|
|
2
|
+
const TemplateEngine = require('../utils/TemplateEngine');
|
|
3
|
+
const Config = require('../Config');
|
|
4
|
+
const StatusMessage = require('../constants/StatusMessageConstants');
|
|
5
|
+
class EmailService {
|
|
6
|
+
constructor(provider, dbClient) {
|
|
7
|
+
if (!provider) throw new Error(StatusMessage.PROVIDER_REQUIRED);
|
|
8
|
+
if (!dbClient) throw new Error(StatusMessage.DB_CLIENT_REQUIRED);
|
|
9
|
+
|
|
10
|
+
this.provider = provider;
|
|
11
|
+
this.EmailTemplate = dbClient.EmailTemplate;
|
|
12
|
+
this.templateEngine = new TemplateEngine(dbClient);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async prepareEmailContent(templateName, placeholders = {}, tenantId, langCode) {
|
|
16
|
+
try {
|
|
17
|
+
return await this.templateEngine.compileTemplate(
|
|
18
|
+
templateName,
|
|
19
|
+
placeholders,
|
|
20
|
+
tenantId,
|
|
21
|
+
langCode
|
|
22
|
+
);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
logger.error(`Error preparing email content: ${error.message}`);
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
replacePlaceholders(content, placeholders = {}) {
|
|
30
|
+
return this.templateEngine.replacePlaceholders(content, placeholders);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async sendEmail(options) {
|
|
34
|
+
const {
|
|
35
|
+
tenantId,
|
|
36
|
+
langCode,
|
|
37
|
+
templateName,
|
|
38
|
+
placeholders = {},
|
|
39
|
+
recipients,
|
|
40
|
+
subject,
|
|
41
|
+
attachments = [],
|
|
42
|
+
from = Config.fromEmail,
|
|
43
|
+
fromName = Config.fromName
|
|
44
|
+
} = options;
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
// Validate required parameters
|
|
48
|
+
if (!tenantId) throw new Error(StatusMessage.TENANT_ID_REQUIRED);
|
|
49
|
+
if (!templateName && !options.html) throw new Error(StatusMessage.TEMPLATE_OR_HTML_REQUIRED);
|
|
50
|
+
if (!langCode) throw new Error(StatusMessage.LANGCODE_IS_REQUIRED);
|
|
51
|
+
if (!recipients || (!recipients.to && !recipients.TO)) throw new Error(StatusMessage.RECIPIENTS_REQUIRED);
|
|
52
|
+
|
|
53
|
+
let emailSubject = subject;
|
|
54
|
+
let html;
|
|
55
|
+
|
|
56
|
+
if (templateName) {
|
|
57
|
+
const templateContent = await this.prepareEmailContent(templateName, placeholders, tenantId, langCode);
|
|
58
|
+
html = templateContent.body;
|
|
59
|
+
if (!emailSubject) {
|
|
60
|
+
emailSubject = templateContent.subject;
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
html = this.replacePlaceholders(options.html, placeholders);
|
|
64
|
+
if (!emailSubject) throw new Error(StatusMessage.SUBJECT_REQUIRED);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Format recipients
|
|
68
|
+
const to = recipients.to || recipients.TO;
|
|
69
|
+
const cc = recipients.cc || recipients.CC;
|
|
70
|
+
const bcc = recipients.bcc || recipients.BCC;
|
|
71
|
+
|
|
72
|
+
// Send email
|
|
73
|
+
return await this.provider.sendEmail({
|
|
74
|
+
tenantId,
|
|
75
|
+
to,
|
|
76
|
+
cc,
|
|
77
|
+
bcc,
|
|
78
|
+
subject: emailSubject,
|
|
79
|
+
html,
|
|
80
|
+
from,
|
|
81
|
+
fromName,
|
|
82
|
+
attachments
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
logger.error(`EmailService failed to send email ${tenantId}: ${error.message}`);
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async sendTemplateEmail(templateName, to, variables = {}, tenantId, langCode, fromName = null) {
|
|
91
|
+
return this.sendEmail({
|
|
92
|
+
templateName,
|
|
93
|
+
recipients: { to },
|
|
94
|
+
placeholders: variables,
|
|
95
|
+
tenantId,
|
|
96
|
+
langCode,
|
|
97
|
+
fromName
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
99
102
|
module.exports = EmailService;
|
package/utils/TemplateEngine.js
CHANGED
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
const handlebars = require('handlebars');
|
|
2
|
-
const logger = require('../Logger');
|
|
3
|
-
|
|
4
|
-
class TemplateEngine {
|
|
5
|
-
constructor(dbClient) {
|
|
6
|
-
if (!dbClient) throw new Error(StatusMessage.DB_CLIENT_REQUIRED);
|
|
7
|
-
this.EmailTemplate = dbClient.EmailTemplate;
|
|
8
|
-
this.registerHelpers();
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
registerHelpers() {
|
|
12
|
-
handlebars.registerHelper('eq', (a, b) => a === b);
|
|
13
|
-
handlebars.registerHelper('ne', (a, b) => a !== b);
|
|
14
|
-
handlebars.registerHelper('gt', (a, b) => a > b);
|
|
15
|
-
handlebars.registerHelper('lt', (a, b) => a < b);
|
|
16
|
-
handlebars.registerHelper('uppercase', (str) => str ? str.toUpperCase() : '');
|
|
17
|
-
handlebars.registerHelper('lowercase', (str) => str ? str.toLowerCase() : '');
|
|
18
|
-
handlebars.registerHelper('formatDate', (date) => {
|
|
19
|
-
if (!date) return '';
|
|
20
|
-
return new Date(date).toLocaleDateString();
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async getTemplateFromDB(templateName, tenantId, langCode) {
|
|
25
|
-
try {
|
|
26
|
-
logger.info(`Fetching template: ${templateName} in language: ${langCode}`);
|
|
27
|
-
const template = await this.EmailTemplate.findOne({
|
|
28
|
-
where: {
|
|
29
|
-
TemplateName: templateName,
|
|
30
|
-
TenantID: tenantId,
|
|
31
|
-
LangCode: langCode
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
if (!template) {
|
|
36
|
-
logger.warn(`Template "${templateName}" not found in language ${langCode}`);
|
|
37
|
-
const defaultTemplate = await this.EmailTemplate.findOne({
|
|
38
|
-
where: {
|
|
39
|
-
TemplateName: templateName,
|
|
40
|
-
TenantID: tenantId,
|
|
41
|
-
LangCode: langCode
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
if (!defaultTemplate) {
|
|
46
|
-
throw new Error(`Template "${templateName}" not found in any language`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
subject: defaultTemplate.Subject,
|
|
51
|
-
body: defaultTemplate.Body
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return {
|
|
56
|
-
subject: template.Subject,
|
|
57
|
-
body: template.Body
|
|
58
|
-
};
|
|
59
|
-
} catch (error) {
|
|
60
|
-
logger.error(`Error fetching template from DB: ${error.message}`);
|
|
61
|
-
throw error;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async compileTemplate(templateName, variables = {}, tenantId, langCode) {
|
|
66
|
-
try {
|
|
67
|
-
logger.info(`Compiling template: ${templateName} in language: ${langCode}`);
|
|
68
|
-
|
|
69
|
-
const templateData = await this.getTemplateFromDB(templateName, tenantId, langCode);
|
|
70
|
-
|
|
71
|
-
const subjectTemplate = handlebars.compile(templateData.subject);
|
|
72
|
-
const bodyTemplate = handlebars.compile(templateData.body);
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
subject: subjectTemplate(variables),
|
|
76
|
-
body: bodyTemplate(variables)
|
|
77
|
-
};
|
|
78
|
-
} catch (error) {
|
|
79
|
-
logger.error(`Error compiling template "${templateName}": ${error.message}`);
|
|
80
|
-
throw error;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
replacePlaceholders(content, placeholders = {}) {
|
|
85
|
-
if (!content || typeof content !== 'string') {
|
|
86
|
-
return content;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
try {
|
|
90
|
-
const template = handlebars.compile(content);
|
|
91
|
-
return template(placeholders);
|
|
92
|
-
} catch (err) {
|
|
93
|
-
logger.error(`Error replacing placeholders: ${err.message}`);
|
|
94
|
-
return content;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
1
|
+
const handlebars = require('handlebars');
|
|
2
|
+
const logger = require('../Logger');
|
|
3
|
+
|
|
4
|
+
class TemplateEngine {
|
|
5
|
+
constructor(dbClient) {
|
|
6
|
+
if (!dbClient) throw new Error(StatusMessage.DB_CLIENT_REQUIRED);
|
|
7
|
+
this.EmailTemplate = dbClient.EmailTemplate;
|
|
8
|
+
this.registerHelpers();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
registerHelpers() {
|
|
12
|
+
handlebars.registerHelper('eq', (a, b) => a === b);
|
|
13
|
+
handlebars.registerHelper('ne', (a, b) => a !== b);
|
|
14
|
+
handlebars.registerHelper('gt', (a, b) => a > b);
|
|
15
|
+
handlebars.registerHelper('lt', (a, b) => a < b);
|
|
16
|
+
handlebars.registerHelper('uppercase', (str) => str ? str.toUpperCase() : '');
|
|
17
|
+
handlebars.registerHelper('lowercase', (str) => str ? str.toLowerCase() : '');
|
|
18
|
+
handlebars.registerHelper('formatDate', (date) => {
|
|
19
|
+
if (!date) return '';
|
|
20
|
+
return new Date(date).toLocaleDateString();
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async getTemplateFromDB(templateName, tenantId, langCode) {
|
|
25
|
+
try {
|
|
26
|
+
logger.info(`Fetching template: ${templateName} in language: ${langCode}`);
|
|
27
|
+
const template = await this.EmailTemplate.findOne({
|
|
28
|
+
where: {
|
|
29
|
+
TemplateName: templateName,
|
|
30
|
+
TenantID: tenantId,
|
|
31
|
+
LangCode: langCode
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (!template) {
|
|
36
|
+
logger.warn(`Template "${templateName}" not found in language ${langCode}`);
|
|
37
|
+
const defaultTemplate = await this.EmailTemplate.findOne({
|
|
38
|
+
where: {
|
|
39
|
+
TemplateName: templateName,
|
|
40
|
+
TenantID: tenantId,
|
|
41
|
+
LangCode: langCode
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
if (!defaultTemplate) {
|
|
46
|
+
throw new Error(`Template "${templateName}" not found in any language`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
subject: defaultTemplate.Subject,
|
|
51
|
+
body: defaultTemplate.Body
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return {
|
|
56
|
+
subject: template.Subject,
|
|
57
|
+
body: template.Body
|
|
58
|
+
};
|
|
59
|
+
} catch (error) {
|
|
60
|
+
logger.error(`Error fetching template from DB: ${error.message}`);
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async compileTemplate(templateName, variables = {}, tenantId, langCode) {
|
|
66
|
+
try {
|
|
67
|
+
logger.info(`Compiling template: ${templateName} in language: ${langCode}`);
|
|
68
|
+
|
|
69
|
+
const templateData = await this.getTemplateFromDB(templateName, tenantId, langCode);
|
|
70
|
+
|
|
71
|
+
const subjectTemplate = handlebars.compile(templateData.subject);
|
|
72
|
+
const bodyTemplate = handlebars.compile(templateData.body);
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
subject: subjectTemplate(variables),
|
|
76
|
+
body: bodyTemplate(variables)
|
|
77
|
+
};
|
|
78
|
+
} catch (error) {
|
|
79
|
+
logger.error(`Error compiling template "${templateName}": ${error.message}`);
|
|
80
|
+
throw error;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
replacePlaceholders(content, placeholders = {}) {
|
|
85
|
+
if (!content || typeof content !== 'string') {
|
|
86
|
+
return content;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
try {
|
|
90
|
+
const template = handlebars.compile(content);
|
|
91
|
+
return template(placeholders);
|
|
92
|
+
} catch (err) {
|
|
93
|
+
logger.error(`Error replacing placeholders: ${err.message}`);
|
|
94
|
+
return content;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
99
|
module.exports = TemplateEngine;
|