diff --git a/backend/src/api/controllers/result.ts b/backend/src/api/controllers/result.ts index 03a6f09eb..8526d4fbe 100644 --- a/backend/src/api/controllers/result.ts +++ b/backend/src/api/controllers/result.ts @@ -36,7 +36,7 @@ import { getDailyLeaderboard } from "../../utils/daily-leaderboards"; import AutoRoleList from "../../constants/auto-roles"; import * as UserDAL from "../../dal/user"; import { buildMonkeyMail } from "../../utils/monkey-mail"; -import { updateStreak } from "./user"; +import { updateStreak } from "../../dal/user"; try { if (anticheatImplemented() === false) throw new Error("undefined"); diff --git a/backend/src/api/controllers/user.ts b/backend/src/api/controllers/user.ts index 317fc5ab0..cda374171 100644 --- a/backend/src/api/controllers/user.ts +++ b/backend/src/api/controllers/user.ts @@ -1,15 +1,10 @@ -import _, { eq } from "lodash"; +import _ from "lodash"; import * as UserDAL from "../../dal/user"; import MonkeyError from "../../utils/error"; import Logger from "../../utils/logger"; import { MonkeyResponse } from "../../utils/monkey-response"; import { getDiscordUser } from "../../utils/discord"; -import { - buildAgentLog, - isToday, - isYesterday, - sanitizeString, -} from "../../utils/misc"; +import { buildAgentLog, sanitizeString } from "../../utils/misc"; import * as George from "../../tasks/george"; import admin from "firebase-admin"; import { deleteAllApeKeys } from "../../dal/ape-keys"; @@ -542,27 +537,3 @@ export async function updateInbox( return new MonkeyResponse("Inbox updated"); } - -export async function updateStreak(uid, timestamp): Promise { - const user = await UserDAL.getUser(uid, "calculate streak"); - const streak: MonkeyTypes.UserStreak = { - lastResultTimestamp: user.streak?.lastResultTimestamp ?? 0, - length: user.streak?.length ?? 0, - maxLength: user.streak?.length ?? 0, - }; - - if (isYesterday(streak.lastResultTimestamp)) { - streak.length += 1; - } else if (!isToday(streak.lastResultTimestamp)) { - streak.length = 1; - } - - if (streak.length > streak.maxLength) { - streak.maxLength = streak.length; - } - - streak.lastResultTimestamp = timestamp; - await UserDAL.getUsersCollection().updateOne({ uid }, { $set: { streak } }); - - return streak.length; -} diff --git a/backend/src/dal/user.ts b/backend/src/dal/user.ts index e03234af5..be0ec1afe 100644 --- a/backend/src/dal/user.ts +++ b/backend/src/dal/user.ts @@ -6,7 +6,7 @@ import * as db from "../init/db"; import MonkeyError from "../utils/error"; import { Collection, ObjectId, WithId, Long, UpdateFilter } from "mongodb"; import Logger from "../utils/logger"; -import { flattenObjectDeep } from "../utils/misc"; +import { flattenObjectDeep, isToday, isYesterday } from "../utils/misc"; const SECONDS_PER_HOUR = 3600; @@ -890,3 +890,30 @@ export async function updateInbox( await getUsersCollection().updateOne({ uid }, mergedUpdates); } + +export async function updateStreak( + uid: string, + timestamp: number +): Promise { + const user = await getUser(uid, "calculate streak"); + const streak: MonkeyTypes.UserStreak = { + lastResultTimestamp: user.streak?.lastResultTimestamp ?? 0, + length: user.streak?.length ?? 0, + maxLength: user.streak?.length ?? 0, + }; + + if (isYesterday(streak.lastResultTimestamp)) { + streak.length += 1; + } else if (!isToday(streak.lastResultTimestamp)) { + streak.length = 1; + } + + if (streak.length > streak.maxLength) { + streak.maxLength = streak.length; + } + + streak.lastResultTimestamp = timestamp; + await getUsersCollection().updateOne({ uid }, { $set: { streak } }); + + return streak.length; +}