From 790aac36df1de001b422fa0a4b24132746e425a0 Mon Sep 17 00:00:00 2001 From: NguyenND Date: Mon, 8 Jun 2026 16:00:14 -0400 Subject: [PATCH] 06/08 Fix inspection photo issues --- app/api/inspections.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/app/api/inspections.py b/app/api/inspections.py index caeee38..fc7146b 100644 --- a/app/api/inspections.py +++ b/app/api/inspections.py @@ -34,6 +34,27 @@ logger = logging.getLogger(__name__) bp = Blueprint('api_inspections', __name__) _ALLOWED_ROLES = {'admin', 'director', 'inspector', 'project_manager'} + + +def _merge_form_data(existing: dict, incoming: dict) -> dict: + """Merge incoming form_data into existing, preserving file paths. + + New non-empty values always win. The one exception: an empty string + coming from the client will NOT overwrite an existing server-side upload + path (any value that starts with 'uploads/'). This protects photo paths + stored during an earlier POST from being silently blanked when the iOS + sends a final PATCH whose form_data was rebuilt without re-including the + already-uploaded paths. + """ + merged = dict(existing) + for k, v in incoming.items(): + existing_v = merged.get(k) + if (not v + and isinstance(existing_v, str) + and existing_v.startswith('uploads/')): + continue # keep the saved photo path + merged[k] = v + return merged _UUID_RE = re.compile( r'^[0-9a-f]{8}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{4}-?[0-9a-f]{12}$', re.IGNORECASE, @@ -424,7 +445,10 @@ def update_inspection(inspection_id): except (json.JSONDecodeError, TypeError): existing_notes = {} if 'form_data' in data: - existing_notes['_form_data'] = data['form_data'] + existing_notes['_form_data'] = _merge_form_data( + existing_notes.get('_form_data') or {}, + data['form_data'] or {}, + ) if 'notes' in data: existing_notes['_inspector_notes'] = data['notes'] inspection.notes = json.dumps(existing_notes)