[web] cleanup archived split-test assignments from user record on login (#33365)

* [web] cleanup archived split-test assignments from user record on login

Co-authored-by: Anna Claire Fields <anna.fields@overleaf.com>

* [migrations] purge archived split tests from all users

Co-authored-by: Anna Claire Fields <anna.fields@overleaf.com>

* [web] add missing mock and update snapshot test

* [web] gracefully access db.users.splitTests

---------

Co-authored-by: Anna Claire Fields <anna.fields@overleaf.com>
GitOrigin-RevId: bd185074a402556d7b7c812208cf834dd52b27a5
This commit is contained in:
Jakob Ackermann
2026-05-13 08:06:49 +00:00
committed by Copybot
co-authored by Anna Claire Fields
parent 13e426b14c
commit b1931d0b3b
5 changed files with 183 additions and 0 deletions
@@ -25,6 +25,7 @@ import Modules from '../../infrastructure/Modules.mjs'
import { expressify, promisify } from '@overleaf/promise-utils'
import { handleAuthenticateErrors } from './AuthenticationErrors.mjs'
import EmailHelper from '../Helpers/EmailHelper.mjs'
import SplitTestHandler from '../SplitTests/SplitTestHandler.mjs'
const { hasAdminAccess } = AdminAuthorizationHelper
@@ -647,6 +648,10 @@ function _loginAsyncHandlers(req, user, anonymousAnalyticsId, isNewUser) {
UserHandler.promises.populateTeamInvites(user).catch(err => {
logger.warn({ err }, 'error setting up login data')
})
SplitTestHandler.promises.userMaintenanceOnLogin(user).catch(err => {
const userId = user._id
logger.warn({ err, userId }, 'error cleaning up split-tests on login')
})
LoginRateLimiter.recordSuccessfulLogin(user.email, () => {})
AuthenticationController._recordSuccessfulLogin(user._id, () => {})
AuthenticationController.ipMatchCheck(req, user)
@@ -978,6 +978,21 @@ async function decrementLabsVariantCounter(splitTestName) {
}
}
async function userMaintenanceOnLogin(user) {
const splitTests = (await SplitTestCache.get('')).values()
const toCleanup = {}
for (const splitTest of splitTests) {
if (splitTest.archived && user.splitTests?.[splitTest.name]) {
toCleanup[`splitTests.${splitTest.name}`] = 1
}
}
if (Object.keys(toCleanup).length > 0) {
await UserUpdater.promises.updateUser(user._id, {
$unset: toCleanup,
})
}
}
export default {
getPercentile,
getAssignment: callbackify(getAssignment),
@@ -997,5 +1012,6 @@ export default {
hasUserBeenAssignedToVariant,
decrementLabsVariantCounter,
incrementLabsVariantCounterIfBelowLimit,
userMaintenanceOnLogin,
},
}