overleaf/services/web/scripts/fix_group_invite_emails_to_lowercase.mjs
Jakob Ackermann a7517eefcb Merge pull request #21427 from overleaf/jpa-populate-db-on-import
[web] populate db with collections on import, ahead of waitForDb() call

GitOrigin-RevId: 7eb4cd61c2052187acd9947d7060f54d9822d314
2024-11-01 09:05:15 +00:00

68 lines
1.6 KiB
JavaScript

import { db } from '../app/src/infrastructure/mongodb.js'
import BatchedUpdateModule from './helpers/batchedUpdate.mjs'
const { batchedUpdate } = BatchedUpdateModule
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('subscriptions', query, processBatch, projection)
}
try {
await main()
console.error('Done.')
process.exit(0)
} catch (error) {
console.error({ error })
process.exit(1)
}