overleaf/services/web/scripts/remove_feature_from_all_users.mjs
Jimmy Domagala-Tang ab5a539c62 Merge pull request #22110 from overleaf/jdt-remove-assistant-from-labs
[Web] End Error Assist Labs Experiment

GitOrigin-RevId: 2570c07aea3d6b71dc59792cd2248cb890b415d3
2024-12-06 09:06:32 +00:00

53 lines
1.3 KiB
JavaScript

import {
db,
READ_PREFERENCE_SECONDARY,
} from '../app/src/infrastructure/mongodb.js'
import parseArgs from 'minimist'
async function _removeFeatureFromAllUsers(feature, commit) {
let removals = 0
const query = {}
query[`features.${feature}`] = true
const usersWithFeature = db.users.find(query, {
readPreference: READ_PREFERENCE_SECONDARY,
})
const update = {}
update[`features.${feature}`] = false
while (await usersWithFeature.hasNext()) {
const user = await usersWithFeature.next()
if (commit) {
await db.users.findOneAndUpdate({ _id: user._id }, { $set: update })
}
removals++
}
console.log(`removed ${feature} from ${removals} users`)
if (!commit) {
console.log(
'this was a dry run, pass --commit to remove features from users'
)
}
}
async function main() {
const argv = parseArgs(process.argv.slice(2), {
string: ['feature'],
boolean: ['commit'],
unknown: function (arg) {
console.error('unrecognised argument', arg)
process.exit(1)
},
})
const feature = argv.feature
const commit = argv.commit || false
await _removeFeatureFromAllUsers(feature, commit)
}
try {
await main()
console.log('Done')
process.exit(0)
} catch (error) {
console.error(error)
process.exit(1)
}