/** * Normalizers to transform backend Sequelize JSON responses * into the shape our frontend TypeScript types expect. * * Backend quirks: * - Entry relations use singular aliases: entryId, entryRating, entrySeen * - Media model uses camelCase: isSelected, isLocal, isCustom, isFavorit, mediaableId * - Collection model uses camelCase: isUserCollection, userRating, posterPath, backdropPath, isLocal, tmdbId * - Credit → People alias is "person" (singular) * - Genres come as string arrays from the backend mapper */ import type { Entry, Collection, Media, Credit, Genre, Network, DiscoveryVideo, } from "@/types"; /* ---------- Media ---------- */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function normalizeMedia(raw: any): Media { return { id: raw.id, mediaable: raw.mediaable, mediaable_id: raw.mediaableId ?? raw.mediaable_id, type: raw.type, path: raw.path, is_local: raw.isLocal ?? raw.is_local ?? false, is_selected: raw.isSelected ?? raw.is_selected ?? false, is_custom: raw.isCustom ?? raw.is_custom ?? false, is_favorit: raw.isFavorit ?? raw.is_favorit ?? false, }; } /* ---------- Credit ---------- */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function normalizeCredit(raw: any): Credit { const people = raw.person ?? raw.people ?? undefined; return { id: raw.id, entry_id: raw.entry_id, people_id: raw.people_id, character: raw.character, cast_type: raw.cast_type, department: raw.department, job: raw.job, order: raw.order, episode_count: raw.episode_count ?? null, people: people, }; } /* ---------- Network ---------- */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function normalizeNetwork(raw: any): Network { return { id: raw.id, name: raw.name, origin_country: raw.originCountry ?? raw.origin_country ?? null, homepage: raw.homepage ?? null, logo_path: raw.logoPath ?? raw.logo_path ?? null, }; } /* ---------- Genre ---------- */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function normalizeGenres(raw: any[]): Genre[] { if (!Array.isArray(raw)) return []; return raw.map((g, i) => { if (typeof g === "string") return { id: i, name: g }; return g as Genre; }); } /* ---------- Entry ---------- */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function normalizeEntry(raw: any): Entry { return { ...raw, // Map Sequelize camelCase property names → snake_case for frontend consistency show_in_calendar: raw.show_in_calendar ?? raw.showInCalendar ?? true, auto_sync: raw.auto_sync ?? raw.autoSync ?? true, // Singular → plural aliases (coerce tmdb_id to number for store comparisons) entryIds: (() => { const ids = raw.entryIds ?? raw.entryId; if (!ids) return undefined; return { ...ids, tmdb_id: ids.tmdb_id != null ? Number(ids.tmdb_id) : null, }; })(), entryRatings: raw.entryRatings ?? raw.entryRating ?? undefined, entrySeens: raw.entrySeens ?? raw.entrySeen ?? undefined, // Normalize nested arrays medias: raw.medias ? raw.medias.map(normalizeMedia) : undefined, genres: raw.genres ? normalizeGenres(raw.genres) : undefined, credits: raw.credits ? raw.credits.map(normalizeCredit) : undefined, collections: raw.collections ? raw.collections.map(normalizeCollection) : undefined, networks: raw.networks ? raw.networks.map(normalizeNetwork) : undefined, // Mark as in library when loaded from the library endpoint isInLibrary: raw.isInLibrary ?? true, }; } /* ---------- Discovery Video ---------- */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function normalizeDiscoveryVideo(raw: any): DiscoveryVideo { return { id: raw.id, title: raw.title, description: raw.description ?? null, filename: raw.filename, original_name: raw.original_name ?? raw.originalName ?? "", mime_type: raw.mime_type ?? raw.mimeType ?? "", size: raw.size ?? 0, duration: raw.duration ?? null, thumbnail_path: raw.thumbnail_path ?? raw.thumbnailPath ?? null, createdAt: raw.createdAt, updatedAt: raw.updatedAt, entries: raw.entries ? raw.entries.map(normalizeEntry) : undefined, }; } /* ---------- Collection ---------- */ // eslint-disable-next-line @typescript-eslint/no-explicit-any export function normalizeCollection(raw: any): Collection { return { id: raw.id, tmdb_id: raw.tmdbId ?? raw.tmdb_id ?? null, name: raw.name, description: raw.description ?? raw.overview ?? null, user_rating: raw.userRating ?? raw.user_rating ?? null, poster_path: raw.posterPath ?? raw.poster_path ?? null, backdrop_path: raw.backdropPath ?? raw.backdrop_path ?? null, is_local: raw.isLocal ?? raw.is_local ?? false, is_user_collection: raw.isUserCollection ?? raw.is_user_collection ?? false, createdAt: raw.createdAt, updatedAt: raw.updatedAt, entries: raw.entries ? raw.entries.map(normalizeEntry) : undefined, medias: raw.medias ? raw.medias.map(normalizeMedia) : undefined, }; }