06/22 Update code fix inspection notify 2

This commit is contained in:
Nguyen Ngo
2026-06-22 13:23:38 -04:00
parent 56f74c5599
commit c405c91a8d
+3 -29
View File
@@ -401,18 +401,7 @@ def create_inspection():
f'#{inspection.id} via mobile API)', f'#{inspection.id} via mobile API)',
) )
# ── Commit inspection + any staged rows ───────────────────────────────
# Commit BEFORE firing notifications so the inspection record is always
# persisted even if notify_by_matrix raises (e.g. a DB hiccup querying
# the notification matrix, a missing table from a pending migration, or
# any other transient error). Without this ordering, a notification
# failure would 500 the entire request, roll back the transaction, and
# leave the inspection unsaved — causing the iPad to retry indefinitely.
db.session.commit()
# ── Notifications ───────────────────────────────────────────────────── # ── Notifications ─────────────────────────────────────────────────────
# Best-effort: log and continue on any failure. The inspection is already
# committed above, so notification errors never prevent saving.
if status == 'completed': if status == 'completed':
score_display = f'{overall_score:.1f}%' if overall_score is not None else 'N/A' score_display = f'{overall_score:.1f}%' if overall_score is not None else 'N/A'
try: try:
@@ -422,27 +411,20 @@ def create_inspection():
except RuntimeError: except RuntimeError:
inspection_link = f'/inspections/{inspection.id}' inspection_link = f'/inspections/{inspection.id}'
try:
notify_by_matrix( notify_by_matrix(
event_type = 'inspection_completed', event_type = 'inspection_completed',
title = f'Inspection #{inspection.id} Completed (Mobile)', title = f'Inspection #{inspection.id} Completed (Mobile)',
body = ( body = (
f'{user.username} completed an inspection at ' f'{user.display_name} completed an inspection at '
f'{facility.name} using the "{template.name}" template. ' f'{facility.name} using the "{template.name}" template. '
f'Overall score: {score_display}.' f'Overall score: {score_display}.'
), ),
link = inspection_link, link = inspection_link,
inspection_id = inspection.id, inspection_id = inspection.id,
facility_id = facility_id, facility_id = facility_id,
exclude_user_ids = {user.id},
) )
db.session.commit() # persist notification rows added by notify()
except Exception as exc: db.session.commit()
logger.error(
'API INSPECTIONS | notification error | inspection_id=%d | error=%s',
inspection.id, exc,
)
db.session.rollback() # discard any partial notification rows
# ── Post-commit audit logging ────────────────────────────────────────── # ── Post-commit audit logging ──────────────────────────────────────────
# All log_action() calls must come AFTER db.session.commit() because # All log_action() calls must come AFTER db.session.commit() because
@@ -533,7 +515,6 @@ def update_inspection(inspection_id):
db.session.commit() db.session.commit()
# Notify when a draft transitions to completed — mirrors the POST handler. # 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 = ( transitioning_to_complete = (
data.get('status') == 'completed' and prev_status != 'completed' data.get('status') == 'completed' and prev_status != 'completed'
) )
@@ -548,7 +529,6 @@ def update_inspection(inspection_id):
inspection_id=inspection.id, _external=False) inspection_id=inspection.id, _external=False)
except RuntimeError: except RuntimeError:
inspection_link = f'/inspections/{inspection.id}' inspection_link = f'/inspections/{inspection.id}'
try:
notify_by_matrix( notify_by_matrix(
event_type = 'inspection_completed', event_type = 'inspection_completed',
title = f'Inspection #{inspection.id} Completed (Mobile)', title = f'Inspection #{inspection.id} Completed (Mobile)',
@@ -562,12 +542,6 @@ def update_inspection(inspection_id):
facility_id = inspection.facility_id, facility_id = inspection.facility_id,
) )
db.session.commit() # persist notification rows added by notify() 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}',