overleaf/services/web/scripts/fix_group_invite_emails_to_lowercase.mjs
Jakob Ackermann 252533b2fd Merge pull request #21668 from overleaf/jpa-mongo-utils
[mongo-utils] create new home for batchedUpdate helper

GitOrigin-RevId: 9f61c5e367a9f4cef63b5cc4c0bbbd3ef57c8ca8
2024-11-08 09:07:09 +00:00

67 lines
1.5 KiB
JavaScript

import { db } from '../app/src/infrastructure/mongodb.js'
import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
const DRY_RUN = process.env.DRY_RUN !== 'false'
console.log({
DRY_RUN,
})
function anyInviteEmailHasUppercaseChars(subscription) {
return subscription.teamInvites.some(invite => {
return /[A-Z]/.test(invite.email)
})
}
async function processBatch(subscriptions) {
for (const subscription of subscriptions) {
if (anyInviteEmailHasUppercaseChars(subscription)) {
console.log('fixing emails in group invites for', subscription._id)
if (!DRY_RUN) {
await db.subscriptions.updateOne({ _id: subscription._id }, [
{
$set: {
teamInvites: {
$map: {
input: '$teamInvites',
in: {
$mergeObjects: [
'$$this',
{
email: {
$toLower: '$$this.email',
},
},
],
},
},
},
},
},
])
}
}
}
}
async function main() {
const projection = {
_id: 1,
teamInvites: 1,
}
const query = {
'teamInvites.0': {
$exists: true,
},
}
await batchedUpdate(db.subscriptions, query, processBatch, projection)
}
try {
await main()
console.error('Done.')
process.exit(0)
} catch (error) {
console.error({ error })
process.exit(1)
}