overleaf/services/web/scripts/ensure_affiliations.mjs
Liangjun Song 26f3f3e2e2 Merge pull request #21097 from overleaf/ls-scripts-to-esm-1
Migrate scripts folder to esm 1/x

GitOrigin-RevId: 4a4bc9a161f144fdb40ce3f2a0a9313b36c6df81
2024-10-21 08:04:42 +00:00

54 lines
1.3 KiB
JavaScript

import { waitForDb } from '../app/src/infrastructure/mongodb.js'
import { User } from '../app/src/models/User.js'
import UserController from '../app/src/Features/User/UserController.js'
import Logger from '@overleaf/logger'
import pLimit from 'p-limit'
Logger.logger.level('error')
const CONCURRENCY = 10
const failure = []
const success = []
console.log('Starting ensure affiliations')
const query = {
'emails.affiliationUnchecked': true,
}
async function _handleEnsureAffiliation(user) {
try {
await UserController.promises.ensureAffiliation(user)
console.log(`${user._id}`)
success.push(user._id)
} catch (error) {
failure.push(user._id)
console.log(`ERROR: ${user._id}`, error)
}
}
async function getUsers() {
return User.find(query, { emails: 1 }).exec()
}
async function run() {
const limit = pLimit(CONCURRENCY)
const users = await getUsers()
console.log(`Found ${users.length} users`)
await Promise.all(
users.map(user => limit(() => _handleEnsureAffiliation(user)))
)
console.log(`${success.length} successes`)
console.log(`${failure.length} failures`)
if (failure.length > 0) {
console.log('Failed to update:', failure)
}
}
try {
await waitForDb()
await run()
process.exit()
} catch (error) {
console.log(error)
process.exit(1)
}