From 46d98e038d0611f6903921d4c405472cad04f79b Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sat, 3 May 2025 16:36:31 +0200 Subject: [PATCH] Update `availableSince` of entries on `POST /videos` --- api/src/controllers/seed/insert/entries.ts | 13 ++------- api/src/controllers/seed/insert/shows.ts | 26 +++++++++++++++++- api/src/controllers/videos.ts | 31 +++++++++++----------- 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/api/src/controllers/seed/insert/entries.ts b/api/src/controllers/seed/insert/entries.ts index 941e635b..8faa6d02 100644 --- a/api/src/controllers/seed/insert/entries.ts +++ b/api/src/controllers/seed/insert/entries.ts @@ -10,7 +10,7 @@ import { conflictUpdateAllExcept, sqlarr, values } from "~/db/utils"; import type { SeedEntry as SEntry, SeedExtra as SExtra } from "~/models/entry"; import { enqueueOptImage } from "../images"; import { guessNextRefresh } from "../refresh"; -import { updateAvailableCount } from "./shows"; +import { updateAvailableCount, updateAvailableSince } from "./shows"; type SeedEntry = SEntry & { video?: undefined; @@ -192,16 +192,7 @@ export const insertEntries = async ( if (!onlyExtras) await updateAvailableCount(tx, [show.pk], show.kind === "serie"); - const entriesPk = [...new Set(vids.map((x) => x.entryPk))]; - await tx - .update(entries) - .set({ availableSince: sql`now()` }) - .where( - and( - eq(entries.pk, sql`any(${sqlarr(entriesPk)})`), - isNull(entries.availableSince), - ), - ); + await updateAvailableSince(tx,[...new Set(vids.map((x) => x.entryPk))]); return ret; }); diff --git a/api/src/controllers/seed/insert/shows.ts b/api/src/controllers/seed/insert/shows.ts index d6b3a84c..6841e8fb 100644 --- a/api/src/controllers/seed/insert/shows.ts +++ b/api/src/controllers/seed/insert/shows.ts @@ -1,4 +1,13 @@ -import { type SQLWrapper, and, count, eq, exists, ne, sql } from "drizzle-orm"; +import { + type SQLWrapper, + and, + count, + eq, + exists, + isNull, + ne, + sql, +} from "drizzle-orm"; import { type Transaction, db } from "~/db"; import { entries, entryVideoJoin, showTranslations, shows } from "~/db/schema"; import { conflictUpdateAllExcept, sqlarr } from "~/db/utils"; @@ -171,3 +180,18 @@ export async function updateAvailableCount( }) .where(eq(shows.pk, sql`any(${showPkQ})`)); } + +export async function updateAvailableSince( + tx: Transaction, + entriesPk: number[], +) { + return await tx + .update(entries) + .set({ availableSince: sql`now()` }) + .where( + and( + eq(entries.pk, sql`any(${sqlarr(entriesPk)})`), + isNull(entries.availableSince), + ), + ); +} diff --git a/api/src/controllers/videos.ts b/api/src/controllers/videos.ts index ffb6401b..51b0ef6c 100644 --- a/api/src/controllers/videos.ts +++ b/api/src/controllers/videos.ts @@ -26,7 +26,10 @@ import { desc as description } from "~/models/utils/descriptions"; import { Guesses, SeedVideo, Video } from "~/models/video"; import { comment } from "~/utils"; import { computeVideoSlug } from "./seed/insert/entries"; -import { updateAvailableCount } from "./seed/insert/shows"; +import { + updateAvailableCount, + updateAvailableSince, +} from "./seed/insert/shows"; const CreatedVideo = t.Object({ id: t.String({ format: "uuid" }), @@ -345,15 +348,15 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] }) {} as Record, ); + const entriesPk = [...new Set(ret.map((x) => x.entryPk))]; await updateAvailableCount( tx, tx .selectDistinct({ pk: entries.showPk }) .from(entries) - .where( - eq(entries.pk, sql`any(${sqlarr(ret.map((x) => x.entryPk))})`), - ), + .where(eq(entries.pk, sql`any(${sqlarr(entriesPk)})`)), ); + await updateAvailableSince(tx, entriesPk); return error( 201, @@ -405,18 +408,16 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] }) .where( and( inArray(entryVideoJoin.videoPk, tx.select().from(vids)), - not( - exists( - tx - .select() - .from(evj) - .where( - and( - eq(evj.entryPk, entryVideoJoin.entryPk), - not(inArray(evj.videoPk, db.select().from(vids))), - ), + notExists( + tx + .select() + .from(evj) + .where( + and( + eq(evj.entryPk, entryVideoJoin.entryPk), + not(inArray(evj.videoPk, db.select().from(vids))), ), - ), + ), ), ), ),