@b2y/email-service 1.0.5 → 1.0.6
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/package.json +4 -3
- package/service/EmailService.js +99 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@b2y/email-service",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./index.js",
|
|
@@ -16,8 +16,9 @@
|
|
|
16
16
|
"templates/",
|
|
17
17
|
"utils/",
|
|
18
18
|
"constants/",
|
|
19
|
-
"enum",
|
|
20
|
-
"model",
|
|
19
|
+
"enum/",
|
|
20
|
+
"model/",
|
|
21
|
+
"service/",
|
|
21
22
|
"*.js"
|
|
22
23
|
],
|
|
23
24
|
"scripts": {
|
|
@@ -0,0 +1,99 @@
|
|
|
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
|
+
} = options;
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
// Validate required parameters
|
|
47
|
+
if (!tenantId) throw new Error(StatusMessage.TENANT_ID_REQUIRED);
|
|
48
|
+
if (!templateName && !options.html) throw new Error(StatusMessage.TEMPLATE_OR_HTML_REQUIRED);
|
|
49
|
+
if (!langCode) throw new Error(StatusMessage.LANGCODE_IS_REQUIRED);
|
|
50
|
+
if (!recipients || (!recipients.to && !recipients.TO)) throw new Error(StatusMessage.RECIPIENTS_REQUIRED);
|
|
51
|
+
|
|
52
|
+
let emailSubject = subject;
|
|
53
|
+
let html;
|
|
54
|
+
|
|
55
|
+
if (templateName) {
|
|
56
|
+
const templateContent = await this.prepareEmailContent(templateName, placeholders, tenantId, langCode);
|
|
57
|
+
html = templateContent.body;
|
|
58
|
+
if (!emailSubject) {
|
|
59
|
+
emailSubject = templateContent.subject;
|
|
60
|
+
}
|
|
61
|
+
} else {
|
|
62
|
+
html = this.replacePlaceholders(options.html, placeholders);
|
|
63
|
+
if (!emailSubject) throw new Error(StatusMessage.SUBJECT_REQUIRED);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Format recipients
|
|
67
|
+
const to = recipients.to || recipients.TO;
|
|
68
|
+
const cc = recipients.cc || recipients.CC;
|
|
69
|
+
const bcc = recipients.bcc || recipients.BCC;
|
|
70
|
+
|
|
71
|
+
// Send email
|
|
72
|
+
return await this.provider.sendEmail({
|
|
73
|
+
tenantId,
|
|
74
|
+
to,
|
|
75
|
+
cc,
|
|
76
|
+
bcc,
|
|
77
|
+
subject: emailSubject,
|
|
78
|
+
html,
|
|
79
|
+
from,
|
|
80
|
+
attachments
|
|
81
|
+
});
|
|
82
|
+
} catch (error) {
|
|
83
|
+
logger.error(`EmailService failed to send email ${tenantId}: ${error.message}`);
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async sendTemplateEmail(templateName, to, variables = {}, tenantId, langCode) {
|
|
89
|
+
return this.sendEmail({
|
|
90
|
+
templateName,
|
|
91
|
+
recipients: { to },
|
|
92
|
+
placeholders: variables,
|
|
93
|
+
tenantId,
|
|
94
|
+
langCode
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = EmailService;
|