Auto-commit local changes before build (2026-01-06 20:04:54) #57

Merged
ivooskamp merged 1 commits from v20260106-22-ticket-link-multiple-jobs into main 2026-01-13 11:22:29 +01:00
3 changed files with 73 additions and 34 deletions

View File

@ -1 +1 @@
backupchecks-v20260106-21-changelog-0.1.17 v20260106-22-ticket-link-multiple-jobs

View File

@ -204,10 +204,44 @@ def api_tickets():
if not re.match(r"^T\d{8}\.\d{4}$", ticket_code): if not re.match(r"^T\d{8}\.\d{4}$", ticket_code):
return jsonify({"status": "error", "message": "Invalid ticket_code format. Expected TYYYYMMDD.####."}), 400 return jsonify({"status": "error", "message": "Invalid ticket_code format. Expected TYYYYMMDD.####."}), 400
# Ensure uniqueness existing = Ticket.query.filter_by(ticket_code=ticket_code).first()
if Ticket.query.filter_by(ticket_code=ticket_code).first():
return jsonify({"status": "error", "message": "ticket_code already exists."}), 409
# If the ticket already exists, link it to this job/run (a single ticket number can apply to multiple jobs).
if existing:
ticket = existing
try:
# Ensure there is at least one job scope for this ticket/job combination.
if job and job.id:
scope = TicketScope.query.filter_by(
ticket_id=ticket.id,
scope_type="job",
job_id=job.id,
).first()
if not scope:
scope = TicketScope(
ticket_id=ticket.id,
scope_type="job",
customer_id=job.customer_id,
backup_software=job.backup_software,
backup_type=job.backup_type,
job_id=job.id,
job_name_match=job.job_name,
job_name_match_mode="exact",
)
db.session.add(scope)
# Link the ticket to this specific run (idempotent due to unique constraint).
existing_link = TicketJobRun.query.filter_by(ticket_id=ticket.id, job_run_id=run.id).first()
if not existing_link:
link = TicketJobRun(ticket_id=ticket.id, job_run_id=run.id, link_source="manual")
db.session.add(link)
db.session.commit()
except Exception as exc:
db.session.rollback()
return jsonify({"status": "error", "message": str(exc) or "Failed to link ticket."}), 500
else:
ticket = Ticket( ticket = Ticket(
ticket_code=ticket_code, ticket_code=ticket_code,
title=None, title=None,
@ -251,8 +285,8 @@ def api_tickets():
"description": ticket.description or "", "description": ticket.description or "",
"start_date": _format_datetime(ticket.start_date), "start_date": _format_datetime(ticket.start_date),
"active_from_date": str(ticket.active_from_date) if getattr(ticket, "active_from_date", None) else "", "active_from_date": str(ticket.active_from_date) if getattr(ticket, "active_from_date", None) else "",
"resolved_at": "", "resolved_at": _format_datetime(ticket.resolved_at) if getattr(ticket, "resolved_at", None) else "",
"active": True, "active": getattr(ticket, "resolved_at", None) is None,
}, },
} }
) )

View File

@ -1,3 +1,8 @@
## v20260106-22-ticket-link-multiple-jobs
- Fixed ticket linking logic to allow the same existing ticket number to be associated with multiple jobs and job runs.
- Prevented duplicate ticket creation errors when reusing an existing ticket_code.
- Ensured tickets are reused and linked instead of rejected when already present in the system.
================================================================================================================================================ ================================================================================================================================================