From 88b267b8bdb8346a62d01757af5116c48d5cff14 Mon Sep 17 00:00:00 2001 From: Ivo Oskamp Date: Tue, 10 Feb 2026 09:55:58 +0100 Subject: [PATCH] Remove date-based logic from ticket propagation The ticket linking query had date-based logic that considered tickets "open" for runs if: - The ticket was unresolved, OR - The resolved date >= run date This caused resolved tickets to still link to new runs, which was unexpected behavior. User confirmed tickets should ONLY link to new runs if they are genuinely unresolved, regardless of dates. Changes: - Simplified query to only find tickets where resolved_at IS NULL - Removed OR clause with date comparison - Removed ui_tz parameter (no longer needed) - Simplified Strategy 1 code (no extra resolved check needed) Now tickets cleanly stop linking to new runs as soon as they are resolved, for both internal and Autotask tickets. Co-Authored-By: Claude Sonnet 4.5 --- .../backupchecks/src/backend/app/ticketing_utils.py | 11 ++++------- docs/changelog-claude.md | 1 + 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/containers/backupchecks/src/backend/app/ticketing_utils.py b/containers/backupchecks/src/backend/app/ticketing_utils.py index d8411a9..4156766 100644 --- a/containers/backupchecks/src/backend/app/ticketing_utils.py +++ b/containers/backupchecks/src/backend/app/ticketing_utils.py @@ -170,8 +170,7 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None: ui_tz = _get_ui_timezone_name() run_date = _to_ui_date(getattr(run, "run_at", None)) or _to_ui_date(datetime.utcnow()) - # Find open tickets scoped to this job for the run date window. - # This matches the logic used by Job Details and Run Checks indicators. + # Find open (unresolved) tickets scoped to this job. rows = [] try: rows = ( @@ -183,14 +182,11 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None: JOIN ticket_scopes ts ON ts.ticket_id = t.id WHERE ts.job_id = :job_id AND t.active_from_date <= :run_date - AND ( - COALESCE(ts.resolved_at, t.resolved_at) IS NULL - OR ((COALESCE(ts.resolved_at, t.resolved_at) AT TIME ZONE 'UTC' AT TIME ZONE :ui_tz)::date) >= :run_date - ) + AND COALESCE(ts.resolved_at, t.resolved_at) IS NULL ORDER BY t.start_date DESC, t.id DESC """ ), - {"job_id": int(job.id), "run_date": run_date, "ui_tz": ui_tz}, + {"job_id": int(job.id), "run_date": run_date}, ) .fetchall() ) @@ -214,6 +210,7 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None: pass # Strategy 1: Use internal ticket code to find matching Autotask-linked run + # The query above only returns unresolved tickets, so we can safely propagate. try: # Use the newest ticket code to find a matching prior Autotask-linked run. newest_code = (rows[0][1] or "").strip() if rows else "" diff --git a/docs/changelog-claude.md b/docs/changelog-claude.md index 338da91..cbf1430 100644 --- a/docs/changelog-claude.md +++ b/docs/changelog-claude.md @@ -6,6 +6,7 @@ This file documents all changes made to this project via Claude Code. ### Fixed - Fixed Autotask ticket not being automatically linked to new runs when internal ticket is resolved by implementing independent Autotask propagation strategy (now checks for most recent non-deleted and non-resolved Autotask ticket on job regardless of internal ticket status, ensuring PSA ticket reference persists across runs until explicitly resolved or deleted) +- Fixed internal and Autotask tickets being linked to new runs even after being resolved by removing date-based "open" logic from ticket query (tickets now only link to new runs if they are genuinely unresolved, not based on run date comparisons) ## [2026-02-09]