cleanup_dangling_user_stubs.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const { db } = require('../app/src/infrastructure/mongojs')
  2. const async = require('async')
  3. const minimist = require('minimist')
  4. const UserMapper = require('../modules/overleaf-integration/app/src/OverleafUsers/UserMapper')
  5. const argv = minimist(process.argv.slice(2))
  6. const commit = argv.commit !== undefined
  7. if (!commit) {
  8. console.log('Doing dry run without --commit')
  9. }
  10. db.userstubs.aggregate(
  11. [
  12. {
  13. $lookup: {
  14. localField: 'overleaf.id',
  15. from: 'users',
  16. foreignField: 'overleaf.id',
  17. as: 'users'
  18. }
  19. },
  20. {
  21. $project: {
  22. email: 1,
  23. overleaf: 1,
  24. _id: 1,
  25. 'users.email': 1,
  26. 'users.emails': 1,
  27. 'users.overleaf': 1,
  28. 'users._id': 1
  29. }
  30. },
  31. {
  32. $match: {
  33. users: { $exists: 1 },
  34. 'overleaf.id': { $exists: 1 }
  35. }
  36. }
  37. ],
  38. (err, stubs) => {
  39. if (err) {
  40. throw err
  41. }
  42. console.log('Found ' + stubs.length + ' dangling stubs')
  43. async.mapLimit(
  44. stubs,
  45. Number(argv.limit || '10'),
  46. (stub, callback) => {
  47. if (commit) {
  48. console.log(
  49. 'Processing stub',
  50. stub._id,
  51. 'for user',
  52. stub.users[0]._id
  53. )
  54. UserMapper._updateUserStubReferences(
  55. stub.overleaf,
  56. stub._id,
  57. stub.users[0]._id,
  58. callback
  59. )
  60. } else {
  61. console.log(
  62. 'Would call UserMapper._updateUserStubReferences with:',
  63. stub.overleaf,
  64. stub._id,
  65. stub.users[0]._id
  66. )
  67. callback()
  68. }
  69. },
  70. err => {
  71. if (err) {
  72. throw err
  73. }
  74. console.log('All done')
  75. process.exit(0)
  76. }
  77. )
  78. }
  79. )