Skip to content

Instantly share code, notes, and snippets.

@SamWSoftware
Created January 1, 2018 07:37
Show Gist options
  • Save SamWSoftware/37063a5076e9351e1fcf26f3d71ca476 to your computer and use it in GitHub Desktop.
Save SamWSoftware/37063a5076e9351e1fcf26f3d71ca476 to your computer and use it in GitHub Desktop.
app.post("/api/survey/webhooks", bodyParser.urlencoded(), (req, res) => {
const p = new Path("/api/survey/:surveyId/:choice");
const { recipient: email, url, event } = req.body;
const match = p.test(new URL(url).pathname);
if (match && event === "clicked") {
Survey.updateOne(
{
_id: match.surveyId,
recipients: {
$elemMatch: { email: email, responded: false }
}
},
{
$inc: { [match.choice]: 1 },
$set: { "recipients.$.responded": true },
lastResponded: new Date()
}
).exec();
}
res.send({});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment