feat: feature enforcement imports (#37/#38) and Universal CSV validation (#71)
Deploy Development / deploy (push) Successful in 1m9s
Build Test / pytest-backend (push) Failing after 5s
Build Test / lint-backend (push) Successful in 0s
Build Test / build-frontend (push) Successful in 24s

Activity and nutrition legacy CSV imports enforce tier limits with UsageBadge UI; Universal CSV gets mapping validation on copy/import, format-check parity, and structured error_details.

version: 0.9u
module: activity 1.2.1, nutrition 1.0.3, csv_import 0.4.0
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
LarsandCursor committed 2026-07-24 16:59:12 +02:00
1 parent 532e17c4cd
commit 79ec249bc3
27 files changed
+690 -95

No files matched your search

@@ -0,0 +1,76 @@
/**
* Strukturierte Zeilenfehler aus Universal-CSV-Import (error_details mit hint/code).
*/
export default function CsvImportErrorDetails({ errors, title, defaultOpen = true, maxVisible = 20 }) {
if (!errors?.length) return null
const shown = errors.slice(0, maxVisible)
const rest = errors.length - shown.length
return (
<details
className="card"
style={{ marginBottom: 16, padding: 16, cursor: 'pointer' }}
open={defaultOpen}
>
<summary style={{ fontWeight: 600, color: 'var(--text1)' }}>
{title || `Zeilenfehler (${errors.length})`}
</summary>
<ul
style={{
margin: '12px 0 0 0',
padding: 0,
listStyle: 'none',
fontSize: 13,
lineHeight: 1.5,
}}
>
{shown.map((err, i) => {
const row = err.row ?? err.row_index
const message = err.error ?? err.message ?? (typeof err === 'string' ? err : JSON.stringify(err))
return (
<li
key={i}
style={{
padding: '10px 12px',
marginBottom: 8,
background: 'var(--surface2)',
borderRadius: 8,
borderLeft: '3px solid var(--danger)',
}}
>
<div style={{ color: 'var(--text1)' }}>
{row != null && (
<strong style={{ marginRight: 6 }}>Zeile {row}:</strong>
)}
{message}
{err.code && (
<code
style={{
marginLeft: 8,
fontSize: 11,
color: 'var(--text3)',
background: 'var(--surface)',
padding: '1px 6px',
borderRadius: 4,
}}
>
{err.code}
</code>
)}
</div>
{err.hint && (
<div style={{ marginTop: 6, fontSize: 12, color: 'var(--text2)' }}>{err.hint}</div>
)}
</li>
)
})}
</ul>
{rest > 0 && (
<p style={{ fontSize: 12, color: 'var(--text3)', margin: '8px 0 0' }}>
… und {rest} weitere Fehler (Details in der Import-Historie).
</p>
)}
</details>
)
}
+20 -8
View File
@@ -364,13 +364,15 @@ function SessionMetricsFields({ schema, values, setValues, metrics }) {
}
// ── Import Panel ──────────────────────────────────────────────────────────────
function ImportPanel({ onImported }) {
function ImportPanel({ onImported, usage = null }) {
const fileRef = useRef()
const [status, setStatus] = useState(null)
const [error, setError] = useState(null)
const [dragging, setDragging] = useState(false)
const atLimit = usage && !usage.allowed
const runImport = async (file) => {
if (atLimit) return
setStatus('loading'); setError(null)
try {
const result = await api.importActivityCsv(file)
@@ -382,24 +384,29 @@ function ImportPanel({ onImported }) {
return (
<div className="card section-gap">
<div className="card-title">📥 Apple Health Import</div>
<div className="card-title badge-container-right">
<span>📥 Apple Health Import</span>
{usage && <UsageBadge {...usage} />}
</div>
<p style={{fontSize:13,color:'var(--text2)',marginBottom:10,lineHeight:1.6}}>
<strong>Health Auto Export App</strong> → Workouts exportieren → CSV → hier hochladen.<br/>
Nur die <em>Workouts-…csv</em> Datei wird benötigt (nicht die Detaildateien).
</p>
<input ref={fileRef} type="file" accept=".csv" style={{display:'none'}}
disabled={atLimit}
onChange={e=>{ const f=e.target.files[0]; if(f) runImport(f); e.target.value='' }}/>
<div
onDragOver={e=>{e.preventDefault();setDragging(true)}}
onDragOver={e=>{ if(!atLimit){ e.preventDefault(); setDragging(true) }}}
onDragLeave={()=>setDragging(false)}
onDrop={e=>{e.preventDefault();setDragging(false);const f=e.dataTransfer.files[0];if(f)runImport(f)}}
onClick={()=>fileRef.current.click()}
onDrop={e=>{ e.preventDefault(); setDragging(false); if(atLimit) return; const f=e.dataTransfer.files[0]; if(f) runImport(f) }}
onClick={()=>{ if(!atLimit) fileRef.current?.click() }}
title={atLimit ? `Limit erreicht (${usage.used}/${usage.limit})` : ''}
style={{border:`2px dashed ${dragging?'var(--accent)':'var(--border2)'}`,borderRadius:10,
padding:'20px 16px',textAlign:'center',background:dragging?'var(--accent-light)':'var(--surface2)',
cursor:'pointer',transition:'all 0.15s'}}>
cursor:atLimit?'not-allowed':'pointer',opacity:atLimit?0.65:1,transition:'all 0.15s'}}>
<Upload size={24} style={{color:dragging?'var(--accent)':'var(--text3)',marginBottom:6}}/>
<div style={{fontSize:13,color:dragging?'var(--accent-dark)':'var(--text2)'}}>
{dragging?'Datei loslassen…':'CSV hierher ziehen oder tippen'}
{atLimit ? '🔒 Limit erreicht — Import nicht möglich' : dragging ? 'Datei loslassen…' : 'CSV hierher ziehen oder tippen'}
</div>
</div>
{status==='loading' && (
@@ -945,7 +952,12 @@ export default function ActivityPage() {
</div>
)}
{tab==='import' && <ImportPanel onImported={load}/>}
{tab==='import' && (
<ImportPanel
usage={activityUsage}
onImported={() => { load(); loadUsage() }}
/>
)}
{tab==='categorize' && (
<div className="card section-gap">
+101 -39
View File
@@ -122,6 +122,68 @@ function buildImportRowProcessingSimple(modFields, fm, groupBy, mode, multiRowPo
return out
}
/** Gleiche Logik wie beim Speichern — für Formatprüfung und Create/Update. */
function resolveEditorImportRowProcessing({
aggregateSleepImport,
rowAggUseCustom,
rowAggIrregular,
rowAggJsonText,
rowAggGroupBy,
rowAggMode,
rowAggMultiRowPolicy,
rowAggDedupeIdentical,
modFields,
fieldMappings,
assignedTargets,
}) {
if (aggregateSleepImport || !rowAggUseCustom) {
return { import_row_processing: null, error: null }
}
if (rowAggIrregular) {
try {
const import_row_processing = JSON.parse(rowAggJsonText || '{}')
if (!import_row_processing || typeof import_row_processing !== 'object') {
return { import_row_processing: null, error: 'Zeilenaggregation: ungültiges JSON.' }
}
const gb = import_row_processing.group_by
if (!Array.isArray(gb) || !gb.length) {
return {
import_row_processing: null,
error: 'Zeilenaggregation (JSON): „group_by“ muss eine nicht-leere Liste sein.',
}
}
return { import_row_processing, error: null }
} catch {
return { import_row_processing: null, error: 'Zeilenaggregation: ungültiges JSON.' }
}
}
if (!rowAggGroupBy.length) {
return { import_row_processing: null, error: 'Zeilenaggregation: mindestens ein Schlüsselfeld auswählen.' }
}
if (!rowAggMode) {
return { import_row_processing: null, error: 'Zeilenaggregation: eine Funktion wählen (Summe, Mittelwert, …).' }
}
for (const g of rowAggGroupBy) {
if (!assignedTargets.has(g)) {
return {
import_row_processing: null,
error: `Zeilenaggregation: Schlüsselfeld „${g}“ muss einer CSV-Spalte zugeordnet sein.`,
}
}
}
return {
import_row_processing: buildImportRowProcessingSimple(
modFields,
fieldMappings,
rowAggGroupBy,
rowAggMode,
rowAggMultiRowPolicy,
rowAggDedupeIdentical,
),
error: null,
}
}
/** Erlaubt Eingaben wie 1,03 oder 1.03 während des Tippens; Finale normalisiert bei Blur/Speichern. */
function normalizeDecimalInputString(raw) {
let s = String(raw).trim().replace(/\s/g, '')
@@ -621,11 +683,28 @@ export default function AdminCsvTemplateEditorPage() {
}
setValidating(true)
try {
const { import_row_processing, error: irpError } = resolveEditorImportRowProcessing({
aggregateSleepImport,
rowAggUseCustom,
rowAggIrregular,
rowAggJsonText,
rowAggGroupBy,
rowAggMode,
rowAggMultiRowPolicy,
rowAggDedupeIdentical,
modFields: modMeta?.fields,
fieldMappings,
assignedTargets,
})
if (irpError) {
setError(irpError)
return
}
const r = await api.adminValidateCsvTemplate({
module,
field_mappings: fieldMappings,
type_conversions: tc,
import_row_processing: null,
import_row_processing,
column_signature: columnSignature.length ? columnSignature : null,
})
setValidationReport(r)
@@ -692,44 +771,25 @@ export default function AdminCsvTemplateEditorPage() {
}
let import_row_processing = null
if (!aggregateSleepImport && rowAggUseCustom) {
if (rowAggIrregular) {
try {
import_row_processing = JSON.parse(rowAggJsonText || '{}')
if (!import_row_processing || typeof import_row_processing !== 'object') throw new Error('bad')
} catch {
setError('Zeilenaggregation: ungültiges JSON.')
return
}
const gb = import_row_processing.group_by
if (!Array.isArray(gb) || !gb.length) {
setError('Zeilenaggregation (JSON): „group_by“ muss eine nicht-leere Liste sein.')
return
}
} else {
if (!rowAggGroupBy.length) {
setError('Zeilenaggregation: mindestens ein Schlüsselfeld auswählen.')
return
}
if (!rowAggMode) {
setError('Zeilenaggregation: eine Funktion wählen (Summe, Mittelwert, …).')
return
}
for (const g of rowAggGroupBy) {
if (!assignedTargets.has(g)) {
setError(`Zeilenaggregation: Schlüsselfeld „${g}“ muss einer CSV-Spalte zugeordnet sein.`)
return
}
}
import_row_processing = buildImportRowProcessingSimple(
modMeta?.fields,
fieldMappings,
rowAggGroupBy,
rowAggMode,
rowAggMultiRowPolicy,
rowAggDedupeIdentical,
)
{
const resolved = resolveEditorImportRowProcessing({
aggregateSleepImport,
rowAggUseCustom,
rowAggIrregular,
rowAggJsonText,
rowAggGroupBy,
rowAggMode,
rowAggMultiRowPolicy,
rowAggDedupeIdentical,
modFields: modMeta?.fields,
fieldMappings,
assignedTargets,
})
if (resolved.error) {
setError(resolved.error)
return
}
import_row_processing = resolved.import_row_processing
}
const payload = {
@@ -1494,12 +1554,13 @@ export default function AdminCsvTemplateEditorPage() {
{validationReport.valid ? <span style={{ color: 'var(--accent)' }}>— speicherfähig</span> : <span style={{ color: 'var(--danger)' }}>— Fehler beheben</span>}
</div>
<p style={{ fontSize: 12, color: 'var(--text3)', marginBottom: 10 }}>
Ohne Zeilenaggregations-JSON; vollständige Prüfung inkl. Aggregation beim Speichern. Warnungen blockieren nicht.
Inkl. Zeilenaggregation (wie beim Speichern). Warnungen blockieren nicht.
</p>
{validationReport.errors?.length ? (
<ul style={{ margin: '0 0 12px 1rem', color: 'var(--danger)', fontSize: 14 }}>
{validationReport.errors.map((e, i) => (
<li key={`e-${i}`}>
{e.code ? <code style={{ fontSize: 11, marginRight: 6 }}>{e.code}</code> : null}
{e.message}
{e.hint ? <span style={{ display: 'block', fontSize: 12, color: 'var(--text2)', marginTop: 4 }}>{e.hint}</span> : null}
</li>
@@ -1510,6 +1571,7 @@ export default function AdminCsvTemplateEditorPage() {
<ul style={{ margin: '0 0 0 1rem', color: 'var(--text2)', fontSize: 13 }}>
{validationReport.warnings.map((w, i) => (
<li key={`w-${i}`}>
{w.code ? <code style={{ fontSize: 11, marginRight: 6 }}>{w.code}</code> : null}
{w.message}
{w.hint ? <span style={{ display: 'block', fontSize: 12, color: 'var(--text3)', marginTop: 4 }}>{w.hint}</span> : null}
</li>
+40 -14
View File
@@ -1,5 +1,6 @@
import { useState, useEffect, useRef } from 'react'
import { Upload, CheckCircle, TrendingUp, Info } from 'lucide-react'
import UsageBadge from '../components/UsageBadge'
import {
LineChart, Line, BarChart, Bar, XAxis, YAxis, Tooltip,
ResponsiveContainer, CartesianGrid, Legend, ReferenceLine, ScatterChart, Scatter
@@ -433,15 +434,17 @@ function ImportHistory() {
}
// ── Import Panel ──────────────────────────────────────────────────────────────
function ImportPanel({ onImported }) {
function ImportPanel({ onImported, usage = null }) {
const fileRef = useRef()
const [status, setStatus] = useState(null)
const [error, setError] = useState(null)
const [dragging,setDragging]= useState(false)
const [tab, setTab] = useState('file') // 'file' | 'paste'
const [pasteText, setPasteText] = useState('')
const atLimit = usage && !usage.allowed
const runImport = async (file) => {
if (atLimit) return
setStatus('loading'); setError(null)
try {
const result = await nutritionApi.importCsv(file)
@@ -468,7 +471,7 @@ function ImportPanel({ onImported }) {
}
const handlePasteImport = async () => {
if (!pasteText.trim()) return
if (!pasteText.trim() || atLimit) return
const blob = new Blob([pasteText], { type: 'text/csv' })
const file = new File([blob], 'paste.csv', { type: 'text/csv' })
await runImport(file)
@@ -476,7 +479,10 @@ function ImportPanel({ onImported }) {
return (
<div className="card section-gap">
<div className="card-title">📥 FDDB CSV Import</div>
<div className="card-title badge-container-right">
<span>📥 FDDB CSV Import</span>
{usage && <UsageBadge {...usage} />}
</div>
<p style={{fontSize:13,color:'var(--text2)',marginBottom:10,lineHeight:1.6}}>
In FDDB: <strong>Mein Tagebuch → Exportieren → CSV</strong> — dann hier importieren.
</p>
@@ -498,23 +504,26 @@ function ImportPanel({ onImported }) {
<>
{/* Drag & Drop Zone */}
<div
onDragOver={e=>{e.preventDefault();setDragging(true)}}
onDragOver={e=>{ if(!atLimit){ e.preventDefault(); setDragging(true) }}}
onDragLeave={()=>setDragging(false)}
onDrop={handleDrop}
onClick={()=>fileRef.current.click()}
onDrop={e=>{ e.preventDefault(); setDragging(false); if(atLimit) return; handleDrop(e) }}
onClick={()=>{ if(!atLimit) fileRef.current?.click() }}
title={atLimit ? `Limit erreicht (${usage.used}/${usage.limit})` : ''}
style={{
border:`2px dashed ${dragging?'var(--accent)':'var(--border2)'}`,
borderRadius:10, padding:'24px 16px', textAlign:'center',
background: dragging?'var(--accent-light)':'var(--surface2)',
cursor:'pointer', transition:'all 0.15s',
cursor:atLimit?'not-allowed':'pointer', opacity:atLimit?0.65:1, transition:'all 0.15s',
}}>
<Upload size={28} style={{color:dragging?'var(--accent)':'var(--text3)',marginBottom:8}}/>
<div style={{fontSize:14,fontWeight:500,color:dragging?'var(--accent-dark)':'var(--text2)'}}>
{dragging ? 'Datei loslassen…' : 'CSV hierher ziehen oder tippen zum Auswählen'}
{atLimit
? '🔒 Limit erreicht — Import nicht möglich'
: dragging ? 'Datei loslassen…' : 'CSV hierher ziehen oder tippen zum Auswählen'}
</div>
<div style={{fontSize:11,color:'var(--text3)',marginTop:4}}>.csv Dateien</div>
</div>
<input ref={fileRef} type="file" accept=".csv" style={{display:'none'}} onChange={handleFile}/>
<input ref={fileRef} type="file" accept=".csv" style={{display:'none'}} disabled={atLimit} onChange={handleFile}/>
</>
)}
@@ -523,14 +532,20 @@ function ImportPanel({ onImported }) {
<textarea
style={{width:'100%',minHeight:120,padding:10,fontFamily:'monospace',fontSize:11,
background:'var(--surface2)',border:'1.5px solid var(--border2)',borderRadius:8,
color:'var(--text1)',resize:'vertical',boxSizing:'border-box'}}
color:'var(--text1)',resize:'vertical',boxSizing:'border-box',
opacity:atLimit?0.65:1}}
placeholder="datum_tag_monat_jahr_stunde_minute;bezeichnung;&#10;13.03.2026 21:54;50 g Hähnchen;..."
value={pasteText}
onChange={e=>setPasteText(e.target.value)}
disabled={atLimit}
/>
<button className="btn btn-primary btn-full" style={{marginTop:8}}
onClick={handlePasteImport} disabled={status==='loading'||!pasteText.trim()}>
{status==='loading'
onClick={handlePasteImport}
disabled={status==='loading'||!pasteText.trim()||atLimit}
title={atLimit ? `Limit erreicht (${usage.used}/${usage.limit})` : ''}>
{atLimit
? '🔒 Limit erreicht'
: status==='loading'
? <><div className="spinner" style={{width:14,height:14}}/> Importiere…</>
: <><Upload size={15}/> CSV-Text importieren</>}
</button>
@@ -781,6 +796,14 @@ export default function NutritionPage() {
const [loading, setLoad] = useState(true)
const [hasData, setHasData]= useState(false)
const [importHistoryKey, setImportHistoryKey] = useState(Date.now()) // BUG-004 fix
const [nutritionUsage, setNutritionUsage] = useState(null)
const loadUsage = () => {
nutritionApi.getFeatureUsage().then(features => {
const nutritionFeature = features.find(f => f.feature_id === 'nutrition_entries')
setNutritionUsage(nutritionFeature ?? null)
}).catch(err => console.error('Failed to load usage:', err))
}
const load = async () => {
setLoad(true)
@@ -800,7 +823,7 @@ export default function NutritionPage() {
finally { setLoad(false) }
}
useEffect(() => { load() }, [])
useEffect(() => { load(); loadUsage() }, [])
return (
<div className="capture-page">
@@ -822,7 +845,10 @@ export default function NutritionPage() {
{/* Import Panel + History */}
{inputTab==='import' && (
<>
<ImportPanel onImported={() => { load(); setImportHistoryKey(Date.now()) }}/>
<ImportPanel
usage={nutritionUsage}
onImported={() => { load(); loadUsage(); setImportHistoryKey(Date.now()) }}
/>
<ImportHistory key={importHistoryKey}/>
</>
)}
+6 -23
View File
@@ -3,6 +3,7 @@ import { useNavigate } from 'react-router-dom'
import { ArrowLeft, FileSpreadsheet, Loader2, Upload } from 'lucide-react'
import { api } from '../utils/api'
import { csvPreviewTdStyle } from '../utils/csvPreviewCells'
import CsvImportErrorDetails from '../components/CsvImportErrorDetails'
/** Ziele, die der Universal-Executor bereits schreiben kann (ohne manuelle Modul-Wahl). */
const EXECUTOR_READY = new Set([
@@ -288,29 +289,11 @@ export default function UniversalCsvImportPage() {
)}
{lastImport?.error_details?.length > 0 && (
<details
className="card"
style={{ marginBottom: 16, padding: 16, cursor: 'pointer' }}
open
>
<summary style={{ fontWeight: 600, color: 'var(--text1)' }}>
Zeilenfehler vom letzten Import ({lastImport.error_details.length}) — zum Kopieren aufklappen
</summary>
<pre
style={{
marginTop: 12,
fontSize: 12,
overflow: 'auto',
maxHeight: 320,
background: 'var(--surface2)',
padding: 12,
borderRadius: 8,
color: 'var(--text1)',
}}
>
{JSON.stringify(lastImport.error_details, null, 2)}
</pre>
</details>
<CsvImportErrorDetails
errors={lastImport.error_details}
title={`Zeilenfehler vom letzten Import (${lastImport.error_details.length})`}
defaultOpen
/>
)}
<div className="card" style={{ marginBottom: 16, padding: 16 }}>
+8
View File
@@ -29,6 +29,13 @@ export function formatFastApiDetail(detail, fallback = '') {
return parts.length ? parts.join(' · ') : fallback || 'Validierungsfehler'
}
if (typeof detail === 'object') {
if (detail.validation?.errors?.length) {
const parts = detail.validation.errors
.map((e) => (e && typeof e === 'object' ? e.message || e.msg || '' : String(e)))
.filter(Boolean)
const prefix = detail.message || 'Vorlage ungültig'
return parts.length ? `${prefix}: ${parts.join(' · ')}` : prefix
}
if (Array.isArray(detail.errors) && detail.errors.length > 0) {
const parts = detail.errors
.map((e) => {
@@ -713,6 +720,7 @@ export const api = {
req(module ? `/csv/mappings?module=${encodeURIComponent(module)}` : '/csv/mappings'),
copyCsvMapping: (mappingId, body = null) =>
req(`/csv/mappings/${mappingId}/copy`, body ? json(body) : { method: 'POST' }),
validateCsvMapping: (mappingId) => req(`/csv/mappings/${mappingId}/validate`),
/** Universal-CSV (Issue #21): Zielmodul steckt in der Vorlage; nur file + mapping_id */
/** Import-Diagnose: keine Datenbank-Schreibung, erste Zeilen + Mapping-Auflösung */
diagnoseUniversalCsv: async (file, mappingId, module = null) => {