feat(csv-templates): Add CSV template validation endpoint and enhance error handling
- Introduced a new endpoint for validating CSV templates without saving, allowing users to check field mappings and type conversions. - Updated the `create_system_template` and `update_system_template` functions to include validation reports in responses. - Enhanced error handling in CSV import processes by integrating `enrich_row_error` for more informative error messages. - Improved the AdminCsvTemplateEditorPage to support format checking and display validation results, enhancing user experience. - Incremented version numbers for `csv_import` and `admin_csv_templates` to reflect these updates.
This commit is contained in:
1 parent
6945b748cb
commit
0629f88b37
10 files changed
+486
-39
No files matched your search
@@ -278,6 +278,8 @@ export default function AdminCsvTemplateEditorPage() {
|
||||
const [loading, setLoading] = useState(!isNew)
|
||||
const [analyzing, setAnalyzing] = useState(false)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [validating, setValidating] = useState(false)
|
||||
const [validationReport, setValidationReport] = useState(null)
|
||||
const [error, setError] = useState(null)
|
||||
/** Entwurf für „Quelle entspricht Ziel“ (nur source_unit custom); Commit bei Blur/Speichern. */
|
||||
const [customEquivalenceDraftByField, setCustomEquivalenceDraftByField] = useState({})
|
||||
@@ -593,6 +595,38 @@ export default function AdminCsvTemplateEditorPage() {
|
||||
setFieldMappings((prev) => ({ ...prev, [col]: dbField || '-' }))
|
||||
}
|
||||
|
||||
const handleFormatCheck = async () => {
|
||||
setError(null)
|
||||
setValidationReport(null)
|
||||
let tc
|
||||
try {
|
||||
tc = JSON.parse(typeConversionsText || '{}')
|
||||
if (tc !== null && typeof tc !== 'object') throw new Error()
|
||||
} catch {
|
||||
setError('type_conversions: ungültiges JSON.')
|
||||
return
|
||||
}
|
||||
if (!module) {
|
||||
setError('Modul wählen.')
|
||||
return
|
||||
}
|
||||
setValidating(true)
|
||||
try {
|
||||
const r = await api.adminValidateCsvTemplate({
|
||||
module,
|
||||
field_mappings: fieldMappings,
|
||||
type_conversions: tc,
|
||||
import_row_processing: null,
|
||||
column_signature: columnSignature.length ? columnSignature : null,
|
||||
})
|
||||
setValidationReport(r)
|
||||
} catch (e) {
|
||||
setError(e.message || 'Formatprüfung fehlgeschlagen')
|
||||
} finally {
|
||||
setValidating(false)
|
||||
}
|
||||
}
|
||||
|
||||
const handleSave = async () => {
|
||||
setError(null)
|
||||
let textForTc = typeConversionsText
|
||||
@@ -1432,7 +1466,49 @@ export default function AdminCsvTemplateEditorPage() {
|
||||
/>
|
||||
</div>
|
||||
|
||||
{validationReport ? (
|
||||
<div className="card" style={{ padding: 16, marginBottom: 16, borderColor: validationReport.valid ? 'var(--accent)' : 'var(--danger)' }}>
|
||||
<div className="form-label" style={{ marginBottom: 8 }}>
|
||||
Formatprüfung (Vorlage){' '}
|
||||
{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.
|
||||
</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.message}
|
||||
{e.hint ? <span style={{ display: 'block', fontSize: 12, color: 'var(--text2)', marginTop: 4 }}>{e.hint}</span> : null}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
{validationReport.warnings?.length ? (
|
||||
<ul style={{ margin: '0 0 0 1rem', color: 'var(--text2)', fontSize: 13 }}>
|
||||
{validationReport.warnings.map((w, i) => (
|
||||
<li key={`w-${i}`}>
|
||||
{w.message}
|
||||
{w.hint ? <span style={{ display: 'block', fontSize: 12, color: 'var(--text3)', marginTop: 4 }}>{w.hint}</span> : null}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 12 }}>
|
||||
<button type="button" className="btn btn-secondary" disabled={validating || saving} onClick={handleFormatCheck} style={{ minWidth: 160 }}>
|
||||
{validating ? (
|
||||
<>
|
||||
<Loader2 size={18} style={{ marginRight: 8, animation: 'spin 0.7s linear infinite' }} />
|
||||
Prüfen …
|
||||
</>
|
||||
) : (
|
||||
'Format prüfen'
|
||||
)}
|
||||
</button>
|
||||
<button type="button" className="btn btn-primary" disabled={saving} onClick={handleSave} style={{ flex: 1, minWidth: 160 }}>
|
||||
{saving ? (
|
||||
<>
|
||||
|
||||
Reference in new issue
Block a user