security: re-secure fp-patch
Some checks failed
Build pitch-deck / build-push-deploy (push) Has been cancelled
CI / go-lint (push) Has been cancelled
CI / python-lint (push) Has been cancelled
CI / nodejs-lint (push) Has been cancelled
CI / test-go-consent (push) Has been cancelled
CI / test-python-voice (push) Has been cancelled
CI / test-bqas (push) Has been cancelled
Some checks failed
Build pitch-deck / build-push-deploy (push) Has been cancelled
CI / go-lint (push) Has been cancelled
CI / python-lint (push) Has been cancelled
CI / nodejs-lint (push) Has been cancelled
CI / test-go-consent (push) Has been cancelled
CI / test-python-voice (push) Has been cancelled
CI / test-bqas (push) Has been cancelled
This commit is contained in:
@@ -1,43 +1,17 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { requireAdmin } from '@/lib/admin-auth'
|
||||
import pool from '@/lib/db'
|
||||
import { computeFinanzplan } from '@/lib/finanzplan/engine'
|
||||
|
||||
/** Admin-only: recompute a Finanzplan scenario. */
|
||||
export async function POST(request: NextRequest) {
|
||||
const guard = await requireAdmin(request)
|
||||
if (guard.kind === 'response') return guard.response
|
||||
|
||||
const body = await request.json().catch(() => ({}))
|
||||
const results: string[] = []
|
||||
const scenarioId = body.scenarioId || (await pool.query("SELECT id FROM fp_scenarios WHERE is_default = true LIMIT 1")).rows[0]?.id
|
||||
if (!scenarioId) return NextResponse.json({ error: 'No scenario found' }, { status: 404 })
|
||||
|
||||
try {
|
||||
const BASE = (await pool.query("SELECT id FROM fp_scenarios WHERE is_default=true LIMIT 1")).rows[0]?.id
|
||||
if (!BASE) { return NextResponse.json({ ok: false, error: 'No base scenario' }) }
|
||||
|
||||
// Import materialaufwand
|
||||
if (body.material?.length) {
|
||||
await pool.query(`DELETE FROM fp_materialaufwand WHERE scenario_id=$1`, [BASE])
|
||||
for (const r of body.material) {
|
||||
await pool.query(
|
||||
`INSERT INTO fp_materialaufwand (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(`material: ${body.material.length}`)
|
||||
}
|
||||
|
||||
// Import investitionen
|
||||
if (body.invest?.length) {
|
||||
await pool.query(`DELETE FROM fp_investitionen WHERE scenario_id=$1`, [BASE])
|
||||
for (const r of body.invest) {
|
||||
await pool.query(
|
||||
`INSERT INTO fp_investitionen (scenario_id,item_name,category,purchase_amount,purchase_date,afa_years,is_editable,values_invest,values_afa,sort_order) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`,
|
||||
[BASE, r.item_name, r.category, r.purchase_amount, r.purchase_date, r.afa_years, r.is_editable, JSON.stringify(r.values_invest), JSON.stringify(r.values_afa), r.sort_order])
|
||||
}
|
||||
results.push(`invest: ${body.invest.length}`)
|
||||
}
|
||||
|
||||
// Recompute
|
||||
const r = await computeFinanzplan(pool, BASE)
|
||||
results.push(`BASE cash_m60=${r.liquiditaet?.endstand?.m60}`)
|
||||
} catch (err) {
|
||||
results.push(`ERROR: ${err instanceof Error ? err.message : String(err)}`)
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, results })
|
||||
const result = await computeFinanzplan(pool, scenarioId)
|
||||
return NextResponse.json({ success: true, scenarioId, cash_m60: result.liquiditaet?.endstand?.m60 })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user