mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-03-04 10:43:02 +08:00
Fix Collection Types (#2864)
This commit is contained in:
parent
a55249925e
commit
58505a9f31
9 changed files with 25 additions and 6 deletions
|
@ -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,
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -20,6 +20,7 @@ export async function addUser(
|
|||
|
||||
const currentDate = Date.now();
|
||||
return await usersCollection.insertOne({
|
||||
_id: new ObjectId(),
|
||||
name,
|
||||
email,
|
||||
uid,
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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];
|
||||
|
|
2
backend/types/types.d.ts
vendored
2
backend/types/types.d.ts
vendored
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue