mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-01 21:31:36 +02:00
Merge pull request #21097 from overleaf/ls-scripts-to-esm-1
Migrate scripts folder to esm 1/x GitOrigin-RevId: 4a4bc9a161f144fdb40ce3f2a0a9313b36c6df81
This commit is contained in:
61
services/web/scripts/split_writefull_disabled_from_unset.mjs
Normal file
61
services/web/scripts/split_writefull_disabled_from_unset.mjs
Normal file
@@ -0,0 +1,61 @@
|
||||
import { db, waitForDb } from '../app/src/infrastructure/mongodb.js'
|
||||
import BatchedUpdateModule from './helpers/batchedUpdate.mjs'
|
||||
import mongodb from 'mongodb-legacy'
|
||||
import fs from 'fs'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const { ObjectId } = mongodb
|
||||
const { batchedUpdate } = BatchedUpdateModule
|
||||
const CHUNK_SIZE = 1000
|
||||
|
||||
// Function to chunk the array
|
||||
function chunkArray(array, size) {
|
||||
const result = []
|
||||
for (let i = 0; i < array.length; i += size) {
|
||||
result.push(array.slice(i, i + size))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
async function main() {
|
||||
// search for file of users who already explicitly opted out first
|
||||
const optOutPath = process.argv[2]
|
||||
const optedOutFile = fs.readFileSync(optOutPath, 'utf8')
|
||||
let optedOutList = optedOutFile
|
||||
|
||||
optedOutList = optedOutFile.split('\n').map(id => new ObjectId(id))
|
||||
|
||||
console.log(`preserving opt-outs of ${optedOutList.length} users`)
|
||||
await waitForDb()
|
||||
// update all applicable user models
|
||||
await batchedUpdate(
|
||||
'users',
|
||||
{ 'writefull.enabled': false }, // and is false
|
||||
{ $set: { 'writefull.enabled': null } }
|
||||
)
|
||||
|
||||
const chunks = chunkArray(optedOutList, CHUNK_SIZE)
|
||||
|
||||
// then reset any explicit false back to being false
|
||||
// Iterate over each chunk and perform the query
|
||||
for (const chunkedIds of chunks) {
|
||||
console.log('batch update started')
|
||||
await db.users.updateMany(
|
||||
{ _id: { $in: chunkedIds } },
|
||||
{ $set: { 'writefull.enabled': false } }
|
||||
)
|
||||
console.log('batch completed')
|
||||
}
|
||||
}
|
||||
|
||||
export default main
|
||||
|
||||
if (fileURLToPath(import.meta.url) === process.argv[1]) {
|
||||
try {
|
||||
await main()
|
||||
process.exit(0)
|
||||
} catch (error) {
|
||||
console.error({ error })
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user