feat(api): Vercel-native platform API + Postgres schema
Route Handlers (Vercel Functions): /api/health, /api/estimates (single-source estimate math, ore), /api/catalog, /api/sources (legal sourcing map). sql/001_init.sql = full docs/03 data model (entities + bridge_provenance + platform tables, pgvector). Ready to wire Vercel Postgres. Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { computeEstimate, type EntityType, type Frequency } from "@/lib/data";
|
||||
|
||||
// POST /api/estimates (docs/04) — price/time/confidence for a configured job.
|
||||
// Single source of truth for the estimate math; the configurator calls this.
|
||||
export async function POST(req: NextRequest) {
|
||||
let body: Record<string, unknown>;
|
||||
try {
|
||||
body = await req.json();
|
||||
} catch {
|
||||
return NextResponse.json({ error: { code: "bad_json", message: "Invalid JSON body" } }, { status: 400 });
|
||||
}
|
||||
|
||||
const markets = Array.isArray(body.markets) ? (body.markets as string[]) : [];
|
||||
const entity_type = (body.entity_type as EntityType) || "Bolag";
|
||||
const fields = Array.isArray(body.fields) ? (body.fields as string[]) : [];
|
||||
const enrichment = Array.isArray(body.enrichment) ? (body.enrichment as string[]) : [];
|
||||
const volume = Number(body.volume) || 1000;
|
||||
const frequency = (body.frequency as Frequency) || "Engångs";
|
||||
const confidence_target = Number(body.confidence_target) || 92;
|
||||
|
||||
const est = computeEstimate({
|
||||
markets,
|
||||
type: entity_type,
|
||||
fieldIds: fields,
|
||||
enrichmentIds: enrichment,
|
||||
volume,
|
||||
frequency,
|
||||
confidence: confidence_target,
|
||||
});
|
||||
|
||||
// amounts in öre (docs/03 convention)
|
||||
return NextResponse.json({
|
||||
total_ore: est.price * 100,
|
||||
deposit_ore: est.deposit * 100,
|
||||
balance_ore: est.remainder * 100,
|
||||
est_days: est.timelineDays,
|
||||
est_confidence: est.estConfidence / 100,
|
||||
recurring: est.recurring,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user