invalidate_tokens.mjs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { db, ObjectId } from '../app/src/infrastructure/mongodb.js'
  2. import minimist from 'minimist'
  3. const argv = minimist(process.argv.slice(2))
  4. const commit = argv.commit !== undefined
  5. const projectIds = argv._.map(x => {
  6. return new ObjectId(x)
  7. })
  8. if (!commit) {
  9. console.log('Doing dry run without --commit')
  10. }
  11. console.log('checking', projectIds.length, 'projects')
  12. const affectedProjects = await db.projects
  13. .find(
  14. { _id: { $in: projectIds } },
  15. {
  16. projection: {
  17. _id: 1,
  18. owner_ref: 1,
  19. tokenAccessReadOnly_refs: 1,
  20. tokenAccessReadAndWrite_refs: 1,
  21. },
  22. }
  23. )
  24. .toArray()
  25. console.log('Found ' + affectedProjects.length + ' affected projects')
  26. affectedProjects.forEach(project => {
  27. console.log(JSON.stringify(project))
  28. })
  29. if (!commit) {
  30. console.log('dry run, not updating')
  31. process.exit(0)
  32. } else {
  33. try {
  34. const result = await db.projects.updateMany(
  35. { _id: { $in: affectedProjects.map(project => project._id) } },
  36. {
  37. $set: {
  38. publicAccesLevel: 'private', // note the spelling in the db is publicAccesLevel (with one 's')
  39. tokenAccessReadOnly_refs: [],
  40. tokenAccessReadAndWrite_refs: [],
  41. },
  42. }
  43. )
  44. console.log('result', JSON.stringify(result))
  45. process.exit(0)
  46. } catch (err) {
  47. console.error('err', err)
  48. process.exit(1)
  49. }
  50. }