diff --git a/api/src/controllers/seed/insert/entries.ts b/api/src/controllers/seed/insert/entries.ts index 126e2562..46f82cf7 100644 --- a/api/src/controllers/seed/insert/entries.ts +++ b/api/src/controllers/seed/insert/entries.ts @@ -10,6 +10,7 @@ import { conflictUpdateAllExcept, values } from "~/db/utils"; import type { SeedEntry as SEntry, SeedExtra as SExtra } from "~/models/entry"; import { processOptImage } from "../images"; import { guessNextRefresh } from "../refresh"; +import { updateAvailableCount } from "./shows"; type SeedEntry = SEntry & { video?: undefined; @@ -41,8 +42,9 @@ const generateSlug = ( }; export const insertEntries = async ( - show: { pk: number; slug: string }, + show: { pk: number; slug: string; kind: "movie" | "serie" | "collection" }, items: (SeedEntry | SeedExtra)[], + onlyExtras = false, ) => { if (!items) return []; @@ -135,29 +137,39 @@ export const insertEntries = async ( })); }); - if (vids.length === 0) + if (vids.length === 0) { + // we have not added videos but we need to update the `entriesCount` + if (show.kind === "serie" && !onlyExtras) + await updateAvailableCount(db, [show.pk], true); return retEntries.map((x) => ({ id: x.id, slug: x.slug, videos: [] })); + } - const retVideos = await db - .insert(entryVideoJoin) - .select( - db - .select({ - entryPk: sql`vids.entryPk::integer`.as("entry"), - videoPk: videos.pk, - slug: computeVideoSlug( - sql`vids.entrySlug::text`, - sql`vids.needRendering::boolean`, - ), - }) - .from(values(vids).as("vids")) - .innerJoin(videos, eq(videos.id, sql`vids.videoId::uuid`)), - ) - .onConflictDoNothing() - .returning({ - slug: entryVideoJoin.slug, - entryPk: entryVideoJoin.entryPk, - }); + const retVideos = await db.transaction(async (tx) => { + const ret = await tx + .insert(entryVideoJoin) + .select( + db + .select({ + entryPk: sql`vids.entryPk::integer`.as("entry"), + videoPk: videos.pk, + slug: computeVideoSlug( + sql`vids.entrySlug::text`, + sql`vids.needRendering::boolean`, + ), + }) + .from(values(vids).as("vids")) + .innerJoin(videos, eq(videos.id, sql`vids.videoId::uuid`)), + ) + .onConflictDoNothing() + .returning({ + slug: entryVideoJoin.slug, + entryPk: entryVideoJoin.entryPk, + }); + + if (!onlyExtras) + await updateAvailableCount(tx, [show.pk], show.kind === "serie"); + return ret; + }); return retEntries.map((entry) => ({ id: entry.id, diff --git a/api/src/controllers/seed/insert/shows.ts b/api/src/controllers/seed/insert/shows.ts index 62ef4a65..e9d8c845 100644 --- a/api/src/controllers/seed/insert/shows.ts +++ b/api/src/controllers/seed/insert/shows.ts @@ -60,6 +60,7 @@ async function insertBaseShow( }) .returning({ pk: shows.pk, + kind: shows.kind, id: shows.id, slug: shows.slug, // https://stackoverflow.com/questions/39058213/differentiate-inserted-and-updated-rows-in-upsert-using-system-columns/39204667#39204667 @@ -81,13 +82,14 @@ async function insertBaseShow( // if at this point ret is still undefined, we could not reconciliate. // simply bail and let the caller handle this. - const [{ pk, id }] = await db - .select({ pk: shows.pk, id: shows.id }) + const [{ pk, id, kind }] = await db + .select({ pk: shows.pk, id: shows.id, kind: shows.kind }) .from(shows) .where(eq(shows.slug, show.slug)) .limit(1); return { status: 409 as const, + kind, pk, id, slug: show.slug, @@ -95,10 +97,11 @@ async function insertBaseShow( } export async function updateAvailableCount( + tx: typeof db | Parameters[0]>[0], showPks: number[], updateEntryCount = true, ) { - return await db + return await tx .update(shows) .set({ availableCount: sql`${db diff --git a/api/src/controllers/seed/movies.ts b/api/src/controllers/seed/movies.ts index ec54c2ab..dcfcb06f 100644 --- a/api/src/controllers/seed/movies.ts +++ b/api/src/controllers/seed/movies.ts @@ -105,7 +105,6 @@ export const seedMovie = async ( videos, }, ]); - await updateAvailableCount([show.pk], false); const retStudios = await insertStudios(studios, show.pk); const retStaff = await insertStaff(staff, show.pk); diff --git a/api/src/controllers/seed/series.ts b/api/src/controllers/seed/series.ts index e6981c26..f3d1048e 100644 --- a/api/src/controllers/seed/series.ts +++ b/api/src/controllers/seed/series.ts @@ -131,8 +131,8 @@ export const seedSerie = async ( const retExtras = await insertEntries( show, (extras ?? []).map((x) => ({ ...x, kind: "extra", extraKind: x.kind })), + true, ); - await updateAvailableCount([show.pk]); const retStudios = await insertStudios(studios, show.pk); const retStaff = await insertStaff(staff, show.pk);