feat: implement bff layer
This commit is contained in:
+135
-121
@@ -2,7 +2,7 @@
|
||||
* Data access layer for Letters.
|
||||
*
|
||||
* Provides queries for browsing letters by year, recipient, and location.
|
||||
* Uses the Prisma Letter model (collection: letters).
|
||||
* Calls the BFF API for all data access (no direct Prisma usage).
|
||||
*
|
||||
* Recipients are stored as an array (e.g., ["Mukunda", "Janaki"]).
|
||||
* The recipient list aggregates individual names across all letters,
|
||||
@@ -10,9 +10,12 @@
|
||||
* ANY recipient (individual or group).
|
||||
*/
|
||||
|
||||
import { prisma } from "@/lib/db";
|
||||
import { bff, bffList } from "@/lib/bff";
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export type LetterSummary = {
|
||||
slug: string;
|
||||
recipients: string[];
|
||||
@@ -52,7 +55,43 @@ export type LocationGroup = {
|
||||
count: number;
|
||||
};
|
||||
|
||||
// Sanitize
|
||||
// ---------------------------------------------------------------------------
|
||||
// BFF response types (snake_case from Go)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
type BffLetterSummary = {
|
||||
slug: string;
|
||||
recipients: string[];
|
||||
recipient_display: string;
|
||||
date: string | null;
|
||||
year: number | null;
|
||||
month: number | null;
|
||||
location: string | null;
|
||||
};
|
||||
|
||||
type BffLetterFull = {
|
||||
slug: string;
|
||||
recipients: string[];
|
||||
recipient_display: string;
|
||||
date: string | null;
|
||||
date_ref: string | null;
|
||||
year: number | null;
|
||||
month: number | null;
|
||||
day: number | null;
|
||||
location: string | null;
|
||||
salutation: string | null;
|
||||
paragraphs: string[];
|
||||
};
|
||||
|
||||
type BffAggregation = {
|
||||
_id: string | number;
|
||||
count: number;
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 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, "");
|
||||
@@ -65,23 +104,60 @@ function sanitizeHtml(html: string): string {
|
||||
return clean;
|
||||
}
|
||||
|
||||
// --- Index/overview queries ---
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mappers (BFF snake_case → frontend camelCase)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
function mapLetterSummary(item: BffLetterSummary): LetterSummary {
|
||||
return {
|
||||
slug: item.slug,
|
||||
recipients: item.recipients ?? [],
|
||||
recipientDisplay: item.recipient_display ?? "",
|
||||
date: item.date ?? null,
|
||||
year: item.year ?? null,
|
||||
month: item.month ?? null,
|
||||
location: item.location ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
function mapLetterFull(item: BffLetterFull): LetterFull {
|
||||
return {
|
||||
slug: item.slug,
|
||||
recipients: item.recipients ?? [],
|
||||
recipientDisplay: item.recipient_display ?? "",
|
||||
date: item.date ?? null,
|
||||
dateRef: item.date_ref ?? null,
|
||||
year: item.year ?? null,
|
||||
month: item.month ?? null,
|
||||
day: item.day ?? null,
|
||||
location: item.location ?? null,
|
||||
salutation: item.salutation ?? null,
|
||||
paragraphs: (item.paragraphs ?? []).map(sanitizeHtml),
|
||||
};
|
||||
}
|
||||
|
||||
function mapYearGroup(item: BffAggregation): YearGroup {
|
||||
return { year: Number(item._id), count: item.count };
|
||||
}
|
||||
|
||||
function mapRecipientGroup(item: BffAggregation): RecipientGroup {
|
||||
return { name: String(item._id), count: item.count };
|
||||
}
|
||||
|
||||
function mapLocationGroup(item: BffAggregation): LocationGroup {
|
||||
return { name: String(item._id), count: item.count };
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Index / overview queries
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export async function getLetterYears(): Promise<YearGroup[]> {
|
||||
const letters = await prisma.letter.findMany({
|
||||
where: { year: { not: null } },
|
||||
select: { year: true },
|
||||
const raw = await bffList<BffAggregation>("/letters/years", {
|
||||
tags: ["letters"],
|
||||
});
|
||||
|
||||
const counts: Record<number, number> = {};
|
||||
for (const l of letters) {
|
||||
if (l.year) {
|
||||
counts[l.year] = (counts[l.year] || 0) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return Object.entries(counts)
|
||||
.map(([year, count]) => ({ year: Number(year), count }))
|
||||
return raw
|
||||
.map(mapYearGroup)
|
||||
.filter((g) => g.year >= 1940 && g.year <= 1980)
|
||||
.sort((a, b) => a.year - b.year);
|
||||
}
|
||||
@@ -90,67 +166,39 @@ export async function getLetterYears(): Promise<YearGroup[]> {
|
||||
* Get all unique individual recipients with letter counts.
|
||||
*
|
||||
* Each letter has a `recipients` array (e.g., ["Mukunda", "Janaki"]).
|
||||
* This function aggregates across all letters so that "Mukunda" appears
|
||||
* once with count = total letters where Mukunda is ANY recipient.
|
||||
* The BFF aggregation endpoint already aggregates across all letters so
|
||||
* that "Mukunda" appears once with count = total letters where Mukunda
|
||||
* is ANY recipient.
|
||||
*/
|
||||
export async function getLetterRecipients(): Promise<RecipientGroup[]> {
|
||||
const letters = await prisma.letter.findMany({
|
||||
select: { recipients: true },
|
||||
const raw = await bffList<BffAggregation>("/letters/recipients", {
|
||||
tags: ["letters"],
|
||||
});
|
||||
|
||||
const counts: Record<string, number> = {};
|
||||
for (const l of letters) {
|
||||
for (const r of l.recipients) {
|
||||
counts[r] = (counts[r] || 0) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return Object.entries(counts)
|
||||
.map(([name, count]) => ({ name, count }))
|
||||
.sort((a, b) => b.count - a.count);
|
||||
return raw.map(mapRecipientGroup).sort((a, b) => b.count - a.count);
|
||||
}
|
||||
|
||||
export async function getLetterLocations(): Promise<LocationGroup[]> {
|
||||
const letters = await prisma.letter.findMany({
|
||||
where: { location: { not: null } },
|
||||
select: { location: true },
|
||||
const raw = await bffList<BffAggregation>("/letters/locations", {
|
||||
tags: ["letters"],
|
||||
});
|
||||
|
||||
const counts: Record<string, number> = {};
|
||||
for (const l of letters) {
|
||||
if (l.location) {
|
||||
counts[l.location] = (counts[l.location] || 0) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return Object.entries(counts)
|
||||
.map(([name, count]) => ({ name, count }))
|
||||
.sort((a, b) => b.count - a.count);
|
||||
return raw.map(mapLocationGroup).sort((a, b) => b.count - a.count);
|
||||
}
|
||||
|
||||
// --- Letter list queries ---
|
||||
// ---------------------------------------------------------------------------
|
||||
// Letter list queries
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export async function getLettersByYear(year: number): Promise<LetterSummary[]> {
|
||||
const letters = await prisma.letter.findMany({
|
||||
where: { year },
|
||||
orderBy: [{ month: "asc" }, { day: "asc" }],
|
||||
select: {
|
||||
slug: true,
|
||||
recipients: true,
|
||||
recipientDisplay: true,
|
||||
date: true,
|
||||
year: true,
|
||||
month: true,
|
||||
location: true,
|
||||
},
|
||||
const raw = await bffList<BffLetterSummary>(`/letters/year/${year}`, {
|
||||
tags: ["letters"],
|
||||
});
|
||||
return letters;
|
||||
return raw.map(mapLetterSummary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all letters where the given name is ANY recipient.
|
||||
*
|
||||
* Uses Prisma's `has` filter on the recipients array.
|
||||
* The BFF endpoint handles the array-contains matching server-side.
|
||||
* Searching for "Mukunda" returns letters addressed to:
|
||||
* - "Mukunda" (individual)
|
||||
* - "Mukunda, Janaki" (group containing Mukunda)
|
||||
@@ -159,63 +207,38 @@ export async function getLettersByYear(year: number): Promise<LetterSummary[]> {
|
||||
export async function getLettersByRecipient(
|
||||
recipient: string,
|
||||
): Promise<LetterSummary[]> {
|
||||
const letters = await prisma.letter.findMany({
|
||||
where: { recipients: { has: recipient } },
|
||||
orderBy: [{ year: "asc" }, { month: "asc" }, { day: "asc" }],
|
||||
select: {
|
||||
slug: true,
|
||||
recipients: true,
|
||||
recipientDisplay: true,
|
||||
date: true,
|
||||
year: true,
|
||||
month: true,
|
||||
location: true,
|
||||
},
|
||||
const encoded = encodeURIComponent(recipient);
|
||||
const raw = await bffList<BffLetterSummary>(`/letters/recipient/${encoded}`, {
|
||||
tags: ["letters"],
|
||||
});
|
||||
return letters;
|
||||
return raw.map(mapLetterSummary);
|
||||
}
|
||||
|
||||
export async function getLettersByLocation(
|
||||
location: string,
|
||||
): Promise<LetterSummary[]> {
|
||||
const letters = await prisma.letter.findMany({
|
||||
where: { location },
|
||||
orderBy: [{ year: "asc" }, { month: "asc" }, { day: "asc" }],
|
||||
select: {
|
||||
slug: true,
|
||||
recipients: true,
|
||||
recipientDisplay: true,
|
||||
date: true,
|
||||
year: true,
|
||||
month: true,
|
||||
location: true,
|
||||
},
|
||||
const encoded = encodeURIComponent(location);
|
||||
const raw = await bffList<BffLetterSummary>(`/letters/location/${encoded}`, {
|
||||
tags: ["letters"],
|
||||
});
|
||||
return letters;
|
||||
return raw.map(mapLetterSummary);
|
||||
}
|
||||
|
||||
// --- Single letter ---
|
||||
// ---------------------------------------------------------------------------
|
||||
// Single letter
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export async function getLetter(slug: string): Promise<LetterFull | null> {
|
||||
const letter = await prisma.letter.findUnique({ where: { slug } });
|
||||
const letter = await bff<BffLetterFull>(`/letters/${slug}`, {
|
||||
tags: ["letters"],
|
||||
});
|
||||
if (!letter) return null;
|
||||
|
||||
return {
|
||||
slug: letter.slug,
|
||||
recipients: letter.recipients,
|
||||
recipientDisplay: letter.recipientDisplay,
|
||||
date: letter.date,
|
||||
dateRef: letter.dateRef,
|
||||
year: letter.year,
|
||||
month: letter.month,
|
||||
day: letter.day,
|
||||
location: letter.location,
|
||||
salutation: letter.salutation,
|
||||
paragraphs: letter.paragraphs.map(sanitizeHtml),
|
||||
};
|
||||
return mapLetterFull(letter);
|
||||
}
|
||||
|
||||
// --- Navigation helpers ---
|
||||
// ---------------------------------------------------------------------------
|
||||
// Navigation helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export async function getAdjacentLetters(
|
||||
slug: string,
|
||||
@@ -223,27 +246,18 @@ export async function getAdjacentLetters(
|
||||
): Promise<{ prev: LetterSummary | null; next: LetterSummary | null }> {
|
||||
if (!year) return { prev: null, next: null };
|
||||
|
||||
const yearLetters = await prisma.letter.findMany({
|
||||
where: { year },
|
||||
orderBy: [{ month: "asc" }, { day: "asc" }, { slug: "asc" }],
|
||||
select: {
|
||||
slug: true,
|
||||
recipients: true,
|
||||
recipientDisplay: true,
|
||||
date: true,
|
||||
year: true,
|
||||
month: true,
|
||||
location: true,
|
||||
},
|
||||
});
|
||||
const letters = await getLettersByYear(year);
|
||||
const idx = letters.findIndex((l) => l.slug === slug);
|
||||
|
||||
const idx = yearLetters.findIndex((l) => l.slug === slug);
|
||||
return {
|
||||
prev: idx > 0 ? yearLetters[idx - 1] : null,
|
||||
next: idx < yearLetters.length - 1 ? yearLetters[idx + 1] : null,
|
||||
prev: idx > 0 ? letters[idx - 1] : null,
|
||||
next: idx < letters.length - 1 ? letters[idx + 1] : null,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getTotalLetterCount(): Promise<number> {
|
||||
return prisma.letter.count();
|
||||
const result = await bff<{ count: number }>("/letters", {
|
||||
tags: ["letters"],
|
||||
});
|
||||
return result?.count ?? 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user