app.post('/api/surveys/webhooks', (req, res) => { const p = new Path('/api/surveys/:surveyId/:choice'); _.chain(req.body) .map(({url, email}) => { const match = p.test(new URL(url).pathname); if (match) { return {email, ...match}; } }) .compact() .uniqBy('email', 'surveyId') .each(({surveyId, email, choice}) => { Survey.updateOne({ _id: surveyId, recipients: { $elemMatch: {email, responded: false}, } }, { $inc: {[choice]: 1}, $set: {'recipients.$.responded': true}, lastResponded: new Date(), }).exec(); }) .value(); res.send({}); });
.uniqBy('source') .sortBy((f) => parseJobFromURL(f.upstream).jobid) .map((item) => ({ source: item.source, upstream: item.upstream }))
.uniqBy( 'email', 'surveyId') .each(({ surveyId, email, choice }) => { Survey.updateOne({