Fix Collection Types (#2864)

This commit is contained in:
Evan 2022-04-23 18:37:05 -05:00 committed by GitHub
parent a55249925e
commit 58505a9f31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 25 additions and 6 deletions

View file

@ -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<MonkeyTypes.ApeKey> {
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,

View file

@ -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<void> {
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(),

View file

@ -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<MonkeyTypes.LeaderboardEntry>("users")
.aggregate<MonkeyTypes.LeaderboardEntry>(
[
{
$match: {
@ -121,7 +121,9 @@ export async function update(
} catch (e) {}
if (lb && lb.length !== 0) {
await db
.collection(`leaderboards.${language}.${mode}.${mode2}`)
.collection<MonkeyTypes.LeaderboardEntry>(
`leaderboards.${language}.${mode}.${mode2}`
)
.insertMany(lb);
}
const end3 = performance.now();

View file

@ -29,6 +29,7 @@ export async function add(
): Promise<AddQuoteReturn | void> {
if (!git) throw new MonkeyError(500, "Git not available.");
const quote = {
_id: new ObjectId(),
text: text,
source: source,
language: language.toLowerCase(),

View file

@ -20,6 +20,7 @@ export async function addUser(
const currentDate = Date.now();
return await usersCollection.insertOne({
_id: new ObjectId(),
name,
email,
uid,

View file

@ -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<MonkeyTypes.Configuration>
if (liveConfiguration) {
const baseConfiguration = _.cloneDeep(BASE_CONFIGURATION);
const liveConfigurationWithoutId = _.omit(
liveConfiguration,
"_id"
@ -75,7 +77,10 @@ export async function getLiveConfiguration(): Promise<MonkeyTypes.Configuration>
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(

View file

@ -4,6 +4,7 @@ import {
Db,
MongoClient,
MongoClientOptions,
WithId,
} from "mongodb";
import Logger from "../utils/logger";
@ -68,9 +69,10 @@ class DatabaseClient {
}
}
static collection<T>(collectionName: string): Collection<T> {
static collection<T>(collectionName: string): Collection<WithId<T>> {
if (!(collectionName in this.collections)) {
this.collections[collectionName] = this.db.collection<T>(collectionName);
this.collections[collectionName] =
this.db.collection<WithId<T>>(collectionName);
}
return this.collections[collectionName];

View file

@ -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;

View file

@ -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,