mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 03:55:38 -04:00
TV episodes were missing when: (1) a title existed as a recommendation "shell" with no episode data, (2) a prior refreshTvChildren() call failed partway through, or (3) the title detail page was loaded before episodes finished importing. importTitle() now re-fetches episodes for existing TV titles with zero seasons, the GET endpoint hydrates shell titles on access, and refreshTvChildren() handles per-season errors gracefully instead of aborting entirely. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
88 lines
2.4 KiB
TypeScript
88 lines
2.4 KiB
TypeScript
import { eq } from "drizzle-orm";
|
|
import { type NextRequest, NextResponse } from "next/server";
|
|
import { db } from "@/lib/db/client";
|
|
import { availabilityOffers, episodes, seasons, titles } from "@/lib/db/schema";
|
|
import { refreshTvChildren } from "@/lib/services/metadata";
|
|
import { getTvDetails } from "@/lib/tmdb/client";
|
|
|
|
export async function GET(
|
|
_req: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> },
|
|
) {
|
|
const { id } = await params;
|
|
let title = db.select().from(titles).where(eq(titles.id, id)).get();
|
|
if (!title)
|
|
return NextResponse.json({ error: "Title not found" }, { status: 404 });
|
|
|
|
// If this is a shell TV title (created by recommendations with no episode data),
|
|
// fetch the full details and episodes now.
|
|
if (title.type === "tv" && !title.lastFetchedAt) {
|
|
try {
|
|
const show = await getTvDetails(title.tmdbId);
|
|
db.update(titles)
|
|
.set({
|
|
overview: show.overview,
|
|
posterPath: show.poster_path,
|
|
backdropPath: show.backdrop_path,
|
|
status: show.status,
|
|
lastFetchedAt: new Date(),
|
|
})
|
|
.where(eq(titles.id, id))
|
|
.run();
|
|
await refreshTvChildren(id, title.tmdbId, show.number_of_seasons);
|
|
title = db.select().from(titles).where(eq(titles.id, id)).get() ?? title;
|
|
} catch {
|
|
// Continue with whatever data we have
|
|
}
|
|
}
|
|
|
|
let titleSeasons: Array<{
|
|
id: string;
|
|
seasonNumber: number;
|
|
name: string | null;
|
|
overview: string | null;
|
|
posterPath: string | null;
|
|
airDate: string | null;
|
|
episodes: Array<{
|
|
id: string;
|
|
episodeNumber: number;
|
|
name: string | null;
|
|
overview: string | null;
|
|
stillPath: string | null;
|
|
airDate: string | null;
|
|
runtimeMinutes: number | null;
|
|
}>;
|
|
}> = [];
|
|
|
|
if (title.type === "tv") {
|
|
const seasonRows = db
|
|
.select()
|
|
.from(seasons)
|
|
.where(eq(seasons.titleId, title.id))
|
|
.orderBy(seasons.seasonNumber)
|
|
.all();
|
|
|
|
titleSeasons = seasonRows.map((s) => ({
|
|
...s,
|
|
episodes: db
|
|
.select()
|
|
.from(episodes)
|
|
.where(eq(episodes.seasonId, s.id))
|
|
.orderBy(episodes.episodeNumber)
|
|
.all(),
|
|
}));
|
|
}
|
|
|
|
const availability = db
|
|
.select()
|
|
.from(availabilityOffers)
|
|
.where(eq(availabilityOffers.titleId, title.id))
|
|
.all();
|
|
|
|
return NextResponse.json({
|
|
...title,
|
|
seasons: titleSeasons,
|
|
availability,
|
|
});
|
|
}
|