create_backing_accounts_for_sl_users.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * create backing accounts for sl users without an overleaf.id
  3. *
  4. * run with:
  5. * node scripts/create_backing_accounts_for_sl_users.js --async-limit=8 --commit
  6. *
  7. * async-limit is the number of concurrent users to process.
  8. */
  9. 'use strict'
  10. const SharelatexAuthHandler = require('../modules/overleaf-integration/app/js/SharelatexAuth/SharelatexAuthHandler')
  11. const UserUpdater = require('../app/js/Features/User/UserUpdater')
  12. const async = require('async')
  13. const { db } = require('../app/js/infrastructure/mongojs')
  14. const logger = require('logger-sharelatex')
  15. const minimist = require('minimist')
  16. logger.logger.level('error')
  17. const argv = minimist(process.argv.slice(2))
  18. const commit = argv.commit !== undefined
  19. const asyncLimit = parseInt(argv['async-limit']) || 1
  20. if (asyncLimit === 1) {
  21. console.log(`running in series. run with --async-limit=n to run in parallel.`)
  22. }
  23. if (!commit) {
  24. console.log(`doing dry run. run with --commit to save changes`)
  25. }
  26. const query = { 'overleaf.id': { $exists: false } }
  27. const projection = {
  28. email: 1,
  29. first_name: 1,
  30. hashedPassword: 1,
  31. last_name: 1
  32. }
  33. db.users.find(query, projection, (err, users) => {
  34. if (err) throw err
  35. async.eachLimit(users, asyncLimit, createBackingAccount, err => {
  36. if (err) throw err
  37. console.log('DONE')
  38. process.exit()
  39. })
  40. })
  41. function createBackingAccount(user, cb) {
  42. console.log(
  43. `Creating backing account for id: ${user._id}, email: ${user.email}`
  44. )
  45. if (!commit) return cb()
  46. SharelatexAuthHandler.createBackingAccount(user, (err, v1Profile) => {
  47. if (err) {
  48. console.error(
  49. `Error creating backing account for id: ${user._id}`,
  50. err.stack
  51. )
  52. return cb()
  53. }
  54. console.log(
  55. `Created backing account for id: ${user._id}, ol id: ${
  56. v1Profile.id
  57. } -- updating user`
  58. )
  59. const update = {
  60. $set: {
  61. 'overleaf.id': v1Profile.id,
  62. 'ace.overallTheme': 'light-'
  63. }
  64. }
  65. UserUpdater.updateUser(user._id, update, err => {
  66. if (err) {
  67. console.error(`Error updating user id: ${user._id}`, err.stack)
  68. return cb()
  69. }
  70. console.log(`Updated user id: ${user._id}`)
  71. cb()
  72. })
  73. })
  74. }