-
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
196 additions
and
32 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,119 @@ | ||
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 "; | ||
|
||
// 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(`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 mailingTopic" ); | ||
console.log( e ); | ||
throw new Error( "Bulkmailer sendBulkEmails: Can't find the topic: " + topicId ); | ||
} | ||
|
||
if ( !mailingTopic.nTemplateMailingId || !mailingTopic.notifyKey ) { | ||
console.log( " Bulkmailer -- sendBulkEmails : check mailingTopic details" ); | ||
console.log( e ); | ||
throw new Error( "Bulkmailer sendBulkEmails: Can't find the topic: " + topicId ); | ||
} | ||
|
||
let subscribers = await mailSend.getConfirmedSubscriberAsArray( topicId ); | ||
if ( !subscribers.length) { | ||
console.log( " Bulkmailer -- sendBulkEmails : No subscribers" ); | ||
console.log( e ); | ||
throw new Error( "Bulkmailer sendBulkEmails: No subscribers 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: 5, // Maximum number of retries | ||
backoff: { | ||
type: 'exponential', // Use exponential backoff | ||
delay: 5000 // Initial delay of 1 second (doubles each retry) | ||
} | ||
} | ||
); | ||
} catch (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: | ||
mongo-1: | ||
image: mongo:4.2 | ||
container_name: x-notify-mongo | ||
container_name: mongo | ||
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 | ||
- NODE_ENV=development | ||
env_file: ".env" | ||
depends_on: | ||
- mongo | ||
- 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
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