""" 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, active_statuses: set[str]) -> List[str]: """ Ticket numbers for a source that are still in an active status - i.e. still worth re-fetching to catch a status change (a cancellation, a fulfillment, or any other transition) even if the ticket wasn't created today. Once a ticket leaves the active set, we stop re-checking it - whatever it became, it's no longer "not done yet". """ 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 status_in(status, active_statuses) ]