2024-10-31 08:16:54 -04:00
|
|
|
import { db } from '../app/src/infrastructure/mongodb.js'
|
2024-10-18 07:04:57 -04:00
|
|
|
import BatchedUpdateModule from './helpers/batchedUpdate.mjs'
|
|
|
|
import mongodb from 'mongodb-legacy'
|
2024-11-04 04:09:11 -05:00
|
|
|
import fs from 'node:fs'
|
|
|
|
import { fileURLToPath } from 'node:url'
|
2024-10-18 07:04:57 -04:00
|
|
|
|
|
|
|
const { ObjectId } = mongodb
|
|
|
|
const { batchedUpdate } = BatchedUpdateModule
|
2024-09-05 09:02:32 -04:00
|
|
|
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`)
|
|
|
|
// 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')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-18 07:04:57 -04:00
|
|
|
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)
|
|
|
|
}
|
2024-09-05 09:02:32 -04:00
|
|
|
}
|