wfh_2020_notifications.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. const NotificationsHandler = require('../app/src/Features/Notifications/NotificationsHandler')
  2. const mongojs = require('../app/src/infrastructure/mongojs')
  3. const { db } = mongojs
  4. const async = require('async')
  5. const templateKey = 'wfh_2020_upgrade_offer'
  6. const key = 'wfh-2020-upgrade-2020-06-01'
  7. db.subscriptions.aggregate(
  8. { $match: { teamName: /(Work From Home|Work from Home)/ } },
  9. { $unwind: '$member_ids' },
  10. { $group: { _id: null, memberIds: { $addToSet: '$member_ids' } } },
  11. function(err, results) {
  12. if (err) {
  13. throw err
  14. }
  15. const userIds = results[0].memberIds
  16. async.eachLimit(
  17. userIds,
  18. 10,
  19. function(userId, callback) {
  20. NotificationsHandler.createNotification(
  21. userId,
  22. key,
  23. templateKey,
  24. {},
  25. null,
  26. true,
  27. function(err) {
  28. if (err) {
  29. return callback(err)
  30. }
  31. console.log('Notification created for user ' + userId)
  32. callback()
  33. }
  34. )
  35. },
  36. function() {
  37. console.log('Done')
  38. process.exit(0)
  39. }
  40. )
  41. }
  42. )