fix_group_invite_emails_to_lowercase.mjs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { db } from '../app/src/infrastructure/mongodb.js'
  2. import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
  3. import { scriptRunner } from './lib/ScriptRunner.mjs'
  4. const DRY_RUN = process.env.DRY_RUN !== 'false'
  5. console.log({
  6. DRY_RUN,
  7. })
  8. function anyInviteEmailHasUppercaseChars(subscription) {
  9. return subscription.teamInvites.some(invite => {
  10. return /[A-Z]/.test(invite.email)
  11. })
  12. }
  13. async function processBatch(subscriptions) {
  14. for (const subscription of subscriptions) {
  15. if (anyInviteEmailHasUppercaseChars(subscription)) {
  16. console.log('fixing emails in group invites for', subscription._id)
  17. if (!DRY_RUN) {
  18. await db.subscriptions.updateOne({ _id: subscription._id }, [
  19. {
  20. $set: {
  21. teamInvites: {
  22. $map: {
  23. input: '$teamInvites',
  24. in: {
  25. $mergeObjects: [
  26. '$$this',
  27. {
  28. email: {
  29. $toLower: '$$this.email',
  30. },
  31. },
  32. ],
  33. },
  34. },
  35. },
  36. },
  37. },
  38. ])
  39. }
  40. }
  41. }
  42. }
  43. async function main(trackProgress) {
  44. const projection = {
  45. _id: 1,
  46. teamInvites: 1,
  47. }
  48. const query = {
  49. 'teamInvites.0': {
  50. $exists: true,
  51. },
  52. }
  53. await batchedUpdate(
  54. db.subscriptions,
  55. query,
  56. processBatch,
  57. projection,
  58. undefined,
  59. { trackProgress }
  60. )
  61. }
  62. try {
  63. await scriptRunner(main)
  64. console.error('Done.')
  65. process.exit(0)
  66. } catch (error) {
  67. console.error({ error })
  68. process.exit(1)
  69. }