Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
61 changes: 55 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();
});
});
Comment thread
fehmer marked this conversation as resolved.

describe("getPartialUser", () => {
it("should throw for unknown user", async () => {
await expect(async () =>
Expand Down
66 changes: 43 additions & 23 deletions backend/src/dal/user.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
import { canFunboxGetPb, checkAndUpdatePb, LbPersonalBests } from "../utils/pb";
import * as db from "../init/db";
import MonkeyError from "../utils/error";
import {
Collection,
ObjectId,
Long,
type UpdateFilter,
type Filter,
} from "mongodb";
import { flattenObjectDeep, isPlainObject, WithObjectId } from "../utils/misc";
import { getCachedConfiguration } from "../init/configuration";
import { getDayOfYear } from "date-fns";
import { UTCDate } from "@date-fns/utc";
import { Configuration } from "@monkeytype/schemas/configuration";
import { Result as ResultType } from "@monkeytype/schemas/results";
import {
Mode,
Mode2,
PersonalBest,
PersonalBests,
} from "@monkeytype/schemas/shared";
import {
AllRewards,
Badge,
CountByYearAndDay,
CustomTheme,
Friend,
MonkeyMail,
ResultFilters,
User,
UserInventory,
UserProfileDetails,
UserQuoteRatings,
UserStreak,
ResultFilters,
UserTag,
User,
CountByYearAndDay,
Friend,
} from "@monkeytype/schemas/users";
import { Mode, Mode2, PersonalBest } from "@monkeytype/schemas/shared";
import { addImportantLog } from "./logs";
import { Result as ResultType } from "@monkeytype/schemas/results";
import { Configuration } from "@monkeytype/schemas/configuration";
import { isToday, isYesterday } from "@monkeytype/util/date-and-time";
import { getDayOfYear } from "date-fns";
import {
Collection,
Long,
ObjectId,
type Filter,
type UpdateFilter,
} from "mongodb";
import { getCachedConfiguration } from "../init/configuration";
import * as db from "../init/db";
import GeorgeQueue from "../queues/george-queue";
import MonkeyError from "../utils/error";
import { flattenObjectDeep, isPlainObject, WithObjectId } from "../utils/misc";
import { canFunboxGetPb, checkAndUpdatePb, LbPersonalBests } from "../utils/pb";
import { aggregateWithAcceptedConnections } from "./connections";
import { addImportantLog } from "./logs";

export type DBUserTag = WithObjectId<UserTag>;

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 Down Expand Up @@ -294,7 +299,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 +1364,18 @@ export async function getFriends(uid: string): Promise<DBFriend[]> {
],
);
}

function migrateUser<T extends { personalBests: PersonalBests }>(user: T): T {
Comment thread
fehmer marked this conversation as resolved.
if (user.personalBests === undefined || user.personalBests === null) {
console.log("migrate");
Comment thread
fehmer marked this conversation as resolved.
Outdated
user.personalBests = {
time: {},
words: {},
quote: {},
zen: {},
custom: {},
};
}

return user;
}