Aug 5 - Update code to follow up ST - Fix pending items

This commit is contained in:
2026-08-05 12:40:31 -04:00
parent 9bd6b364b7
commit 97c1dec54d
4 changed files with 474 additions and 11 deletions
+49
View File
@@ -644,6 +644,22 @@ def update_inspection(inspection_id):
_sched = _resolve_schedule(_sched_id, user)
if _sched is not None:
inspection.inspection_schedule_id = _sched.id
# phase48 parity with the POST path: a schedule created by
# "Schedule Follow-up" knows which inspection it answers, so a draft
# that only gets its schedule attached here still becomes a properly
# linked re-inspection. Never overrides an explicit parent.
if not inspection.parent_inspection_id and _sched.parent_inspection_id:
inspection.parent_inspection_id = _sched.parent_inspection_id
logger.info('API INSPECTIONS | parent inherited from schedule on '
'PATCH | schedule=%s | parent=%s | user=%s',
_sched.id, _sched.parent_inspection_id, user.username)
# An explicitly supplied parent still wins, and can be set on the draft
# before submit — mirrors the POST handler's field list.
if 'parent_inspection_id' in data:
_pid = data.get('parent_inspection_id')
if isinstance(_pid, int) and db.session.get(Inspection, _pid) is not None:
inspection.parent_inspection_id = _pid
if 'status' in data:
inspection.status = data['status']
@@ -670,8 +686,41 @@ def update_inspection(inspection_id):
if transitioning_to_complete:
_fulfill_schedule(inspection)
# ── Auto-clear follow-up flag on parent ───────────────────────────────
# Mirrors the POST handler. This was previously MISSING here, so an iPad
# that created a follow-up as a draft and submitted it via PATCH left the
# parent flagged forever — the re-inspection happened, but the parent still
# showed "Follow-up Inspection Required" and stayed in every manager's
# outstanding list. phase48 made that a normal path, since a schedule-started
# follow-up is a draft first.
#
# Same commit-ordering rule as the POST handler: log_action() must fire AFTER
# db.session.commit(), because audit.py commits internally and would
# otherwise persist the parent's flag change before this inspection's own
# changes are committed — a partial state if the main commit then failed.
_parent_log_args = None
if transitioning_to_complete and inspection.parent_inspection_id:
parent_insp = db.session.get(Inspection, inspection.parent_inspection_id)
if parent_insp and parent_insp.follow_up_required:
parent_insp.follow_up_required = False
logger.info(
'API INSPECTIONS | follow_up cleared on PATCH | parent_id=%s | '
'by_inspection_id=%s | user=%s',
parent_insp.id, inspection.id, user.username,
)
# Snapshot label strings now — ORM objects may be expired after commit.
_parent_log_args = (
parent_insp.id,
f'{parent_insp.template.name} @ {parent_insp.facility.name}',
f'follow_up_required=False (cleared by re-inspection '
f'#{inspection.id} via mobile API)',
)
db.session.commit()
if _parent_log_args:
log_action(ACTION_UPDATE, 'Inspection', *_parent_log_args)
# Notify when a draft transitions to completed — mirrors the POST handler.
if transitioning_to_complete:
score_val = inspection.overall_score