diff --git a/api/src/controllers/seed/insert/shows.ts b/api/src/controllers/seed/insert/shows.ts index fcc66ea3..d6b3a84c 100644 --- a/api/src/controllers/seed/insert/shows.ts +++ b/api/src/controllers/seed/insert/shows.ts @@ -1,4 +1,4 @@ -import { and, count, eq, exists, ne, sql } from "drizzle-orm"; +import { type SQLWrapper, and, count, eq, exists, 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"; @@ -138,9 +138,10 @@ async function insertBaseShow(tx: Transaction, show: Show) { export async function updateAvailableCount( tx: Transaction, - showPks: number[], - updateEntryCount = true, + showPks: number[] | SQLWrapper, + updateEntryCount = false, ) { + const showPkQ = Array.isArray(showPks) ? sqlarr(showPks) : showPks; return await tx .update(shows) .set({ @@ -168,5 +169,5 @@ export async function updateAvailableCount( )}`, }), }) - .where(eq(shows.pk, sql`any(${sqlarr(showPks)})`)); + .where(eq(shows.pk, sql`any(${showPkQ})`)); } diff --git a/api/src/controllers/videos.ts b/api/src/controllers/videos.ts index 38ad0fdd..ffb6401b 100644 --- a/api/src/controllers/videos.ts +++ b/api/src/controllers/videos.ts @@ -8,6 +8,7 @@ import { isUniqueConstraint, jsonbBuildObject, jsonbObjectAgg, + sqlarr, values, } from "~/db/utils"; import { KError } from "~/models/error"; @@ -343,6 +344,17 @@ export const videosH = new Elysia({ prefix: "/videos", tags: ["videos"] }) }, {} as Record, ); + + await updateAvailableCount( + tx, + tx + .selectDistinct({ pk: entries.showPk }) + .from(entries) + .where( + eq(entries.pk, sql`any(${sqlarr(ret.map((x) => x.entryPk))})`), + ), + ); + return error( 201, vids.map((x) => ({ diff --git a/api/src/models/utils/relations.ts b/api/src/models/utils/relations.ts index 2b800024..443cdf30 100644 --- a/api/src/models/utils/relations.ts +++ b/api/src/models/utils/relations.ts @@ -4,7 +4,7 @@ import type { SelectResultField } from "drizzle-orm/query-builders/select.types" export const buildRelations = < R extends string, P extends object, - Rel extends Record Subquery>, + Rel extends Record Subquery>, >( enabled: R[], relations: Rel,