Add debug logging for ticket linking investigation

User reports that resolved internal tickets are still being linked to
new runs, even though Autotask tickets correctly stop linking. Added
debug logging to understand what the query is finding.

Changes:
- Query now returns resolved_at values for both ticket and scope
- Added logger.info statements showing found tickets and their status
- This will help diagnose whether tickets are truly resolved in DB

Temporary debug code to be removed after issue is identified.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Ivo Oskamp 2026-02-10 10:00:35 +01:00
parent 88b267b8bd
commit c228d6db19
2 changed files with 16 additions and 2 deletions

View File

@ -177,7 +177,7 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None:
db.session.execute( db.session.execute(
text( text(
""" """
SELECT t.id, t.ticket_code SELECT t.id, t.ticket_code, t.resolved_at, ts.resolved_at as scope_resolved_at
FROM tickets t FROM tickets t
JOIN ticket_scopes ts ON ts.ticket_id = t.id JOIN ticket_scopes ts ON ts.ticket_id = t.id
WHERE ts.job_id = :job_id WHERE ts.job_id = :job_id
@ -193,11 +193,21 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None:
except Exception: except Exception:
rows = [] rows = []
# Debug logging
if rows:
try:
from flask import current_app
current_app.logger.info(f"[TICKET_LINK_DEBUG] Found {len(rows)} open tickets for job_id={job.id}, run_id={run.id}")
for tid, code, t_resolved, ts_resolved in rows:
current_app.logger.info(f" - ticket_id={tid}, code={code}, t.resolved_at={t_resolved}, ts.resolved_at={ts_resolved}")
except Exception:
pass
if not rows: if not rows:
return return
# Link all open tickets to this run (idempotent) # Link all open tickets to this run (idempotent)
for tid, _code in rows: for tid, code, t_resolved, ts_resolved in rows:
if not TicketJobRun.query.filter_by(ticket_id=int(tid), job_run_id=int(run.id)).first(): if not TicketJobRun.query.filter_by(ticket_id=int(tid), job_run_id=int(run.id)).first():
db.session.add(TicketJobRun(ticket_id=int(tid), job_run_id=int(run.id), link_source="inherit")) db.session.add(TicketJobRun(ticket_id=int(tid), job_run_id=int(run.id), link_source="inherit"))
@ -213,6 +223,7 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None:
# The query above only returns unresolved tickets, so we can safely propagate. # The query above only returns unresolved tickets, so we can safely propagate.
try: try:
# Use the newest ticket code to find a matching prior Autotask-linked run. # Use the newest ticket code to find a matching prior Autotask-linked run.
# rows format: (tid, code, t_resolved, ts_resolved)
newest_code = (rows[0][1] or "").strip() if rows else "" newest_code = (rows[0][1] or "").strip() if rows else ""
if newest_code: if newest_code:
prior = ( prior = (

View File

@ -8,6 +8,9 @@ This file documents all changes made to this project via Claude Code.
- 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 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) - 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)
### Changed
- Added debug logging to ticket linking function to troubleshoot resolved ticket propagation issues (logs ticket_id, ticket_code, resolved_at values for both ticket and scope)
## [2026-02-09] ## [2026-02-09]
### Added ### Added