overleaf/services/web/modules/server-ce-scripts/scripts/upgrade-user-features.mjs
Miguel Serrano c4edd2fffa Merge pull request #21154 from overleaf/msm-esm-ce-scrits
[web] Migrate `modules/server-ce-scripts` to ESM

GitOrigin-RevId: 8856f83b12d5d092e51e5765763737259c4b3985
2024-10-22 08:05:58 +00:00

61 lines
1.5 KiB
JavaScript

import { fileURLToPath } from 'node:url'
import Settings from '@overleaf/settings'
import logger from '@overleaf/logger'
import { db, waitForDb } from '../../../app/src/infrastructure/mongodb.js'
import {
mergeFeatures,
compareFeatures,
} from '../../../app/src/Features/Subscription/FeaturesHelper.js'
const DRY_RUN = !process.argv.includes('--dry-run=false')
async function main(DRY_RUN, defaultFeatures) {
await waitForDb()
logger.info({ defaultFeatures }, 'default features')
const cursor = db.users.find(
{},
{ projection: { _id: 1, email: 1, features: 1 } }
)
for await (const user of cursor) {
const newFeatures = mergeFeatures(user.features, defaultFeatures)
const diff = compareFeatures(newFeatures, user.features)
if (Object.keys(diff).length > 0) {
logger.warn(
{
userId: user._id,
email: user.email,
oldFeatures: user.features,
newFeatures,
},
'user features upgraded'
)
if (!DRY_RUN) {
await db.users.updateOne(
{ _id: user._id },
{ $set: { features: newFeatures } }
)
}
}
}
}
export default main
if (fileURLToPath(import.meta.url) === process.argv[1]) {
if (DRY_RUN) {
console.error('---')
console.error('Dry-run enabled, use --dry-run=false to commit changes')
console.error('---')
}
main(DRY_RUN, Settings.defaultFeatures)
.then(() => {
console.log('Done.')
process.exit(0)
})
.catch(error => {
console.error({ error })
process.exit(1)
})
}