@dereekb/nestjs 13.0.0 → 13.0.2
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/index.cjs.default.js +1 -0
- package/index.cjs.js +136 -118
- package/index.cjs.mjs +2 -0
- package/index.esm.js +136 -118
- package/mailgun/index.cjs.default.js +1 -0
- package/mailgun/index.cjs.js +496 -503
- package/mailgun/index.cjs.mjs +2 -0
- package/mailgun/index.esm.js +496 -503
- package/mailgun/package.json +19 -20
- package/openai/index.cjs.default.js +1 -0
- package/openai/index.cjs.js +194 -161
- package/openai/index.cjs.mjs +2 -0
- package/openai/index.esm.js +194 -161
- package/openai/package.json +19 -20
- package/package.json +32 -55
- package/stripe/index.cjs.default.js +1 -0
- package/stripe/index.cjs.js +158 -128
- package/stripe/index.cjs.mjs +2 -0
- package/stripe/index.esm.js +158 -128
- package/stripe/package.json +19 -20
- package/typeform/index.cjs.default.js +1 -0
- package/typeform/index.cjs.js +257 -240
- package/typeform/index.cjs.mjs +2 -0
- package/typeform/index.esm.js +257 -240
- package/typeform/package.json +19 -20
- package/vapiai/index.cjs.default.js +1 -0
- package/vapiai/index.cjs.js +203 -190
- package/vapiai/index.cjs.mjs +2 -0
- package/vapiai/index.esm.js +203 -190
- package/vapiai/package.json +19 -20
package/mailgun/index.esm.js
CHANGED
|
@@ -18,173 +18,155 @@ const MAX_BATCH_SEND_RECIPIENTS = 1000;
|
|
|
18
18
|
* @returns
|
|
19
19
|
*/
|
|
20
20
|
function convertMailgunTemplateEmailRequestToMailgunMessageData(config) {
|
|
21
|
-
|
|
22
|
-
request
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
21
|
+
const { request, defaultSender, isTestingEnvironment: testEnvironment, recipientVariablePrefix: inputRecipientVariablePrefix = DEFAULT_RECIPIENT_VARIABLE_PREFIX, addRecipientVariablePrefixToAllMergedGlobalVariables: inputAddRecipientVariablePrefixToAllMergedGlobalVariables = true, addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables: inputAddCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables = false, addCopyOfTemplateVariablesWithRecipientVariablePrefix: inputAddCopyOfTemplateVariablesWithRecipientVariablePrefix = false } = config;
|
|
22
|
+
const { recipientVariablesConfig } = request;
|
|
23
|
+
const recipientVariablePrefix = recipientVariablesConfig?.recipientVariablePrefix ?? inputRecipientVariablePrefix;
|
|
24
|
+
const addRecipientVariablePrefixToAllMergedGlobalVariables = recipientVariablesConfig?.addRecipientVariablePrefixToAllMergedGlobalVariables ?? inputAddRecipientVariablePrefixToAllMergedGlobalVariables;
|
|
25
|
+
const addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables = recipientVariablesConfig?.addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables ?? inputAddCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables;
|
|
26
|
+
const addCopyOfTemplateVariablesWithRecipientVariablePrefix = recipientVariablesConfig?.addCopyOfTemplateVariablesWithRecipientVariablePrefix ?? inputAddCopyOfTemplateVariablesWithRecipientVariablePrefix;
|
|
27
|
+
const finalizeRecipientVariables = request.finalizeRecipientVariables ?? MAP_IDENTITY;
|
|
28
|
+
const allowBatchSending = request.batchSend ?? true;
|
|
29
|
+
const mergeRecipientVariablesIntoGlobalVariable = !allowBatchSending;
|
|
30
|
+
function mapEmailToLowercase(x) {
|
|
31
|
+
return { ...x, email: x.email.toLowerCase() };
|
|
32
|
+
}
|
|
33
|
+
const toInput = asArray(request.to).map(mapEmailToLowercase);
|
|
34
|
+
const ccInput = request.cc ? asArray(request.cc).map(mapEmailToLowercase) : undefined;
|
|
35
|
+
const bccInput = request.bcc ? asArray(request.bcc).map(mapEmailToLowercase) : undefined;
|
|
36
|
+
const from = request.from ? convertMailgunRecipientToString(request.from) : defaultSender;
|
|
37
|
+
const to = convertMailgunRecipientsToStrings(toInput);
|
|
38
|
+
const cc = ccInput?.length ? convertMailgunRecipientsToStrings(ccInput) : undefined;
|
|
39
|
+
const bcc = bccInput?.length ? convertMailgunRecipientsToStrings(bccInput) : undefined;
|
|
40
|
+
// throw an error if batchSend is not defined and cc or bcc is defined
|
|
41
|
+
if (request.batchSend == null && (ccInput || bccInput)) {
|
|
42
|
+
throw new Error('convertMailgunTemplateEmailRequestToMailgunMessageData(): batchSend must be false when either "cc" or "bcc" is defined.');
|
|
43
|
+
}
|
|
44
|
+
if (allowBatchSending && to.length > MAX_BATCH_SEND_RECIPIENTS) {
|
|
45
|
+
throw new Error(`convertMailgunTemplateEmailRequestToMailgunMessageData(): Can only batch send to a maximum of ${MAX_BATCH_SEND_RECIPIENTS} recipients.`);
|
|
46
|
+
}
|
|
47
|
+
const data = {
|
|
48
|
+
from,
|
|
49
|
+
to,
|
|
50
|
+
cc,
|
|
51
|
+
bcc,
|
|
52
|
+
subject: request.subject,
|
|
53
|
+
template: request.template,
|
|
54
|
+
...request.messageData
|
|
44
55
|
};
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const ccInput = request.cc ? asArray(request.cc).map(mapEmailToLowercase) : undefined;
|
|
48
|
-
const bccInput = request.bcc ? asArray(request.bcc).map(mapEmailToLowercase) : undefined;
|
|
49
|
-
const from = request.from ? convertMailgunRecipientToString(request.from) : defaultSender;
|
|
50
|
-
const to = convertMailgunRecipientsToStrings(toInput);
|
|
51
|
-
const cc = ccInput?.length ? convertMailgunRecipientsToStrings(ccInput) : undefined;
|
|
52
|
-
const bcc = bccInput?.length ? convertMailgunRecipientsToStrings(bccInput) : undefined;
|
|
53
|
-
// throw an error if batchSend is not defined and cc or bcc is defined
|
|
54
|
-
if (request.batchSend == null && (ccInput || bccInput)) {
|
|
55
|
-
throw new Error('convertMailgunTemplateEmailRequestToMailgunMessageData(): batchSend must be false when either "cc" or "bcc" is defined.');
|
|
56
|
-
}
|
|
57
|
-
if (allowBatchSending && to.length > MAX_BATCH_SEND_RECIPIENTS) {
|
|
58
|
-
throw new Error(`convertMailgunTemplateEmailRequestToMailgunMessageData(): Can only batch send to a maximum of ${MAX_BATCH_SEND_RECIPIENTS} recipients.`);
|
|
59
|
-
}
|
|
60
|
-
const data = {
|
|
61
|
-
from,
|
|
62
|
-
to,
|
|
63
|
-
cc,
|
|
64
|
-
bcc,
|
|
65
|
-
subject: request.subject,
|
|
66
|
-
template: request.template,
|
|
67
|
-
...request.messageData
|
|
68
|
-
};
|
|
69
|
-
if (request.replyTo != null) {
|
|
70
|
-
data[MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY] = convertMailgunRecipientToString(request.replyTo);
|
|
71
|
-
}
|
|
72
|
-
if (request.testEmail === true || (testEnvironment ?? isTestNodeEnv()) && request.testEmail !== false) {
|
|
73
|
-
data['o:testmode'] = true;
|
|
74
|
-
}
|
|
75
|
-
if (request.templateVariables) {
|
|
76
|
-
forEachKeyValue(request.templateVariables, {
|
|
77
|
-
forEach: x => {
|
|
78
|
-
const [key, value] = x;
|
|
79
|
-
const encodedValue = encodeMailgunTemplateVariableValue(value);
|
|
80
|
-
if (encodedValue != null) {
|
|
81
|
-
data[`v:${key}`] = encodedValue;
|
|
82
|
-
if (recipientVariablePrefix && addCopyOfTemplateVariablesWithRecipientVariablePrefix) {
|
|
83
|
-
data[`v:${recipientVariablePrefix}${key}`] = encodedValue;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
const hasUserVariables = Boolean(data['recipient-variables']) || toInput.findIndex(x => x.userVariables != null) !== -1;
|
|
90
|
-
if (hasUserVariables) {
|
|
91
|
-
let recipientVariables = {};
|
|
92
|
-
const allRecipientVariableKeys = new Set();
|
|
93
|
-
toInput.forEach(({
|
|
94
|
-
email,
|
|
95
|
-
userVariables
|
|
96
|
-
}) => {
|
|
97
|
-
if (userVariables != null && !objectIsEmpty(userVariables)) {
|
|
98
|
-
recipientVariables[email] = userVariables;
|
|
99
|
-
addToSet(allRecipientVariableKeys, Object.keys(userVariables));
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
if (data['recipient-variables']) {
|
|
103
|
-
const decoded = JSON.parse(data['recipient-variables']);
|
|
104
|
-
forEachKeyValue(decoded, {
|
|
105
|
-
forEach: x => {
|
|
106
|
-
const [recipientEmail, userVariables] = x;
|
|
107
|
-
const email = recipientEmail.toLowerCase();
|
|
108
|
-
if (recipientVariables[email] != null) {
|
|
109
|
-
overrideInObject(recipientVariables[email], {
|
|
110
|
-
from: [userVariables],
|
|
111
|
-
filter: {
|
|
112
|
-
valueFilter: KeyValueTypleValueFilter.UNDEFINED
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
} else {
|
|
116
|
-
recipientVariables[email] = userVariables;
|
|
117
|
-
}
|
|
118
|
-
addToSet(allRecipientVariableKeys, Object.keys(userVariables));
|
|
119
|
-
}
|
|
120
|
-
});
|
|
56
|
+
if (request.replyTo != null) {
|
|
57
|
+
data[MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY] = convertMailgunRecipientToString(request.replyTo);
|
|
121
58
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
59
|
+
if (request.testEmail === true || ((testEnvironment ?? isTestNodeEnv()) && request.testEmail !== false)) {
|
|
60
|
+
data['o:testmode'] = true;
|
|
61
|
+
}
|
|
62
|
+
if (request.templateVariables) {
|
|
63
|
+
forEachKeyValue(request.templateVariables, {
|
|
64
|
+
forEach: (x) => {
|
|
65
|
+
const [key, value] = x;
|
|
66
|
+
const encodedValue = encodeMailgunTemplateVariableValue(value);
|
|
67
|
+
if (encodedValue != null) {
|
|
68
|
+
data[`v:${key}`] = encodedValue;
|
|
69
|
+
if (recipientVariablePrefix && addCopyOfTemplateVariablesWithRecipientVariablePrefix) {
|
|
70
|
+
data[`v:${recipientVariablePrefix}${key}`] = encodedValue;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const hasUserVariables = Boolean(data['recipient-variables']) || toInput.findIndex((x) => x.userVariables != null) !== -1;
|
|
77
|
+
if (hasUserVariables) {
|
|
78
|
+
let recipientVariables = {};
|
|
79
|
+
const allRecipientVariableKeys = new Set();
|
|
80
|
+
toInput.forEach(({ email, userVariables }) => {
|
|
81
|
+
if (userVariables != null && !objectIsEmpty(userVariables)) {
|
|
82
|
+
recipientVariables[email] = userVariables;
|
|
83
|
+
addToSet(allRecipientVariableKeys, Object.keys(userVariables));
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
if (data['recipient-variables']) {
|
|
87
|
+
const decoded = JSON.parse(data['recipient-variables']);
|
|
88
|
+
forEachKeyValue(decoded, {
|
|
89
|
+
forEach: (x) => {
|
|
90
|
+
const [recipientEmail, userVariables] = x;
|
|
91
|
+
const email = recipientEmail.toLowerCase();
|
|
92
|
+
if (recipientVariables[email] != null) {
|
|
93
|
+
overrideInObject(recipientVariables[email], { from: [userVariables], filter: { valueFilter: KeyValueTypleValueFilter.UNDEFINED } });
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
recipientVariables[email] = userVariables;
|
|
97
|
+
}
|
|
98
|
+
addToSet(allRecipientVariableKeys, Object.keys(userVariables));
|
|
142
99
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
// Finalize the recipient variables before they are re-encoded back to JSON
|
|
103
|
+
recipientVariables =
|
|
104
|
+
finalizeRecipientVariables(recipientVariables, {
|
|
105
|
+
messageData: data,
|
|
106
|
+
config
|
|
107
|
+
}) ?? recipientVariables;
|
|
108
|
+
if (mergeRecipientVariablesIntoGlobalVariable) {
|
|
109
|
+
const addRecipientPrefixVariable = recipientVariablePrefix && addRecipientVariablePrefixToAllMergedGlobalVariables;
|
|
110
|
+
// iterate all recipient variables and merge them into the global variables
|
|
111
|
+
forEachKeyValue(recipientVariables, {
|
|
112
|
+
forEach: (x) => {
|
|
113
|
+
const [email, userVariables] = x;
|
|
114
|
+
forEachKeyValue(userVariables, {
|
|
115
|
+
forEach: (y) => {
|
|
116
|
+
const [key, value] = y;
|
|
117
|
+
const encodedValue = encodeMailgunTemplateVariableValue(value);
|
|
118
|
+
if (encodedValue != null) {
|
|
119
|
+
if (addRecipientPrefixVariable) {
|
|
120
|
+
// also add the variable using the recipient variable prefix
|
|
121
|
+
const recipientVariableKey = `${recipientVariablePrefix}${key}`;
|
|
122
|
+
data[`v:${recipientVariableKey}`] = encodedValue;
|
|
123
|
+
}
|
|
124
|
+
// add if not adding the prefix variable, or if it was requested
|
|
125
|
+
if (!addRecipientPrefixVariable || addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables) {
|
|
126
|
+
data[`v:${key}`] = encodedValue;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
});
|
|
146
131
|
}
|
|
147
|
-
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
// set back on the data object
|
|
136
|
+
data['recipient-variables'] = JSON.stringify(recipientVariables);
|
|
137
|
+
// add all recipient variable to the other variables so they can be used easily/directly in templates as variables too.
|
|
138
|
+
// https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages
|
|
139
|
+
if (recipientVariablePrefix) {
|
|
140
|
+
forEachInIterable(allRecipientVariableKeys, (key) => {
|
|
141
|
+
const recipientVariableKey = `${recipientVariablePrefix}${key}`;
|
|
142
|
+
// v:recipient-id=%recipient.id%
|
|
143
|
+
data[`v:${recipientVariableKey}`] = `%recipient.${key}%`;
|
|
144
|
+
});
|
|
148
145
|
}
|
|
149
|
-
});
|
|
150
146
|
}
|
|
151
|
-
});
|
|
152
|
-
} else {
|
|
153
|
-
// set back on the data object
|
|
154
|
-
data['recipient-variables'] = JSON.stringify(recipientVariables);
|
|
155
|
-
// add all recipient variable to the other variables so they can be used easily/directly in templates as variables too.
|
|
156
|
-
// https://documentation.mailgun.com/en/latest/user_manual.html#attaching-data-to-messages
|
|
157
|
-
if (recipientVariablePrefix) {
|
|
158
|
-
forEachInIterable(allRecipientVariableKeys, key => {
|
|
159
|
-
const recipientVariableKey = `${recipientVariablePrefix}${key}`;
|
|
160
|
-
// v:recipient-id=%recipient.id%
|
|
161
|
-
data[`v:${recipientVariableKey}`] = `%recipient.${key}%`;
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
147
|
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
148
|
+
// double check and remove the recipient variables if we merged them into the global variables
|
|
149
|
+
if (mergeRecipientVariablesIntoGlobalVariable) {
|
|
150
|
+
delete data['recipient-variables'];
|
|
151
|
+
}
|
|
152
|
+
const inputAttachments = request.attachments;
|
|
153
|
+
if (inputAttachments) {
|
|
154
|
+
if (data.attachment) {
|
|
155
|
+
throw new Error(`Cannot specify attachments in both messageData and in the request's attachments field.`);
|
|
156
|
+
}
|
|
157
|
+
data.attachment = inputAttachments;
|
|
174
158
|
}
|
|
175
|
-
data
|
|
176
|
-
}
|
|
177
|
-
return data;
|
|
159
|
+
return data;
|
|
178
160
|
}
|
|
179
161
|
function convertMailgunRecipientsToStrings(recipients) {
|
|
180
|
-
|
|
162
|
+
return recipients.map((x) => convertMailgunRecipientToString(x));
|
|
181
163
|
}
|
|
182
164
|
function convertMailgunRecipientToString(recipient) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
165
|
+
let address = recipient.email;
|
|
166
|
+
if (recipient.name) {
|
|
167
|
+
address = `${recipient.name} <${address}>`;
|
|
168
|
+
}
|
|
169
|
+
return address;
|
|
188
170
|
}
|
|
189
171
|
/**
|
|
190
172
|
* Encodes a value to a string for use as a Mailgun template variable. Throws an error if the value is not supported.
|
|
@@ -193,29 +175,30 @@ function convertMailgunRecipientToString(recipient) {
|
|
|
193
175
|
* @returns The encoded value, or undefined if the value is null or undefined.
|
|
194
176
|
*/
|
|
195
177
|
function encodeMailgunTemplateVariableValue(value) {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
178
|
+
let encodedValue;
|
|
179
|
+
switch (typeof value) {
|
|
180
|
+
case 'object':
|
|
181
|
+
if (value) {
|
|
182
|
+
if (value instanceof Date) {
|
|
183
|
+
encodedValue = value.toISOString();
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
encodedValue = JSON.stringify(value);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
break;
|
|
190
|
+
case 'bigint':
|
|
191
|
+
case 'boolean':
|
|
192
|
+
case 'number':
|
|
193
|
+
case 'string':
|
|
194
|
+
encodedValue = String(value); // encoded as a string value
|
|
195
|
+
break;
|
|
196
|
+
default:
|
|
197
|
+
if (value) {
|
|
198
|
+
throw new Error(`Invalid value "${value}" passed to encodeMailgunTemplateVariableValue().`);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return encodedValue;
|
|
219
202
|
}
|
|
220
203
|
|
|
221
204
|
/******************************************************************************
|
|
@@ -256,170 +239,190 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
256
239
|
};
|
|
257
240
|
|
|
258
241
|
class MailgunServiceConfig {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
242
|
+
// Mailgun Config
|
|
243
|
+
mailgun;
|
|
244
|
+
/**
|
|
245
|
+
* Base URL to the client.
|
|
246
|
+
*/
|
|
247
|
+
clientUrl;
|
|
248
|
+
/**
|
|
249
|
+
* Main domain to send emails from.
|
|
250
|
+
*/
|
|
251
|
+
domain;
|
|
252
|
+
/**
|
|
253
|
+
* Mailgun sender string.
|
|
254
|
+
*/
|
|
255
|
+
sender;
|
|
256
|
+
/**
|
|
257
|
+
* Additional messages config
|
|
258
|
+
*/
|
|
259
|
+
messages;
|
|
260
|
+
static assertValidConfig(config) {
|
|
261
|
+
if (!config.mailgun.username) {
|
|
262
|
+
throw new Error('No mailgun username specified.');
|
|
263
|
+
}
|
|
264
|
+
else if (!config.mailgun.key) {
|
|
265
|
+
throw new Error('No mailgun key specified.');
|
|
266
|
+
}
|
|
267
|
+
else if (!config.domain) {
|
|
268
|
+
throw new Error('No mailgun domain specified.');
|
|
269
|
+
}
|
|
270
|
+
else if (!config.clientUrl) {
|
|
271
|
+
throw new Error('No client url specified.');
|
|
272
|
+
}
|
|
273
|
+
else if (!config.sender) {
|
|
274
|
+
throw new Error('No mailgun sender specified.');
|
|
275
|
+
}
|
|
276
|
+
else if (!config.messages) {
|
|
277
|
+
throw new Error('No mailgun messages config specified.');
|
|
278
|
+
}
|
|
290
279
|
}
|
|
291
|
-
}
|
|
292
280
|
}
|
|
293
281
|
|
|
294
282
|
let MailgunApi = class MailgunApi {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
283
|
+
config;
|
|
284
|
+
client;
|
|
285
|
+
constructor(config) {
|
|
286
|
+
this.config = config;
|
|
287
|
+
this.client = new Mailgun(FormData).client({
|
|
288
|
+
...config.mailgun
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
get messages() {
|
|
292
|
+
return this.client.messages;
|
|
293
|
+
}
|
|
294
|
+
get clientUrl() {
|
|
295
|
+
return this.config.clientUrl;
|
|
296
|
+
}
|
|
297
|
+
get domain() {
|
|
298
|
+
return this.config.domain;
|
|
299
|
+
}
|
|
300
|
+
get sender() {
|
|
301
|
+
return this.config.sender;
|
|
302
|
+
}
|
|
315
303
|
};
|
|
316
|
-
MailgunApi = __decorate([
|
|
304
|
+
MailgunApi = __decorate([
|
|
305
|
+
Injectable(),
|
|
306
|
+
__param(0, Inject(MailgunServiceConfig)),
|
|
307
|
+
__metadata("design:paramtypes", [MailgunServiceConfig])
|
|
308
|
+
], MailgunApi);
|
|
317
309
|
|
|
318
310
|
let MailgunService = class MailgunService {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
status: 200,
|
|
356
|
-
message: 'Success. Env prevented sending email.'
|
|
357
|
-
};
|
|
311
|
+
_mailgunApi;
|
|
312
|
+
_serverEnvironmentService;
|
|
313
|
+
constructor(mailgunApi, serverEnvironmentService) {
|
|
314
|
+
this._mailgunApi = mailgunApi;
|
|
315
|
+
this._serverEnvironmentService = serverEnvironmentService;
|
|
316
|
+
}
|
|
317
|
+
get mailgunApi() {
|
|
318
|
+
return this._mailgunApi;
|
|
319
|
+
}
|
|
320
|
+
get serverEnvironmentService() {
|
|
321
|
+
return this._serverEnvironmentService;
|
|
322
|
+
}
|
|
323
|
+
async sendTemplateEmail(request) {
|
|
324
|
+
const domain = this.mailgunApi.domain;
|
|
325
|
+
const sender = this.mailgunApi.sender;
|
|
326
|
+
const isTestingEnvironment = this.serverEnvironmentService.isTestingEnv;
|
|
327
|
+
const { recipientVariablePrefix } = this.mailgunApi.config.messages;
|
|
328
|
+
const data = convertMailgunTemplateEmailRequestToMailgunMessageData({ request, defaultSender: sender, recipientVariablePrefix, isTestingEnvironment });
|
|
329
|
+
let result;
|
|
330
|
+
const shouldSend = !isTestingEnvironment || request.sendTestEmails || this.mailgunApi.config.messages.sendTestEmails;
|
|
331
|
+
if (shouldSend) {
|
|
332
|
+
try {
|
|
333
|
+
result = await this.mailgunApi.messages.create(domain, data);
|
|
334
|
+
}
|
|
335
|
+
catch (e) {
|
|
336
|
+
console.error('Failed sending email: ', e);
|
|
337
|
+
throw e;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
result = {
|
|
342
|
+
status: 200,
|
|
343
|
+
message: 'Success. Env prevented sending email.'
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
return result;
|
|
358
347
|
}
|
|
359
|
-
return result;
|
|
360
|
-
}
|
|
361
348
|
};
|
|
362
|
-
MailgunService = __decorate([
|
|
349
|
+
MailgunService = __decorate([
|
|
350
|
+
Injectable(),
|
|
351
|
+
__param(0, Inject(MailgunApi)),
|
|
352
|
+
__param(1, Inject(ServerEnvironmentService)),
|
|
353
|
+
__metadata("design:paramtypes", [MailgunApi, ServerEnvironmentService])
|
|
354
|
+
], MailgunService);
|
|
363
355
|
|
|
364
356
|
function mailgunServiceConfigFactory(configService, serverEnvironmentService) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
357
|
+
const isTestingEnv = serverEnvironmentService.isTestingEnv;
|
|
358
|
+
const useSandbox = configService.get('USE_MAILGUN_SANDBOX') === 'true' || isTestingEnv;
|
|
359
|
+
const sendTestEmails = configService.get('MAILGUN_SEND_TEST_EMAILS') === 'true';
|
|
360
|
+
let key = configService.get('MAILGUN_API_KEY');
|
|
361
|
+
let domain = configService.get('MAILGUN_DOMAIN');
|
|
362
|
+
if (useSandbox) {
|
|
363
|
+
key = configService.get('MAILGUN_SANDBOX_API_KEY');
|
|
364
|
+
domain = configService.get('MAILGUN_SANDBOX_DOMAIN');
|
|
365
|
+
if (!key || !domain) {
|
|
366
|
+
throw new Error('USE_MAILGUN_SANDBOX is set to "true" (or current environment is a testing environment), but no environment variables for the sandbox (MAILGUN_SANDBOX_API_KEY, MAILGUN_SANDBOX_DOMAIN) are provided.');
|
|
367
|
+
}
|
|
368
|
+
else if (!serverEnvironmentService.isTestingEnv) {
|
|
369
|
+
console.log('Using Mailgun Sandbox Domain: ', domain);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
else if (!serverEnvironmentService.isTestingEnv) {
|
|
373
|
+
console.log('Using Mailgun Production Domain: ', domain);
|
|
374
|
+
}
|
|
375
|
+
const name = configService.get('MAILGUN_SENDER_NAME');
|
|
376
|
+
const email = configService.get('MAILGUN_SENDER_EMAIL');
|
|
377
|
+
const url = configService.get('MAILGUN_API_URL');
|
|
378
|
+
const recipientVariablePrefix = configService.get('MAILGUN_MESSAGES_RECIPIENT_VARIABLE_PREFIX') ?? undefined;
|
|
379
|
+
if (!email) {
|
|
380
|
+
throw new Error('MAILGUN_SENDER_EMAIL is required but was not configured.');
|
|
377
381
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
}
|
|
381
|
-
const name = configService.get('MAILGUN_SENDER_NAME');
|
|
382
|
-
const email = configService.get('MAILGUN_SENDER_EMAIL');
|
|
383
|
-
const url = configService.get('MAILGUN_API_URL');
|
|
384
|
-
const recipientVariablePrefix = configService.get('MAILGUN_MESSAGES_RECIPIENT_VARIABLE_PREFIX') ?? undefined;
|
|
385
|
-
if (!email) {
|
|
386
|
-
throw new Error('MAILGUN_SENDER_EMAIL is required but was not configured.');
|
|
387
|
-
} else if (!key) {
|
|
388
|
-
throw new Error('MAILGUN_API_KEY (or MAILGUN_SANDBOX_API_KEY for tests) is required but was not configured.');
|
|
389
|
-
} else if (!domain) {
|
|
390
|
-
throw new Error('MAILGUN_DOMAIN (or MAILGUN_SANDBOX_DOMAIN for tests) is required but was not configured.');
|
|
391
|
-
}
|
|
392
|
-
const clientUrl = configService.get('CLIENT_APP_URL') ?? domain;
|
|
393
|
-
const config = {
|
|
394
|
-
mailgun: {
|
|
395
|
-
username: configService.get('MAILGUN_USERNAME') ?? 'api',
|
|
396
|
-
key,
|
|
397
|
-
url
|
|
398
|
-
},
|
|
399
|
-
domain,
|
|
400
|
-
clientUrl,
|
|
401
|
-
sender: convertMailgunRecipientToString({
|
|
402
|
-
name,
|
|
403
|
-
email
|
|
404
|
-
}),
|
|
405
|
-
messages: {
|
|
406
|
-
sendTestEmails,
|
|
407
|
-
recipientVariablePrefix
|
|
382
|
+
else if (!key) {
|
|
383
|
+
throw new Error('MAILGUN_API_KEY (or MAILGUN_SANDBOX_API_KEY for tests) is required but was not configured.');
|
|
408
384
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
385
|
+
else if (!domain) {
|
|
386
|
+
throw new Error('MAILGUN_DOMAIN (or MAILGUN_SANDBOX_DOMAIN for tests) is required but was not configured.');
|
|
387
|
+
}
|
|
388
|
+
const clientUrl = configService.get('CLIENT_APP_URL') ?? domain;
|
|
389
|
+
const config = {
|
|
390
|
+
mailgun: {
|
|
391
|
+
username: configService.get('MAILGUN_USERNAME') ?? 'api',
|
|
392
|
+
key,
|
|
393
|
+
url
|
|
394
|
+
},
|
|
395
|
+
domain,
|
|
396
|
+
clientUrl,
|
|
397
|
+
sender: convertMailgunRecipientToString({
|
|
398
|
+
name,
|
|
399
|
+
email
|
|
400
|
+
}),
|
|
401
|
+
messages: {
|
|
402
|
+
sendTestEmails,
|
|
403
|
+
recipientVariablePrefix
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
MailgunServiceConfig.assertValidConfig(config);
|
|
407
|
+
return config;
|
|
412
408
|
}
|
|
413
|
-
let MailgunServiceModule = class MailgunServiceModule {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
409
|
+
let MailgunServiceModule = class MailgunServiceModule {
|
|
410
|
+
};
|
|
411
|
+
MailgunServiceModule = __decorate([
|
|
412
|
+
Module({
|
|
413
|
+
imports: [ConfigModule],
|
|
414
|
+
providers: [
|
|
415
|
+
{
|
|
416
|
+
provide: MailgunServiceConfig,
|
|
417
|
+
inject: [ConfigService, ServerEnvironmentService],
|
|
418
|
+
useFactory: mailgunServiceConfigFactory
|
|
419
|
+
},
|
|
420
|
+
MailgunApi,
|
|
421
|
+
MailgunService
|
|
422
|
+
],
|
|
423
|
+
exports: [MailgunApi, MailgunService]
|
|
424
|
+
})
|
|
425
|
+
], MailgunServiceModule);
|
|
423
426
|
|
|
424
427
|
/**
|
|
425
428
|
* The default template subject to use when batch sending emails.
|
|
@@ -431,9 +434,9 @@ const MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE = `%recipient.subject%`;
|
|
|
431
434
|
* Creates a composite key from the from/replyTo email addresses used to group MailgunRecipientBatchSendTarget values.
|
|
432
435
|
*/
|
|
433
436
|
function mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey(recipient) {
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
+
const fromEmail = (recipient.from?.email ?? '').toLowerCase();
|
|
438
|
+
const replyToEmail = (recipient.replyTo?.email ?? '').toLowerCase();
|
|
439
|
+
return `f:${fromEmail}|r:${replyToEmail}`;
|
|
437
440
|
}
|
|
438
441
|
/**
|
|
439
442
|
* Creates a ExpandMailgunRecipientBatchSendTargetRequestFactory from the input config.
|
|
@@ -442,158 +445,150 @@ function mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey(recipient) {
|
|
|
442
445
|
* @returns
|
|
443
446
|
*/
|
|
444
447
|
function expandMailgunRecipientBatchSendTargetRequestFactory(config) {
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
useSubjectFromRecipientUserVariables
|
|
448
|
-
|
|
449
|
-
recipientVariablesConfig,
|
|
450
|
-
mailgunRecipientBatchSendTargetEntityKeyRecipientLookup,
|
|
451
|
-
overrideCarbonCopyVariablesWithCarbonCopyKeyRecipients
|
|
452
|
-
} = config;
|
|
453
|
-
const defaultSubject = inputBaseRequest.subject;
|
|
454
|
-
if (!defaultSubject && !useSubjectFromRecipientUserVariables) {
|
|
455
|
-
throw new Error('defaultSubject must be set when "useSubjectFromRecipientUserVariables" is false');
|
|
456
|
-
}
|
|
457
|
-
/**
|
|
458
|
-
* Returns the carbon copy recipients, based on the input.
|
|
459
|
-
*
|
|
460
|
-
* Will return undefined if the array would be empty.
|
|
461
|
-
*
|
|
462
|
-
* @param input
|
|
463
|
-
* @returns
|
|
464
|
-
*/
|
|
465
|
-
function determineCarbonCopyRecipients(input) {
|
|
466
|
-
const {
|
|
467
|
-
baseRequestCarbonCopyRecipients,
|
|
468
|
-
carbonCopyRecipients,
|
|
469
|
-
carbonCopyRecipientsKeys
|
|
470
|
-
} = input;
|
|
471
|
-
let cc = carbonCopyRecipients ? asArray(carbonCopyRecipients) : baseRequestCarbonCopyRecipients;
|
|
472
|
-
const resolvedCc = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup?.getRecipientsForKeys(carbonCopyRecipientsKeys);
|
|
473
|
-
if (resolvedCc?.length) {
|
|
474
|
-
if (overrideCarbonCopyVariablesWithCarbonCopyKeyRecipients) {
|
|
475
|
-
cc = resolvedCc;
|
|
476
|
-
} else {
|
|
477
|
-
cc = [...(cc ?? []), ...resolvedCc];
|
|
478
|
-
}
|
|
448
|
+
const { request: inputBaseRequest, useSubjectFromRecipientUserVariables, allowSingleRecipientBatchSendRequests, recipientVariablesConfig, mailgunRecipientBatchSendTargetEntityKeyRecipientLookup, overrideCarbonCopyVariablesWithCarbonCopyKeyRecipients } = config;
|
|
449
|
+
const defaultSubject = inputBaseRequest.subject;
|
|
450
|
+
if (!defaultSubject && !useSubjectFromRecipientUserVariables) {
|
|
451
|
+
throw new Error('defaultSubject must be set when "useSubjectFromRecipientUserVariables" is false');
|
|
479
452
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
delete baseRequest.fromKey;
|
|
500
|
-
delete baseRequest.replyToKey;
|
|
501
|
-
delete baseRequest.ccKeys;
|
|
502
|
-
delete baseRequest.bccKeys;
|
|
503
|
-
const configAllowBatchSend = baseRequest.batchSend !== false;
|
|
504
|
-
return inputRecipients => {
|
|
505
|
-
// Process recipients to resolve keys
|
|
506
|
-
const recipients = inputRecipients.map(recipient => {
|
|
507
|
-
let from = recipient.from;
|
|
508
|
-
let replyTo = recipient.replyTo;
|
|
509
|
-
if (mailgunRecipientBatchSendTargetEntityKeyRecipientLookup) {
|
|
510
|
-
// try the fromKey, otherwise use the baseRequest.from
|
|
511
|
-
if (!from) {
|
|
512
|
-
from = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.fromKey, baseRequest.from);
|
|
513
|
-
}
|
|
514
|
-
// try the replyToKey, otherwise use the baseRequest.replyTo
|
|
515
|
-
if (!replyTo) {
|
|
516
|
-
replyTo = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.replyToKey, baseRequest.replyTo);
|
|
517
|
-
}
|
|
518
|
-
} else {
|
|
519
|
-
// use defaults from base request
|
|
520
|
-
if (!from) {
|
|
521
|
-
from = baseRequest.from;
|
|
522
|
-
}
|
|
523
|
-
if (!replyTo) {
|
|
524
|
-
replyTo = baseRequest.replyTo;
|
|
453
|
+
/**
|
|
454
|
+
* Returns the carbon copy recipients, based on the input.
|
|
455
|
+
*
|
|
456
|
+
* Will return undefined if the array would be empty.
|
|
457
|
+
*
|
|
458
|
+
* @param input
|
|
459
|
+
* @returns
|
|
460
|
+
*/
|
|
461
|
+
function determineCarbonCopyRecipients(input) {
|
|
462
|
+
const { baseRequestCarbonCopyRecipients, carbonCopyRecipients, carbonCopyRecipientsKeys } = input;
|
|
463
|
+
let cc = carbonCopyRecipients ? asArray(carbonCopyRecipients) : baseRequestCarbonCopyRecipients;
|
|
464
|
+
const resolvedCc = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup?.getRecipientsForKeys(carbonCopyRecipientsKeys);
|
|
465
|
+
if (resolvedCc?.length) {
|
|
466
|
+
if (overrideCarbonCopyVariablesWithCarbonCopyKeyRecipients) {
|
|
467
|
+
cc = resolvedCc;
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
cc = [...(cc ?? []), ...resolvedCc];
|
|
471
|
+
}
|
|
525
472
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
carbonCopyRecipients:
|
|
530
|
-
carbonCopyRecipientsKeys:
|
|
531
|
-
});
|
|
532
|
-
const bcc = determineCarbonCopyRecipients({
|
|
533
|
-
baseRequestCarbonCopyRecipients: baseRequestBcc,
|
|
534
|
-
carbonCopyRecipients: recipient.bcc,
|
|
535
|
-
carbonCopyRecipientsKeys: recipient.bccKeys
|
|
536
|
-
});
|
|
537
|
-
const result = {
|
|
538
|
-
...recipient,
|
|
539
|
-
from,
|
|
540
|
-
replyTo,
|
|
541
|
-
cc,
|
|
542
|
-
bcc
|
|
543
|
-
};
|
|
544
|
-
return result;
|
|
473
|
+
return cc?.length ? cc : undefined;
|
|
474
|
+
}
|
|
475
|
+
const baseRequestCc = determineCarbonCopyRecipients({
|
|
476
|
+
carbonCopyRecipients: inputBaseRequest.cc,
|
|
477
|
+
carbonCopyRecipientsKeys: inputBaseRequest.ccKeys
|
|
545
478
|
});
|
|
546
|
-
const
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
recipients.forEach(recipient => {
|
|
550
|
-
const recipientHasCarbonCopy = Boolean(recipient.cc?.length || recipient.bcc?.length);
|
|
551
|
-
if (allowBatchSend && !recipientHasCarbonCopy) {
|
|
552
|
-
// add to batch send recipients
|
|
553
|
-
batchSendRequestRecipients.push(recipient);
|
|
554
|
-
} else {
|
|
555
|
-
// add to non-batch send requests
|
|
556
|
-
// use the subject from the recipient's user variables if available as a default
|
|
557
|
-
const cc = recipient.cc;
|
|
558
|
-
const bcc = recipient.bcc;
|
|
559
|
-
const subject = (useSubjectFromRecipientUserVariables ? recipient.userVariables?.['subject'] : undefined) ?? defaultSubject ?? recipient.userVariables?.['subject'];
|
|
560
|
-
const request = {
|
|
561
|
-
...baseRequest,
|
|
562
|
-
from: recipient.from ?? baseRequest.from,
|
|
563
|
-
replyTo: recipient.replyTo ?? baseRequest.replyTo,
|
|
564
|
-
recipientVariablesConfig: baseRequest.recipientVariablesConfig ?? recipientVariablesConfig,
|
|
565
|
-
to: recipient,
|
|
566
|
-
cc,
|
|
567
|
-
bcc,
|
|
568
|
-
subject,
|
|
569
|
-
batchSend: false // explicitly disable batch send for non-batch requests
|
|
570
|
-
};
|
|
571
|
-
nonBatchSendRequests.push(request);
|
|
572
|
-
}
|
|
479
|
+
const baseRequestBcc = determineCarbonCopyRecipients({
|
|
480
|
+
carbonCopyRecipients: inputBaseRequest.bcc,
|
|
481
|
+
carbonCopyRecipientsKeys: inputBaseRequest.bccKeys
|
|
573
482
|
});
|
|
574
|
-
|
|
575
|
-
const
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
483
|
+
const baseRequestFrom = inputBaseRequest.from ?? mailgunRecipientBatchSendTargetEntityKeyRecipientLookup?.getRecipientOrDefaultForKey(inputBaseRequest.fromKey);
|
|
484
|
+
const baseRequestReplyTo = inputBaseRequest.replyTo ?? mailgunRecipientBatchSendTargetEntityKeyRecipientLookup?.getRecipientOrDefaultForKey(inputBaseRequest.replyToKey);
|
|
485
|
+
const baseRequest = {
|
|
486
|
+
...inputBaseRequest,
|
|
487
|
+
from: baseRequestFrom,
|
|
488
|
+
replyTo: baseRequestReplyTo,
|
|
489
|
+
cc: baseRequestCc,
|
|
490
|
+
bcc: baseRequestBcc
|
|
491
|
+
};
|
|
492
|
+
delete baseRequest.fromKey;
|
|
493
|
+
delete baseRequest.replyToKey;
|
|
494
|
+
delete baseRequest.ccKeys;
|
|
495
|
+
delete baseRequest.bccKeys;
|
|
496
|
+
const configAllowBatchSend = baseRequest.batchSend !== false;
|
|
497
|
+
return (inputRecipients) => {
|
|
498
|
+
// Process recipients to resolve keys
|
|
499
|
+
const recipients = inputRecipients.map((recipient) => {
|
|
500
|
+
let from = recipient.from;
|
|
501
|
+
let replyTo = recipient.replyTo;
|
|
502
|
+
if (mailgunRecipientBatchSendTargetEntityKeyRecipientLookup) {
|
|
503
|
+
// try the fromKey, otherwise use the baseRequest.from
|
|
504
|
+
if (!from) {
|
|
505
|
+
from = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.fromKey, baseRequest.from);
|
|
506
|
+
}
|
|
507
|
+
// try the replyToKey, otherwise use the baseRequest.replyTo
|
|
508
|
+
if (!replyTo) {
|
|
509
|
+
replyTo = mailgunRecipientBatchSendTargetEntityKeyRecipientLookup.getRecipientOrDefaultForKey(recipient.replyToKey, baseRequest.replyTo);
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
else {
|
|
513
|
+
// use defaults from base request
|
|
514
|
+
if (!from) {
|
|
515
|
+
from = baseRequest.from;
|
|
516
|
+
}
|
|
517
|
+
if (!replyTo) {
|
|
518
|
+
replyTo = baseRequest.replyTo;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
const cc = determineCarbonCopyRecipients({
|
|
522
|
+
baseRequestCarbonCopyRecipients: baseRequestCc,
|
|
523
|
+
carbonCopyRecipients: recipient.cc,
|
|
524
|
+
carbonCopyRecipientsKeys: recipient.ccKeys
|
|
525
|
+
});
|
|
526
|
+
const bcc = determineCarbonCopyRecipients({
|
|
527
|
+
baseRequestCarbonCopyRecipients: baseRequestBcc,
|
|
528
|
+
carbonCopyRecipients: recipient.bcc,
|
|
529
|
+
carbonCopyRecipientsKeys: recipient.bccKeys
|
|
530
|
+
});
|
|
531
|
+
const result = {
|
|
532
|
+
...recipient,
|
|
533
|
+
from,
|
|
534
|
+
replyTo,
|
|
535
|
+
cc,
|
|
536
|
+
bcc
|
|
537
|
+
};
|
|
538
|
+
return result;
|
|
539
|
+
});
|
|
540
|
+
const allowBatchSend = configAllowBatchSend && (allowSingleRecipientBatchSendRequests || recipients.length > 1);
|
|
541
|
+
const nonBatchSendRequests = [];
|
|
542
|
+
const batchSendRequestRecipients = [];
|
|
543
|
+
recipients.forEach((recipient) => {
|
|
544
|
+
const recipientHasCarbonCopy = Boolean(recipient.cc?.length || recipient.bcc?.length);
|
|
545
|
+
if (allowBatchSend && !recipientHasCarbonCopy) {
|
|
546
|
+
// add to batch send recipients
|
|
547
|
+
batchSendRequestRecipients.push(recipient);
|
|
548
|
+
}
|
|
549
|
+
else {
|
|
550
|
+
// add to non-batch send requests
|
|
551
|
+
// use the subject from the recipient's user variables if available as a default
|
|
552
|
+
const cc = recipient.cc;
|
|
553
|
+
const bcc = recipient.bcc;
|
|
554
|
+
const subject = (useSubjectFromRecipientUserVariables ? recipient.userVariables?.['subject'] : undefined) ?? defaultSubject ?? recipient.userVariables?.['subject'];
|
|
555
|
+
const request = {
|
|
556
|
+
...baseRequest,
|
|
557
|
+
from: recipient.from ?? baseRequest.from,
|
|
558
|
+
replyTo: recipient.replyTo ?? baseRequest.replyTo,
|
|
559
|
+
recipientVariablesConfig: baseRequest.recipientVariablesConfig ?? recipientVariablesConfig,
|
|
560
|
+
to: recipient,
|
|
561
|
+
cc,
|
|
562
|
+
bcc,
|
|
563
|
+
subject,
|
|
564
|
+
batchSend: false // explicitly disable batch send for non-batch requests
|
|
565
|
+
};
|
|
566
|
+
nonBatchSendRequests.push(request);
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
// create batch send request(s)
|
|
570
|
+
const batchSendRequests = [];
|
|
571
|
+
if (batchSendRequestRecipients.length > 0) {
|
|
572
|
+
const subject = useSubjectFromRecipientUserVariables ? MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE : defaultSubject;
|
|
573
|
+
// Group recipients by their from/replyTo values
|
|
574
|
+
const batchSendRecipientGroups = makeValuesGroupMap(batchSendRequestRecipients, mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey);
|
|
575
|
+
batchSendRecipientGroups.forEach((groupRecipients) => {
|
|
576
|
+
// All recipients in this group should share the same from/replyTo values
|
|
577
|
+
const firstRecipient = groupRecipients[0];
|
|
578
|
+
const batchRequest = {
|
|
579
|
+
...baseRequest,
|
|
580
|
+
from: firstRecipient.from,
|
|
581
|
+
replyTo: firstRecipient.replyTo,
|
|
582
|
+
recipientVariablesConfig: baseRequest.recipientVariablesConfig ?? recipientVariablesConfig,
|
|
583
|
+
to: groupRecipients,
|
|
584
|
+
subject,
|
|
585
|
+
batchSend: true
|
|
586
|
+
};
|
|
587
|
+
batchSendRequests.push(batchRequest);
|
|
588
|
+
});
|
|
589
|
+
}
|
|
590
|
+
return filterMaybeArrayValues([...batchSendRequests, ...nonBatchSendRequests]);
|
|
591
|
+
};
|
|
597
592
|
}
|
|
598
593
|
/**
|
|
599
594
|
* Creates a MailgunRecipientBatchSendTargetEntityKeyRecipientLookup given the input configuration.
|
|
@@ -602,32 +597,30 @@ function expandMailgunRecipientBatchSendTargetRequestFactory(config) {
|
|
|
602
597
|
* @returns The lookup.
|
|
603
598
|
*/
|
|
604
599
|
function mailgunRecipientBatchSendTargetEntityKeyRecipientLookup(config) {
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
600
|
+
const { recipientsMap } = config;
|
|
601
|
+
function getRecipientOrDefaultForKey(input, defaultRecipient) {
|
|
602
|
+
let result = defaultRecipient;
|
|
603
|
+
if (input) {
|
|
604
|
+
result = recipientsMap.get(input) ?? defaultRecipient;
|
|
605
|
+
}
|
|
606
|
+
return result;
|
|
612
607
|
}
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
608
|
+
function getRecipientsForKeys(input) {
|
|
609
|
+
let result = undefined;
|
|
610
|
+
if (input) {
|
|
611
|
+
const keysArray = asArray(input);
|
|
612
|
+
const recipients = filterMaybeArrayValues(keysArray.map((key) => recipientsMap.get(key)));
|
|
613
|
+
if (recipients.length > 0) {
|
|
614
|
+
result = recipients;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
return result;
|
|
623
618
|
}
|
|
624
|
-
return
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
getRecipientsForKeys
|
|
630
|
-
};
|
|
619
|
+
return {
|
|
620
|
+
recipientsMap,
|
|
621
|
+
getRecipientOrDefaultForKey,
|
|
622
|
+
getRecipientsForKeys
|
|
623
|
+
};
|
|
631
624
|
}
|
|
632
625
|
|
|
633
626
|
export { DEFAULT_RECIPIENT_VARIABLE_PREFIX, MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE, MAILGUN_REPLY_TO_EMAIL_HEADER_DATA_VARIABLE_KEY, MAX_BATCH_SEND_RECIPIENTS, MailgunApi, MailgunService, MailgunServiceModule, convertMailgunRecipientToString, convertMailgunRecipientsToStrings, convertMailgunTemplateEmailRequestToMailgunMessageData, encodeMailgunTemplateVariableValue, expandMailgunRecipientBatchSendTargetRequestFactory, mailgunRecipientBatchSendTargetEntityKeyRecipientLookup, mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey, mailgunServiceConfigFactory };
|