57 lines
2.1 KiB
Python
57 lines
2.1 KiB
Python
"""
|
|
Status matching helpers.
|
|
|
|
Kept separate from companies.py because this is about order lifecycle
|
|
state rather than company identity - a different axis of classification
|
|
that grows independently.
|
|
|
|
The model is deliberately an allowlist, not a denylist: ACTIVE_STATUSES
|
|
is the small set of statuses that represent real work still to do
|
|
(just "Created" by default). Anything NOT in ACTIVE_STATUSES or
|
|
CANCELLED_STATUSES is treated as done, automatically - including
|
|
statuses this app has never been told about by name (like a JIRA
|
|
automation status such as "1st Contact Attempt" that fires after a
|
|
ticket is already handled). That's on purpose: enumerating every
|
|
possible "this means it's done" status would be a losing game as more
|
|
JIRA-side automation gets added; only naming the couple of statuses
|
|
that mean "not done yet" is far more robust.
|
|
|
|
Matching is case-insensitive since JIRA and ShipStation don't
|
|
necessarily agree on casing (e.g. "Cancelled" vs "cancelled").
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from app import config
|
|
|
|
|
|
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 get_active_statuses() -> set[str]:
|
|
"""Statuses that mean real, unfinished work - default just 'Created'."""
|
|
return parse_status_list(config.get("ACTIVE_STATUSES", config.DEFAULT_ACTIVE_STATUSES))
|
|
|
|
|
|
def get_cancelled_statuses() -> set[str]:
|
|
return parse_status_list(config.get("CANCELLED_STATUSES", config.DEFAULT_CANCELLED_STATUSES))
|
|
|
|
|
|
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)
|
|
]
|