SPB Git

Repository Public

route.ts 2.1 KB 43 lines typescript

1// UQO Éval — modélisation hédonique : export CSV de l'échantillon (pour R / Python / Stata).2import { NextRequest, NextResponse } from "next/server";3import { buildSample } from "@/lib/hedonic/data";4import { DEFAULT_SPEC, type HedonicSpec } from "@/lib/hedonic/spec";56export const dynamic = "force-dynamic";78const esc = (v: unknown) => {9  if (v == null) return "";10  const s = String(v);11  return /[",\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s;12};1314export async function POST(req: NextRequest) {15  let spec: HedonicSpec;16  try {17    const body = (await req.json()) as Partial<HedonicSpec>;18    spec = { ...DEFAULT_SPEC, ...body, area: { ...DEFAULT_SPEC.area, ...(body.area ?? {}) }, period: { ...DEFAULT_SPEC.period, ...(body.period ?? {}) }, price: { ...DEFAULT_SPEC.price, ...(body.price ?? {}) }, fe: { ...DEFAULT_SPEC.fe, ...(body.fe ?? {}) } };19  } catch {20    return NextResponse.json({ error: "JSON invalide" }, { status: 400 });21  }22  try {23    const { obs } = buildSample(spec);24    const cols = ["id", "price", "date", "year", "quarter", "month", "lat", "lng", "street", "city", "muni", "arrond", "voisinage", "type", "aire", "terrain", "annee", "age", "nb_logements", "nb_etages", "genre", "lien", "valeur_role", "bedrooms", "bathrooms", "lab_estimate"];25    const lines = [cols.join(",")];26    for (const o of obs) {27      lines.push(28        [o.id, o.price, o.date, o.year, o.quarter, o.month, o.lat, o.lng, o.street, o.city, o.muni, o.arrond, o.voisinage, o.type, o.aire, o.terrain, o.annee, o.annee ? o.year - o.annee : null, o.nb_logements, o.nb_etages, o.genre, o.lien, o.valeur_role, o.bedrooms, o.bathrooms, o.lab != null ? Math.round(o.lab) : null]29          .map(esc)30          .join(",")31      );32    }33    return new NextResponse(lines.join("\n"), {34      headers: {35        "Content-Type": "text/csv; charset=utf-8",36        "Content-Disposition": `attachment; filename="echantillon_uqo_eval_${spec.source}_${(spec.area.muni ?? "quebec").replace(/[^\w-]+/g, "_")}.csv"`,37      },38    });39  } catch (e) {40    return NextResponse.json({ error: e instanceof Error ? e.message : String(e) }, { status: 422 });41  }42}43
Files main
Menu

Theme