05/05 Update API for iPad inspection follow-up
This commit is contained in:
+37
-25
@@ -51,20 +51,24 @@ def _parse_datetime(value):
|
|||||||
def _inspection_payload(inspection):
|
def _inspection_payload(inspection):
|
||||||
"""Serialize an Inspection to the dict returned in API responses."""
|
"""Serialize an Inspection to the dict returned in API responses."""
|
||||||
return {
|
return {
|
||||||
'id': inspection.id,
|
'id': inspection.id,
|
||||||
'template_id': inspection.template_id,
|
'template_id': inspection.template_id,
|
||||||
'template_name': inspection.template.name if inspection.template else '',
|
'template_name': inspection.template.name if inspection.template else '',
|
||||||
'facility_id': inspection.facility_id,
|
'facility_id': inspection.facility_id,
|
||||||
'facility_name': inspection.facility.name if inspection.facility else '',
|
'facility_name': inspection.facility.name if inspection.facility else '',
|
||||||
'area_id': inspection.area_id,
|
'area_id': inspection.area_id,
|
||||||
'area_name': inspection.area.name if inspection.area else None,
|
'area_name': inspection.area.name if inspection.area else None,
|
||||||
'status': inspection.status,
|
'status': inspection.status,
|
||||||
'overall_score': inspection.overall_score,
|
'overall_score': inspection.overall_score,
|
||||||
'inspection_date': inspection.inspection_date.isoformat()
|
'inspection_date': inspection.inspection_date.isoformat()
|
||||||
if inspection.inspection_date else None,
|
if inspection.inspection_date else None,
|
||||||
'completed_at': inspection.completed_at.isoformat()
|
'completed_at': inspection.completed_at.isoformat()
|
||||||
if inspection.completed_at else None,
|
if inspection.completed_at else None,
|
||||||
'mobile_local_id': inspection.mobile_local_id,
|
'mobile_local_id': inspection.mobile_local_id,
|
||||||
|
# ── Follow-up / re-inspection fields ──────────────────────────────
|
||||||
|
'follow_up_required': inspection.follow_up_required,
|
||||||
|
'follow_up_note': inspection.follow_up_note,
|
||||||
|
'parent_inspection_id': inspection.parent_inspection_id,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -214,6 +218,13 @@ def create_inspection():
|
|||||||
if status not in ('in_progress', 'completed'):
|
if status not in ('in_progress', 'completed'):
|
||||||
return api_error('status must be "in_progress" or "completed"', 400)
|
return api_error('status must be "in_progress" or "completed"', 400)
|
||||||
|
|
||||||
|
# ── Optional parent link (re-inspection) ──────────────────────────────
|
||||||
|
parent_inspection_id = data.get('parent_inspection_id')
|
||||||
|
if parent_inspection_id:
|
||||||
|
parent = db.session.get(Inspection, parent_inspection_id)
|
||||||
|
if parent is None:
|
||||||
|
return api_error('Parent inspection not found', 404)
|
||||||
|
|
||||||
# ── Score calculation ─────────────────────────────────────────────────
|
# ── Score calculation ─────────────────────────────────────────────────
|
||||||
overall_score = data.get('overall_score')
|
overall_score = data.get('overall_score')
|
||||||
if overall_score is None and status == 'completed':
|
if overall_score is None and status == 'completed':
|
||||||
@@ -232,16 +243,17 @@ def create_inspection():
|
|||||||
notes_payload['_form_data'] = form_data
|
notes_payload['_form_data'] = form_data
|
||||||
|
|
||||||
inspection = Inspection(
|
inspection = Inspection(
|
||||||
template_id = template_id,
|
template_id = template_id,
|
||||||
facility_id = facility_id,
|
facility_id = facility_id,
|
||||||
area_id = area_id,
|
area_id = area_id,
|
||||||
inspector_id = user.id,
|
inspector_id = user.id,
|
||||||
inspection_date = inspection_date,
|
inspection_date = inspection_date,
|
||||||
overall_score = overall_score,
|
overall_score = overall_score,
|
||||||
status = status,
|
status = status,
|
||||||
notes = json.dumps(notes_payload),
|
notes = json.dumps(notes_payload),
|
||||||
completed_at = completed_at if status == 'completed' else None,
|
completed_at = completed_at if status == 'completed' else None,
|
||||||
mobile_local_id = mobile_local_id,
|
mobile_local_id = mobile_local_id,
|
||||||
|
parent_inspection_id = parent_inspection_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
db.session.add(inspection)
|
db.session.add(inspection)
|
||||||
@@ -276,7 +288,7 @@ def create_inspection():
|
|||||||
log_action(ACTION_CREATE, 'Inspection', inspection.id,
|
log_action(ACTION_CREATE, 'Inspection', inspection.id,
|
||||||
f'{template.name} @ {facility.name}',
|
f'{template.name} @ {facility.name}',
|
||||||
f'source=mobile; status={status}; score={overall_score}; '
|
f'source=mobile; status={status}; score={overall_score}; '
|
||||||
f'local_id={mobile_local_id}')
|
f'local_id={mobile_local_id}; parent_id={parent_inspection_id}')
|
||||||
|
|
||||||
logger.info('API INSPECTIONS | created | inspection_id=%d | facility=%s | '
|
logger.info('API INSPECTIONS | created | inspection_id=%d | facility=%s | '
|
||||||
'template=%s | status=%s | score=%s | user=%s',
|
'template=%s | status=%s | score=%s | user=%s',
|
||||||
|
|||||||
Reference in New Issue
Block a user