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 <noreply@anthropic.com>
This commit is contained in:
Ivo Oskamp 2026-02-10 09:55:58 +01:00
parent 4f208aedd0
commit 88b267b8bd
2 changed files with 5 additions and 7 deletions

View File

@ -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 ""

View File

@ -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]