-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbulkApiMailer.js
More file actions
286 lines (241 loc) · 9.5 KB
/
Copy pathbulkApiMailer.js
File metadata and controls
286 lines (241 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
const MongoClient = require('mongodb').MongoClient;
const mailingManager = require('./mailing');
const { bulkQueue } = require("../notifyQueue");
const { letUsKnow } = require("./subscriptions");
const BASE_URL = process.env.BASE_URL || "https://apps.canada.ca/x-notify";
const BULK_API = process.env.BULK_API || "https://api.notification.canada.ca/v2/notifications/bulk";
const BULK_GC_NOTIFY_PREPEND = process.env.BULK_GC_NOTIFY_PREPEND || "ApiKey-v1 ";
const BULK_Q_ATTEMPTS = parseInt(process.env.BULK_Q_ATTEMPTS) || 5;
const BULK_Q_TYPE = process.env.BULK_Q_TYPE || "exponential";
const BULK_Q_DELAY = parseInt(process.env.BULK_Q_DELAY) || 5 * 60 * 1000; // 5 min
const BULK_Q_MAX_EMAIL_BATCH_SIZE = parseInt(process.env.BULK_Q_MAX_EMAIL_BATCH_SIZE) || 45000;
const BULK_Q_JOB_DELAY_TIME = parseInt(process.env.BULK_Q_JOB_DELAY_TIME) || 1 * 60 * 1000; // 1 min
const _notifyUsTimeLimit = parseInt(process.env.notifyUsTimeLimit) || 180000;
const BULK_Q_REMOVE_ON_COMP = parseInt(process.env.BULK_Q_REMOVE_ON_COMP) || 500;
const BULK_Q_REMOVE_ON_FAIL = parseInt(process.env.BULK_Q_REMOVE_ON_FAIL) || 2500;
const _subsLinkSuffix = process.env.subsLinkSuffix || "853e0212b92a127";
let mongoInstance,
dbConn,
_notifyUsNotBeforeTimeLimit = 0;
bulkQueue.process(async (job) => {
let jobData, emailLength,
jobSuccess=false;
try {
const mailingState = mailingManager.mailingState;
jobData = job.data;
emailLength = Buffer.byteLength( JSON.stringify(jobData.bulkEmailBody) , "utf8" );
let response = await fetch( BULK_API, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
"Authorization" : BULK_GC_NOTIFY_PREPEND + jobData.notifyKey
},
body: JSON.stringify( jobData.bulkEmailBody ),
});
if (!response.ok) {
console.error(`Bulk API failed with status: ${response.status} - HTTP Error Status: ${response.statusText}`);
let errorDetails = {};
try {
errorDetails = await response.json();
} catch (parseErr) {
console.error('Failed to parse error body:', parseErr);
}
const err = new Error(`GC Notify Bulk API error: ${response.status} ${response.statusText}`);
// Attach status + parsed body to error object
err.httpStatus = response.status;
err.statusText = response.statusText;
err.errorBody = errorDetails?.errors?.[0] || errorDetails || errorDetails.toString();
err.errorCode = errorDetails?.errors?.[0]?.code || errorDetails?.status_code || null;
err.errorMessage = errorDetails?.errors?.[0]?.message || errorDetails?.message ||response.statusText;
console.error(` --------------------> GC Notify Bulk API error: `);
console.error(err);
throw err;
} else {
jobSuccess = true;
// If request is successful, update mailing status
mailingManager.mailingUpdate(jobData.mailingId, mailingState.sent, { historyState: mailingState.sending });
return await response.json();
}
} catch ( error ) {
const currDate = new Date(),
currDateTime = currDate.getTime();
const httpStatus = error.httpStatus || null;
const errCode = error.errorCode || error.code || null;
const errMsg = error.errorMessage || error.message;
const errDetails = error.errorBody ? JSON.stringify(error.errorBody) : error.toString();
// Connect to MongoDB
mongoInstance = await MongoClient.connect(process.env.MONGODB_URI || '', { useUnifiedTopology: true });
dbConn = mongoInstance.db(process.env.MONGODB_NAME || 'subs');
// Log the error into MongoDB
if ( dbConn ) {
try {
await dbConn.collection("notify_logs").insertOne({
createdAt: currDate,
jobData: job.data,
errorMessage: errMsg, // GCNotify message
http_status: httpStatus, // HTTP status
notify_Errorcode: errCode, // GC Notify error code or Node error.code
errDetails: errDetails, // full JSON/error string
emailLength: emailLength,
});
} catch ( dbError ) {
console.error( "Failed to log error in notify_logs:", dbError );
throw new Error ( "Bulk Queue process DB error " + dbError.message )
}
}
//
// Try to email us (only with the predefined interval)
//
if ( _notifyUsNotBeforeTimeLimit <= currDateTime ) {
letUsKnow( "Bulk Queue error " + error.message + " - " + errMsg, {
type: "bulk_q_process_error",
currTime: currDateTime,
lastTime: _notifyUsNotBeforeTimeLimit
},
true );
// Readjust the limit for the next period
_notifyUsNotBeforeTimeLimit = currDateTime + _notifyUsTimeLimit;
}
// Handle retryable errors
if ( error.message.includes("GC Notify Bulk API error: 5")) {
console.log("===============================>>>>>>>>>>>>>>>>================ Retrying job due to server error...");
throw new Error("Retryable error"); // Ensures Bull retries
}
} finally {
// Close MongoDB connection if it was opened
if (mongoInstance) {
mongoInstance.close();
}
const jobRetries = job.opts.attempts || 1;
//Wait for next job to be processed only after max retires (BULK_Q_ATTEMPTS)
if ( jobSuccess || job.attemptsMade >= jobRetries-1) {
await new Promise(resolve => setTimeout( resolve, BULK_Q_JOB_DELAY_TIME )); // delay between each API call set by CDS
}
}
});
// Listen for failures
bulkQueue.on('failed', (job, err) => {
console.error(`bulkQueue Job ${job.id} failed: ${err.message}`);
console.log(err)
});
exports.sendBulkEmails = async ( mailingId, topicId ) => {
let mailingTopic, mailing_name, emailLength, bulkEmailBody;
try {
mailing_name = "Bulk_email-" + topicId;
mailingTopic = await mailingManager.getTopic( topicId );
if ( !mailingTopic ) {
console.log( " Bulkmailer -- sendBulkEmails: no mailingTopic found with: " + topicId);
throw new Error( "Bulkmailer sendBulkEmails: no mailingTopic found with: " + topicId );
}
if ( !mailingTopic.nTemplateMailingId || !mailingTopic.notifyKey ) {
console.log( " Bulkmailer -- sendBulkEmails: check mailingTopic details with topicId: " + topicId );
throw new Error( "Bulkmailer -- sendBulkEmails: check mailingTopic details with topicId: " + topicId );
}
let subscribers = await getConfirmedSubscriberAsArray( topicId );
if ( !subscribers.length) {
console.log( " Bulkmailer -- sendBulkEmails : No subscribers found for the topic: " + topicId );
throw new Error( "Bulkmailer -- sendBulkEmails: No subscribers found for the topic: " + topicId );
}
for (let i = 0; i < subscribers.length; i += BULK_Q_MAX_EMAIL_BATCH_SIZE) {
const batchSubs = subscribers.slice(i, i + BULK_Q_MAX_EMAIL_BATCH_SIZE);
let formattedSubsArray = await formatSubsArray( batchSubs );
bulkEmailBody = {
"name": mailing_name,
"template_id": mailingTopic.nTemplateMailingId,
"rows": formattedSubsArray
};
emailLength = Buffer.byteLength( JSON.stringify(bulkEmailBody) , "utf8" );
bulkQueue.add(
{
bulkEmailBody: bulkEmailBody,
notifyKey: mailingTopic.notifyKey,
mailingId: mailingId,
},
{
attempts: BULK_Q_ATTEMPTS, // Maximum number of retries
backoff: {
type: BULK_Q_TYPE, // Use exponential backoff or fixed
delay: BULK_Q_DELAY // Initial delay of 1 second (doubles each retry)
},
removeOnComplete: BULK_Q_REMOVE_ON_COMP,
removeOnFail: BULK_Q_REMOVE_ON_FAIL
}
);
}
} catch (err) {
console.error(" bulkApiMailer -- sendBulkEmails error ")
console.log(err.message)
const currDate = new Date(),
currDateTime = currDate.getTime();
mongoInstance = await MongoClient.connect(process.env.MONGODB_URI || '', { useUnifiedTopology: true });
dbConn = mongoInstance.db(process.env.MONGODB_NAME || 'subs');
dbConn.collection( "notify_logs" ).insertOne(
{
createdAt: currDate,
e: err.message,
error: err.toString(),
nTemplateMailingId: mailingTopic.nTemplateMailingId,
emailLength: emailLength,
bulkEmailBody: bulkEmailBody
}
).catch( (e2) => {
console.log( "bulkApiMailer -- sendBulkEmails: notify_logs: " + userCodeUrl );
console.log( e2 );
console.log( err );
});
//
// Try to email us (only with the predefined interval)
//
if ( _notifyUsNotBeforeTimeLimit <= currDateTime ) {
letUsKnow( "Adding Bulk Queue error", {
type: "bulk_q_add_error",
currTime: currDateTime,
lastTime: _notifyUsNotBeforeTimeLimit
},
true );
// Readjust the limit for the next period
_notifyUsNotBeforeTimeLimit = currDateTime + _notifyUsTimeLimit;
}
}
}
formatSubsArray = async ( listEmail ) => {
let i, i_len = listEmail.length, subscriber;
let subsArray = [
[ "email address", "unsub_link" ]
];
for( i = 0; i !== i_len; i++) {
subscriber = listEmail[ i ];
const { email, subscode } = subscriber;
const userCodeUrl = ( subscode ? subscode.toHexString() : subscode );
if ( !email || !userCodeUrl ) {
continue;
}
let unsub_link = BASE_URL + "/subs/remove/" + userCodeUrl + "/" + _subsLinkSuffix;
subsArray.push( [ email, unsub_link ] );
}
return subsArray;
}
/*
* Utilities function
*/
getConfirmedSubscriberAsArray = async ( topicId ) => {
mongoInstance = await MongoClient.connect(process.env.MONGODB_URI || '', { useUnifiedTopology: true });
dbConn = mongoInstance.db(process.env.MONGODB_NAME || 'subs');
// Get all the emails for the given topic
let docs = await dbConn.collection( "subsConfirmed" ).find(
{
topicId: topicId
},
{
projection: {
email: 1,
subscode: 1
}
}
);
let docsItems = await docs.toArray();
if (mongoInstance) {
mongoInstance.close();
}
return docsItems;
};