Always log ticket linking attempts, not just when tickets found

Previous debug code only logged when tickets were found, making it
impossible to verify that the function was being called at all.

Changes:
- Move logging outside the if rows: block
- Always create audit log entry for every run import
- Log "No open tickets found" when rows is empty
- Use commit() instead of flush() to ensure persistence
- Add exception logging to catch any errors in debug code
- New event_type "ticket_link_error" for debug code failures

Now every email import will create a ticket_link_debug entry showing:
- Whether the function was called
- How many tickets were found (0 or more)
- Details of each ticket if found

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Ivo Oskamp 2026-02-10 10:10:23 +01:00
parent aea6a866c9
commit c1aeee2a8c
2 changed files with 26 additions and 12 deletions

View File

@ -193,22 +193,36 @@ def link_open_internal_tickets_to_run(*, run: JobRun, job: Job) -> None:
except Exception:
rows = []
# Debug logging to audit log (visible in UI)
if rows:
try:
from .models import AuditLog
details = []
# Debug logging to audit log (visible in UI) - ALWAYS log, even if no tickets
try:
from .models import AuditLog
details = []
if rows:
for tid, code, t_resolved, ts_resolved in rows:
details.append(f"ticket_id={tid}, code={code}, t.resolved_at={t_resolved}, ts.resolved_at={ts_resolved}")
else:
details.append("No open tickets found for this job")
audit = AuditLog(
audit = AuditLog(
user="system",
event_type="ticket_link_debug",
message=f"link_open_internal_tickets_to_run called: run_id={run.id}, job_id={job.id}, found={len(rows)} ticket(s)",
details="\n".join(details)
)
db.session.add(audit)
# Use commit instead of flush to ensure it's persisted
db.session.commit()
except Exception as e:
# Log the exception so we know if something goes wrong
try:
audit_err = AuditLog(
user="system",
event_type="ticket_link_debug",
message=f"Linking {len(rows)} ticket(s) to run_id={run.id} (job_id={job.id})",
details="\n".join(details)
event_type="ticket_link_error",
message=f"Error in ticket link debug logging",
details=str(e)
)
db.session.add(audit)
db.session.flush()
db.session.add(audit_err)
db.session.commit()
except Exception:
pass

View File

@ -9,7 +9,7 @@ This file documents all changes made to this project via Claude Code.
- 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 (writes to AuditLog table with event_type "ticket_link_debug", visible on Logging page, shows ticket_id, ticket_code, resolved_at values for both ticket and scope)
- Added debug logging to ticket linking function to troubleshoot resolved ticket propagation issues (writes to AuditLog table with event_type "ticket_link_debug", visible on Logging page, logs EVERY run import to show whether tickets were found and their resolved_at status, uses commit instead of flush to ensure persistence)
## [2026-02-09]