-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string>(); | ||
|
||
for await (const user of UserModel.find({})) { | ||
userIds.add(user._id.toString()); | ||
} | ||
|
||
for await (const level of LevelModel.find<Level>({ 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(); |