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
+82 -35
View File
@@ -1,13 +1,11 @@
/**
* Data access layer for Journals.
*
* Uses dedicated Prisma models:
* - JournalMeta (collection: journal_meta)
* - JournalChapter (collection: journal_chapters)
* 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";
@@ -65,6 +63,31 @@ export type JournalSidebarItem = {
chapter: number;
};
// --- BFF response types ---
type BffJournalMeta = {
id: string;
book: string;
data: {
title: string;
author: string;
year: number | null;
description: string | null;
totalChapters: number;
chapters: JournalChapterEntry[];
};
};
type BffJournalChapter = {
book: string;
chapter: number;
title: string;
type: 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, "");
@@ -80,7 +103,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 {
@@ -97,6 +120,51 @@ function normalizeParagraphs(paragraphs: string[] | JsonValue): string[] {
return result.map(sanitizeHtml);
}
// --- BFF → domain mappers ---
function mapBffMetaToJournalMetaData(raw: BffJournalMeta): JournalMetaData {
return {
book: raw.book,
title: raw.data.title,
author: raw.data.author,
year: raw.data.year ?? null,
description: raw.data.description ?? null,
totalChapters: raw.data.totalChapters,
chapters: raw.data.chapters,
};
}
function mapBffMetaToSummary(raw: BffJournalMeta): JournalSummary {
return {
book: raw.book,
title: raw.data.title,
author: raw.data.author,
year: raw.data.year ?? null,
description: raw.data.description ?? null,
totalChapters: raw.data.totalChapters,
};
}
function mapBffChapter(raw: BffJournalChapter): JournalChapterData {
// 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,
chapter: raw.chapter,
title: raw.title,
type: raw.type,
paragraphs: normalizeParagraphs(paragraphs),
};
}
// Local JSON fallback helpers
function findLocalDirForBook(bookCode: string): string | null {
if (!fs.existsSync(LOCAL_JSON_DIR)) return null;
@@ -160,21 +228,9 @@ function getLocalChapterBySlug(
// Public API
export async function getAllJournals(): Promise<JournalSummary[]> {
try {
const results = await prisma.journalMeta.findMany({
orderBy: { book: "asc" },
});
const results = await bffList<BffJournalMeta>("/journals");
if (results.length > 0) {
return results.map((row) => {
const data = row.data as unknown as JournalMetaData;
return {
book: row.book,
title: data.title,
author: data.author,
year: data.year ?? null,
description: data.description ?? null,
totalChapters: data.totalChapters,
};
});
return results.map(mapBffMetaToSummary);
}
} catch {
/* fall through */
@@ -194,10 +250,8 @@ export async function getJournalMeta(
bookCode: string,
): Promise<JournalMetaData | null> {
try {
const row = await prisma.journalMeta.findUnique({
where: { book: bookCode },
});
if (row) return row.data as unknown as JournalMetaData;
const raw = await bff<BffJournalMeta>(`/journals/${bookCode}`);
if (raw) return mapBffMetaToJournalMetaData(raw);
} catch {
/* fall through */
}
@@ -210,17 +264,10 @@ export async function getJournalChapterBySlug(
): Promise<JournalChapterData | null> {
if (!slug) return null;
try {
const doc = await prisma.journalChapter.findUnique({
where: { book_slug: { book: bookCode, slug } },
});
if (doc)
return {
book: doc.book,
chapter: doc.chapter,
title: doc.title,
type: doc.type,
paragraphs: normalizeParagraphs(doc.paragraphs),
};
const raw = await bff<BffJournalChapter>(
`/journals/${bookCode}/chapters/${slug}`,
);
if (raw) return mapBffChapter(raw);
} catch {
/* fall through */
}