06/22 Update code fix inspection notify
This commit is contained in:
@@ -513,6 +513,8 @@ def update_inspection(inspection_id):
|
|||||||
existing_notes['_inspector_notes'] = data['notes']
|
existing_notes['_inspector_notes'] = data['notes']
|
||||||
inspection.notes = json.dumps(existing_notes)
|
inspection.notes = json.dumps(existing_notes)
|
||||||
|
|
||||||
|
prev_status = inspection.status
|
||||||
|
|
||||||
if 'status' in data:
|
if 'status' in data:
|
||||||
inspection.status = data['status']
|
inspection.status = data['status']
|
||||||
|
|
||||||
@@ -530,6 +532,43 @@ def update_inspection(inspection_id):
|
|||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
# Notify when a draft transitions to completed — mirrors the POST handler.
|
||||||
|
# Best-effort: log and continue on any failure so the PATCH always succeeds.
|
||||||
|
transitioning_to_complete = (
|
||||||
|
data.get('status') == 'completed' and prev_status != 'completed'
|
||||||
|
)
|
||||||
|
if transitioning_to_complete:
|
||||||
|
score_val = inspection.overall_score
|
||||||
|
score_display = f'{score_val:.1f}%' if score_val is not None else 'N/A'
|
||||||
|
template_name = inspection.template.name if inspection.template else 'Unknown'
|
||||||
|
facility_name = inspection.facility.name if inspection.facility else 'Unknown'
|
||||||
|
try:
|
||||||
|
from flask import url_for
|
||||||
|
inspection_link = url_for('inspections.view',
|
||||||
|
inspection_id=inspection.id, _external=False)
|
||||||
|
except RuntimeError:
|
||||||
|
inspection_link = f'/inspections/{inspection.id}'
|
||||||
|
try:
|
||||||
|
notify_by_matrix(
|
||||||
|
event_type = 'inspection_completed',
|
||||||
|
title = f'Inspection #{inspection.id} Completed (Mobile)',
|
||||||
|
body = (
|
||||||
|
f'{user.display_name} completed an inspection at '
|
||||||
|
f'{facility_name} using the "{template_name}" template. '
|
||||||
|
f'Overall score: {score_display}.'
|
||||||
|
),
|
||||||
|
link = inspection_link,
|
||||||
|
inspection_id = inspection.id,
|
||||||
|
facility_id = inspection.facility_id,
|
||||||
|
)
|
||||||
|
db.session.commit() # persist notification rows added by notify()
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error(
|
||||||
|
'API INSPECTIONS | PATCH notification error | inspection_id=%d | error=%s',
|
||||||
|
inspection.id, exc,
|
||||||
|
)
|
||||||
|
db.session.rollback()
|
||||||
|
|
||||||
log_action(ACTION_UPDATE, 'Inspection', inspection.id,
|
log_action(ACTION_UPDATE, 'Inspection', inspection.id,
|
||||||
f'{inspection.template.name} @ {inspection.facility.name}',
|
f'{inspection.template.name} @ {inspection.facility.name}',
|
||||||
f'source=mobile; fields_updated={list(data.keys())}')
|
f'source=mobile; fields_updated={list(data.keys())}')
|
||||||
|
|||||||
Reference in New Issue
Block a user