Files
Order-Manager/app/status_rules.py
T

36 lines
1.2 KiB
Python

"""
Status matching helpers.
Kept separate from companies.py because this is about order lifecycle
state (cancelled, terminal-for-recheck-purposes, and later probably
"fulfilled"/"shipped"/etc.) rather than company identity - a different
axis of classification that will grow independently.
Matching is case-insensitive since JIRA and ShipStation don't
necessarily agree on casing (e.g. "Cancelled" vs "cancelled").
"""
from __future__ import annotations
def parse_status_list(raw: str) -> set[str]:
return {s.strip().lower() for s in (raw or "").split(",") if s.strip()}
def status_in(status: str | None, status_set: set[str]) -> bool:
return (status or "").strip().lower() in status_set
def find_new_cancellations(status_changes: list[dict], cancelled_statuses: set[str]) -> list[dict]:
"""
From a batch of status changes, return only the ones that just BECAME
cancelled this import (old status wasn't already cancelled, new one
is) - so we notify once, at the moment it happens, not every import
thereafter.
"""
return [
change
for change in status_changes
if status_in(change["new_status"], cancelled_statuses)
and not status_in(change["old_status"], cancelled_statuses)
]