← back to Govarbitrage

scripts/seed-digest-snapshots.ts

118 lines

// Seed the public /deals archive:
//   1. A REAL capture for today's slot via captureDigestSnapshot() — freezes
//      whatever currently clears the hot-deal gate (may be 0 deals; the
//      archive renders that honestly).
//   2. A clearly-marked FABRICATED edition for 2026-07-12 with realistic
//      deals so the teaser rendering (confidence labels, ROI bands, withheld
//      numbers) is actually exercised before launch. Every fabricated deal's
//      disclaimer says SEED DATA. Delete before go-live:
//        DELETE FROM "DigestSnapshot" WHERE date = '2026-07-12';
//
// Run: npx tsx scripts/seed-digest-snapshots.ts

import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import type { Prisma } from "@prisma/client";

function loadEnv() {
  const root = join(dirname(fileURLToPath(import.meta.url)), "..");
  try {
    const raw = readFileSync(join(root, ".env"), "utf8");
    for (const line of raw.split("\n")) {
      const m = line.match(/^\s*([A-Z0-9_]+)\s*=\s*(.*)\s*$/i);
      if (!m) continue;
      const key = m[1];
      let val = m[2].trim();
      if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
        val = val.slice(1, -1);
      }
      if (process.env[key] === undefined) process.env[key] = val;
    }
  } catch {
    /* no .env — rely on ambient env */
  }
}
loadEnv();

const { prisma } = await import("../src/lib/db");
const { captureDigestSnapshot, currentSlot } = await import("../src/lib/digest-snapshot");
type SnapshotDeal = import("../src/lib/digest-snapshot").SnapshotDeal;

const SEED_DATE = "2026-07-12";
const SEED_DISCLAIMER = "SEED DATA — fabricated example for pre-launch rendering tests, not a real listing.";

const seedDeals: SnapshotDeal[] = [
  {
    rank: 1,
    title: "2018 John Deere 320G Skid Steer (1,240 hrs) — County Fleet Surplus",
    source: "GOVDEALS",
    locationCity: "Bakersfield",
    locationState: "CA",
    currentBid: 6200,
    recMax: 14800,
    expectedSaleLow: 21500,
    confidence: "HIGH",
    roiConservative: 0.58,
    roiCapped: false,
    disclaimer: SEED_DISCLAIMER,
    closingAt: "2026-07-14T19:00:00.000Z",
    sourceUrl: null,
  },
  {
    rank: 2,
    title: "Pallet of 24 Dell Latitude 5520 Laptops (i5/16GB, wiped) — School District IT Refresh",
    source: "PUBLICSURPLUS",
    locationCity: "Mesa",
    locationState: "AZ",
    currentBid: 1150,
    recMax: 3400,
    expectedSaleLow: 5300,
    confidence: "MEDIUM",
    roiConservative: 0.49,
    roiCapped: false,
    disclaimer: SEED_DISCLAIMER,
    closingAt: "2026-07-13T22:30:00.000Z",
    sourceUrl: null,
  },
  {
    rank: 3,
    title: "2015 Ford F-250 XL 4x4 Utility Truck w/ Liftgate — Municipal Water Dept.",
    source: "MUNICIBID",
    locationCity: "Spokane",
    locationState: "WA",
    currentBid: 3875,
    recMax: 9200,
    expectedSaleLow: 12800,
    confidence: "LOW",
    roiConservative: 0.41,
    roiCapped: true,
    disclaimer: SEED_DISCLAIMER,
    closingAt: null, // make-offer, no deadline
    sourceUrl: null,
  },
];

async function main() {
  const real = await captureDigestSnapshot(currentSlot());
  console.log(`Real capture: ${real.date} ${real.slot} — ${real.dealCount} deal(s)`);

  const dealsJson = seedDeals as unknown as Prisma.InputJsonValue;
  const fabricated = await prisma.digestSnapshot.upsert({
    where: { date_slot: { date: SEED_DATE, slot: "AM" } },
    create: { date: SEED_DATE, slot: "AM", dealsJson, dealCount: seedDeals.length, isSeed: true },
    update: { dealsJson, dealCount: seedDeals.length, isSeed: true },
  });
  console.log(`Fabricated seed edition: ${fabricated.date} ${fabricated.slot} — ${fabricated.dealCount} deal(s) (SEED DATA)`);
}

main()
  .then(async () => {
    await prisma.$disconnect();
  })
  .catch(async (e) => {
    console.error(e);
    await prisma.$disconnect();
    process.exit(1);
  });