""" Read-only queries against the local order cache. Separate from app/workers.py (which owns saving orders and dashboard stats) because this module is about a service asking "what do I already know about" before it fetches - a different concern that other sources (ShipStation, Odoo) will likely need their own version of too. """ from __future__ import annotations from typing import List from sqlalchemy import select from app.database import get_session from app.models import Order from app.status_rules import status_in def get_open_ticket_numbers(source: str, terminal_statuses: set[str]) -> List[str]: """ Ticket numbers for a source that haven't reached a terminal status yet - i.e. still worth re-fetching to catch status changes (like a cancellation) even if the ticket wasn't created today. """ session = get_session() try: rows = session.execute( select(Order.ticket_number, Order.status).where( Order.source == source, Order.ticket_number.is_not(None), ) ).all() finally: session.close() return [ ticket_number for ticket_number, status in rows if ticket_number and not status_in(status, terminal_statuses) ]