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