2024-10-31 08:16:54 -04:00
|
|
|
import { db } from '../app/src/infrastructure/mongodb.js'
|
2024-11-08 03:34:14 -05:00
|
|
|
import { batchedUpdate } from '@overleaf/mongo-utils/batchedUpdate.js'
|
2023-02-07 08:00:11 -05:00
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
const DRY_RUN = process.env.DRY_RUN !== 'false'
|
2023-02-07 08:00:11 -05:00
|
|
|
|
|
|
|
console.log({
|
|
|
|
DRY_RUN,
|
|
|
|
})
|
|
|
|
|
|
|
|
function anyInviteEmailHasUppercaseChars(subscription) {
|
|
|
|
return subscription.teamInvites.some(invite => {
|
|
|
|
return /[A-Z]/.test(invite.email)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-03-22 07:21:47 -04:00
|
|
|
async function processBatch(subscriptions) {
|
2023-02-08 09:36:30 -05:00
|
|
|
for (const subscription of subscriptions) {
|
2023-02-07 08:00:11 -05:00
|
|
|
if (anyInviteEmailHasUppercaseChars(subscription)) {
|
|
|
|
console.log('fixing emails in group invites for', subscription._id)
|
|
|
|
if (!DRY_RUN) {
|
2023-02-08 04:21:01 -05:00
|
|
|
await db.subscriptions.updateOne({ _id: subscription._id }, [
|
2023-02-07 08:00:11 -05:00
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
teamInvites: {
|
|
|
|
$map: {
|
|
|
|
input: '$teamInvites',
|
|
|
|
in: {
|
|
|
|
$mergeObjects: [
|
|
|
|
'$$this',
|
|
|
|
{
|
|
|
|
email: {
|
|
|
|
$toLower: '$$this.email',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
2023-02-08 09:36:30 -05:00
|
|
|
}
|
2023-02-07 08:00:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
const projection = {
|
|
|
|
_id: 1,
|
|
|
|
teamInvites: 1,
|
|
|
|
}
|
|
|
|
const query = {
|
|
|
|
'teamInvites.0': {
|
|
|
|
$exists: true,
|
|
|
|
},
|
|
|
|
}
|
2024-11-08 03:34:14 -05:00
|
|
|
await batchedUpdate(db.subscriptions, query, processBatch, projection)
|
2023-02-07 08:00:11 -05:00
|
|
|
}
|
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
try {
|
|
|
|
await main()
|
|
|
|
console.error('Done.')
|
|
|
|
process.exit(0)
|
|
|
|
} catch (error) {
|
|
|
|
console.error({ error })
|
|
|
|
process.exit(1)
|
|
|
|
}
|