enable_wf_autoCreatedAccount.mjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { db } from '../../app/src/infrastructure/mongodb.js'
  2. import mongodb from 'mongodb-legacy'
  3. import fs from 'node:fs'
  4. import { fileURLToPath } from 'node:url'
  5. import { chunkArray } from '../helpers/chunkArray.mjs'
  6. import { scriptRunner } from '../lib/ScriptRunner.mjs'
  7. const { ObjectId } = mongodb
  8. async function main() {
  9. // search for file of users to transition
  10. const userIdsPath = process.argv[2]
  11. const userIdsFile = fs.readFileSync(userIdsPath, 'utf8')
  12. let userIdsList = userIdsFile
  13. userIdsList = userIdsList
  14. .split('\n')
  15. .filter(id => id?.length)
  16. .map(id => new ObjectId(id))
  17. const chunks = chunkArray(userIdsList)
  18. console.log(
  19. `transitioning ${userIdsList.length} users to auto-account-created state in ${chunks.length} chunks`
  20. )
  21. // Iterate over each chunk and update their autoAccountCreated flag
  22. for (const chunkedIds of chunks) {
  23. console.log('batch update started')
  24. await db.users.updateMany(
  25. { _id: { $in: chunkedIds } },
  26. { $set: { 'writefull.autoCreatedAccount': true } }
  27. )
  28. console.log('batch completed')
  29. }
  30. }
  31. export default main
  32. if (fileURLToPath(import.meta.url) === process.argv[1]) {
  33. try {
  34. await scriptRunner(main)
  35. process.exit(0)
  36. } catch (error) {
  37. console.error({ error })
  38. process.exit(1)
  39. }
  40. }