upgrade-user-features.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { fileURLToPath } from 'node:url'
  2. import Settings from '@overleaf/settings'
  3. import logger from '@overleaf/logger'
  4. import { db, waitForDb } from '../../../app/src/infrastructure/mongodb.js'
  5. import {
  6. mergeFeatures,
  7. compareFeatures,
  8. } from '../../../app/src/Features/Subscription/FeaturesHelper.js'
  9. const DRY_RUN = !process.argv.includes('--dry-run=false')
  10. async function main(DRY_RUN, defaultFeatures) {
  11. await waitForDb()
  12. logger.info({ defaultFeatures }, 'default features')
  13. const cursor = db.users.find(
  14. {},
  15. { projection: { _id: 1, email: 1, features: 1 } }
  16. )
  17. for await (const user of cursor) {
  18. const newFeatures = mergeFeatures(user.features, defaultFeatures)
  19. const diff = compareFeatures(newFeatures, user.features)
  20. if (Object.keys(diff).length > 0) {
  21. logger.warn(
  22. {
  23. userId: user._id,
  24. email: user.email,
  25. oldFeatures: user.features,
  26. newFeatures,
  27. },
  28. 'user features upgraded'
  29. )
  30. if (!DRY_RUN) {
  31. await db.users.updateOne(
  32. { _id: user._id },
  33. { $set: { features: newFeatures } }
  34. )
  35. }
  36. }
  37. }
  38. }
  39. export default main
  40. if (fileURLToPath(import.meta.url) === 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. }