split_writefull_disabled_from_unset.mjs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { db } from '../../app/src/infrastructure/mongodb.mjs'
  2. import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
  3. import mongodb from 'mongodb-legacy'
  4. import fs from 'node:fs'
  5. import { fileURLToPath } from 'node:url'
  6. import { chunkArray } from '../helpers/chunkArray.mjs'
  7. import { scriptRunner } from '../lib/ScriptRunner.mjs'
  8. const { ObjectId } = mongodb
  9. async function main(trackProgress) {
  10. // search for file of users who already explicitly opted out first
  11. const optOutPath = process.argv[2]
  12. const optedOutFile = fs.readFileSync(optOutPath, 'utf8')
  13. let optedOutList = optedOutFile
  14. optedOutList = optedOutFile.split('\n').map(id => new ObjectId(id))
  15. console.log(`preserving opt-outs of ${optedOutList.length} users`)
  16. // update all applicable user models
  17. await batchedUpdate(
  18. db.users,
  19. { 'writefull.enabled': false }, // and is false
  20. { $set: { 'writefull.enabled': null } },
  21. undefined,
  22. undefined,
  23. { trackProgress }
  24. )
  25. const chunks = chunkArray(optedOutList)
  26. // then reset any explicit false back to being false
  27. // Iterate over each chunk and perform the query
  28. for (const chunkedIds of chunks) {
  29. console.log('batch update started')
  30. await db.users.updateMany(
  31. { _id: { $in: chunkedIds } },
  32. { $set: { 'writefull.enabled': false } }
  33. )
  34. console.log('batch completed')
  35. }
  36. }
  37. export default main
  38. if (fileURLToPath(import.meta.url) === process.argv[1]) {
  39. try {
  40. await scriptRunner(main)
  41. process.exit(0)
  42. } catch (error) {
  43. console.error({ error })
  44. process.exit(1)
  45. }
  46. }