2020-10-05 04:29:37 -04:00
|
|
|
const { waitForDb } = require('../app/src/infrastructure/mongodb')
|
2020-03-11 09:21:44 -04:00
|
|
|
const { User } = require('../app/src/models/User')
|
|
|
|
const UserController = require('../app/src/Features/User/UserController')
|
2021-11-10 08:40:18 -05:00
|
|
|
require('@overleaf/logger').logger.level('error')
|
2020-03-11 09:21:44 -04:00
|
|
|
const pLimit = require('p-limit')
|
|
|
|
const CONCURRENCY = 10
|
|
|
|
const failure = []
|
|
|
|
const success = []
|
|
|
|
console.log('Starting ensure affiliations')
|
|
|
|
|
|
|
|
const query = {
|
2021-04-27 03:52:58 -04:00
|
|
|
'emails.affiliationUnchecked': true,
|
2020-03-11 09:21:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 04:29:37 -04:00
|
|
|
waitForDb()
|
|
|
|
.then(run)
|
2020-03-11 09:21:44 -04:00
|
|
|
.then(() => {
|
|
|
|
process.exit()
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.log(error)
|
|
|
|
process.exit(1)
|
|
|
|
})
|