wfh_2020_upgrades.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 oldKey = 'wfh-2020-upgrade-2020-06-01'
  7. const key = 'wfh-2020-upgrade-2020-06-18'
  8. db.subscriptions.aggregate(
  9. { $match: { teamName: /(Work From Home|Work from Home)/ } },
  10. { $unwind: '$member_ids' },
  11. { $group: { _id: null, memberIds: { $addToSet: '$member_ids' } } },
  12. function(err, results) {
  13. if (err) {
  14. throw err
  15. }
  16. const userIds = results[0].memberIds
  17. async.eachLimit(
  18. userIds,
  19. 10,
  20. function(userId, callback) {
  21. async.series(
  22. [
  23. function(cb) {
  24. db.notifications.findOne(
  25. { user_id: userId, key: oldKey },
  26. function(err, notification) {
  27. if (err) {
  28. console.log('Error finding notification for ' + userId)
  29. cb(err)
  30. } else if (!notification) {
  31. cb()
  32. } else {
  33. if (notification.templateKey && notification.messageOpts) {
  34. db.notifications.update(
  35. {
  36. _id: notification._id
  37. },
  38. {
  39. $unset: { templateKey: true, messageOpts: true }
  40. },
  41. cb
  42. )
  43. } else {
  44. cb()
  45. }
  46. }
  47. }
  48. )
  49. },
  50. function(cb) {
  51. NotificationsHandler.createNotification(
  52. userId,
  53. key,
  54. templateKey,
  55. {},
  56. null,
  57. true,
  58. cb
  59. )
  60. }
  61. ],
  62. function(err) {
  63. if (err) {
  64. callback(err)
  65. } else {
  66. console.log('Notification created for user ' + userId)
  67. callback()
  68. }
  69. }
  70. )
  71. },
  72. function(err) {
  73. if (err) {
  74. console.log(err)
  75. process.exit(1)
  76. } else {
  77. console.log('Done')
  78. process.exit(0)
  79. }
  80. }
  81. )
  82. }
  83. )