mirror of
https://github.com/Sea-Haven-Industries/procurement-ingest.git
synced 2026-10-02 17:53:23 +00:00
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
|
|
"""Read access to the purchase-order tables for the procurement API.
|
||
|
|
|
||
|
|
Same unordered cursor-paginated Scan pattern as wo_repo (no GSIs; listing
|
||
|
|
serves reconciliation, not ranked queries). VendorReplies is deliberately
|
||
|
|
absent: the table is dead (its writer was deleted) and must not gain new
|
||
|
|
consumers.
|
||
|
|
"""
|
||
|
|
|
||
|
|
import os
|
||
|
|
|
||
|
|
import boto3
|
||
|
|
|
||
|
|
from pagination import decode_cursor, encode_cursor
|
||
|
|
|
||
|
|
dynamodb = boto3.resource("dynamodb")
|
||
|
|
|
||
|
|
PO_TABLE = os.environ.get("PO_TABLE", "purchase-orders")
|
||
|
|
VERIFIED_SITES_TABLE = os.environ.get("VERIFIED_SITES_TABLE", "verified-sites")
|
||
|
|
|
||
|
|
PO_KEY_ATTRS = frozenset({"po_number"})
|
||
|
|
SITE_KEY_ATTRS = frozenset({"siteCode"})
|
||
|
|
|
||
|
|
|
||
|
|
def _page(response) -> tuple[list, str | None]:
|
||
|
|
items = response.get("Items", [])
|
||
|
|
lek = response.get("LastEvaluatedKey")
|
||
|
|
return items, (encode_cursor(lek) if lek else None)
|
||
|
|
|
||
|
|
|
||
|
|
def list_purchase_orders(limit: int, cursor: str | None) -> tuple[list, str | None]:
|
||
|
|
table = dynamodb.Table(PO_TABLE)
|
||
|
|
kwargs = {"Limit": limit}
|
||
|
|
if cursor:
|
||
|
|
kwargs["ExclusiveStartKey"] = decode_cursor(cursor, PO_KEY_ATTRS)
|
||
|
|
return _page(table.scan(**kwargs))
|
||
|
|
|
||
|
|
|
||
|
|
def get_purchase_order(po_number: str) -> dict | None:
|
||
|
|
table = dynamodb.Table(PO_TABLE)
|
||
|
|
return table.get_item(Key={"po_number": po_number}).get("Item")
|
||
|
|
|
||
|
|
|
||
|
|
def list_verified_sites(limit: int, cursor: str | None) -> tuple[list, str | None]:
|
||
|
|
table = dynamodb.Table(VERIFIED_SITES_TABLE)
|
||
|
|
kwargs = {"Limit": limit}
|
||
|
|
if cursor:
|
||
|
|
kwargs["ExclusiveStartKey"] = decode_cursor(cursor, SITE_KEY_ATTRS)
|
||
|
|
return _page(table.scan(**kwargs))
|
||
|
|
|
||
|
|
|
||
|
|
def get_verified_site(site_code: str) -> dict | None:
|
||
|
|
table = dynamodb.Table(VERIFIED_SITES_TABLE)
|
||
|
|
return table.get_item(Key={"siteCode": site_code}).get("Item")
|