upgrade-user-features.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import Settings from '@overleaf/settings'
  2. import logger from '@overleaf/logger'
  3. import { db } from '../../../app/src/infrastructure/mongodb.js'
  4. import {
  5. mergeFeatures,
  6. compareFeatures,
  7. } from '../../../app/src/Features/Subscription/FeaturesHelper.js'
  8. import { fileURLToPath } from 'url'
  9. const DRY_RUN = !process.argv.includes('--dry-run=false')
  10. async function main(DRY_RUN, defaultFeatures) {
  11. logger.info({ defaultFeatures }, 'default features')
  12. const cursor = db.users.find(
  13. {},
  14. { projection: { _id: 1, email: 1, features: 1 } }
  15. )
  16. for await (const user of cursor) {
  17. const newFeatures = mergeFeatures(user.features, defaultFeatures)
  18. const diff = compareFeatures(newFeatures, user.features)
  19. if (Object.keys(diff).length > 0) {
  20. logger.warn(
  21. {
  22. userId: user._id,
  23. email: user.email,
  24. oldFeatures: user.features,
  25. newFeatures,
  26. },
  27. 'user features upgraded'
  28. )
  29. if (!DRY_RUN) {
  30. await db.users.updateOne(
  31. { _id: user._id },
  32. { $set: { features: newFeatures } }
  33. )
  34. }
  35. }
  36. }
  37. }
  38. export default main
  39. const filename = fileURLToPath(import.meta.url)
  40. if (filename === process.argv[1]) {
  41. if (DRY_RUN) {
  42. console.error('---')
  43. console.error('Dry-run enabled, use --dry-run=false to commit changes')
  44. console.error('---')
  45. }
  46. main(DRY_RUN, Settings.defaultFeatures)
  47. .then(() => {
  48. console.log('Done.')
  49. process.exit(0)
  50. })
  51. .catch(error => {
  52. console.error({ error })
  53. process.exit(1)
  54. })
  55. }