mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 06:15:39 -04:00
Add all 10 milestones: Drizzle ORM + SQLite database with WAL mode, Better Auth email/password authentication, TMDB API integration for search and metadata import, TV season/episode caching, user tracking (watchlist/status/watches/ratings with auto-transitions), discovery feeds (continue watching, library, recommendations), US streaming availability via TMDB providers, background job scheduler with instrumentation hook, and dark cinema-themed frontend with DM Serif Display + DM Sans typography and amber accent design system. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
import { and, eq } from "drizzle-orm";
|
|
import { db } from "@/lib/db/client";
|
|
import { availabilityOffers, titles } from "@/lib/db/schema";
|
|
import { getWatchProviders } from "@/lib/tmdb/client";
|
|
|
|
export async function refreshAvailability(titleId: string) {
|
|
const title = db.select().from(titles).where(eq(titles.id, titleId)).get();
|
|
if (!title) return;
|
|
|
|
const data = await getWatchProviders(title.tmdbId, title.type);
|
|
const us = data.results?.US;
|
|
if (!us) return;
|
|
|
|
const now = new Date();
|
|
const offerTypes = ["flatrate", "rent", "buy", "free", "ads"] as const;
|
|
|
|
// Delete existing offers for this title+region
|
|
db.delete(availabilityOffers)
|
|
.where(
|
|
and(
|
|
eq(availabilityOffers.titleId, titleId),
|
|
eq(availabilityOffers.region, "US"),
|
|
),
|
|
)
|
|
.run();
|
|
|
|
for (const offerType of offerTypes) {
|
|
const providers = us[offerType];
|
|
if (!providers) continue;
|
|
|
|
for (const p of providers) {
|
|
db.insert(availabilityOffers)
|
|
.values({
|
|
titleId,
|
|
region: "US",
|
|
providerId: p.provider_id,
|
|
providerName: p.provider_name,
|
|
logoPath: p.logo_path,
|
|
offerType,
|
|
link: us.link ?? null,
|
|
lastFetchedAt: now,
|
|
})
|
|
.onConflictDoNothing()
|
|
.run();
|
|
}
|
|
}
|
|
}
|
|
|
|
export function getAvailability(titleId: string) {
|
|
return db
|
|
.select()
|
|
.from(availabilityOffers)
|
|
.where(eq(availabilityOffers.titleId, titleId))
|
|
.all();
|
|
}
|