diff --git a/pitch-deck/app/api/admin/fp-patch/route.ts b/pitch-deck/app/api/admin/fp-patch/route.ts index 333550d..ed07410 100644 --- a/pitch-deck/app/api/admin/fp-patch/route.ts +++ b/pitch-deck/app/api/admin/fp-patch/route.ts @@ -1,47 +1,46 @@ -import { NextResponse } from 'next/server' +import { NextRequest, NextResponse } from 'next/server' import pool from '@/lib/db' +import { computeFinanzplan } from '@/lib/finanzplan/engine' -export async function POST() { - const VER_1M = 'b7204781-aba0-45cc-a655-a0ff88d1173a' +export async function POST(request: NextRequest) { + const body = await request.json().catch(() => ({})) const results: string[] = [] try { - // 1. Get current funding data from version - const { rows: fundingRows } = await pool.query( - `SELECT data FROM pitch_version_data WHERE version_id=$1 AND table_name='funding'`, [VER_1M]) - - if (fundingRows.length > 0) { - const funding = typeof fundingRows[0].data === 'string' ? JSON.parse(fundingRows[0].data) : fundingRows[0].data - results.push(`Current funding: ${JSON.stringify(funding)}`) - - // Update: remove Wandeldarlehen from instrument, set round to Pre-Seed - if (Array.isArray(funding) && funding.length > 0) { - funding[0].instrument = 'Equity' - funding[0].round_name = 'Pre-Seed' - await pool.query( - `UPDATE pitch_version_data SET data=$1 WHERE version_id=$2 AND table_name='funding'`, - [JSON.stringify(funding), VER_1M]) - results.push('Updated: instrument=Equity, round=Pre-Seed') - } - } else { - results.push('No funding data in version') - } - - // 2. Check what fm_scenarios the 1M version uses - const { rows: scenRows } = await pool.query( - `SELECT data FROM pitch_version_data WHERE version_id=$1 AND table_name='fm_scenarios'`, [VER_1M]) - if (scenRows.length > 0) { - results.push(`fm_scenarios: ${JSON.stringify(scenRows[0].data)}`) - } - - // 3. Check Base Case scenario data counts + // Get Base Case scenario ID on production const BASE = (await pool.query("SELECT id FROM fp_scenarios WHERE is_default=true LIMIT 1")).rows[0]?.id - if (BASE) { - for (const t of ['fp_kunden', 'fp_umsatzerloese', 'fp_personalkosten']) { - const { rows: [c] } = await pool.query(`SELECT COUNT(*) as cnt FROM ${t} WHERE scenario_id=$1`, [BASE]) - results.push(`${t}: ${c.cnt} rows`) + if (!BASE) { results.push('NO BASE SCENARIO'); return NextResponse.json({ ok: false, results }) } + + // Import kunden + if (body.kunden?.length) { + await pool.query(`DELETE FROM fp_kunden WHERE scenario_id=$1`, [BASE]) + for (const r of body.kunden) { + await pool.query( + `INSERT INTO fp_kunden (scenario_id,segment_name,segment_index,row_label,row_index,is_editable,values,sort_order) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)`, + [BASE, r.segment_name, r.segment_index, r.row_label, r.row_index, r.is_editable, JSON.stringify(r.values), r.sort_order]) } + results.push(`kunden: ${body.kunden.length}`) } + + // Import umsatz + if (body.umsatz?.length) { + await pool.query(`DELETE FROM fp_umsatzerloese WHERE scenario_id=$1`, [BASE]) + for (const r of body.umsatz) { + await pool.query( + `INSERT INTO fp_umsatzerloese (scenario_id,section,row_label,row_index,is_editable,values,sort_order) VALUES ($1,$2,$3,$4,$5,$6,$7)`, + [BASE, r.section, r.row_label, r.row_index, r.is_editable, JSON.stringify(r.values), r.sort_order]) + } + results.push(`umsatz: ${body.umsatz.length}`) + } + + // Recompute Base Case + const r = await computeFinanzplan(pool, BASE) + results.push(`BASE cash_m60=${r.liquiditaet?.endstand?.m60}`) + + // Verify + const { rows: guv } = await pool.query( + `SELECT (values->>'y2026')::numeric as y26, (values->>'y2030')::numeric as y30 FROM fp_guv WHERE scenario_id=$1 AND row_label='Umsatzerlöse'`, [BASE]) + results.push(`Umsatz: ${JSON.stringify(guv[0])}`) } catch (err) { results.push(`ERROR: ${err instanceof Error ? err.message : String(err)}`) }