fix_group_invite_emails_to_lowercase.mjs 1.5 KB

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