diff --git a/api/src/controllers/seed/insert/collection.ts b/api/src/controllers/seed/insert/collection.ts index 99890bfe..a93fa443 100644 --- a/api/src/controllers/seed/insert/collection.ts +++ b/api/src/controllers/seed/insert/collection.ts @@ -1,18 +1,69 @@ +import { sql } from "drizzle-orm"; +import { db } from "~/db"; +import { showTranslations, shows } from "~/db/schema"; +import { conflictUpdateAllExcept } from "~/db/utils"; import type { SeedCollection } from "~/models/collections"; -import { insertShow } from "./shows"; +import type { SeedMovie } from "~/models/movie"; +import type { SeedSerie } from "~/models/serie"; +import { processOptImage } from "../images"; -export const insertCollection = async (collection?: SeedCollection) => { +type ShowTrans = typeof showTranslations.$inferInsert; + +export const insertCollection = async ( + collection: SeedCollection | undefined, + show: (({ kind: "movie" } & SeedMovie) | ({ kind: "serie" } & SeedSerie)) & { + nextRefresh: string; + }, +) => { if (!collection) return null; - const { translations: colTrans, ...col } = collection; - // TODO: need to compute start/end year & if missing tags & genres - const { updated, status, ...ret } = await insertShow( - { - kind: "collection", - status: "unknown", - nextRefresh, - ...col, - }, - colTrans, - ); - return ret; + const { translations, ...col } = collection; + + return await db.transaction(async (tx) => { + const [ret] = await tx + .insert(shows) + .values({ + kind: "collection", + status: "unknown", + startAir: show.kind === "movie" ? show.airDate : show.startAir, + endAir: show.kind === "movie" ? show.airDate : show.endAir, + nextRefresh: show.nextRefresh, + ...col, + }) + .onConflictDoUpdate({ + target: shows.slug, + set: { + ...conflictUpdateAllExcept(shows, [ + "pk", + "id", + "slug", + "createdAt", + "startAir", + "endAir", + ]), + startAir: sql`least(${shows.startAir}, excluded.start_air)`, + endAir: sql`greatest(${shows.endAir}, excluded.end_air)`, + }, + }) + .returning({ pk: shows.pk, id: shows.id, slug: shows.slug }); + + const trans: ShowTrans[] = Object.entries(translations).map( + ([lang, tr]) => ({ + pk: ret.pk, + language: lang, + ...tr, + poster: processOptImage(tr.poster), + thumbnail: processOptImage(tr.thumbnail), + logo: processOptImage(tr.logo), + banner: processOptImage(tr.banner), + }), + ); + await tx + .insert(showTranslations) + .values(trans) + .onConflictDoUpdate({ + target: [showTranslations.pk, showTranslations.language], + set: conflictUpdateAllExcept(showTranslations, ["pk", "language"]), + }); + return ret; + }); }; diff --git a/api/src/controllers/seed/movies.ts b/api/src/controllers/seed/movies.ts index c20188e0..968461d2 100644 --- a/api/src/controllers/seed/movies.ts +++ b/api/src/controllers/seed/movies.ts @@ -41,7 +41,11 @@ export const seedMovie = async ( const { translations, videos, collection, ...bMovie } = seed; const nextRefresh = guessNextRefresh(bMovie.airDate ?? new Date()); - const col = await insertCollection(collection); + const col = await insertCollection(collection, { + kind: "movie", + nextRefresh, + ...seed, + }); const show = await insertShow( { diff --git a/api/src/controllers/seed/series.ts b/api/src/controllers/seed/series.ts index 124d3b9c..53e9e777 100644 --- a/api/src/controllers/seed/series.ts +++ b/api/src/controllers/seed/series.ts @@ -68,7 +68,11 @@ export const seedSerie = async ( const { translations, seasons, entries, extras, collection, ...serie } = seed; const nextRefresh = guessNextRefresh(serie.startAir ?? new Date()); - const col = await insertCollection(collection); + const col = await insertCollection(collection, { + kind: "serie", + nextRefresh, + ...seed, + }); const show = await insertShow( {