From 54131b6762a122cbf849fd150e062c654e4513e8 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Fri, 14 Mar 2025 19:43:04 +0100 Subject: [PATCH] wip: nextEntry --- api/src/controllers/shows/logic.ts | 39 ++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/api/src/controllers/shows/logic.ts b/api/src/controllers/shows/logic.ts index c9932a09..f4f86bf9 100644 --- a/api/src/controllers/shows/logic.ts +++ b/api/src/controllers/shows/logic.ts @@ -1,4 +1,5 @@ import { type SQL, and, desc, eq, exists, ne, sql } from "drizzle-orm"; +import type { PgSelect } from "drizzle-orm/pg-core"; import { db } from "~/db"; import { entries, @@ -182,6 +183,44 @@ const showRelations = { .limit(1) .as("firstEntry"); }, + nextEntry: ({ + languages, + userId, + watchStatusQ, + }: { + languages: string[]; + userId: number; + watchStatusQ: PgSelect; + }) => { + const transQ = db + .selectDistinctOn([entryTranslations.pk]) + .from(entryTranslations) + .orderBy( + entryTranslations.pk, + sql`array_position(${sqlarr(languages)}, ${entryTranslations.language})`, + ) + .as("t"); + const { pk, ...transCol } = getColumns(transQ); + + const progressQ = getEntryProgressQ(userId); + + return db + .select({ + nextEntry: jsonbBuildObject({ + ...getColumns(entries), + ...transCol, + number: entries.episodeNumber, + videos: entryVideosQ.videos, + progress: getColumns(progressQ), + }).as("nextEntry"), + }) + .from(entries) + .innerJoin(transQ, eq(entries.pk, transQ.pk)) + .leftJoin(progressQ, eq(entries.pk, progressQ.entryPk)) + .leftJoinLateral(entryVideosQ, sql`true`) + .where(eq(watchStatusQ.nextEntryPk, entries.pk)) + .as("nextEntry"); + }, }; export async function getShows({