Merge pull request #2643 from overleaf/jel-affiliations-cron-job
Ensure affiliations cron job GitOrigin-RevId: 4ac6f8b29b1e1460d627a86172fcdf1fa27a59a8
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
const { User } = require('../app/src/models/User')
|
||||
const UserUpdater = require('../app/src/Features/User/UserUpdater')
|
||||
require('logger-sharelatex').logger.level('error')
|
||||
const pLimit = require('p-limit')
|
||||
const CONCURRENCY = 10
|
||||
const unexpectedUserStates = []
|
||||
|
||||
console.log('Starting SSO affiliation backfill')
|
||||
|
||||
const query = {
|
||||
emails: {
|
||||
$elemMatch: {
|
||||
samlProviderId: { $exists: true },
|
||||
confirmedAt: { $exists: false }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function backfillAffiliation(user) {
|
||||
const ssoEmail = user.emails.filter(
|
||||
emailData => !emailData.confirmedAt && emailData.samlProviderId
|
||||
)
|
||||
if (ssoEmail.length > 1 || ssoEmail.length === 0) {
|
||||
unexpectedUserStates.push(user._id)
|
||||
}
|
||||
const { email } = ssoEmail[0]
|
||||
await UserUpdater.promises.confirmEmail(user._id, email)
|
||||
}
|
||||
|
||||
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(() => backfillAffiliation(user))))
|
||||
console.log('Finished')
|
||||
console.log(
|
||||
`Found ${unexpectedUserStates.length} in unexpected states`,
|
||||
unexpectedUserStates
|
||||
)
|
||||
}
|
||||
|
||||
run()
|
||||
.then(() => {
|
||||
process.exit()
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error)
|
||||
process.exit(1)
|
||||
})
|
||||
@@ -0,0 +1,51 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
run()
|
||||
.then(() => {
|
||||
process.exit()
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error)
|
||||
process.exit(1)
|
||||
})
|
||||
Reference in New Issue
Block a user