diff --git a/backend/api/controllers/ape-key.ts b/backend/api/controllers/ape-key.ts index 2c2cd72c1..1e8c0ae14 100644 --- a/backend/api/controllers/ape-key.ts +++ b/backend/api/controllers/ape-key.ts @@ -5,6 +5,7 @@ import ApeKeysDAO from "../../dao/ape-keys"; import MonkeyError from "../../utils/error"; import { MonkeyResponse } from "../../utils/monkey-response"; import { base64UrlEncode } from "../../utils/misc"; +import { ObjectId } from "mongodb"; function cleanApeKey(apeKey: MonkeyTypes.ApeKey): Partial { return _.omit(apeKey, "hash", "_id", "uid", "useCount"); @@ -39,6 +40,7 @@ export async function generateApeKey( const saltyHash = await hash(apiKey, apeKeySaltRounds); const apeKey: MonkeyTypes.ApeKey = { + _id: new ObjectId(), name, enabled, uid, diff --git a/backend/api/controllers/quote.ts b/backend/api/controllers/quote.ts index f4a4ad9ab..b557eca1d 100644 --- a/backend/api/controllers/quote.ts +++ b/backend/api/controllers/quote.ts @@ -8,6 +8,7 @@ import MonkeyError from "../../utils/error"; import { verify } from "../../utils/captcha"; import Logger from "../../utils/logger"; import { MonkeyResponse } from "../../utils/monkey-response"; +import { ObjectId } from "mongodb"; async function verifyCaptcha(captcha: string): Promise { if (!(await verify(captcha))) { @@ -139,6 +140,7 @@ export async function reportQuote( await verifyCaptcha(captcha); const newReport: MonkeyTypes.Report = { + _id: new ObjectId(), id: uuidv4(), type: "quote", timestamp: new Date().getTime(), diff --git a/backend/dao/leaderboards.ts b/backend/dao/leaderboards.ts index 10e5f8210..f226d5826 100644 --- a/backend/dao/leaderboards.ts +++ b/backend/dao/leaderboards.ts @@ -63,8 +63,8 @@ export async function update( const str = `lbPersonalBests.${mode}.${mode2}.${language}`; const start1 = performance.now(); const lb = await db - .collection("users") - .aggregate( + .collection("users") + .aggregate( [ { $match: { @@ -121,7 +121,9 @@ export async function update( } catch (e) {} if (lb && lb.length !== 0) { await db - .collection(`leaderboards.${language}.${mode}.${mode2}`) + .collection( + `leaderboards.${language}.${mode}.${mode2}` + ) .insertMany(lb); } const end3 = performance.now(); diff --git a/backend/dao/new-quotes.ts b/backend/dao/new-quotes.ts index 53b5de763..a4648a0bc 100644 --- a/backend/dao/new-quotes.ts +++ b/backend/dao/new-quotes.ts @@ -29,6 +29,7 @@ export async function add( ): Promise { if (!git) throw new MonkeyError(500, "Git not available."); const quote = { + _id: new ObjectId(), text: text, source: source, language: language.toLowerCase(), diff --git a/backend/dao/user.ts b/backend/dao/user.ts index ae3efc8d2..1d8b445b0 100644 --- a/backend/dao/user.ts +++ b/backend/dao/user.ts @@ -20,6 +20,7 @@ export async function addUser( const currentDate = Date.now(); return await usersCollection.insertOne({ + _id: new ObjectId(), name, email, uid, diff --git a/backend/init/configuration.ts b/backend/init/configuration.ts index 6c5d62218..76c233b82 100644 --- a/backend/init/configuration.ts +++ b/backend/init/configuration.ts @@ -3,6 +3,7 @@ import _ from "lodash"; import Logger from "../utils/logger"; import { identity } from "../utils/misc"; import BASE_CONFIGURATION from "../constants/base-configuration"; +import { ObjectId } from "mongodb"; const CONFIG_UPDATE_INTERVAL = 10 * 60 * 1000; // 10 Minutes @@ -66,6 +67,7 @@ export async function getLiveConfiguration(): Promise if (liveConfiguration) { const baseConfiguration = _.cloneDeep(BASE_CONFIGURATION); + const liveConfigurationWithoutId = _.omit( liveConfiguration, "_id" @@ -75,7 +77,10 @@ export async function getLiveConfiguration(): Promise pushConfiguration(baseConfiguration); configuration = baseConfiguration; } else { - await configurationCollection.insertOne(BASE_CONFIGURATION); // Seed the base configuration. + await configurationCollection.insertOne({ + ...BASE_CONFIGURATION, + _id: new ObjectId(), + }); // Seed the base configuration. } } catch (error) { Logger.logToDb( diff --git a/backend/init/db.ts b/backend/init/db.ts index 22c1a648f..0a4f585bb 100644 --- a/backend/init/db.ts +++ b/backend/init/db.ts @@ -4,6 +4,7 @@ import { Db, MongoClient, MongoClientOptions, + WithId, } from "mongodb"; import Logger from "../utils/logger"; @@ -68,9 +69,10 @@ class DatabaseClient { } } - static collection(collectionName: string): Collection { + static collection(collectionName: string): Collection> { if (!(collectionName in this.collections)) { - this.collections[collectionName] = this.db.collection(collectionName); + this.collections[collectionName] = + this.db.collection>(collectionName); } return this.collections[collectionName]; diff --git a/backend/types/types.d.ts b/backend/types/types.d.ts index 2b63be5fb..88e6dfc67 100644 --- a/backend/types/types.d.ts +++ b/backend/types/types.d.ts @@ -116,6 +116,7 @@ declare namespace MonkeyTypes { } interface ApeKey { + _id: ObjectId; uid: string; name: string; hash: string; @@ -244,6 +245,7 @@ declare namespace MonkeyTypes { type ReportTypes = "quote"; interface Report { + _id: ObjectId; id: string; type: ReportTypes; timestamp: number; diff --git a/backend/utils/logger.ts b/backend/utils/logger.ts index 787bcb3f8..b40b64ac6 100644 --- a/backend/utils/logger.ts +++ b/backend/utils/logger.ts @@ -2,6 +2,7 @@ import db from "../init/db"; import chalk from "chalk"; import winston, { format } from "winston"; import { resolve } from "path"; +import { ObjectId } from "mongodb"; const errorColor = chalk.red.bold; const warningColor = chalk.yellow.bold; @@ -93,6 +94,7 @@ const logToDb = async ( logger.info(`${event}\t${uid}\t${JSON.stringify(message)}`); logsCollection.insertOne({ + _id: new ObjectId(), timestamp: Date.now(), uid: uid ?? "", event,