-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mws-3953 -- bulk api with subscribers and topic details
- Loading branch information
Showing
11 changed files
with
205 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
const fetch = require('node-fetch'); | ||
const mailingManager = require('./mailing'); | ||
const mailSend = require("../helpers/mailSend"); | ||
const { bulkQueue } = require("../notifyQueue"); | ||
|
||
const BASE_URL = process.env.BASE_URL || "https://apps.canada.ca/x-notify"; | ||
const bulkAPI = "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) || 20; | ||
const BULK_Q_TYPE = process.env.BULK_Q_TYPE || "exponential"; | ||
const BULK_Q_DELAY = parseInt(process.env.BULK_Q_DELAY) || 300000; // 5 min | ||
|
||
// Process jobs | ||
bulkQueue.process(async (job) => { | ||
try { | ||
let mailingState = mailingManager.mailingState; | ||
let jobData = job.data; | ||
|
||
// Making the Bulk API POST request | ||
let response = await fetch(bulkAPI, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
"Authorization" : BULK_GC_NOTIFY_PREPEND + jobData.notifyKey | ||
}, | ||
body: JSON.stringify( jobData.bulkEmailBody ), | ||
}).then(response => { | ||
if (!response.ok) { | ||
throw new Error(`HTTP Error Status: ${response.status}`); | ||
} | ||
return response.json(); | ||
}) | ||
.then( result => { | ||
mailingManager.mailingUpdate( jobData.mailingId, mailingState.sent, { historyState: mailingState.sending } ); | ||
}) | ||
|
||
} catch (error) { | ||
if (error.message.includes('HTTP Error Status: 5')) { | ||
throw new Error('Retryable error'); // Ensures Bull retries | ||
} | ||
} | ||
}); | ||
|
||
// Listen for failures | ||
bulkQueue.on('failed', (job, err) => { | ||
console.error(` bulkQueue Job ${job.id} failed: ${err.message}`); | ||
}); | ||
|
||
exports.sendBulkEmails = async ( mailingId, topicId, subject, mailingBody ) => { | ||
try { | ||
let mailing_name = "Bulk_email-" + topicId; | ||
let 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 mailSend.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 ); | ||
} | ||
|
||
let formattedSubsArray = await formatSubsArray( subscribers, mailingBody, subject); | ||
let bulkEmailBody = { | ||
"name": mailing_name, | ||
"template_id": mailingTopic.nTemplateMailingId, | ||
"rows": formattedSubsArray | ||
} | ||
|
||
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) | ||
} | ||
} | ||
); | ||
} catch (err) { | ||
console.log( 'sendBulkEmails error: ') | ||
console.log( err ) | ||
throw Error('sendBulkEmails error: ' + err, 500) | ||
} | ||
} | ||
|
||
formatSubsArray = async ( listEmail, mailingBody, subject) => { | ||
|
||
let i, i_len = listEmail.length, subscriber; | ||
let subsArray = [ | ||
["subject", "email address", "body", "unsub_link"] | ||
] | ||
for( i = 0; i !== i_len; i++) { | ||
subscriber = listEmail[ i ]; | ||
|
||
const { email, _id } = subscriber; | ||
|
||
const userCodeUrl = ( _id ? _id.toHexString() : _id ); | ||
|
||
if ( !email || !userCodeUrl ) { | ||
continue; | ||
} | ||
|
||
let unsub_link = BASE_URL + "/subs/remove/" + userCodeUrl | ||
subsArray.push( [subject, email, mailingBody, unsub_link] ) | ||
} | ||
|
||
return subsArray | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,48 @@ | ||
version: '3.8' | ||
services: | ||
mongo: | ||
notify-mongo-1: | ||
image: mongo:4.2 | ||
container_name: x-notify-mongo | ||
container_name: notify-mongo-1 | ||
ports: | ||
- target: 27017 | ||
published: 27016 | ||
protocol: tcp | ||
mode: host | ||
networks: | ||
- x-notify-net | ||
redis: | ||
- notify-net-1 | ||
volumes: | ||
- mongo_data:/data/db # Persistent volume for MongoDB | ||
- mongo_config:/data/configdb # Configuration volume | ||
|
||
notify-redis-1: | ||
image: redis:6.0.1 | ||
container_name: x-notify-redis | ||
container_name: notify-redis-1 | ||
ports: | ||
- "6379:6379" | ||
networks: | ||
- x-notify-net | ||
x-notify: | ||
- notify-net-1 | ||
notify-node-1: | ||
build: ./ | ||
container_name: x-notify | ||
container_name: notify-node-1 | ||
ports: | ||
- "8080:8080" | ||
restart: on-failure | ||
environment: | ||
- MONGODB_URI=mongodb://mongo:27017/test | ||
- MONGODB_URI=mongodb://notify-mongo-1 | ||
- NODE_ENV=development | ||
env_file: ".env" | ||
depends_on: | ||
- mongo | ||
- notify-mongo-1 | ||
- notify-redis-1 | ||
volumes: | ||
- .:/x-notify | ||
- .:/notify-1 | ||
- /node_modules | ||
networks: | ||
- x-notify-net | ||
- notify-net-1 | ||
volumes: | ||
mongo_data: | ||
mongo_config: | ||
|
||
networks: | ||
x-notify-net: | ||
notify-net-1: | ||
driver: bridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.