devcontainer_setup.mjs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { promiseMapWithLimit } from '@overleaf/promise-utils'
  2. import Settings from '@overleaf/settings'
  3. import { waitForDb, db } from '../app/src/infrastructure/mongodb.mjs'
  4. import GracefulShutdown from '../app/src/infrastructure/GracefulShutdown.mjs'
  5. import UserRegistrationHandler from '../app/src/Features/User/UserRegistrationHandler.mjs'
  6. import minimist from 'minimist'
  7. import {
  8. createProjectWithOldHistoryId,
  9. provisionSplitTests,
  10. } from './e2e_test_setup.mjs'
  11. const { email: USER_EMAIL, password: PASSWORD } = minimist(
  12. process.argv.slice(2),
  13. { string: ['email', 'password'] }
  14. )
  15. /**
  16. * @param {string} email
  17. * @return {Promise<string>}
  18. */
  19. async function createUser(email) {
  20. let userId
  21. try {
  22. const user = await UserRegistrationHandler.promises.registerNewUser({
  23. email,
  24. password: PASSWORD,
  25. })
  26. userId = user._id
  27. } catch (err) {
  28. if (err.message.includes('EmailAlreadyRegistered')) {
  29. userId = err.info.userId
  30. } else {
  31. throw err
  32. }
  33. }
  34. const features = email.startsWith('free')
  35. ? Settings.defaultFeatures
  36. : Settings.features.professional
  37. const isAdmin = email === USER_EMAIL || email === 'admin@overleaf.com'
  38. let adminRoles = []
  39. if (isAdmin) {
  40. adminRoles = ['engineering']
  41. }
  42. await db.users.updateOne(
  43. { _id: userId },
  44. {
  45. $set: {
  46. // Set admin flag.
  47. isAdmin,
  48. adminRoles,
  49. // Override features.
  50. features,
  51. featuresOverrides: [{ features }],
  52. // disable AI features, does not work with custom GH Code Spaces domain.
  53. 'aiFeatures.enabled': false,
  54. },
  55. }
  56. )
  57. return userId.toString()
  58. }
  59. /**
  60. * @param {string} email
  61. * @return {Promise<void>}
  62. */
  63. async function provisionUser(email) {
  64. const userId = await createUser(email)
  65. await createProjectWithOldHistoryId(userId)
  66. }
  67. async function provisionUsers() {
  68. const emails = [
  69. USER_EMAIL,
  70. 'admin@overleaf.com',
  71. 'free@overleaf.com',
  72. 'premium@overleaf.com',
  73. ]
  74. await promiseMapWithLimit(5, emails, provisionUser)
  75. }
  76. async function main() {
  77. if (process.env.NODE_ENV !== 'development') {
  78. throw new Error('only available in dev-env')
  79. }
  80. await waitForDb()
  81. await Promise.all([provisionUsers(), provisionSplitTests()])
  82. }
  83. if (import.meta.main) {
  84. await main()
  85. await GracefulShutdown.gracefulShutdown()
  86. }