diff --git a/api/src/controllers/seed/insert/shows.ts b/api/src/controllers/seed/insert/shows.ts index 48e14d62..8a616664 100644 --- a/api/src/controllers/seed/insert/shows.ts +++ b/api/src/controllers/seed/insert/shows.ts @@ -1,7 +1,7 @@ -import { eq, sql } from "drizzle-orm"; +import { and, count, eq, exists, sql } from "drizzle-orm"; import { db } from "~/db"; -import { showTranslations, shows } from "~/db/schema"; -import { conflictUpdateAllExcept } from "~/db/utils"; +import { entries, entryVideoJoin, showTranslations, shows } from "~/db/schema"; +import { conflictUpdateAllExcept, sqlarr } from "~/db/utils"; import type { SeedCollection } from "~/models/collections"; import type { SeedMovie } from "~/models/movie"; import type { SeedSerie } from "~/models/serie"; @@ -93,3 +93,34 @@ async function insertBaseShow( slug: show.slug, }; } + +export async function updateAvailableCount( + showPks: number[], + updateEntryCount = true, +) { + return await db + .update(shows) + .set({ + availableCount: db + .select({ availableCount: count() }) + .from(entries) + .where( + and( + eq(entries.showPk, shows.pk), + exists( + db + .select() + .from(entryVideoJoin) + .where(eq(entryVideoJoin.entryPk, entries.pk)), + ), + ), + ), + ...(updateEntryCount && { + entriesCount: db + .select({ entriesCount: count() }) + .from(entries) + .where(eq(entries.showPk, shows.pk)), + }), + }) + .where(eq(shows.pk, sql`any(${sqlarr(showPks)})`)); +} diff --git a/api/src/controllers/seed/movies.ts b/api/src/controllers/seed/movies.ts index e2300489..2a83d04d 100644 --- a/api/src/controllers/seed/movies.ts +++ b/api/src/controllers/seed/movies.ts @@ -3,7 +3,7 @@ import type { SeedMovie } from "~/models/movie"; import { getYear } from "~/utils"; import { insertCollection } from "./insert/collection"; import { insertEntries } from "./insert/entries"; -import { insertShow } from "./insert/shows"; +import { insertShow, updateAvailableCount } from "./insert/shows"; import { insertStudios } from "./insert/studios"; import { guessNextRefresh } from "./refresh"; @@ -81,6 +81,7 @@ export const seedMovie = async ( videos, }, ]); + await updateAvailableCount([show.pk], false); const retStudios = await insertStudios(studios, show.pk); diff --git a/api/src/controllers/seed/series.ts b/api/src/controllers/seed/series.ts index 1390e61e..2b145e7b 100644 --- a/api/src/controllers/seed/series.ts +++ b/api/src/controllers/seed/series.ts @@ -4,7 +4,7 @@ import { getYear } from "~/utils"; import { insertCollection } from "./insert/collection"; import { insertEntries } from "./insert/entries"; import { insertSeasons } from "./insert/seasons"; -import { insertShow } from "./insert/shows"; +import { insertShow, updateAvailableCount } from "./insert/shows"; import { insertStudios } from "./insert/studios"; import { guessNextRefresh } from "./refresh"; @@ -107,6 +107,7 @@ export const seedSerie = async ( show, (extras ?? []).map((x) => ({ ...x, kind: "extra", extraKind: x.kind })), ); + await updateAvailableCount([show.pk]); const retStudios = await insertStudios(studios, show.pk); diff --git a/api/src/db/schema/shows.ts b/api/src/db/schema/shows.ts index f92bed0e..a28bef1e 100644 --- a/api/src/db/schema/shows.ts +++ b/api/src/db/schema/shows.ts @@ -73,6 +73,7 @@ export const shows = schema.table( onDelete: "set null", }), entriesCount: integer().notNull(), + availableCount: integer().notNull().default(0), externalId: externalid(), diff --git a/api/src/models/serie.ts b/api/src/models/serie.ts index 6715aee0..428ba505 100644 --- a/api/src/models/serie.ts +++ b/api/src/models/serie.ts @@ -48,6 +48,9 @@ const BaseSerie = t.Object({ entriesCount: t.Integer({ description: "The number of episodes in this serie", }), + availableCount: t.Integer({ + description: "The number of episodes that can be played right away", + }), externalId: ExternalId(), });