mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Merge pull request #3803 from overleaf/ab-regenerate-duplicate-referral-ids-script
Add script to regenerate duplicate referral IDs GitOrigin-RevId: cbcd7027f8dc342140ca5c22ed20c7814561e118
This commit is contained in:
parent
c8df1da69a
commit
c2f16621db
1 changed files with 68 additions and 0 deletions
68
services/web/scripts/regenerate_duplicate_referral_ids.js
Normal file
68
services/web/scripts/regenerate_duplicate_referral_ids.js
Normal file
|
@ -0,0 +1,68 @@
|
|||
const WRITE_CONCURRENCY = parseInt(process.env.WRITE_CONCURRENCY, 10) || 10
|
||||
|
||||
const logger = require('logger-sharelatex')
|
||||
const { db, waitForDb } = require('../app/src/infrastructure/mongodb')
|
||||
const { promiseMapWithLimit } = require('../app/src/util/promises')
|
||||
const TokenGenerator = require('../app/src/Features/TokenGenerator/TokenGenerator')
|
||||
const UserUpdater = require('../app/src/Features/User/UserUpdater')
|
||||
|
||||
async function main() {
|
||||
logger.info({}, 'Regenerating duplicate referral IDs')
|
||||
|
||||
await waitForDb()
|
||||
|
||||
const duplicates = await db.users.aggregate(
|
||||
[
|
||||
{ $match: { referal_id: { $exists: true } } },
|
||||
{ $group: { _id: '$referal_id', count: { $sum: 1 } } },
|
||||
{ $match: { count: { $gt: 1 } } }
|
||||
],
|
||||
{ allowDiskUse: true }
|
||||
)
|
||||
|
||||
const duplicateReferralIds = []
|
||||
let duplicate
|
||||
while ((duplicate = await duplicates.next())) {
|
||||
duplicateReferralIds.push(duplicate._id)
|
||||
}
|
||||
logger.info(
|
||||
{},
|
||||
`Found ${duplicateReferralIds.length} duplicate referral ID to regenerate`
|
||||
)
|
||||
|
||||
await promiseMapWithLimit(
|
||||
WRITE_CONCURRENCY,
|
||||
duplicateReferralIds,
|
||||
async referralId => {
|
||||
const users = await db.users
|
||||
.find({
|
||||
referal_id: referralId
|
||||
})
|
||||
.toArray()
|
||||
try {
|
||||
for (const user of users) {
|
||||
const newReferralId = TokenGenerator.generateReferralId()
|
||||
await UserUpdater.promises.updateUser(user._id, {
|
||||
$set: {
|
||||
referal_id: newReferralId
|
||||
}
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(
|
||||
{ err: error },
|
||||
`Failed to generate new referral ID for duplicate ID: ${referralId}`
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
main()
|
||||
.then(() => {
|
||||
process.exit(0)
|
||||
})
|
||||
.catch(error => {
|
||||
console.error({ error })
|
||||
process.exit(1)
|
||||
})
|
Loading…
Reference in a new issue