← back to Trademarks Copyright

src/app/api/drops/latest/route.ts

45 lines

import { NextResponse } from "next/server";
import { query } from "@/lib/db";

/**
 * Public snapshot of the most recent published/sent drop, teased to 2 items.
 * Drives the "sample drop" preview on the landing page.
 */
export async function GET() {
  const { rows: drops } = await query<{
    id: number; drop_date: string; subject: string; intro: string | null;
  }>(
    `SELECT id, drop_date, subject, intro
     FROM drops
     WHERE status IN ('published', 'sent')
     ORDER BY drop_date DESC
     LIMIT 1`
  );
  if (!drops.length) return NextResponse.json({ drop: null });

  const drop = drops[0];
  const { rows: items } = await query<{
    position: number; headline: string; blurb: string;
  }>(
    `SELECT position, headline, blurb
     FROM drop_items WHERE drop_id = $1 ORDER BY position LIMIT 2`,
    [drop.id]
  );
  const { rows: counts } = await query<{ total: string }>(
    `SELECT COUNT(*)::text AS total FROM drop_items WHERE drop_id = $1`,
    [drop.id]
  );
  const total = Number(counts[0]?.total ?? 0);

  return NextResponse.json({
    drop: {
      id: drop.id,
      date: drop.drop_date,
      subject: drop.subject,
      intro: drop.intro,
      items,
      hidden_count: Math.max(0, total - items.length),
    },
  });
}