@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.
@@ -20,173 +20,155 @@ const MAX_BATCH_SEND_RECIPIENTS = 1000;
20
20
  * @returns
21
21
  */
22
22
  function convertMailgunTemplateEmailRequestToMailgunMessageData(config) {
23
- const {
24
- request,
25
- defaultSender,
26
- isTestingEnvironment: testEnvironment,
27
- recipientVariablePrefix: inputRecipientVariablePrefix = DEFAULT_RECIPIENT_VARIABLE_PREFIX,
28
- addRecipientVariablePrefixToAllMergedGlobalVariables: inputAddRecipientVariablePrefixToAllMergedGlobalVariables = true,
29
- addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables: inputAddCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables = false,
30
- addCopyOfTemplateVariablesWithRecipientVariablePrefix: inputAddCopyOfTemplateVariablesWithRecipientVariablePrefix = false
31
- } = config;
32
- const {
33
- recipientVariablesConfig
34
- } = request;
35
- const recipientVariablePrefix = recipientVariablesConfig?.recipientVariablePrefix ?? inputRecipientVariablePrefix;
36
- const addRecipientVariablePrefixToAllMergedGlobalVariables = recipientVariablesConfig?.addRecipientVariablePrefixToAllMergedGlobalVariables ?? inputAddRecipientVariablePrefixToAllMergedGlobalVariables;
37
- const addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables = recipientVariablesConfig?.addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables ?? inputAddCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables;
38
- const addCopyOfTemplateVariablesWithRecipientVariablePrefix = recipientVariablesConfig?.addCopyOfTemplateVariablesWithRecipientVariablePrefix ?? inputAddCopyOfTemplateVariablesWithRecipientVariablePrefix;
39
- const finalizeRecipientVariables = request.finalizeRecipientVariables ?? util.MAP_IDENTITY;
40
- const allowBatchSending = request.batchSend ?? true;
41
- const mergeRecipientVariablesIntoGlobalVariable = !allowBatchSending;
42
- function mapEmailToLowercase(x) {
43
- return {
44
- ...x,
45
- email: x.email.toLowerCase()
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
- const toInput = util.asArray(request.to).map(mapEmailToLowercase);
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
- // Finalize the recipient variables before they are re-encoded back to JSON
125
- recipientVariables = finalizeRecipientVariables(recipientVariables, {
126
- messageData: data,
127
- config
128
- }) ?? recipientVariables;
129
- if (mergeRecipientVariablesIntoGlobalVariable) {
130
- const addRecipientPrefixVariable = recipientVariablePrefix && addRecipientVariablePrefixToAllMergedGlobalVariables;
131
- // iterate all recipient variables and merge them into the global variables
132
- util.forEachKeyValue(recipientVariables, {
133
- forEach: x => {
134
- const [email, userVariables] = x;
135
- util.forEachKeyValue(userVariables, {
136
- forEach: y => {
137
- const [key, value] = y;
138
- const encodedValue = encodeMailgunTemplateVariableValue(value);
139
- if (encodedValue != null) {
140
- if (addRecipientPrefixVariable) {
141
- // also add the variable using the recipient variable prefix
142
- const recipientVariableKey = `${recipientVariablePrefix}${key}`;
143
- data[`v:${recipientVariableKey}`] = encodedValue;
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
- // add if not adding the prefix variable, or if it was requested
146
- if (!addRecipientPrefixVariable || addCopyOfMergedRecipientVariablesWithoutPrefixToGlobalVariables) {
147
- data[`v:${key}`] = encodedValue;
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
- // double check and remove the recipient variables if we merged them into the global variables
169
- if (mergeRecipientVariablesIntoGlobalVariable) {
170
- delete data['recipient-variables'];
171
- }
172
- const inputAttachments = request.attachments;
173
- if (inputAttachments) {
174
- if (data.attachment) {
175
- throw new Error(`Cannot specify attachments in both messageData and in the request's attachments field.`);
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.attachment = inputAttachments;
178
- }
179
- return data;
161
+ return data;
180
162
  }
181
163
  function convertMailgunRecipientsToStrings(recipients) {
182
- return recipients.map(x => convertMailgunRecipientToString(x));
164
+ return recipients.map((x) => convertMailgunRecipientToString(x));
183
165
  }
184
166
  function convertMailgunRecipientToString(recipient) {
185
- let address = recipient.email;
186
- if (recipient.name) {
187
- address = `${recipient.name} <${address}>`;
188
- }
189
- return address;
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
- let encodedValue;
199
- switch (typeof value) {
200
- case 'object':
201
- if (value) {
202
- if (value instanceof Date) {
203
- encodedValue = value.toISOString();
204
- } else {
205
- encodedValue = JSON.stringify(value);
206
- }
207
- }
208
- break;
209
- case 'bigint':
210
- case 'boolean':
211
- case 'number':
212
- case 'string':
213
- encodedValue = String(value); // encoded as a string value
214
- break;
215
- default:
216
- if (value) {
217
- throw new Error(`Invalid value "${value}" passed to encodeMailgunTemplateVariableValue().`);
218
- }
219
- }
220
- return encodedValue;
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
- // Mailgun Config
262
- mailgun;
263
- /**
264
- * Base URL to the client.
265
- */
266
- clientUrl;
267
- /**
268
- * Main domain to send emails from.
269
- */
270
- domain;
271
- /**
272
- * Mailgun sender string.
273
- */
274
- sender;
275
- /**
276
- * Additional messages config
277
- */
278
- messages;
279
- static assertValidConfig(config) {
280
- if (!config.mailgun.username) {
281
- throw new Error('No mailgun username specified.');
282
- } else if (!config.mailgun.key) {
283
- throw new Error('No mailgun key specified.');
284
- } else if (!config.domain) {
285
- throw new Error('No mailgun domain specified.');
286
- } else if (!config.clientUrl) {
287
- throw new Error('No client url specified.');
288
- } else if (!config.sender) {
289
- throw new Error('No mailgun sender specified.');
290
- } else if (!config.messages) {
291
- throw new Error('No mailgun messages config specified.');
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
- config;
298
- client;
299
- constructor(config) {
300
- this.config = config;
301
- this.client = new Mailgun(FormData).client({
302
- ...config.mailgun
303
- });
304
- }
305
- get messages() {
306
- return this.client.messages;
307
- }
308
- get clientUrl() {
309
- return this.config.clientUrl;
310
- }
311
- get domain() {
312
- return this.config.domain;
313
- }
314
- get sender() {
315
- return this.config.sender;
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([common.Injectable(), __param(0, common.Inject(MailgunServiceConfig)), __metadata("design:paramtypes", [MailgunServiceConfig])], exports.MailgunApi);
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
- _mailgunApi;
322
- _serverEnvironmentService;
323
- constructor(mailgunApi, serverEnvironmentService) {
324
- this._mailgunApi = mailgunApi;
325
- this._serverEnvironmentService = serverEnvironmentService;
326
- }
327
- get mailgunApi() {
328
- return this._mailgunApi;
329
- }
330
- get serverEnvironmentService() {
331
- return this._serverEnvironmentService;
332
- }
333
- async sendTemplateEmail(request) {
334
- const domain = this.mailgunApi.domain;
335
- const sender = this.mailgunApi.sender;
336
- const isTestingEnvironment = this.serverEnvironmentService.isTestingEnv;
337
- const {
338
- recipientVariablePrefix
339
- } = this.mailgunApi.config.messages;
340
- const data = convertMailgunTemplateEmailRequestToMailgunMessageData({
341
- request,
342
- defaultSender: sender,
343
- recipientVariablePrefix,
344
- isTestingEnvironment
345
- });
346
- let result;
347
- const shouldSend = !isTestingEnvironment || request.sendTestEmails || this.mailgunApi.config.messages.sendTestEmails;
348
- if (shouldSend) {
349
- try {
350
- result = await this.mailgunApi.messages.create(domain, data);
351
- } catch (e) {
352
- console.error('Failed sending email: ', e);
353
- throw e;
354
- }
355
- } else {
356
- result = {
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([common.Injectable(), __param(0, common.Inject(exports.MailgunApi)), __param(1, common.Inject(nestjs.ServerEnvironmentService)), __metadata("design:paramtypes", [exports.MailgunApi, nestjs.ServerEnvironmentService])], exports.MailgunService);
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
- const isTestingEnv = serverEnvironmentService.isTestingEnv;
368
- const useSandbox = configService.get('USE_MAILGUN_SANDBOX') === 'true' || isTestingEnv;
369
- const sendTestEmails = configService.get('MAILGUN_SEND_TEST_EMAILS') === 'true';
370
- let key = configService.get('MAILGUN_API_KEY');
371
- let domain = configService.get('MAILGUN_DOMAIN');
372
- if (useSandbox) {
373
- key = configService.get('MAILGUN_SANDBOX_API_KEY');
374
- domain = configService.get('MAILGUN_SANDBOX_DOMAIN');
375
- if (!key || !domain) {
376
- 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.');
377
- } else if (!serverEnvironmentService.isTestingEnv) {
378
- console.log('Using Mailgun Sandbox Domain: ', domain);
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
- } else if (!serverEnvironmentService.isTestingEnv) {
381
- console.log('Using Mailgun Production Domain: ', domain);
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
- MailgunServiceConfig.assertValidConfig(config);
413
- return config;
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
- exports.MailgunServiceModule = __decorate([common.Module({
417
- imports: [config.ConfigModule],
418
- providers: [{
419
- provide: MailgunServiceConfig,
420
- inject: [config.ConfigService, nestjs.ServerEnvironmentService],
421
- useFactory: mailgunServiceConfigFactory
422
- }, exports.MailgunApi, exports.MailgunService],
423
- exports: [exports.MailgunApi, exports.MailgunService]
424
- })], exports.MailgunServiceModule);
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
- const fromEmail = (recipient.from?.email ?? '').toLowerCase();
437
- const replyToEmail = (recipient.replyTo?.email ?? '').toLowerCase();
438
- return `f:${fromEmail}|r:${replyToEmail}`;
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
- const {
448
- request: inputBaseRequest,
449
- useSubjectFromRecipientUserVariables,
450
- allowSingleRecipientBatchSendRequests,
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
- return cc?.length ? cc : undefined;
483
- }
484
- const baseRequestCc = determineCarbonCopyRecipients({
485
- carbonCopyRecipients: inputBaseRequest.cc,
486
- carbonCopyRecipientsKeys: inputBaseRequest.ccKeys
487
- });
488
- const baseRequestBcc = determineCarbonCopyRecipients({
489
- carbonCopyRecipients: inputBaseRequest.bcc,
490
- carbonCopyRecipientsKeys: inputBaseRequest.bccKeys
491
- });
492
- const baseRequestFrom = inputBaseRequest.from ?? mailgunRecipientBatchSendTargetEntityKeyRecipientLookup?.getRecipientOrDefaultForKey(inputBaseRequest.fromKey);
493
- const baseRequestReplyTo = inputBaseRequest.replyTo ?? mailgunRecipientBatchSendTargetEntityKeyRecipientLookup?.getRecipientOrDefaultForKey(inputBaseRequest.replyToKey);
494
- const baseRequest = {
495
- ...inputBaseRequest,
496
- from: baseRequestFrom,
497
- replyTo: baseRequestReplyTo,
498
- cc: baseRequestCc,
499
- bcc: baseRequestBcc
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
- const cc = determineCarbonCopyRecipients({
530
- baseRequestCarbonCopyRecipients: baseRequestCc,
531
- carbonCopyRecipients: recipient.cc,
532
- carbonCopyRecipientsKeys: recipient.ccKeys
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 allowBatchSend = configAllowBatchSend && (allowSingleRecipientBatchSendRequests || recipients.length > 1);
549
- const nonBatchSendRequests = [];
550
- const batchSendRequestRecipients = [];
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
- // create batch send request(s)
577
- const batchSendRequests = [];
578
- if (batchSendRequestRecipients.length > 0) {
579
- const subject = useSubjectFromRecipientUserVariables ? MAILGUN_BATCH_SEND_RECIPIENT_SUBJECT_TEMPLATE : defaultSubject;
580
- // Group recipients by their from/replyTo values
581
- const batchSendRecipientGroups = util.makeValuesGroupMap(batchSendRequestRecipients, mailgunRecipientBatchSendTargetFromReplyToBatchGroupKey);
582
- batchSendRecipientGroups.forEach(groupRecipients => {
583
- // All recipients in this group should share the same from/replyTo values
584
- const firstRecipient = groupRecipients[0];
585
- const batchRequest = {
586
- ...baseRequest,
587
- from: firstRecipient.from,
588
- replyTo: firstRecipient.replyTo,
589
- recipientVariablesConfig: baseRequest.recipientVariablesConfig ?? recipientVariablesConfig,
590
- to: groupRecipients,
591
- subject,
592
- batchSend: true
593
- };
594
- batchSendRequests.push(batchRequest);
595
- });
596
- }
597
- return util.filterMaybeArrayValues([...batchSendRequests, ...nonBatchSendRequests]);
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
- const {
608
- recipientsMap
609
- } = config;
610
- function getRecipientOrDefaultForKey(input, defaultRecipient) {
611
- let result = defaultRecipient;
612
- if (input) {
613
- result = recipientsMap.get(input) ?? defaultRecipient;
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
- return result;
616
- }
617
- function getRecipientsForKeys(input) {
618
- let result = undefined;
619
- if (input) {
620
- const keysArray = util.asArray(input);
621
- const recipients = util.filterMaybeArrayValues(keysArray.map(key => recipientsMap.get(key)));
622
- if (recipients.length > 0) {
623
- result = recipients;
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 result;
627
- }
628
- return {
629
- recipientsMap,
630
- getRecipientOrDefaultForKey,
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;