feat(csv-import): Refactor CSV import logic and enhance data handling
- Updated the CSV import architecture to clarify the distinction between import and data layer responsibilities, as outlined in the new section of ARCHITECTURE.md. - Enhanced the build_row_after_mapping function to include module-specific context for improved data processing. - Introduced source unit options in the admin CSV template editor to facilitate user-defined conversions, improving flexibility in handling various data formats. - Added new tests to validate the handling of source units and ensure accurate conversions during CSV imports. - Updated module definitions to include unit specifications for nutritional and activity data fields, enhancing data integrity.
This commit is contained in:
1 parent
41cc0ed2a8
commit
d6d7e738a5
10 files changed
+324
-27
No files matched your search
@@ -159,6 +159,57 @@ export default function AdminCsvTemplateEditorPage() {
|
||||
return requiredTargets.filter((r) => !assignedTargets.has(r))
|
||||
}, [requiredTargets, assignedTargets])
|
||||
|
||||
const unitTargets = useMemo(() => {
|
||||
if (!modMeta?.fields || aggregateSleepImport) return []
|
||||
const list = []
|
||||
const seen = new Set()
|
||||
for (const t of assignedTargets) {
|
||||
const opts = modMeta.fields[t]?.source_unit_options
|
||||
if (!opts?.length || seen.has(t)) continue
|
||||
seen.add(t)
|
||||
list.push({ field: t, options: opts })
|
||||
}
|
||||
return list.sort((a, b) => a.field.localeCompare(b.field))
|
||||
}, [modMeta, assignedTargets, aggregateSleepImport])
|
||||
|
||||
const getSourceUnitSelectValue = (fieldKey) => {
|
||||
let tc
|
||||
try {
|
||||
tc = JSON.parse(typeConversionsText || '{}')
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
const opts = modMeta?.fields?.[fieldKey]?.source_unit_options || []
|
||||
const canonical = opts.find((o) => o.is_canonical)?.id || opts[0]?.id
|
||||
const su = tc[fieldKey]?.source_unit
|
||||
if (su && opts.some((o) => o.id === su)) return su
|
||||
return canonical || ''
|
||||
}
|
||||
|
||||
const updateSourceUnit = (fieldKey, sourceUnitId) => {
|
||||
let tc
|
||||
try {
|
||||
tc = JSON.parse(typeConversionsText || '{}')
|
||||
} catch {
|
||||
setError('type_conversions: ungültiges JSON (Quelleinheit kann nicht gesetzt werden).')
|
||||
return
|
||||
}
|
||||
const opts = modMeta?.fields?.[fieldKey]?.source_unit_options || []
|
||||
const canonical = opts.find((o) => o.is_canonical)?.id
|
||||
const base =
|
||||
tc[fieldKey] && typeof tc[fieldKey] === 'object'
|
||||
? { ...tc[fieldKey] }
|
||||
: { type: 'float', decimal_separator: 'auto', flexible: true }
|
||||
if (sourceUnitId === canonical || sourceUnitId === '' || sourceUnitId == null) {
|
||||
delete base.source_unit
|
||||
} else {
|
||||
base.source_unit = sourceUnitId
|
||||
}
|
||||
tc[fieldKey] = base
|
||||
setTypeConversionsText(JSON.stringify(tc, null, 2))
|
||||
setError(null)
|
||||
}
|
||||
|
||||
const handleAnalyze = async () => {
|
||||
if (!file) {
|
||||
setError('Bitte eine CSV-Datei wählen.')
|
||||
@@ -530,10 +581,48 @@ export default function AdminCsvTemplateEditorPage() {
|
||||
)}
|
||||
</div>
|
||||
|
||||
{unitTargets.length > 0 && (
|
||||
<div className="card" style={{ padding: 16, marginBottom: 16 }}>
|
||||
<div className="form-label">3b. Quelleinheit (optional)</div>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', marginTop: 8, lineHeight: 1.55 }}>
|
||||
Ziel-Einheit kommt aus dem Datenmodell (z. B. kcal, g, kg, km). Hier nur angeben, falls die CSV
|
||||
abweicht (z. B. FDDB kJ → kcal, Makros in kg → g, Gewicht in lb → kg).
|
||||
</p>
|
||||
<div style={{ marginTop: 14, display: 'flex', flexDirection: 'column', gap: 12 }}>
|
||||
{unitTargets.map(({ field: fkey, options }) => (
|
||||
<label key={fkey} style={{ display: 'block' }}>
|
||||
<span className="form-label" style={{ display: 'block', marginBottom: 6 }}>
|
||||
{fkey}
|
||||
</span>
|
||||
<select
|
||||
className="form-input"
|
||||
value={getSourceUnitSelectValue(fkey) || ''}
|
||||
onChange={(e) => updateSourceUnit(fkey, e.target.value)}
|
||||
style={{
|
||||
width: '100%',
|
||||
minHeight: 46,
|
||||
textAlign: 'left',
|
||||
padding: '11px 14px',
|
||||
fontSize: 15,
|
||||
}}
|
||||
>
|
||||
{options.map((o) => (
|
||||
<option key={o.id} value={o.id}>
|
||||
{o.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="card" style={{ padding: 16, marginBottom: 16 }}>
|
||||
<div className="form-label">4. type_conversions (JSON)</div>
|
||||
<p style={{ fontSize: 13, color: 'var(--text2)', marginTop: 8 }}>
|
||||
Vom Vorschlag übernommen; bei Bedarf manuell anpassen (z. B. kJ-Faktor, Datumsformat).
|
||||
Vom Vorschlag übernommen; Quelleinheiten setzen zusätzlich <code>source_unit</code> (siehe 3b).
|
||||
Manuell z. B. Datumsformat oder legacy <code>conversion_factor</code>.
|
||||
</p>
|
||||
<textarea
|
||||
className="form-input"
|
||||
|
||||
Reference in new issue
Block a user