Handle duplicated entries in the history

This commit is contained in:
Zoe Roux 2025-04-07 21:40:41 +02:00
parent e32fc229f8
commit c0e00c0fd4
No known key found for this signature in database
2 changed files with 20 additions and 2 deletions

View File

@ -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,

View File

@ -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 });