refactor: filter out stale profile field values by requiring backend-resolved labels

This commit is contained in:
pradeepkumar
2026-04-15 12:42:48 +05:30
parent 56c300f368
commit 691a9a04a9

View File

@@ -291,13 +291,21 @@ export function mapFieldValuesToAvailability(fieldValues: FieldValueResponse[]):
return trimmed; return trimmed;
}; };
// Prefer backend-resolved labels, fall back to raw value with snake_case formatting // Use backend-resolved labels only. Drop stale/orphan values whose option is no longer
const resolvedLabels = rawValues.map((val, i) => { // defined in the profile field (backend returns no label for them) — prevents old
// selections from lingering when admin renames/removes options.
const resolvedLabels: string[] = [];
rawValues.forEach((val, i) => {
const fromBackend = labelValues[i]; const fromBackend = labelValues[i];
if (fromBackend && typeof fromBackend === 'string' && fromBackend.trim()) { if (fromBackend && typeof fromBackend === 'string' && fromBackend.trim()) {
return formatIfSnakeCase(fromBackend); resolvedLabels.push(formatIfSnakeCase(fromBackend));
return;
} }
return formatIfSnakeCase(val); // No backend label → likely a stale option; skip unless it's already a human-readable string
const raw = String(val || '').trim();
if (!raw) return;
if (/^[a-z0-9]+(_[a-z0-9]+)+$/i.test(raw)) return; // skip snake_case orphans
resolvedLabels.push(formatIfSnakeCase(raw));
}); });
// Deduplicate by normalized comparison (case/whitespace insensitive) // Deduplicate by normalized comparison (case/whitespace insensitive)