Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 73 additions & 6 deletions backend/__tests__/__integration__/dal/user.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { describe, it, expect, vi } from "vitest";
import { describe, expect, it, vi } from "vitest";

import { CustomThemeColors } from "@monkeytype/schemas/configs";
import { PersonalBest, PersonalBests } from "@monkeytype/schemas/shared";
import { MonkeyMail, ResultFilters } from "@monkeytype/schemas/users";
import { ObjectId } from "mongodb";
import * as UserDAL from "../../../src/dal/user";
import * as UserTestData from "../../__testData__/users";
import { createConnection as createFriend } from "../../__testData__/connections";
import { ObjectId } from "mongodb";
import { MonkeyMail, ResultFilters } from "@monkeytype/schemas/users";
import { PersonalBest, PersonalBests } from "@monkeytype/schemas/shared";
import { CustomThemeColors } from "@monkeytype/schemas/configs";
import * as UserTestData from "../../__testData__/users";

const mockPersonalBest: PersonalBest = {
acc: 1,
Expand Down Expand Up @@ -1122,6 +1122,55 @@ describe("UserDal", () => {
expect(year2024[93]).toEqual(2);
});
});

describe("getUser", () => {
it("should get with missing personalBests", async () => {
//GIVEN
let user = await UserTestData.createUser({ personalBests: undefined });

//WHEN
const read = await UserDAL.getUser(user.uid, "read");

expect(read.personalBests).toEqual({
custom: {},
quote: {},
time: {},
words: {},
zen: {},
});
});
});

describe("getUserByName", () => {
it("should get with missing personalBests", async () => {
//GIVEN
let user = await UserTestData.createUser({ personalBests: undefined });

//WHEN
const read = await UserDAL.getUserByName(user.name, "read");

expect(read.personalBests).toEqual({
custom: {},
quote: {},
time: {},
words: {},
zen: {},
});
});
});

describe("getPersonalBests", () => {
it("should get with missing personalBests", async () => {
//GIVEN
let user = await UserTestData.createUser({ personalBests: undefined });

//WHEN
const read = await UserDAL.getPersonalBests(user.uid, "time", "15");

expect(read).toBeUndefined();
});
});

describe("getPartialUser", () => {
it("should throw for unknown user", async () => {
await expect(async () =>
Expand Down Expand Up @@ -1156,6 +1205,24 @@ describe("UserDal", () => {
},
});
});
it("should get with missing personalBests", async () => {
//GIVEN
let user = await UserTestData.createUser({ personalBests: undefined });

//WHEN
const read = await UserDAL.getPartialUser(user.uid, "read", [
"uid",
"personalBests",
]);

expect(read.personalBests).toEqual({
custom: {},
quote: {},
time: {},
words: {},
zen: {},
});
});
});
describe("updateEmail", () => {
it("throws for nonexisting user", async () => {
Expand Down
35 changes: 29 additions & 6 deletions backend/src/dal/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ import {
CountByYearAndDay,
Friend,
} from "@monkeytype/schemas/users";
import { Mode, Mode2, PersonalBest } from "@monkeytype/schemas/shared";
import {
Mode,
Mode2,
PersonalBest,
PersonalBests,
} from "@monkeytype/schemas/shared";
import { addImportantLog } from "./logs";
import { Result as ResultType } from "@monkeytype/schemas/results";
import { Configuration } from "@monkeytype/schemas/configuration";
Expand Down Expand Up @@ -246,7 +251,7 @@ export async function updateEmail(
export async function getUser(uid: string, stack: string): Promise<DBUser> {
const user = await getUsersCollection().findOne({ uid });
if (!user) throw new MonkeyError(404, "User not found", stack);
return user;
return migrateUser(user);
}

/**
Expand All @@ -263,10 +268,16 @@ export async function getPartialUser<K extends keyof DBUser>(
fields: K[],
): Promise<Pick<DBUser, K>> {
const projection = new Map(fields.map((it) => [it, 1]));
const results = await getUsersCollection().findOne({ uid }, { projection });
if (results === null) throw new MonkeyError(404, "User not found", stack);
const partialUser = await getUsersCollection().findOne(
{ uid },
{ projection },
);
if (partialUser === null) throw new MonkeyError(404, "User not found", stack);

return results;
if (fields.includes("personalBests" as K)) {
return migrateUser(partialUser);
}
return partialUser;
}

export async function findByName(name: string): Promise<DBUser | undefined> {
Expand Down Expand Up @@ -294,7 +305,7 @@ export async function getUserByName(
): Promise<DBUser> {
const user = await findByName(name);
if (!user) throw new MonkeyError(404, "User not found", stack);
return user;
return migrateUser(user);
}

export async function isDiscordIdAvailable(
Expand Down Expand Up @@ -1359,3 +1370,15 @@ export async function getFriends(uid: string): Promise<DBFriend[]> {
],
);
}

function migrateUser<T extends { personalBests: PersonalBests }>(user: T): T {
user.personalBests ??= {
time: {},
words: {},
quote: {},
zen: {},
custom: {},
};

return user;
}