mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Merge pull request #11669 from overleaf/tm-group-invite-emails-to-lowercase
Add migration that normalises group invite emails to lowercase GitOrigin-RevId: 6352848b2bf13b2eb00ce5c9bff21a3f421c82b6
This commit is contained in:
parent
7bae6f1397
commit
9ff8d93ab8
1 changed files with 70 additions and 0 deletions
70
services/web/scripts/fix_group_invite_emails_to_lowercase.js
Normal file
70
services/web/scripts/fix_group_invite_emails_to_lowercase.js
Normal file
|
@ -0,0 +1,70 @@
|
|||
const DRY_RUN = process.env.DRY_RUN !== 'false'
|
||||
|
||||
const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
|
||||
const { batchedUpdate } = require('./helpers/batchedUpdate')
|
||||
|
||||
console.log({
|
||||
DRY_RUN,
|
||||
})
|
||||
|
||||
function anyInviteEmailHasUppercaseChars(subscription) {
|
||||
return subscription.teamInvites.some(invite => {
|
||||
return /[A-Z]/.test(invite.email)
|
||||
})
|
||||
}
|
||||
|
||||
async function processBatch(_, subscriptions) {
|
||||
subscriptions.forEach(subscription => {
|
||||
if (anyInviteEmailHasUppercaseChars(subscription)) {
|
||||
console.log('fixing emails in group invites for', subscription._id)
|
||||
if (!DRY_RUN) {
|
||||
db.subscriptions.updateOne({ _id: subscription._id }, [
|
||||
{
|
||||
$set: {
|
||||
teamInvites: {
|
||||
$map: {
|
||||
input: '$teamInvites',
|
||||
in: {
|
||||
$mergeObjects: [
|
||||
'$$this',
|
||||
{
|
||||
email: {
|
||||
$toLower: '$$this.email',
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await waitForDb()
|
||||
|
||||
const projection = {
|
||||
_id: 1,
|
||||
teamInvites: 1,
|
||||
}
|
||||
const query = {
|
||||
'teamInvites.0': {
|
||||
$exists: true,
|
||||
},
|
||||
}
|
||||
await batchedUpdate('subscriptions', query, processBatch, projection)
|
||||
}
|
||||
|
||||
main()
|
||||
.then(() => {
|
||||
console.error('Done.')
|
||||
process.exit(0)
|
||||
})
|
||||
.catch(error => {
|
||||
console.error({ error })
|
||||
process.exit(1)
|
||||
})
|
Loading…
Reference in a new issue