Добавлен поллер накладной
This commit is contained in:
@@ -29,6 +29,19 @@ class CDEKOrderRegistrationResult:
|
||||
waybill_url: str | None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CDEKOrderInfo:
|
||||
order_uuid: str
|
||||
status_code: str | None
|
||||
waybill_uuid: str | None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CDEKWaybillInfo:
|
||||
waybill_uuid: str
|
||||
url: str | None
|
||||
|
||||
|
||||
def map_cdek_order_request(request: InitPaymentRequest) -> dict[str, Any]:
|
||||
payload: dict[str, Any] = {
|
||||
"number": request.order_uuid,
|
||||
@@ -89,6 +102,65 @@ def map_cdek_existing_order_response(
|
||||
)
|
||||
|
||||
|
||||
def map_cdek_order_info_response(payload: dict[str, Any]) -> CDEKOrderInfo:
|
||||
entity = payload.get("entity")
|
||||
if not isinstance(entity, dict):
|
||||
raise CDEKOrderMappingError(
|
||||
"CDEK order info response must include entity object."
|
||||
)
|
||||
|
||||
order_uuid = entity.get("uuid")
|
||||
if not isinstance(order_uuid, str) or not order_uuid:
|
||||
raise CDEKOrderMappingError(
|
||||
"CDEK order info response must include entity.uuid."
|
||||
)
|
||||
|
||||
status_code = _latest_status_code(entity.get("statuses"))
|
||||
waybill_uuid, _ = _extract_waybill(payload)
|
||||
return CDEKOrderInfo(
|
||||
order_uuid=order_uuid,
|
||||
status_code=status_code,
|
||||
waybill_uuid=waybill_uuid,
|
||||
)
|
||||
|
||||
|
||||
def map_cdek_waybill_info_response(payload: dict[str, Any]) -> CDEKWaybillInfo:
|
||||
entity = payload.get("entity")
|
||||
if not isinstance(entity, dict):
|
||||
raise CDEKOrderMappingError(
|
||||
"CDEK waybill info response must include entity object."
|
||||
)
|
||||
|
||||
waybill_uuid = entity.get("uuid")
|
||||
if not isinstance(waybill_uuid, str) or not waybill_uuid:
|
||||
raise CDEKOrderMappingError(
|
||||
"CDEK waybill info response must include entity.uuid."
|
||||
)
|
||||
|
||||
url_value = entity.get("url")
|
||||
waybill_url = url_value if isinstance(url_value, str) and url_value else None
|
||||
return CDEKWaybillInfo(waybill_uuid=waybill_uuid, url=waybill_url)
|
||||
|
||||
|
||||
def _latest_status_code(statuses: object) -> str | None:
|
||||
if not isinstance(statuses, list) or not statuses:
|
||||
return None
|
||||
dated: list[tuple[str, str]] = []
|
||||
for entry in statuses:
|
||||
if not isinstance(entry, dict):
|
||||
continue
|
||||
code = entry.get("code")
|
||||
if not isinstance(code, str) or not code:
|
||||
continue
|
||||
date_time = entry.get("date_time")
|
||||
date_time_key = date_time if isinstance(date_time, str) else ""
|
||||
dated.append((date_time_key, code))
|
||||
if not dated:
|
||||
return None
|
||||
dated.sort(key=lambda item: item[0])
|
||||
return dated[-1][1]
|
||||
|
||||
|
||||
def _extract_waybill(payload: dict[str, Any]) -> tuple[str | None, str | None]:
|
||||
related_entities = payload.get("related_entities")
|
||||
if not isinstance(related_entities, list):
|
||||
|
||||
Reference in New Issue
Block a user