feat: implement bff layer

This commit is contained in:
gosangam
2026-05-10 00:35:14 +05:30
parent 5951eb5e18
commit 32da38b24f
91 changed files with 2434 additions and 11784 deletions
+92 -38
View File
@@ -1,13 +1,11 @@
/**
* Data access layer for Magazines (Back to Godhead, etc.).
*
* Uses dedicated Prisma models:
* - MagazineMeta (collection: magazine_meta)
* - MagazineIssue (collection: magazine_issues)
* Uses the BFF API instead of direct Prisma queries.
* Falls back to local JSON files when BFF is unavailable.
*/
import { prisma } from "@/lib/db";
import type { JsonValue } from "@prisma/client/runtime/library";
import { bff, bffList } from "@/lib/bff";
import fs from "fs";
import path from "path";
@@ -57,6 +55,35 @@ export type MagazineSidebarItem = {
type: string;
};
// --- BFF response types ---
type BffMagazineMeta = {
id: string;
book: string;
data: {
title: string;
author: string;
year: number | null;
description: string | null;
totalIssues: number;
totalArticles: number;
flag: string | null;
chapters: MagazineChapterEntry[];
};
};
type BffMagazineIssue = {
book: string;
volume: number | null;
part: number;
title: string;
slug: string;
paragraphs: string[];
paragraphs_json?: string;
};
// --- HTML sanitisation ---
function sanitizeHtml(html: string): string {
let clean = html.replace(/<script[\s>][\s\S]*?<\/script>/gi, "");
clean = clean.replace(/<style[\s>][\s\S]*?<\/style>/gi, "");
@@ -71,7 +98,7 @@ function sanitizeHtml(html: string): string {
return clean;
}
function normalizeParagraphs(paragraphs: string[] | JsonValue): string[] {
function normalizeParagraphs(paragraphs: string[] | unknown): string[] {
let result: string[];
if (typeof paragraphs === "string") {
try {
@@ -88,6 +115,57 @@ function normalizeParagraphs(paragraphs: string[] | JsonValue): string[] {
return result.map(sanitizeHtml);
}
// --- BFF → domain mappers ---
function mapBffMetaToMagazineMetaData(raw: BffMagazineMeta): MagazineMetaData {
return {
book: raw.book,
title: raw.data.title,
author: raw.data.author,
year: raw.data.year ?? null,
description: raw.data.description ?? null,
totalIssues: raw.data.totalIssues,
totalArticles: raw.data.totalArticles,
flag: raw.data.flag ?? null,
chapters: raw.data.chapters,
};
}
function mapBffMetaToSummary(raw: BffMagazineMeta): MagazineSummary {
return {
book: raw.book,
title: raw.data.title,
author: raw.data.author,
year: raw.data.year ?? null,
description: raw.data.description ?? null,
totalIssues: raw.data.totalIssues,
flag: raw.data.flag ?? null,
};
}
function mapBffIssue(raw: BffMagazineIssue): MagazineIssueData {
// Handle paragraphs_json fallback
let paragraphs = raw.paragraphs;
if ((!paragraphs || !paragraphs.length) && raw.paragraphs_json) {
try {
paragraphs = JSON.parse(raw.paragraphs_json);
} catch {
paragraphs = [];
}
}
return {
book: raw.book,
volume: raw.volume,
part: raw.part,
title: raw.title,
slug: raw.slug,
paragraphs: normalizeParagraphs(paragraphs),
};
}
// --- Local JSON fallback helpers ---
function findLocalDirForBook(bookCode: string): string | null {
if (!fs.existsSync(LOCAL_JSON_DIR)) return null;
const entries = fs.readdirSync(LOCAL_JSON_DIR);
@@ -152,22 +230,9 @@ function getLocalIssue(
export async function getAllMagazines(): Promise<MagazineSummary[]> {
try {
const results = await prisma.magazineMeta.findMany({
orderBy: { book: "asc" },
});
const results = await bffList<BffMagazineMeta>("/magazines");
if (results.length > 0) {
return results.map((row) => {
const data = row.data as unknown as MagazineMetaData;
return {
book: row.book,
title: data.title,
author: data.author,
year: data.year ?? null,
description: data.description ?? null,
totalIssues: data.totalIssues,
flag: data.flag ?? null,
};
});
return results.map(mapBffMetaToSummary);
}
} catch {
/* fall through */
@@ -188,10 +253,8 @@ export async function getMagazineMeta(
bookCode: string,
): Promise<MagazineMetaData | null> {
try {
const row = await prisma.magazineMeta.findUnique({
where: { book: bookCode },
});
if (row) return row.data as unknown as MagazineMetaData;
const raw = await bff<BffMagazineMeta>(`/magazines/${bookCode}`);
if (raw) return mapBffMetaToMagazineMetaData(raw);
} catch {
/* fall through */
}
@@ -204,19 +267,10 @@ export async function getMagazineIssue(
): Promise<MagazineIssueData | null> {
if (!slug) return null;
try {
const doc = await prisma.magazineIssue.findUnique({
where: { book_slug: { book: bookCode, slug } },
});
if (doc) {
return {
book: doc.book,
volume: doc.volume,
part: doc.part,
title: doc.title,
slug: doc.slug,
paragraphs: normalizeParagraphs(doc.paragraphs),
};
}
const raw = await bff<BffMagazineIssue>(
`/magazines/${bookCode}/issues/${slug}`,
);
if (raw) return mapBffIssue(raw);
} catch {
/* fall through */
}