Files
Order-Manager/app/companies.py
T
2026-08-26 09:15:35 -05:00

60 lines
1.9 KiB
Python

"""
Company resolution.
Two ways an order tells us which company it belongs to:
- JIRA orders: the SKU prefix (SH... -> Signify Health, OK... -> Oak Street Health)
- ShipStation orders: which store the order lives in (each company has
its own store because they have separate UPS accounts)
Both mappings are configurable in Settings so adding a third company
later doesn't require touching code - just add another "PREFIX:Company
Name" or "storeId:Company Name" entry.
"""
from __future__ import annotations
UNKNOWN_COMPANY = "Unknown"
def parse_mapping(raw: str) -> dict[str, str]:
"""Parse a 'KEY:Value,KEY2:Value2' settings string into a dict."""
mapping: dict[str, str] = {}
if not raw:
return mapping
for part in raw.split(","):
part = part.strip()
if not part or ":" not in part:
continue
key, _, value = part.partition(":")
key, value = key.strip(), value.strip()
if key and value:
mapping[key] = value
return mapping
def resolve_company_by_sku(sku: str, sku_map: dict[str, str]) -> str:
"""Match a SKU against configured prefixes, e.g. 'SH-1029' -> 'Signify Health'."""
if not sku:
return UNKNOWN_COMPANY
sku_upper = sku.strip().upper()
for prefix, company in sku_map.items():
if sku_upper.startswith(prefix.upper()):
return company
return UNKNOWN_COMPANY
def resolve_company_for_skus(skus: list[str], sku_map: dict[str, str]) -> str:
"""
Resolve the company for a set of SKUs on one ticket. Per business rule,
companies never mix on a single ticket, so the first recognizable SKU
decides it.
"""
for sku in skus:
company = resolve_company_by_sku(sku, sku_map)
if company != UNKNOWN_COMPANY:
return company
return UNKNOWN_COMPANY
def resolve_company_by_store(store_id: str, store_map: dict[str, str]) -> str:
return store_map.get(str(store_id), UNKNOWN_COMPANY)