sync_v1_subscriptions.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. let USER_QUERY = null
  2. if (process.argv[2] === '--all') {
  3. USER_QUERY = {}
  4. } else if (process.argv[2] === '--v1-only') {
  5. USER_QUERY = { 'overleaf.id': { $exists: true } }
  6. } else {
  7. console.error(`Usage:\n\n\tnode ${process.argv[1]} (--all|--v1-only)`)
  8. process.exit(1)
  9. }
  10. const {db} = require('../app/js/infrastructure/mongojs')
  11. const FeaturesUpdater = require(
  12. '../app/js/Features/Subscription/FeaturesUpdater'
  13. )
  14. const V1SubscriptionManager = require(
  15. '../app/js/Features/Subscription/V1SubscriptionManager'
  16. )
  17. const async = require('async')
  18. const logger = require('logger-sharelatex')
  19. logger.logger.level('error')
  20. const areFeaturesEqual = function (featuresA, featuresB) {
  21. for (const feature in featuresA) {
  22. if (featuresA[feature] !== featuresB[feature]) {
  23. return false
  24. }
  25. }
  26. return true
  27. }
  28. var outOfSyncUserCount = 0
  29. var userCount = null
  30. db.users.find(USER_QUERY, {
  31. overleaf: 1,
  32. features: 1
  33. }, function (error, users) {
  34. if (error) throw error
  35. console.log('USER COUNT', userCount = users.length)
  36. async.mapSeries(users, function (user, callback) {
  37. console.log('REFRESHING IN v2', user._id)
  38. FeaturesUpdater.refreshFeatures(user._id, false, function (error) {
  39. if (error) console.error('ERROR', error)
  40. console.log('REFRESHING IN v1', user._id)
  41. V1SubscriptionManager.notifyV1OfFeaturesChange(
  42. user._id,
  43. function (error) {
  44. if (error) console.error('ERROR', error)
  45. db.users.find({
  46. _id: user._id
  47. }, {
  48. features: 1
  49. }, function (error, [updatedUser]) {
  50. if (error) throw error
  51. if (areFeaturesEqual(user.features, updatedUser.features)) {
  52. console.log('UNCHANGED', user._id)
  53. } else {
  54. console.log('MODIFIED', user._id)
  55. outOfSyncUserCount = outOfSyncUserCount + 1
  56. }
  57. callback()
  58. })
  59. }
  60. )
  61. })
  62. }, function (error) {
  63. if (error) throw error
  64. console.log('FINISHED!')
  65. console.log('OUT OF SYNC USERS', outOfSyncUserCount, '/', userCount)
  66. process.exit()
  67. })
  68. })