split_writefull_disabled_from_unset.mjs 1.4 KB

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