← back to Govarbitrage

src/app/api/listings/[id]/buyer-page/route.ts

47 lines

import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { requireWrite } from "@/lib/auth";

export const dynamic = "force-dynamic";

function slugify(s: string) {
  return s.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "").slice(0, 60);
}

// Create/update a CONTINGENT buyer-interest page for a listing.
export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  const denied = await requireWrite(req);
  if (denied) return denied;
  const { id } = await params;
  const listing = await prisma.listing.findUnique({ where: { id } });
  if (!listing) return NextResponse.json({ error: "Not found" }, { status: 404 });

  const body = (await req.json().catch(() => ({}))) as {
    headline?: string;
    estDelivered?: number;
    published?: boolean;
  };
  const slug = slugify(`${listing.manufacturer ?? listing.title}-${listing.sourceAuctionId}`);

  const page = await prisma.buyerInterestPage.upsert({
    where: { listingId: id },
    create: {
      listingId: id,
      slug,
      headline: body.headline ?? `${listing.title} — Contingent Interest`,
      estDelivered: body.estDelivered ?? null,
      published: body.published ?? true,
      contingent: true,
    },
    update: {
      headline: body.headline,
      estDelivered: body.estDelivered,
      published: body.published,
    },
  });
  await prisma.listingEvent.create({
    data: { listingId: id, type: "MANUAL_EDIT", message: `Buyer-interest page ${page.slug} saved` },
  });
  return NextResponse.json(page);
}