From 9ebba128e030877720b5d69cc43b416ab5b114bb Mon Sep 17 00:00:00 2001 From: Spencer Spenst Date: Thu, 16 May 2024 11:54:07 -0700 Subject: [PATCH] archive levels script --- server/scripts/archive-levels.ts | 53 ++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 server/scripts/archive-levels.ts diff --git a/server/scripts/archive-levels.ts b/server/scripts/archive-levels.ts new file mode 100644 index 000000000..0460c0494 --- /dev/null +++ b/server/scripts/archive-levels.ts @@ -0,0 +1,53 @@ +// ts-node -r tsconfig-paths/register --files server/scripts/archive-levels.ts + +import { Games } from '@root/constants/Games'; +import TestId from '@root/constants/testId'; +import { generateLevelSlug } from '@root/helpers/generateSlug'; +import { TimerUtil } from '@root/helpers/getTs'; +import Level from '@root/models/db/level'; +import { calcCreatorCounts } from '@root/models/schemas/userSchema'; +import dotenv from 'dotenv'; +import { Types } from 'mongoose'; +import dbConnect from '../../lib/dbConnect'; +import { LevelModel, UserModel } from '../../models/mongoose'; + +'use strict'; + +dotenv.config(); +console.log('loaded env vars'); + +async function init() { + // const args = process.argv.slice(2); + + console.log('connecting to db...'); + await dbConnect(); + console.log('connected'); + + const userIds = new Set(); + + for await (const user of UserModel.find({})) { + userIds.add(user._id.toString()); + } + + for await (const level of LevelModel.find({ isDeleted: { $ne: true } })) { + // archive all levels that were supposed to be archived (the user is deleted) + if (!userIds.has(level.userId.toString())) { + const slug = await generateLevelSlug(level.gameId, 'archive', level.name, level._id.toString()); + + console.log(level._id.toString(), slug); + + await LevelModel.updateOne({ _id: level._id }, { $set: { + userId: new Types.ObjectId(TestId.ARCHIVE), + archivedBy: level.userId, + archivedTs: TimerUtil.getTs(), + slug: slug, + } }); + } + } + + await Promise.all([Object.values(Games).map(game => calcCreatorCounts(game.id, new Types.ObjectId(TestId.ARCHIVE)))]); + + process.exit(0); +} + +init();