enable_wf_autoCreatedAccount.mjs 1.2 KB

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