Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

7.1 KB
Raw Blame
from dataclasses import field

from products.warehouse_sources.sources.sdk import IncrementalField, IncrementalFieldType, frozen


def _timestamp_field(name: str) -> IncrementalField:
    return {
        "label": name,
        "type": IncrementalFieldType.DateTime,
        "field": name,
        "field_type": IncrementalFieldType.DateTime,
    }


# Katana mutable resources expose both created_at and updated_at; updated_at is the default cursor
# so incremental syncs also pick up edits to existing rows.
_UPDATED_AND_CREATED: list[IncrementalField] = [_timestamp_field("updated_at"), _timestamp_field("created_at")]
# Append-only logs (inventory movements) are immutable, so created_at is the only stable cursor.
_CREATED_ONLY: list[IncrementalField] = [_timestamp_field("created_at")]


@frozen
class KatanaEndpointConfig:
    name: str
    path: str
    # Katana ids are globally unique per resource, so a single-column key is enough for most tables.
    # Inventory has no id (it's a per-(variant, location) balance), so it uses a composite key.
    primary_keys: list[str] = field(default_factory=lambda: ["id"])
    incremental_fields: list[IncrementalField] = field(default_factory=list)
    # Stable creation-time column to partition on. Never updated_at — partitions must not move.
    partition_key: str | None = "created_at"
    default_incremental_field: str = "updated_at"
    # JSON path to the row list. /operators returns a bare array instead of the {"data": [...]} envelope.
    data_selector: str | None = "data"


KATANA_ENDPOINTS: dict[str, KatanaEndpointConfig] = {
    # --- Catalog ---
    "products": KatanaEndpointConfig(name="products", path="/products", incremental_fields=_UPDATED_AND_CREATED),
    "materials": KatanaEndpointConfig(name="materials", path="/materials", incremental_fields=_UPDATED_AND_CREATED),
    "variants": KatanaEndpointConfig(name="variants", path="/variants", incremental_fields=_UPDATED_AND_CREATED),
    "services": KatanaEndpointConfig(name="services", path="/services", incremental_fields=_UPDATED_AND_CREATED),
    "bom_rows": KatanaEndpointConfig(name="bom_rows", path="/bom_rows", incremental_fields=_UPDATED_AND_CREATED),
    # --- Partners ---
    "customers": KatanaEndpointConfig(name="customers", path="/customers", incremental_fields=_UPDATED_AND_CREATED),
    "suppliers": KatanaEndpointConfig(name="suppliers", path="/suppliers", incremental_fields=_UPDATED_AND_CREATED),
    # --- Orders ---
    "sales_orders": KatanaEndpointConfig(
        name="sales_orders", path="/sales_orders", incremental_fields=_UPDATED_AND_CREATED
    ),
    "purchase_orders": KatanaEndpointConfig(
        name="purchase_orders", path="/purchase_orders", incremental_fields=_UPDATED_AND_CREATED
    ),
    "manufacturing_orders": KatanaEndpointConfig(
        name="manufacturing_orders", path="/manufacturing_orders", incremental_fields=_UPDATED_AND_CREATED
    ),
    "sales_returns": KatanaEndpointConfig(
        name="sales_returns", path="/sales_returns", incremental_fields=_UPDATED_AND_CREATED
    ),
    "sales_order_fulfillments": KatanaEndpointConfig(
        name="sales_order_fulfillments", path="/sales_order_fulfillments", incremental_fields=_UPDATED_AND_CREATED
    ),
    # --- Order line items ---
    "sales_order_rows": KatanaEndpointConfig(
        name="sales_order_rows", path="/sales_order_rows", incremental_fields=_UPDATED_AND_CREATED
    ),
    "purchase_order_rows": KatanaEndpointConfig(
        name="purchase_order_rows", path="/purchase_order_rows", incremental_fields=_UPDATED_AND_CREATED
    ),
    "manufacturing_order_recipe_rows": KatanaEndpointConfig(
        name="manufacturing_order_recipe_rows",
        path="/manufacturing_order_recipe_rows",
        incremental_fields=_UPDATED_AND_CREATED,
    ),
    "manufacturing_order_productions": KatanaEndpointConfig(
        name="manufacturing_order_productions",
        path="/manufacturing_order_productions",
        incremental_fields=_UPDATED_AND_CREATED,
    ),
    "manufacturing_order_operation_rows": KatanaEndpointConfig(
        name="manufacturing_order_operation_rows",
        path="/manufacturing_order_operation_rows",
        incremental_fields=_UPDATED_AND_CREATED,
    ),
    "sales_return_rows": KatanaEndpointConfig(
        name="sales_return_rows", path="/sales_return_rows", incremental_fields=_UPDATED_AND_CREATED
    ),
    # --- Stock movements ---
    "stock_adjustments": KatanaEndpointConfig(
        name="stock_adjustments", path="/stock_adjustments", incremental_fields=_UPDATED_AND_CREATED
    ),
    "stock_transfers": KatanaEndpointConfig(
        name="stock_transfers", path="/stock_transfers", incremental_fields=_UPDATED_AND_CREATED
    ),
    "stocktakes": KatanaEndpointConfig(name="stocktakes", path="/stocktakes", incremental_fields=_UPDATED_AND_CREATED),
    "stocktake_rows": KatanaEndpointConfig(
        name="stocktake_rows", path="/stocktake_rows", incremental_fields=_UPDATED_AND_CREATED
    ),
    "inventory_movements": KatanaEndpointConfig(
        name="inventory_movements",
        path="/inventory_movements",
        incremental_fields=_CREATED_ONLY,
        default_incremental_field="created_at",
    ),
    # --- Full refresh only ---
    # /inventory has no created_at/updated_at filter (it's a live per-(variant, location) balance),
    # so there is no server-side cursor to sync incrementally.
    "inventory": KatanaEndpointConfig(
        name="inventory",
        path="/inventory",
        primary_keys=["variant_id", "location_id"],
        incremental_fields=[],
        partition_key=None,
    ),
    # /price_lists exposes no timestamp filter, and these config tables are small, so full refresh.
    "price_lists": KatanaEndpointConfig(
        name="price_lists", path="/price_lists", incremental_fields=[], partition_key=None
    ),
    # /price_list_rows has no timestamp filter either; it is one row per (price list, variant).
    "price_list_rows": KatanaEndpointConfig(name="price_list_rows", path="/price_list_rows", incremental_fields=[]),
    "locations": KatanaEndpointConfig(name="locations", path="/locations", incremental_fields=[], partition_key=None),
    "bin_locations": KatanaEndpointConfig(
        name="bin_locations", path="/bin_locations", incremental_fields=[], partition_key=None
    ),
    # Live per-(location, variant, bin) balance at the default VARIANT granularity. bin_location_id is
    # null for stock with no bin assigned, and Katana drops a row once all its quantities reach zero.
    "bin_inventory": KatanaEndpointConfig(
        name="bin_inventory",
        path="/bin_inventory",
        primary_keys=["location_id", "variant_id", "bin_location_id"],
        incremental_fields=[],
        partition_key=None,
    ),
    "operators": KatanaEndpointConfig(
        name="operators", path="/operators", incremental_fields=[], partition_key=None, data_selector=None
    ),
    "tax_rates": KatanaEndpointConfig(name="tax_rates", path="/tax_rates", incremental_fields=[], partition_key=None),
}

ENDPOINTS = tuple(KATANA_ENDPOINTS.keys())

INCREMENTAL_FIELDS: dict[str, list[IncrementalField]] = {
    name: config.incremental_fields for name, config in KATANA_ENDPOINTS.items()
}