sync_v1_subscriptions.js 1.8 KB

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