Skip to content

Commit

Permalink
update database router on webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
bipinstha7 committed Feb 23, 2019
1 parent fa8a9fb commit e512699
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions routes/surveyRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { uniqBy } = require('lodash')
const requireLogin = require('../middlewares/requireLogin')
const requireCredits = require('../middlewares/requireCredits')
const surveyTemplate = require('../services/emailTemplates/surveyTemplate')
const Survey = require('../models/Survey')

const keys = require('../config/keys')

Expand Down Expand Up @@ -63,6 +64,20 @@ module.exports = app => {
const compactEvents = events.filter(event => event !== undefined)
const uniqueEvents = uniqBy(compactEvents, 'email', 'surveyId')

uniqueEvents.map(({ email, surveyId, choice }) => {
Survey.updateOne({
_id: surveyId,
recipients: {
$elemMatch: {email: email, responded: false}
}
},
{
$inc: {[choice]: 1},
$set: {'recipients.$.responded': true}
}
).exec()
})

res.send({})
})
}

0 comments on commit e512699

Please sign in to comment.