forked from mozilla/blurts-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
for mozilla#251: remove subscribers on bounce or complaint
- Loading branch information
1 parent
872d5f8
commit c2b9953
Showing
9 changed files
with
1,905 additions
and
1,727 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
"use strict"; | ||
|
||
const DB = require("../db/DB"); | ||
|
||
|
||
const notification = async function(req, res) { | ||
try { | ||
const notification = JSON.parse(req.body); | ||
// TODO: verifyNotification(notification) or use http basic auth | ||
await handleNotification(notification); | ||
|
||
res.status(200).json( | ||
{status: "OK"} | ||
); | ||
} catch (e) { | ||
res.status(500).json( | ||
{info: "Internal error."} | ||
); | ||
} | ||
}; | ||
|
||
|
||
const handleNotification = async function(notification) { | ||
console.log("Received SES message, ID: ", notification.MessageId); | ||
const message = JSON.parse(notification.Message); | ||
switch (message.eventType) { | ||
case "Bounce": | ||
await handleBounceMessage(message); | ||
break; | ||
case "Complaint": | ||
await handleComplaintMessage(message); | ||
break; | ||
default: | ||
console.log("Unhandled eventType: ", message.eventType); | ||
} | ||
}; | ||
|
||
|
||
const handleBounceMessage = async function(message) { | ||
const bounce = message.bounce; | ||
if ( | ||
bounce.bounceType === "Permanent" && | ||
["General", "NoEmail"].includes(bounce.bounceSubType) | ||
) { | ||
return await removeSubscribersFromDB(bounce.bouncedRecipients); | ||
} | ||
}; | ||
|
||
|
||
const handleComplaintMessage = async function(message) { | ||
const complaint = message.complaint; | ||
return await removeSubscribersFromDB(complaint.complainedRecipients); | ||
}; | ||
|
||
|
||
const removeSubscribersFromDB = async function(recipients) { | ||
for (const recipient of recipients) { | ||
await DB.removeSubscriber(recipient.emailAddress); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
notification, | ||
handleNotification, | ||
handleBounceMessage, | ||
handleComplaintMessage, | ||
removeSubscribersFromDB, | ||
}; |
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.