From c0e00c0fd411b517ee6f9e7fb5468c1711e6547f Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 7 Apr 2025 21:40:41 +0200 Subject: [PATCH] Handle duplicated entries in the history --- api/src/controllers/entries.ts | 4 +++- api/src/controllers/profiles/history.ts | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/api/src/controllers/entries.ts b/api/src/controllers/entries.ts index 42294fd0..2e213a5d 100644 --- a/api/src/controllers/entries.ts +++ b/api/src/controllers/entries.ts @@ -166,6 +166,7 @@ export async function getEntries({ filter, languages, userId, + progressQ = entryProgressQ, }: { after: string | undefined; limit: number; @@ -174,6 +175,7 @@ export async function getEntries({ filter: SQL | undefined; languages: string[]; userId: string; + progressQ?: typeof entryProgressQ; }): Promise<(Entry | Extra | UnknownEntry)[]> { const transQ = db .selectDistinctOn([entryTranslations.pk]) @@ -218,7 +220,7 @@ export async function getEntries({ .from(entries) .innerJoin(transQ, eq(entries.pk, transQ.pk)) .leftJoinLateral(entryVideosQ, sql`true`) - .leftJoin(entryProgressQ, eq(entries.pk, entryProgressQ.entryPk)) + .leftJoin(progressQ, eq(entries.pk, progressQ.entryPk)) .where( and( filter, diff --git a/api/src/controllers/profiles/history.ts b/api/src/controllers/profiles/history.ts index a3b0c35e..29a6eb79 100644 --- a/api/src/controllers/profiles/history.ts +++ b/api/src/controllers/profiles/history.ts @@ -2,7 +2,7 @@ import { and, eq, isNotNull, ne, not, or, sql } from "drizzle-orm"; import Elysia, { t } from "elysia"; import { auth, getUserInfo } from "~/auth"; import { db } from "~/db"; -import { entries, history, videos } from "~/db/schema"; +import { entries, history, profiles, videos } from "~/db/schema"; import { values } from "~/db/utils"; import { Entry } from "~/models/entry"; import { KError } from "~/models/error"; @@ -24,6 +24,20 @@ import { } from "../entries"; import { getOrCreateProfile } from "./profile"; +const historyProgressQ: typeof entryProgressQ = db + .select({ + percent: history.percent, + time: history.time, + entryPk: history.entryPk, + playedDate: history.playedDate, + videoId: videos.id, + }) + .from(history) + .leftJoin(videos, eq(history.videoPk, videos.pk)) + .leftJoin(profiles, eq(history.profilePk, profiles.pk)) + .where(eq(profiles.id, sql.placeholder("userId"))) + .as("progress"); + export const historyH = new Elysia({ tags: ["profiles"] }) .use(auth) .guard( @@ -68,6 +82,7 @@ export const historyH = new Elysia({ tags: ["profiles"] }) ), languages: langs, userId: sub, + progressQ: historyProgressQ, })) as Entry[]; return createPage(items, { url, sort, limit }); @@ -113,6 +128,7 @@ export const historyH = new Elysia({ tags: ["profiles"] }) ), languages: langs, userId: uInfo.id, + progressQ: historyProgressQ, })) as Entry[]; return createPage(items, { url, sort, limit });