mirror of
https://github.com/overleaf/overleaf.git
synced 2024-12-11 20:52:00 -05:00
ad52f7dc01
[misc] add waitForDb wrapper to scripts with indirect mongo access GitOrigin-RevId: 009331f91a45f075a35b0b6e90c4352e55908c21
53 lines
1.3 KiB
JavaScript
53 lines
1.3 KiB
JavaScript
const { waitForDb } = require('../app/src/infrastructure/mongodb')
|
|
const { User } = require('../app/src/models/User')
|
|
const UserController = require('../app/src/Features/User/UserController')
|
|
require('logger-sharelatex').logger.level('error')
|
|
const pLimit = require('p-limit')
|
|
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)
|
|
}
|
|
}
|
|
|
|
waitForDb()
|
|
.then(run)
|
|
.then(() => {
|
|
process.exit()
|
|
})
|
|
.catch(error => {
|
|
console.log(error)
|
|
process.exit(1)
|
|
})
|