diff --git a/RELEASES.json b/RELEASES.json index 628ca3b036b0ac68216a4174c3cddca9c672c701..58b8b0eedd133950dc8e593362ae5cad4bc5abb0 100644 --- a/RELEASES.json +++ b/RELEASES.json @@ -1,4 +1,4 @@ { - "current": "eb8f3aa0d", + "current": "add0f45e", "releases": [] } \ No newline at end of file diff --git a/VERSION b/VERSION index 94074ee0b66df1534d50568ca61973e74d2dd715..4d06a651a1dca26bcb5cac41393937d1b32d48c4 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -eb8f3aa0d +add0f45e diff --git a/api/amazon_relational.py b/api/amazon_relational.py index abe55de9927f27755971fd27a65fddeffe73233c..486dad5302595e53dbccfa506f96b7f254f98a58 100644 --- a/api/amazon_relational.py +++ b/api/amazon_relational.py @@ -34,6 +34,7 @@ them to read-through too. The shared document holds their schemas, never a trunc import hashlib import json import os +import re import sys from collections import OrderedDict from pathlib import Path @@ -132,9 +133,61 @@ _MONEY.update({"estimated_fee_total", "estimated_referral_fee_per_unit", "estimated_pick_pack_fee_per_unit", "estimated_weight_handling_fee_per_unit", "expected_fulfillment_fee_per_unit", "estimated_monthly_storage_fee"}) -#: Percentages and rates are fractional too, but they are not money. `currency` is the only -#: fractional field type the grid offers, so they take it and the LABEL carries the meaning. -_RATE_SUFFIX = ("_percentage", "_percentage_b2_b", "_rate", "_ctr") +#: S78-T01: money the report stores as text or names without an `_amount` / `_fee` suffix. +#: ⚠ No `_price` suffix rule: `attributes_list_price` and the `*_median_*_price` details are JSON. +_MONEY.update({"business_price", "label_cost", "fee_total", "sales_price", "item_price_per_unit", + "featuredoffer_price", "lowest_price_new_plus_shipping", "lowest_price_used", + "recommended_sales_price", "estimated_cost_savings_of_recommended_actions", + "estimated_storage_cost_next_month", "sales_last_30_days", "amount_charged", + "rate_surcharge", "estimated_referral_fee_per_item", "amount_per_unit", + "amount_total", "base_rate", "est_base_msf", "est_sus", "utilization_surcharge_rate", + "estimated_future_fee_current_selling_on_amazon_future_fulfillment_fees", + "estimated_future_order_handling_fee_per_order", + "estimated_future_pick_pack_fee_per_unit", + "estimated_future_weight_handling_fee_per_unit", + "expected_future_fulfillment_fee_per_unit", "lost_revenue_due_to_oos", + "non_subscriber_average_revenue", "subscriber_average_revenue", + "total_subscriptions_revenue"}) +_MONEY_PREFIX = ("cost_", "sales_shipped_last_", "estimated_ais_", "sns_sale_price_week_", + "revenue_from_") +_MONEY_INFIX = ("_life_time_value_",) + +#: ⭐ S78-T01: rates and shares are `pct`, and the grid shows a `pct` value as the number plus +#: "%" with no scaling. So the scale is MEASURED per column on the staging mirror (2026-10-20): +#: `_PERCENT` columns already hold 0 to 100, `_FRACTION_PERCENT` columns hold 0 to 1 and are +#: multiplied by 100 in the read path (`grid_source`), so 0.15 reads "15%" and never "0.15%". +#: ⛔ A rate whose scale was not measured stays out of both sets (see `oos_rate_week_*`). +_PERCENT_SUFFIX = ("_percentage", "_percentage_b2_b") +_PERCENT = {"sales_by_asin_refund_rate", "sales_by_asin_refund_rate_b2_b", + "cart_add_data_asin_cart_add_share", "click_data_asin_click_share", + "impression_data_asin_impression_share", "purchase_data_asin_purchase_share", + "cart_add_data_total_cart_add_rate", "click_data_total_click_rate", + "purchase_data_total_purchase_rate", "revenue_penetration", + "coupons_revenue_penetration", "share_of_coupon_subscriptions", + "subscriber_retention_for30_days", "subscriber_retention_for90_days", + *(f"{metric}_for{n}_percent_seller_funding" for metric in + ("revenue_penetration", "signup_conversion") for n in (0, 5, 10))} +_FRACTION_PERCENT = {"click_share", "conversion_share", "click_data_click_rate", + "purchase_data_conversion_rate", "combination_pct"} +#: Account health reports each rate metric and its target as a 0 to 1 fraction. +_FRACTION_PERCENT_RE = re.compile(r"[a-z_]+_rate(?:_afn|_mfn)?_(?:rate|target_value)") + +#: Non-money numbers stored as text. With the DOUBLE columns that are not money, they take +#: `currency` (the grid's only fractional type) with an EMPTY currency symbol, so no "$" shows. +_NUMBER = {"storage_utilization_ratio"} + + +def _is_money(col): + return (col in _MONEY or col.endswith(("_amount", "_fee", "_fees")) or col.startswith(_MONEY_PREFIX) + or any(part in col for part in _MONEY_INFIX)) + + +def _is_fraction_percent(col): + return col in _FRACTION_PERCENT or bool(_FRACTION_PERCENT_RE.fullmatch(col)) + + +def _is_percent(col): + return col in _PERCENT or col.endswith(_PERCENT_SUFFIX) or _is_fraction_percent(col) # Inventory reports remain VARCHAR when a quantity column mixes numbers and blanks. # Names identify units; identifiers such as SKU and FNSKU must stay text. @@ -161,7 +214,9 @@ def _ftype(col, duck_type): return "int" if any(col == s or col.endswith(s) for s in _DATEISH): return "date" - if col in _MONEY or col.endswith(("_amount", "_fee")) or any(col.endswith(s) for s in _RATE_SUFFIX): + if _is_percent(col): + return "pct" + if _is_money(col) or col in _NUMBER: return "currency" base = str(duck_type or "").upper().split("(")[0] if base in ("DOUBLE", "FLOAT", "DECIMAL"): @@ -171,6 +226,18 @@ def _ftype(col, duck_type): return "text" +def _format(col, duck_type): + """The display format a field ships with, or None for the grid's default.""" + kind = _ftype(col, duck_type) + if col == "per_unit_volume": + return {"decimals": 4} + if kind == "pct": + return {"decimals": 2} + if kind == "currency" and not _is_money(col): + return {"currencySymbol": "", "decimals": 2} + return None + + def mirror_columns(cur, table): """[(name, type)] for a mirror table, or [] when it has never been synced.""" try: @@ -197,6 +264,8 @@ def mirror_columns(cur, table): LISTINGS = PREFIX + "listings" LOOKUP_PARENT, LOOKUP_SKU, LOOKUP_ASIN = "lookup_parent_asin", "lookup_sku", "lookup_asin" LOOKUP_KEYS = frozenset((LOOKUP_PARENT, LOOKUP_SKU, LOOKUP_ASIN)) +#: Native columns that already show a row's ASIN (orders, order items, replenishment carry `asin`). +_NATIVE_ASIN = frozenset(("asin", "asin1", "child_asin")) _LOOKUP_LABELS = {LOOKUP_PARENT: "Parent ASIN", LOOKUP_SKU: "SKU", LOOKUP_ASIN: "ASIN"} _LOOKUP_DESCRIPTIONS = { LOOKUP_PARENT: ("The parent ASIN of this row's product, from Amazon's catalog, else the " @@ -342,9 +411,11 @@ def lookups(cur, key, names=None): elif sku and latest: ref = f"TRIM(CAST({table}.{_quoted(sku)} AS VARCHAR))" via = sku_via if "seller_sku" in listing_names else None - add(LOOKUP_ASIN, sku, - f"(SELECT string_agg(DISTINCT _l.asin, ', ' ORDER BY _l.asin) " - f"FROM ({latest}) AS _l WHERE _l.sku = {ref})", via) + # S78-T01: a table that carries its own ASIN column gains no second "ASIN". + if not names & _NATIVE_ASIN: + add(LOOKUP_ASIN, sku, + f"(SELECT string_agg(DISTINCT _l.asin, ', ' ORDER BY _l.asin) " + f"FROM ({latest}) AS _l WHERE _l.sku = {ref})", via) if parents: add(LOOKUP_PARENT, sku, f"(SELECT string_agg(DISTINCT _p.parent_asin, ', ' ORDER BY _p.parent_asin) " @@ -359,6 +430,8 @@ def _fields_for(cur, key, *, read_through=False): if not cols: return [] declared = {field["name"]: field for field in connector_spec.AMAZON_FIELDS.get(key, ())} + written = connector_spec.AMAZON_LABELS.get(key, {}) + shown = default_columns(key) names = {c[0] for c in cols} pinned = PINNED.get(key) if PINNED.get(key) in names else None # S67-T30: each lookup lands IMMEDIATELY after the column it derives from, shown by default, @@ -373,13 +446,15 @@ def _fields_for(cur, key, *, read_through=False): # report's first column is whatever Amazon put there (`item_name`, `_data_origin`), so a # listing opened from an order must lead with its seller SKU, as Odoo's pinned fields do. for name, dtype in sorted(cols, key=lambda col: col[0] != pinned): + fmt = _format(name, dtype) out.append(_odoo._preset({ - "key": name, "label": declared.get(name, {}).get("label") or _label(name), + "key": name, + "label": declared.get(name, {}).get("label") or written.get(name) or _label(name), "type": _ftype(name, dtype), "source": "overlay", - "default": name == pinned or name in _DEFAULT_COLUMNS or name in product, + "default": name == pinned or name in shown or name in product, "pinned": name == pinned, - **({"format": {"decimals": 4}} if name == "per_unit_volume" else {}), + **({"format": fmt} if fmt else {}), }, flow="amazon_relational")) for lookup in derived.get(name, ()): field = {"key": lookup["key"], "label": lookup["label"], "type": "text", @@ -402,9 +477,15 @@ def _fields_for(cur, key, *, read_through=False): return out -#: Columns worth showing before a person picks their own. Deliberately short: these tables are -#: wide, and a default of "everything" is the same as no default at all. -_DEFAULT_COLUMNS = { +def default_columns(key): + """The columns `key` opens on beyond the pinned, product, lookup and link fields.""" + declared = connector_spec.AMAZON_DEFAULT_COLUMNS.get(key) + return frozenset(declared) if declared is not None else _UNDECLARED_DEFAULT_COLUMNS + + +#: The shared fallback for a table `connector_spec.AMAZON_DEFAULT_COLUMNS` does not declare yet +#: (one the staging mirror has never synced). A declared table reads only its own list. +_UNDECLARED_DEFAULT_COLUMNS = frozenset({ "amazon_order_id", "purchase_date", "order_status", "sku", "seller_sku", "asin", "asin1", "quantity", "quantity_purchased", "item_price", "item_status", "product_name", "item_name", "settlement_id", "amount", "amount_type", "amount_description", "posted_date", @@ -413,7 +494,10 @@ _DEFAULT_COLUMNS = { "backend_keywords", "month", "search_query", "impressions", "clicks", "cart_adds", "purchases", "source_basis", "keyword", "date", "organic_rank", "sponsored_rank", "sponsored_rank_basis", -} +}) + +#: Every name any Amazon table may open on (`verify_agent_knowledge` reads it by name). +_DEFAULT_COLUMNS = _UNDECLARED_DEFAULT_COLUMNS.union(*connector_spec.AMAZON_DEFAULT_COLUMNS.values()) #: The original seven used first-field identities. Retained for legacy-id migration @@ -549,9 +633,11 @@ def grid_source(cur, key): continue kind = _ftype(name, dtype) expr = _quoted(name) - fractional = kind == "currency" or name == "per_unit_volume" - if kind in ("currency", "int"): + fractional = kind in ("currency", "pct") or name == "per_unit_volume" + if kind in ("currency", "pct", "int"): expr = f"TRY_CAST({expr} AS {'DOUBLE' if fractional else 'BIGINT'})" + if kind == "pct" and _is_fraction_percent(name): + expr = f"ROUND({expr} * 100, 8)" # 8 places drops float noise, keeps every digit cols[name] = (expr, _number if fractional else {"int": _integer}.get(kind, _text)) # S67-T30: the product lookups bind like any mirror column, so a filter (`=` a parent), a # group (count by parent) and a sort compile against them in SQL with no per-row Python. @@ -613,7 +699,7 @@ def _read(cur, key): quoted = ", ".join(spec["cols"][c][0] for c in cols) personal = set(amazon_store.personal_columns(key)) numeric = {name for name, dtype in mirror_columns(cur, SOURCE[key]) - if name not in personal and _ftype(name, dtype) in ("currency", "int")} + if name not in personal and _ftype(name, dtype) in ("currency", "pct", "int")} # S67-T30: a stored snapshot carries the same product lookups, computed by the SAME SQL # expressions the read-through binding uses, in this one SELECT (never per row). derived = [(lookup["key"], lookup["expr"]) for lookup in lookups(cur, key)] @@ -729,6 +815,11 @@ def plan(cur, rt=None): built[key] = {"readThrough": True, "fields": _fields_for(cur, key, read_through=True), "count": n} continue + if not n: + # S78-T02: an emptied mirror table is an empty population, never a skip, or the stored + # definition keeps rows the source no longer holds. `apply_plan` creates no table for it. + built[key] = [] + continue rows = _read(cur, key) if rows: built[key] = rows @@ -750,7 +841,9 @@ def plan_fingerprint(cur, built): rows = built.get(key) or [] canonical = sorted(rows, key=lambda row: json.dumps( row, sort_keys=True, ensure_ascii=True, separators=(",", ":"), default=str)) - tables.append({"key": key, "fields": _fields_for(cur, key), "rows": canonical}) + # S78-T02: an empty population differs from an absent table, so a stale copy is rewritten. + empty = {"empty": True} if built.get(key) == [] else {} + tables.append({"key": key, "fields": _fields_for(cur, key), "rows": canonical, **empty}) payload = json.dumps(tables, sort_keys=True, ensure_ascii=True, separators=(",", ":"), default=str) return hashlib.sha256(payload.encode("utf-8")).hexdigest() @@ -979,10 +1072,15 @@ def apply_plan(rt, built, username="amazon", today=None): not_stored.clear() live_columns = {key: {field["key"] for field in table.get("fields", ())} for key, table in doc.items() if key in SOURCE and isinstance(table, dict)} + # S78-T02: an empty population reconciles an EXISTING definition only; it creates none. + unborn = {key for key, _label, _fields, rows in plans + if isinstance(rows, list) and not rows and not isinstance(doc.get(key), dict)} live_columns.update({key: {field["key"] for field in fields} - for key, _label, fields, _rows in plans}) + for key, _label, fields, _rows in plans if key not in unborn}) generated = connector_spec.system_link_fields("amazon", live_columns) for key, label, fields, rows in plans: + if key in unborn: + continue stub = isinstance(rows, dict) and rows.get("readThrough") is True def unbacked_refreshed(field): @@ -1038,6 +1136,15 @@ def apply_plan(rt, built, username="amazon", today=None): removed = len((doc.get(key) or {}).get("rows") or {}) if stub else 0 written[key] = _odoo._ensure_table_inplace(doc, key, label, fields, None if stub else rows, username, stamp) + # S78-T01: `_ensure_table_inplace` does not forward `format`, so a stored preset field + # would keep showing "$" on a weight or 0 decimals on a percent. + formats = {f["key"]: f["format"] for f in fields if f.get("format")} + for stored_field in doc[key].get("fields", []): + if (stored_field.get("key") in formats + and (stored_field.get("automation") or {}).get("flowId") == "amazon_relational" + and (stored_field.get("automation") or {}).get("preset") + and not _odoo._ut().user_edited(stored_field)): + stored_field["format"] = dict(formats[stored_field["key"]]) if stub: doc[key]["readThrough"] = True doc[key]["rows"] = {} @@ -1048,7 +1155,7 @@ def apply_plan(rt, built, username="amazon", today=None): # The shared merger compares string spellings, so an old "2" and a new # integer 2 otherwise compare equal and leave the old cell untyped. numeric = {f["key"] for f in doc[key].get("fields", []) - if f.get("type") in ("currency", "int") + if f.get("type") in ("currency", "pct", "int") and (f.get("automation") or {}).get("flowId") == "amazon_relational" and not _odoo._ut().user_edited(f)} stored = doc[key].get("rows") or {} diff --git a/api/application_registry.py b/api/application_registry.py index 151fb4244ddb8c35ff2d9dbfd05f143f8bc21e20..edf5482ef6d03aaaea4044f2aeb0fb2045ed5bf9 100644 --- a/api/application_registry.py +++ b/api/application_registry.py @@ -69,6 +69,76 @@ _TEMPLATE_CONTRACTS = { APPLICATION_WIZARD_TEMPLATE_KINDS = tuple(_TEMPLATE_CONTRACTS) APPLICATION_TEMPLATE_KINDS = tuple(_TEMPLATE_CONTRACTS) +#: S78-T17 (C4, R20, R21): templates offered only to a tenant that passes the kind's gate. +#: They stay out of the ungated tuples above, which other gates pin to Routing and Catalog. +BRAND_ANALYTICS_DATABASE = "ut_amzn_brand_analytics" +_GATED_TEMPLATE_CONTRACTS = { + "competitor_watch": { + "kind": "competitor_watch", + "label": "Competitor watch", + # The screen reads the competitor watch routes; the source names the Amazon database. + "applicationType": "map", + "displayMode": "map", + "fieldRoles": [ + {"key": "term", "label": "Search term", "required": True, "types": []}, + ], + "defaultPlacement": {"section": "app", "order": 0}, + "sourceDatabase": BRAND_ANALYTICS_DATABASE, + "sourceLabel": "Amazon brand analytics", + }, +} +GATED_TEMPLATE_KINDS = tuple(_GATED_TEMPLATE_CONTRACTS) + + +def _template_contract(kind): + """The contract for an ungated or gated template kind, or None.""" + return _TEMPLATE_CONTRACTS.get(kind) or _GATED_TEMPLATE_CONTRACTS.get(kind) + + +def brand_analytics_holds(connected, enabled, refused): + """R21: Amazon is connected, brand analytics is switched on and Amazon has not refused it.""" + return connected is True and enabled is True and refused is not True + + +def _brand_analytics_refused(runtime): + """True when the mirror's last brand analytics pass was refused; unreadable is no evidence.""" + try: + import duckdb + from pathlib import Path + from harness import amazon_store as az, datastore + path = az._tenant_mirror_path(runtime.key, rt=runtime) + if not path.is_file(): + return False + query = "SELECT phase FROM _sync_state WHERE entity = ?" + key = [az.entity_key("brand_analytics")] + if path.resolve() == Path(datastore.DB_PATH).resolve(): + row = datastore.ro_cursor().execute(query, key).fetchone() + else: + with duckdb.connect(str(path), read_only=True) as cursor: + row = cursor.execute(query, key).fetchone() + return bool(row) and row[0] == "refused" + except Exception: # noqa: BLE001 + return False + + +def brand_analytics_offered(session): + """R21 gate for the Competitor watch template. Any read failure withholds the template.""" + try: + import connectors_amazon as cx + from routes_connectors import _entry_types + types, _scopes, failure = _entry_types(session.runtime, session.uname) + connected = failure is None and cx.ENTRY_TYPE in set(types) + enablement = cx.read_database_enablement(session.runtime) + enabled = (enablement.get("available") is True + and (enablement.get("databases") or {}).get(BRAND_ANALYTICS_DATABASE) is True) + refused = _brand_analytics_refused(session.runtime) if connected and enabled else None + return brand_analytics_holds(connected, enabled, refused) + except Exception: # noqa: BLE001 + return False + + +_TEMPLATE_GATES = {"competitor_watch": brand_analytics_offered} + class ApplicationError(ValueError): pass @@ -132,8 +202,19 @@ def own_template_apps(runtime, user): return owned -def template_contracts(): - """The trusted renderer contracts offered by the Application creation flow.""" +def template_contracts(session=None): + """The trusted renderer contracts offered by the Application creation flow. + + A gated template joins only for a session whose tenant passes its gate (R21). + """ + rows = _ungated_template_contracts() + if session is not None: + rows.extend(copy.deepcopy(contract) for kind, contract in _GATED_TEMPLATE_CONTRACTS.items() + if _TEMPLATE_GATES[kind](session)) + return rows + + +def _ungated_template_contracts(): return [copy.deepcopy(contract) for contract in _TEMPLATE_CONTRACTS.values()] @@ -627,7 +708,7 @@ def _refuse_legacy_catalog_changes(app, config): def _template_definition(kind, payload, *, semantic_records=None, field_contracts=None, allowed_databases=None): - contract = _TEMPLATE_CONTRACTS.get(kind) + contract = _TEMPLATE_CONTRACTS.get(kind) or _GATED_TEMPLATE_CONTRACTS.get(kind) if contract is None: raise ApplicationError("application template kind is not recognized") template_kind = str(payload.get("templateKind") or "").strip().lower() @@ -636,6 +717,10 @@ def _template_definition(kind, payload, *, semantic_records=None, field_contract source = str(payload.get("sourceDatabase") or "").strip()[:120] if not source: raise ApplicationError("sourceDatabase is required") + if contract.get("sourceDatabase") and source != contract["sourceDatabase"]: + raise ApplicationError(f"{contract['label']} reads " + f"{contract.get('sourceLabel') or contract['sourceDatabase']} only. " + "Choose it as the source database.") if source not in set(allowed_databases or ()): raise PermissionError("an Application cannot bind a database you cannot open") if "compatibleDatabases" in payload: @@ -706,6 +791,14 @@ def validate_application(payload, tenant, *, semantic_records=None, field_contra if "tenant" in payload and str(payload.get("tenant") or "") != str(tenant): raise PermissionError("an Application cannot be created for another tenant") template_kind = str(payload.get("templateKind") or "").strip().lower() + gated = _GATED_TEMPLATE_CONTRACTS.get(template_kind) + if gated is not None: + if str(payload.get("type") or gated["applicationType"]).strip().lower() != gated["applicationType"]: + raise ApplicationError( + f"{template_kind} template Applications require type {gated['applicationType']!r}") + return _template_definition( + template_kind, payload, semantic_records=semantic_records, + field_contracts=field_contracts, allowed_databases=allowed_databases) if template_kind and template_kind not in _TEMPLATE_CONTRACTS: raise ApplicationError("application template kind is not recognized") expected_type = (_TEMPLATE_CONTRACTS[template_kind]["applicationType"] @@ -843,7 +936,7 @@ def _build_application(tenant, user, payload, *, semantic_records=None, field_co """Build the same validated record for authored Apps and boot migrations.""" payload=payload if isinstance(payload,dict) else {} template_kind=str(payload.get("templateKind") or "").strip().lower() - expected_type = (_TEMPLATE_CONTRACTS.get(template_kind) or {}).get("applicationType") + expected_type = (_template_contract(template_kind) or {}).get("applicationType") kind=str(payload.get("type") or expected_type or "").strip().lower() definition = validate_application( payload, tenant, semantic_records=semantic_records, field_contracts=field_contracts, @@ -858,7 +951,7 @@ def _build_application(tenant, user, payload, *, semantic_records=None, field_co raw_config = payload.get("config") if definition and raw_config is None: raw_config = {"schemaVersion": 1, - "display": {"mode": _TEMPLATE_CONTRACTS[template_kind]["displayMode"]}} + "display": {"mode": _template_contract(template_kind)["displayMode"]}} app={"id":app_id,"tenant":str(tenant),"type":kind,"name":name, "compatibleDatabases":compatible, "semanticRoleBindings":_governed_bindings( @@ -867,8 +960,8 @@ def _build_application(tenant, user, payload, *, semantic_records=None, field_co "share":{"mode":"private","users":[]},"starredBy":[],"createdBy":user, "createdAt":now,"updatedAt":now,"revision":1} app.update(definition) - if definition and _TEMPLATE_CONTRACTS[template_kind].get("deepLink"): - app["deepLink"] = _TEMPLATE_CONTRACTS[template_kind]["deepLink"].replace("", app_id) + if definition and _template_contract(template_kind).get("deepLink"): + app["deepLink"] = _template_contract(template_kind)["deepLink"].replace("", app_id) if kind == "code": app.update(deepLink=f"/app/code/{app_id}", origin=None) app["origin"] = _server_origin(origin_door, via) @@ -1237,7 +1330,8 @@ def mutate(runtime, app_id, user, is_admin, patch, *, semantic_records=None, fie if not _is_finance(app) and not _allowed(app.get("compatibleDatabases"), allowed_databases): if not _database_free_code(app): raise ApplicationError("application does not exist") - if app.get("templateKind") in APPLICATION_TEMPLATE_KINDS and "compatibleDatabases" in patch: + if (app.get("templateKind") in APPLICATION_TEMPLATE_KINDS + GATED_TEMPLATE_KINDS + and "compatibleDatabases" in patch): raise ApplicationError("a template Application's sourceDatabase is immutable") star_only = set(patch) <= {"starred"} if star_only: @@ -1522,7 +1616,7 @@ def _retirement_plan(runtime, tenant): for app in applications.values()): raise ApplicationError("the Application registry has an invalid tenant record") removed = {identity: app for identity, app in applications.items() - if app.get("templateKind") not in ("route", "catalog")} + if app.get("templateKind") not in ("route", "catalog", *GATED_TEMPLATE_KINDS)} tables = _retirement_read(runtime, user_tables.STORE_KEY) databases = {"customer_data", "product_data"} | {key for key in tables if key.startswith("ut_")} for app in applications.values(): diff --git a/api/catalog_store.py b/api/catalog_store.py index 2b81bdd91fd941f36a87606a5ff249ca315b05e0..f02c4225151fd221fb6fdc8488373cba9dd3ce39 100644 --- a/api/catalog_store.py +++ b/api/catalog_store.py @@ -381,3 +381,100 @@ def delete_catalog(session, catalog_id): return {"deleted": catalog_id} return _update(session.runtime, apply) + + +# Long PDF exports: an MCP caller polls one of these through operation_status. +EXPORT_KEY = "catalog_exports_v1" +EXPORT_RETENTION_SECONDS = 24 * 3600 +# A running export older than this lost its worker (a restart), so it reads as failed. +EXPORT_STALE_SECONDS = 30 * 60 + + +def _export_bindings(session): + from routes_external import _operation_bindings + return _operation_bindings(session) + + +def _exports(raw, now): + doc = raw if isinstance(raw, dict) and isinstance(raw.get("exports"), dict) else {"exports": {}} + doc = copy.deepcopy(doc) + doc["exports"] = {key: value for key, value in doc["exports"].items() + if isinstance(value, dict) and float(value.get("retainUntil") or 0) >= now} + return doc + + +def start_export(session, catalog_id): + """Record a running export bound to this caller's account and connection, and return its id.""" + import secrets + import time + + operation_id = secrets.token_hex(32) + now = time.time() + item = {"bindings": _export_bindings(session), "catalogId": catalog_id, "status": "running", + "startedAt": now, "retainUntil": now + EXPORT_RETENTION_SECONDS} + + def apply(raw): + doc = _exports(raw, now) + doc["exports"][operation_id] = item + return doc + + session.runtime.update(EXPORT_KEY, apply, flush="sync") + return operation_id + + +def _export_error(error): + from fastapi import HTTPException + from core import sheets + + if isinstance(error, sheets.SheetRefusal): + return {"code": error.code, "message": error.message, "status": 409} + if isinstance(error, HTTPException) and isinstance(error.detail, dict): + info = error.detail.get("error") or {} + if isinstance(info.get("code"), str) and isinstance(info.get("message"), str): + return {"code": info["code"], "message": info["message"], "status": error.status_code} + return {"code": "catalog_export_failed", "status": 503, + "message": "The PDF export did not finish. Call apps.catalog_pdf again with the same catalog_id."} + + +def finish_export(session, operation_id, result, error=None): + """Store the worker's outcome on its export record.""" + import time + + now = time.time() + + def apply(raw): + doc = _exports(raw, now) + item = doc["exports"].get(operation_id) + if item is not None: + item.update(status="failed" if error is not None else "done", finishedAt=now, + retainUntil=now + EXPORT_RETENTION_SECONDS) + if error is not None: + item["error"] = _export_error(error) + else: + item["result"] = copy.deepcopy(result) + return doc + + session.runtime.update(EXPORT_KEY, apply, flush="sync") + + +def export_status(session, operation_id): + """Read this caller's export by id, or None when no export of theirs has that id.""" + import time + + now = time.time() + try: + raw = session.runtime.get(EXPORT_KEY, fresh=True) + except Exception: + # The confirmation read that follows reports an unavailable store with its own code. + return None + item = _exports(raw, now)["exports"].get(operation_id) + if not isinstance(item, dict) or item.get("bindings") != _export_bindings(session): + return None + base = {"operation_id": operation_id, "catalog_id": item.get("catalogId")} + if item.get("status") == "done": + return {**copy.deepcopy(item["result"]), **base, "state": "done", "retry": False} + if item.get("status") == "failed" or now - float(item.get("startedAt") or 0) > EXPORT_STALE_SECONDS: + error = item.get("error") or _export_error(None) + return {**base, "ok": False, "state": "failed", "retry": False, "error": copy.deepcopy(error)} + return {**base, "state": "running", "retry": True, + "message": "The PDF is still rendering. Call operation_status again shortly."} diff --git a/api/connectors_amazon.py b/api/connectors_amazon.py index dedd0d1621f22529db1bfb922698eb19c4c76397..9027e3d8a9d0753000849ee17e8a51af01d12096 100644 --- a/api/connectors_amazon.py +++ b/api/connectors_amazon.py @@ -534,7 +534,7 @@ def sync_state(rt, tenant_key, *, cadence_seconds=None, now=None): if plan is None and landed.get("phase") not in ("refused", "failed", "throttled"): unknown.append("The sync schedule is unknown because a saved timestamp could not be read. Retry sync to refresh it.") limit = None - if (table in az.GROUP_A_TABLES and table != "catalog_items") or table == "transactions": + if (table in az.GROUP_A_TABLES and table != "catalog_items") or table in ("transactions", "brand_analytics"): marker = entities.get(az.entity_key("limit_" + table)) or {} try: limit = json.loads(marker.get("cursor") or "{}").get("reason") @@ -594,7 +594,7 @@ def sync_state(rt, tenant_key, *, cadence_seconds=None, now=None): "stuck": stuck, "stuckCause": stuck_cause, "unknownCause": " ".join(unknown), } - if table == az.SQP_TABLE or table in az.GROUP_A_TABLES or table == "transactions": + if table == az.SQP_TABLE or table in az.GROUP_A_TABLES or table in ("transactions", "brand_analytics"): answer[table]["limit"] = limit answer["sku_keywords"] = { "rowsLoaded": mirrored.get("sku_keywords"), diff --git a/api/external_freshness.py b/api/external_freshness.py index 1b525b873777b92df91a3a997867890b98d66f47..17809296326c5cb23874a9b7d41a352da2aef74a 100644 --- a/api/external_freshness.py +++ b/api/external_freshness.py @@ -59,27 +59,64 @@ def _sync_date(value): return None -def amazon_sync_stamp(cur, table_key): - """Read a table's last successful source pass before its legacy raw-table receipt.""" +#: S78-T02 (R11): a monthly reconciliation proof, `amzn_reconcile___`. +_PERIOD_PROOF = re.compile(r"_\d{4}_\d{2}") +#: A derived view is as fresh as its oldest input. ⛔ Never fresher than the data it reads. +_AMAZON_DERIVED_INPUTS = {"sku_keywords": ("listing_items", "search_query_performance")} +#: A derived table whose rows arrive by an import with its own receipt. +_AMAZON_DERIVED_RECEIPTS = {"keyword_ranks": "amzn_apify_keyword_ranks"} + + +def amazon_sync_state(cur): + """`{entity: updated_at}` for every `done` Amazon `_sync_state` row, in one read.""" + return {str(entity): at for entity, at in cur.execute( + "SELECT entity, updated_at FROM _sync_state " + "WHERE phase = 'done' AND starts_with(entity, 'amzn_')").fetchall()} + + +def _amazon_table_stamp(state, table): + """The mark, else the legacy raw receipt, else the newest reconciliation proof, or None. + + ⛔ `sync_tick_` is never a receipt: a refused pass also writes it `done`. + """ + for entity in ("amzn_sync_mark_" + table, "amzn_" + table): + if state.get(entity) is not None: + return state[entity] + proof = "amzn_reconcile_" + table + return _latest(*(at for entity, at in state.items() + if entity == proof or entity.startswith(proof + "_") + and _PERIOD_PROOF.fullmatch(entity[len(proof):]))) + + +def amazon_sync_stamp(cur, table_key, state=None): + """A table's true last successful sync from its `_sync_state` receipts, or None. + + R11: a refusal overwrites the raw receipt and a pass with no window writes no mark, so the + reconciliation proofs also count. The time is the receipt's own, never a restore or read time. + """ import amazon_relational raw = amazon_relational.SOURCE.get(table_key) - if not raw or cur is None: + if not raw or (cur is None and state is None): return None - marker = "amzn_sync_mark_" + raw.removeprefix("amzn_") - for entity in (marker, raw): - row = cur.execute("SELECT updated_at FROM _sync_state WHERE entity = ? AND phase = 'done'", - [entity]).fetchone() - if row: - return row[0] - return None + if state is None: + state = amazon_sync_state(cur) + table = raw.removeprefix("amzn_") + if table in _AMAZON_DERIVED_RECEIPTS: + return state.get(_AMAZON_DERIVED_RECEIPTS[table]) + if table in _AMAZON_DERIVED_INPUTS: + stamps = [stamp for name in _AMAZON_DERIVED_INPUTS[table] + if (stamp := utc_stamp(_amazon_table_stamp(state, name))) is not None] + return min(stamps, key=lambda stamp: datetime.fromisoformat(stamp.replace("Z", "+00:00"))) \ + if stamps else None + return _amazon_table_stamp(state, table) def _amazon_refreshes(session, keys): """Read only sync metadata for the authorized Amazon databases on this tenant's cursor.""" import assistant_mirror - cur, refreshed = None, {} + cur, state, refreshed = None, None, {} for key in keys: try: if cur is None: @@ -87,12 +124,61 @@ def _amazon_refreshes(session, keys): if refusal is not None or cur is None: cur = None continue - refreshed[key] = amazon_sync_stamp(cur, key) + if state is None: + state = amazon_sync_state(cur) + refreshed[key] = amazon_sync_stamp(cur, key, state=state) except Exception: continue return refreshed +def amazon_nav(session, pages): + """Add `group` and `sync` to each Amazon nav row in place; returns `pages`. Never raises. + + `sync` is `{"at": | None, "empty": bool | None}` and is read on the request, + never stored: one `_sync_state` read and one EXISTS per mirror table, no product value. + It is absent when the mirror cannot answer, so "unknown" never reads as "never synced". + """ + try: + from core import connector_spec + rows = [page for page in pages if str(page.get("key") or "").startswith("ut_amzn_")] + for page in rows: + group = connector_spec.table_group(page["key"]) + if group: + page["group"] = group + if not rows: + return pages + import amazon_relational + import assistant_mirror + + cur, refusal = assistant_mirror.open_cursor(session, rows[0]["key"]) + if refusal is not None or cur is None: + return pages + state = amazon_sync_state(cur) + if not state: + # An unrestored or absent mirror holds no receipt at all: unknown, not never synced. + return pages + bases = {str(name) for (name,) in cur.execute( + "SELECT table_name FROM information_schema.tables WHERE table_schema = 'main' " + "AND table_type = 'BASE TABLE' AND starts_with(table_name, 'amzn_')").fetchall()} + raws = {page["key"]: amazon_relational.SOURCE.get(page["key"]) for page in rows} + probed = sorted({raw for raw in raws.values() if raw in bases}) + held = {} + if probed: + held = {str(name): bool(has) for name, has in cur.execute(" UNION ALL ".join( + f"SELECT '{raw}' AS name, EXISTS (SELECT 1 FROM \"{raw}\") AS has" + for raw in probed)).fetchall()} + for page in rows: + raw = raws.get(page["key"]) + if not raw: + continue + stamp = utc_stamp(amazon_sync_stamp(cur, page["key"], state=state)) + page["sync"] = {"at": stamp, "empty": (not held[raw]) if raw in held else None} + except Exception: + pass + return pages + + def _mirror_refreshes(session, definitions, keys): """Read authorized mirror stamps with cached successful cursors and no product-row reads.""" import assistant_mirror diff --git a/api/loopy_catalog_tools.py b/api/loopy_catalog_tools.py index c396a82da498c66a356bdee6e34c1ea2fd9c61f6..9fd7c0aeee5eb39a786ef259ce6b7736dc956dd2 100644 --- a/api/loopy_catalog_tools.py +++ b/api/loopy_catalog_tools.py @@ -172,7 +172,8 @@ def catalog_build(ctx, args): keys = [str(row.get(mapping["key"], "")).strip() if row.get(mapping["key"]) is not None else "" for row in rows] if any(not key for key in keys) or len(set(keys)) != len(keys): _build_refusal("catalog_key_ambiguous", "Products need distinct nonempty source keys. Rebind the App's record key before building.") - name = args.get("name") or _no_dashes(str(title or "Catalog"))[:120] + # The store refuses long dashes in a name, so normalise an AI's name before the preview. + name = _no_dashes(str(args.get("name") or title or "Catalog")).strip()[:120] or "Catalog" kinds = ["cover", "toc", *(["product"] * len(rows)), "closing"] pages = [] for kind in kinds: @@ -241,13 +242,101 @@ def catalog_pdf(session, args): mcp = (getattr(session, "claims", None) or {}).get("external") is True link = (assistant_files.capability_url(session.runtime, saved["id"]) if door == "whatsapp" and not mcp else saved["link"]) - return {"ok": True, "fileId": saved["id"], "link": _secure_link(link), - "message": "Catalog PDF ready"} + result = {"ok": True, "fileId": saved["id"], "link": _secure_link(link), + "message": "Catalog PDF ready"} + if mcp: + result.update(external_download(saved["id"], len(data))) + else: + # Declared provenance keeps the Assistant turn from marking this artifact unknown. + result["sources"] = [app["sourceDatabase"]] + return result + + +EXTERNAL_FILES_PATH = "/api/v1/external/files/" +FILE_RESOURCE_PREFIX = "loopable://files/" + + +def external_download(file_id, size): + """Name the two credential-bound ways an MCP caller fetches the PDF bytes.""" + from loopy_file_tools import _links, _secure_link + + base = _links().public_base() + return {"mime": "application/pdf", "bytes": size, + "downloadUrl": _secure_link((base or "") + EXTERNAL_FILES_PATH + file_id), + "resourceUri": FILE_RESOURCE_PREFIX + file_id, + "download": ("GET downloadUrl with this connection's Authorization header, " + "or read resourceUri with MCP resources/read. Both return application/pdf.")} + + +# A render past this many seconds answers with an operation_id instead of holding the call. +EXPORT_BUDGET_SECONDS = 20.0 + + +def export_budget(): + import os + try: + value = float(os.environ.get("AIOS_CATALOG_PDF_BUDGET_SECONDS", "") or EXPORT_BUDGET_SECONDS) + except ValueError: + value = EXPORT_BUDGET_SECONDS + return value if value > 0 else EXPORT_BUDGET_SECONDS + + +def catalog_pdf_within_budget(session, args, render=None, budget=None): + """Render in a worker thread. Past the budget, return an operation_id that operation_status reads.""" + import contextvars + import sys + import threading + import catalog_store + + render = render or sys.modules[__name__].catalog_pdf + budget = export_budget() if budget is None else budget + lock = threading.Lock() + state = {"finished": False, "operation_id": None} + + def work(): + try: + outcome = (render(session, args), None) + except BaseException as exc: # The caller or the export record receives every failure. + outcome = (None, exc) + with lock: + state.update(finished=True, outcome=outcome) + operation_id = state["operation_id"] + if operation_id is not None: + try: + catalog_store.finish_export(session, operation_id, *outcome) + except Exception as exc: + # The record then reads as failed once it is stale, so the caller retries. + import logging + logging.getLogger(__name__).warning("Catalog export result not saved (%s)", type(exc).__name__) + + context = contextvars.copy_context() + worker = threading.Thread(target=context.run, args=(work,), daemon=True, name="catalog-pdf") + try: + worker.start() + except Exception: + # No worker can start here, so the call renders inline and holds until it finishes. + return render(session, args) + worker.join(budget) + with lock: + if state["finished"]: + result, error = state["outcome"] + if error is not None: + raise error + return result + # The worker waits on this lock, so it reads the id after the record exists. + state["operation_id"] = catalog_store.start_export(session, args["catalog_id"]) + return {"operation_id": state["operation_id"], "state": "running", "retry": True, + "catalog_id": args["catalog_id"], + "message": ("The PDF is still rendering. Call table with action operation_status and this " + "operation_id until state is done. The done result carries fileId, link and downloadUrl.")} def _schema(): return {"type": "object", - "description": "Make a PDF of a stored catalog's designed pages and product images. Returns fileId and link for a person to send. This tool does not send.", + "description": ("Make a PDF of a stored catalog's designed pages and product images. Use the catalog_id that " + "catalog_build returned after confirm_change. Returns fileId and link for a person to send. " + "An MCP call also gets downloadUrl and resourceUri for the bytes, or an operation_id to poll " + "with operation_status when the render is slow. This tool does not send."), "properties": {"catalog_id": {"type": "string", "minLength": 1, "maxLength": 256}}, "required": ["catalog_id"], "additionalProperties": False} diff --git a/api/loopy_links.py b/api/loopy_links.py index 726aeade64feddeec9d8805f200748776781b03c..34a8d52af9c29952faa671479756572901fd3f60 100644 --- a/api/loopy_links.py +++ b/api/loopy_links.py @@ -6,12 +6,37 @@ import re from urllib.parse import quote, urlsplit FRAGMENT = re.compile(r"(? str | None: + """Answer the base this container's own identity implies, or None off a Space.""" + try: + from core import data_binding + except ImportError: + return None + if data_binding.is_production_deployment(): + return LIVE_BASE + # HF sets SPACE_HOST inside a Space. An identity alone (a gate box) implies no host. + host = os.environ.get("SPACE_HOST", "").strip().rstrip("/") + return _checked_base("https://" + host) if host and data_binding.deployment_id() else None def public_base() -> str | None: - """Return the configured HTTPS base without guessing a production host.""" - base = (os.environ.get("AIOS_PUBLIC_BASE", "").strip() - or os.environ.get("APP_BASE_URL", "").strip()).rstrip("/") + """Return the configured HTTPS base, else the deployment's own. Live never answers *.hf.space.""" + configured = _checked_base(os.environ.get("AIOS_PUBLIC_BASE", "").strip() + or os.environ.get("APP_BASE_URL", "").strip()) + identity = _deployment_base() + if configured is not None and not ( + identity == LIVE_BASE and (urlsplit(configured).hostname or "").endswith(".hf.space")): + return configured + return identity + + +def _checked_base(base: str) -> str | None: + """Accept one HTTPS origin with an optional path and nothing else.""" + base = (base or "").strip().rstrip("/") if (not base or any(char.isspace() or ord(char) < 32 for char in base) or any(char in base for char in "\\?#")): return None diff --git a/api/main.py b/api/main.py index fa8217543b89d063f9c4f9687f3043d222f62e40..0494a71adb08ad7e55621b97f0661cfa107aa4e2 100644 --- a/api/main.py +++ b/api/main.py @@ -45,6 +45,7 @@ from starlette.exceptions import HTTPException as StarletteHTTPException # noqa import aios_session # noqa: E402 import routes_admin # noqa: E402 +import routes_competitor_watch # noqa: E402 (S78-T16 / S78-T17 competitor watch) import routes_alerts # noqa: E402 (wave 20 item 25 — the Alerts inbox) import routes_assets # noqa: E402 (wave 18 C2-ASSET — catalog product imagery) import routes_auth # noqa: E402 @@ -549,7 +550,10 @@ app.include_router(routes_tickets.router) # rate-limited by POLITENESS rather than by a bill (~1 req/s, and bulk harvesting is forbidden). app.include_router(routes_geo.router) # R14 / A11 — D's router, A's line (map providers) app.include_router(routes_external.router) +app.include_router(routes_competitor_watch.router) # S78-T16 / S78-T17 competitor watch _mount_router("routes_security") # W59-T43: lane F declares the router; keep before static mount. +_mount_router("routes_amazon_profit") # S78-T03: the Amazon SKU profit refresh and settings doors. +_mount_router("routes_odoo_attachments") # S78-T14 (lane G, C5): delivery photo doors; before static mount. # --- DEPRECATED ALIASES (removed when S2's shell flips; kept so the current bundle keeps working) diff --git a/api/odoo_attachments.py b/api/odoo_attachments.py new file mode 100644 index 0000000000000000000000000000000000000000..8afbaf701c3ed587c9923e4140ed3bc6e3acdaf5 --- /dev/null +++ b/api/odoo_attachments.py @@ -0,0 +1,103 @@ +"""Odoo delivery photos (S78-T14, contract C5): list and stream a picking's image attachments. + +AntsRoute stores each proof-of-delivery photo as an `ir.attachment` on `stock.picking`. It has no +photo field of its own, only the `antsroute_nb_delivery_photo` count. +Rule 4: Odoo is read with `search_read` only. Rule 1: every photo of the picking is listed. +The bytes pass through the response and are never stored. +The caller's read wall on the delivery databases is checked BEFORE any photo byte is read. +""" +import base64 +import binascii + +from deps import err + +ATTACHMENT_MODEL = "ir.attachment" +PICKING_MODEL = "stock.picking" +#: Raster types only. An SVG can carry script, so it is never served from our origin. +IMAGE_TYPES = ("image/jpeg", "image/png", "image/gif", "image/webp") +_META_FIELDS = ["id", "name", "mimetype", "res_model", "res_id", "type"] + + +def attachment_path(attachment_id): + """The C5 bytes path for one attachment.""" + return f"/api/v1/odoo/attachments/{int(attachment_id)}" + + +def _search_read(session, model, domain, fields, **kw): + """One read through the caller's tenant Odoo connection.""" + from core import odoo + source = session.runtime.odoo_source() + if source is None: + raise err(409, "odoo_not_connected", "Connect Odoo in Settings to see delivery photos.") + with source._bound(): + return odoo.search_read(model, domain, fields, **kw) + + +def _wall_admits(session, key, picking_id): + """True when the caller's read wall on `key` returns the picking's row.""" + import routes_odoo_tables as rot + page = rot.odoo_table_rows(key, offset=0, limit=1, filters=None, filterConj="and", + sorts=None, search=None, related=None, groupBy=None, + groupKey=None, session=session, body={"ids": [int(picking_id)]}) + return any(int(row.get("pid", -1)) == int(picking_id) for row in page.get("rows") or []) + + +def require_delivery_read(session, picking_id, key_allowed=None): + """Return the delivery database key that admits the caller, or raise 403. + + A 403 or 404 from one key falls through to the next. `key_allowed(key)` may raise 403 to + narrow the keys further, as an MCP credential's database scope does. + """ + import odoo_relational as rel + other = None + for key in rel.DELIVERY_KEYS: + try: + if key_allowed is not None: + key_allowed(key) + if _wall_admits(session, key, picking_id): + return key + except Exception as exc: # noqa: BLE001 + status = getattr(exc, "status_code", None) + if status in (403, 404): + continue + other = other or exc + if other is not None: + raise other + raise err(403, "delivery_read_forbidden", + "You cannot read this delivery, so ask an admin for access to Odoo shipments.") + + +def _photo(row, base=""): + return {"id": int(row["id"]), "name": str(row.get("name") or ""), + "mimetype": str(row.get("mimetype") or ""), "url": base + attachment_path(row["id"])} + + +def list_photos(session, picking_id, key_allowed=None, base=""): + """Every image attachment of one picking, oldest first, after the read wall passes.""" + require_delivery_read(session, picking_id, key_allowed) + rows = _search_read(session, ATTACHMENT_MODEL, [ + ("res_model", "=", PICKING_MODEL), ("res_id", "=", int(picking_id)), + ("type", "=", "binary"), ("mimetype", "in", list(IMAGE_TYPES)), + ], ["id", "name", "mimetype"], order="id asc") + return [_photo(row, base) for row in rows or []] + + +def read_photo(session, attachment_id): + """`(bytes, mimetype, name)` of one delivery photo. The bytes are read only after the wall.""" + meta = _search_read(session, ATTACHMENT_MODEL, [("id", "=", int(attachment_id))], _META_FIELDS) + row = (meta or [None])[0] + res_id = row.get("res_id") if row else None + if (not row or row.get("res_model") != PICKING_MODEL or row.get("type") != "binary" + or row.get("mimetype") not in IMAGE_TYPES or isinstance(res_id, bool) + or not isinstance(res_id, int) or res_id <= 0): + raise err(404, "photo_not_found", "This delivery photo was not found.") + require_delivery_read(session, res_id) + data = _search_read(session, ATTACHMENT_MODEL, [("id", "=", int(attachment_id))], ["datas"]) + raw = (data or [{}])[0].get("datas") + if not raw: + raise err(404, "photo_not_found", "This delivery photo has no image data.") + try: + body = base64.b64decode(raw, validate=False) + except (binascii.Error, ValueError, TypeError): + raise err(502, "photo_unreadable", "Odoo sent a photo that could not be read.") + return body, row["mimetype"], str(row.get("name") or f"photo-{int(attachment_id)}") diff --git a/api/odoo_relational.py b/api/odoo_relational.py index 0d1f4d1decf010763b9e424a639b48b77edb61ea..073793aa64cb9660a0c614e5d6bbe1efb9c34667 100644 --- a/api/odoo_relational.py +++ b/api/odoo_relational.py @@ -106,6 +106,9 @@ from odoo_purchasing import (ORDERS_KEY as PURCHASE_ORDERS_KEY, LINES_KEY as PUR source_specs as purchase_source_specs, validate as validate_purchasing) +#: S78-T14: the databases whose row id is a `stock.picking` id. Their read wall admits a delivery photo. +DELIVERY_KEYS = (SHIPMENTS_KEY, PICKINGS_KEY) + #: The join column the partner-grain tables carry. Derived links resolve through it (`on`/`from`). JOIN_KEY = "partner_id" #: The product-grain equivalent. diff --git a/api/routes_amazon_profit.py b/api/routes_amazon_profit.py new file mode 100644 index 0000000000000000000000000000000000000000..85de51d047e84355d73abace8b48dfef79b5e15c --- /dev/null +++ b/api/routes_amazon_profit.py @@ -0,0 +1,200 @@ +"""Amazon SKU profit doors: read the settings, switch the period, recompute, set the owner, +and confirm or reject a suggested Odoo match. + +The table itself (`ut_amazon_sku_profit`) is read and edited through the ordinary table doors. +A refresh rewrites only computed columns, so owner, status and due date survive a period switch. +""" + +from __future__ import annotations + +from typing import Any + +from fastapi import APIRouter, Body, Depends, HTTPException + +from deps import Session, err, require_session +from modules import amazon_profit + +router = APIRouter(prefix="/api/v1") + + +def _admin(session): + if not session.admin: + raise err(403, "forbidden", "Only an Administrator can refresh Amazon SKU profit.") + + +def _may_edit(session): + """Editors and Administrators of the table; a viewer reads only.""" + import core.user_tables as ut + return bool(session.admin) or ut.may_edit_database( + amazon_profit.TABLE_KEY, session.uname, is_admin=session.admin, st=session.runtime) + + +def _cost_reader(runtime): + """Odoo cost for linked references, bound to this tenant's own read-only connection.""" + def read(codes): + source = runtime.odoo_source() + if source is None: + return {}, [f"{code}: this workspace has no Odoo connection" for code in sorted(codes)] + try: + with source._bound(): + return amazon_profit.odoo_costs(codes) + except amazon_profit.SourceUnavailable: + raise + except Exception as exc: # noqa: BLE001 + raise amazon_profit.SourceUnavailable( + f"The Odoo cost read failed ({type(exc).__name__}). Try again.") from None + return read + + +def _mirror(runtime): + """A private read cursor on this tenant's mirror. Amazon readiness is its own table check, + not the Odoo backfill gate `ro_con` waits on, so this asserts the tenant and opens a cursor.""" + from harness import datastore as DS + from harness import runtime as R + from pathlib import Path + try: + runtime.assert_datastore_matches() + if not Path(runtime.datastore_path).is_file(): + raise err(503, "amazon_source_unavailable", "This workspace has no Amazon data yet.") + return DS.ro_cursor() + except R.DatastoreMismatch: + raise err(500, "store_mismatch", "This workspace is reading another workspace's data.") from None + except RuntimeError: + raise err(503, "store_not_ready", "Amazon data is still loading. Try again shortly.") from None + + +@router.get("/amazon-profit") +def profit_status(session: Session = Depends(require_session)): + """The current period, thin threshold, last refresh and owner setting, with the choices.""" + import core.user_tables as ut + settings = amazon_profit.read_settings(session.runtime) + return {"tableKey": amazon_profit.TABLE_KEY, + "tableExists": ut.get_one(amazon_profit.TABLE_KEY, session.runtime) is not None, + "period": amazon_profit.period_bounds(settings["period"]), + "thin": settings["thin"], + "refreshedAt": settings.get("refreshedAt"), + "skus": settings.get("skus"), + "linked": settings.get("linked"), + "verifiedMonth": settings.get("verifiedMonth"), + "refusal": settings.get("refusal"), + "defaultOwner": settings["defaultOwner"], + "periods": amazon_profit.period_choices(), + "statuses": list(amazon_profit.STATUSES), + "matchStates": list(amazon_profit.MATCH_STATES), + "matchedAt": settings.get("matchedAt"), + "matches": settings.get("matches"), + "matchError": settings.get("matchError"), + "dueDays": amazon_profit.DUE_DAYS, + "reopenDays": amazon_profit.REOPEN_DAYS, + "canEdit": _may_edit(session), + "canAdmin": bool(session.admin)} + + +def _recompute(session, period=None, thin=None): + """Run one refresh through the tenant's mirror and Odoo cost read; 409 on a failed tie.""" + try: + if period is not None: + amazon_profit.period_bounds(period) + if thin is not None: + amazon_profit.clean_thin(thin) + except ValueError as exc: + raise err(400, "invalid_setting", str(exc)) from None + con = _mirror(session.runtime) + try: + report = amazon_profit.refresh(session.runtime, con, period=period, thin=thin, + by=session.uname, cost_reader=_cost_reader(session.runtime), + tenant=session.tenant) + except amazon_profit.SourceUnavailable as exc: + raise err(503, "amazon_source_unavailable", str(exc)) from None + except amazon_profit.TableRefused as exc: + raise err(409, "table_refused", str(exc)) from None + except ValueError as exc: + raise err(400, "invalid_setting", str(exc)) from None + finally: + try: + con.close() + except Exception: # noqa: BLE001 + pass + if report.get("refusal"): + raise err(409, "not_verified", report["refusal"]) + return report + + +@router.post("/amazon-profit/refresh") +def profit_refresh(body: Any = Body(default=None), session: Session = Depends(require_session)): + """Recompute every SKU for the period (30d, 90d or YYYY-MM) and store the settings.""" + _admin(session) + body = body if isinstance(body, dict) else {} + return _recompute(session, body.get("period"), body.get("thin")) + + +@router.post("/amazon-profit/period") +def profit_period(body: Any = Body(default=None), session: Session = Depends(require_session)): + """Switch the table to another period and recompute every row; owned cells stay.""" + if not _may_edit(session): + raise err(403, "forbidden", "Only an editor of Amazon SKU profit can change its period.") + body = body if isinstance(body, dict) else {} + period = body.get("period") + if not period: + raise err(400, "invalid_setting", "Choose 30 days, 90 days or a calendar month.") + return _recompute(session, period) + + +@router.put("/amazon-profit/settings") +def profit_settings(body: Any = Body(default=None), session: Session = Depends(require_session)): + """Administrator settings: the default owner of new items and the thin margin.""" + if not session.admin: + raise err(403, "forbidden", "Only an Administrator can change Amazon SKU profit settings.") + body = body if isinstance(body, dict) else {} + try: + if "defaultOwner" in body: + amazon_profit.set_default_owner(session.runtime, body.get("defaultOwner"), + tenant=session.tenant, by=session.uname) + if "thin" in body: + amazon_profit.write_settings(session.runtime, + thin=amazon_profit.clean_thin(body.get("thin"))) + except ValueError as exc: + raise err(400, "invalid_setting", str(exc)) from None + settings = amazon_profit.read_settings(session.runtime) + return {"defaultOwner": settings["defaultOwner"], "thin": settings["thin"]} + + +@router.post("/amazon-profit/matches/{sku:path}/{decision}") +def profit_match(sku: str, decision: str, session: Session = Depends(require_session)): + """Confirm or reject the suggested Odoo product for one SKU; editors only. + + Confirm copies the suggestion into the link and recomputes the period, so COGS and profit + fill at once. Reject clears the suggestion, keeps the SKU unlinked and remembers the code.""" + import core.user_tables as ut + states = {"confirm": amazon_profit.CONFIRMED, "reject": amazon_profit.REJECTED} + if decision not in states: + raise err(404, "not_found", "Choose confirm or reject.") + if not _may_edit(session): + raise err(403, "forbidden", "Only an editor of Amazon SKU profit can confirm or reject a match.") + sku = str(sku or "").strip() + rows = [r for r in amazon_profit.table_rows(session.runtime).values() + if str(r.get("sku") or "").strip() == sku] + if ut.get_one(amazon_profit.TABLE_KEY, session.runtime) is None or not rows: + raise err(404, "not_found", "This SKU is not in Amazon SKU profit.") + row = rows[0] + if str(row.get("odoo_product") or "").strip(): + raise err(409, "already_linked", "This SKU already has an Odoo product.") + if not str(row.get("suggested_product") or "").strip(): + raise err(409, "no_suggestion", "This SKU has no suggested Odoo product.") + with amazon_profit._LOCK: + result = amazon_profit.apply_decisions(session.runtime, by=session.uname, sku=sku, + state=states[decision]) + out = {"sku": sku, "matchState": (result["row"] or {}).get("match_state"), + "odooProduct": (result["row"] or {}).get("odoo_product"), + "unitsPerSku": (result["row"] or {}).get("units_per_sku")} + if decision == "confirm": + try: + report = _recompute(session) + out["refresh"] = {k: report.get(k) for k in ("skus", "linked", "refreshedAt")} + except HTTPException as exc: + out["refreshError"] = ((exc.detail or {}).get("error") or {}).get("message") + current = next((r for r in amazon_profit.table_rows(session.runtime).values() + if str(r.get("sku") or "").strip() == sku), {}) + out.update(cogsPerUnit=current.get("cogs_per_unit"), netProfit=current.get("net_profit"), + margin=current.get("margin"), band=current.get("band")) + return out diff --git a/api/routes_competitor_watch.py b/api/routes_competitor_watch.py new file mode 100644 index 0000000000000000000000000000000000000000..c938861dfc304b5934f4fac26a71018e40a98f35 --- /dev/null +++ b/api/routes_competitor_watch.py @@ -0,0 +1,217 @@ +"""Competitor watch App routes: watched terms, term detail, settings and owned alerts.""" +from fastapi import APIRouter, Body, Depends, Query + +from deps import Session, err, require_session + +router = APIRouter(prefix="/api/v1/competitor-watch", tags=["competitor-watch"]) +SOURCE = "ut_amzn_brand_analytics" + + +def _module(): + from modules import competitor_watch + return competitor_watch + + +def _reader(session: Session = Depends(require_session)) -> Session: + """Reads need read access to the brand analytics database.""" + from core import perm_scope + if not perm_scope.may_read(session.user, SOURCE, st=session.runtime): + raise err(403, "competitor_watch_forbidden", "Your account cannot read Amazon brand analytics.") + return session + + +def _writer(session: Session = Depends(_reader)) -> Session: + """Writes also need a role that may edit.""" + if str(session.user.get("role") or "user").strip().lower() == "viewer": + raise err(403, "competitor_watch_read_only", "Your account can view the competitor watch but not change it.") + return session + + +def _tenant(session): + return str(getattr(session.runtime, "key", "") or session.tenant) + + +def _call(fn, *args, **kwargs): + try: + return fn(*args, **kwargs) + except ValueError as exc: + raise err(400, "competitor_watch_invalid", str(exc)) from exc + except KeyError as exc: + raise err(404, "competitor_watch_alert_not_found", "That alert does not exist.") from exc + except Exception as exc: # noqa: BLE001 + raise err(503, "competitor_watch_unavailable", "The competitor watch could not be read. Try again shortly.") from exc + + +# --------------------------------------------------------------------------- S78-T17 App binding + +TEMPLATE_KIND = "competitor_watch" + + +def _app(session, app_id): + """The Competitor watch App record this request names, readable by the caller (404 otherwise).""" + import application_registry + try: + app = application_registry.load(session.runtime)["applications"].get(str(app_id or "")) + except Exception as exc: # noqa: BLE001 + raise err(503, "competitor_watch_unavailable", "The App could not be read. Try again shortly.") from exc + if (not isinstance(app, dict) or app.get("templateKind") != TEMPLATE_KIND or app.get("retired") is True + or not application_registry.may_read(app, session.uname, is_admin=session.admin)): + raise err(404, "competitor_watch_app_not_found", "That Competitor watch App does not exist.") + return app + + +def _may_edit(session, app): + import application_registry + role = str(session.user.get("role") or "user").strip().lower() + return role != "viewer" and application_registry.may_edit(app, session.uname, is_admin=session.admin) + + +def _app_reader(app: str = Query(..., min_length=1), session: Session = Depends(_reader)) -> Session: + """C4: a read names the App, and the caller must be able to read that App.""" + _app(session, app) + return session + + +def _app_writer(app: str = Query(..., min_length=1), session: Session = Depends(_writer)) -> Session: + """C4: a write names the App, and only an editor of that App may change it.""" + if not _may_edit(session, _app(session, app)): + raise err(403, "competitor_watch_read_only", "You can view this App but not change it.") + return session + + +@router.get("/terms") +def list_terms(session: Session = Depends(_app_reader)): + cw = _module() + return _call(cw.terms_summary, _tenant(session), st=session.runtime) + + +@router.get("/terms/detail") +def term_detail(term: str = Query(..., min_length=1), session: Session = Depends(_app_reader)): + cw = _module() + return _call(cw.term_detail, _tenant(session), term, st=session.runtime) + + +@router.post("/terms") +def add_terms(terms: list[str] = Body(..., embed=True), session: Session = Depends(_app_writer)): + cw = _module() + return {"terms": _call(cw.add_terms, _tenant(session), terms, st=session.runtime)} + + +@router.post("/terms/remove") +def remove_terms(terms: list[str] = Body(..., embed=True), session: Session = Depends(_app_writer)): + cw = _module() + return {"terms": _call(cw.remove_terms, _tenant(session), terms, st=session.runtime)} + + +@router.get("/settings") +def read_settings(session: Session = Depends(_app_reader)): + cw = _module() + return _call(cw.settings, _tenant(session), st=session.runtime) + + +@router.patch("/settings") +def write_settings(undercutPct: float | None = Body(None), rankSurgePct: float | None = Body(None), + defaultOwner: str | None = Body(None), session: Session = Depends(_app_writer)): + cw = _module() + return _call(cw.set_settings, _tenant(session), undercut_pct=undercutPct, rank_surge_pct=rankSurgePct, + default_owner=defaultOwner, st=session.runtime) + + +@router.get("/alerts") +def list_alerts(app: str = Query(..., min_length=1), status: str | None = Query(None), + session: Session = Depends(_app_reader)): + cw = _module() + listed = _call(cw.alerts, _tenant(session), status=status, st=session.runtime) + return {**listed, "canEdit": _may_edit(session, _app(session, app))} + + +@router.patch("/alerts/{alert_id}") +def update_alert(alert_id: str, owner: str | None = Body(None), status: str | None = Body(None), + due: str | None = Body(None), session: Session = Depends(_app_writer)): + cw = _module() + return _call(cw.update_alert, _tenant(session), alert_id, owner=owner, status=status, due=due, + user=session.uname or "unknown", st=session.runtime) + + +def _week_end(start): + import datetime as dt + try: + return (dt.date.fromisoformat(str(start)[:10]) + dt.timedelta(days=6)).isoformat() + except ValueError: + return str(start) + + +def _not_ready_message(cw, periods): + """The plain sentence naming when comparison starts (one week held, or none).""" + import datetime as dt + if len(periods) == 1: + try: + due = dt.date.fromisoformat(str(periods[0])[:10]) + dt.timedelta(days=14) + return f"Comparison starts after the next weekly pull, about {due.strftime('%b')} {due.day}." + except ValueError: + pass + return cw.NOT_READY + + +def screen_payload(cw, detail, terms, term, can_edit): + """Project `term_detail` to the screen: shares in percent points, previous values, causes, trend.""" + periods = list(detail.get("periods") or ()) + top3 = list(detail.get("top3") or ()) + shares = [abs(v) for row in top3 for v in (row.get("clickShare"), row.get("conversionShare")) + if isinstance(v, (int, float))] + shares += [abs(v) for row in detail.get("trend") or () for v in row.get("clickShare") or () + if isinstance(v, (int, float))] + # Unknown: whether Amazon sends shares as fractions or percent. All values at or under 1 read as fractions. + scale = 100.0 if shares and max(shares) <= 1 else 1.0 + + def pts(value): + return None if not isinstance(value, (int, float)) else round(value * scale, 4) + + def previous(value, delta): + return None if value is None or not isinstance(delta, (int, float)) else round(value - delta * scale, 4) + rows = [] + for row in top3: + change = row.get("change") or {} + click, conv = pts(row.get("clickShare")), pts(row.get("conversionShare")) + causes = [str(row[key]) for key in ("priceCause", "rankCause") if row.get(key)] + rows.append({"rank": row.get("rank"), "asin": row.get("asin"), "ours": row.get("isOurs") is True, + "clickShare": click, "conversionShare": conv, + "previousRank": change.get("previousRank"), + "previousClickShare": previous(click, change.get("clickShareDelta")), + "previousConversionShare": previous(conv, change.get("conversionShareDelta")), + "cause": " ".join(causes) or None}) + # One line per ASIN in the newest top 3, plus our ASIN when it fell out (at most four lines). + shown = [row["asin"] for row in rows] + ours = [row for row in detail.get("trend") or () if row.get("isOurs") and row.get("asin") not in shown] + wanted = shown + [row["asin"] for row in ours[:1]] + by_asin = {row.get("asin"): row for row in detail.get("trend") or ()} + trend = [] + for asin in wanted: + row = by_asin.get(asin) or {} + values = list(row.get("clickShare") or ()) + points = [{"start": period, "clickShare": pts(values[i] if i < len(values) else None)} + for i, period in enumerate(periods)] + trend.append({"asin": asin, "ours": row.get("isOurs") is True, + "points": sorted(points, key=lambda p: p["start"])}) + comparison = detail.get("comparison") or {} + ready = comparison.get("ready") is True + return {"canEdit": bool(can_edit), "terms": list(terms), "term": term, + "week": {"start": periods[0], "end": _week_end(periods[0])} if periods else None, + "previousWeek": {"start": periods[1], "end": _week_end(periods[1])} if len(periods) > 1 else None, + "comparisonReady": ready, + "comparisonMessage": "" if ready else _not_ready_message(cw, periods), + "top3": rows, "trend": trend, "problems": []} + + +@router.get("/screen") +def app_screen(app: str = Query(..., min_length=1), term: str | None = Query(None), + session: Session = Depends(_app_reader)): + """S78-T17: the Terms view in one read (watched terms, the picked term's top 3 and trend).""" + cw = _module() + tenant = _tenant(session) + record = _app(session, app) + terms = [row["term"] for row in _call(cw.terms_summary, tenant, st=session.runtime)["terms"]] + picked = " ".join(str(term or "").split()).lower() + picked = picked if picked in terms else (terms[0] if terms else None) + detail = _call(cw.term_detail, tenant, picked, st=session.runtime) if picked else {} + return screen_payload(cw, detail, terms, picked, _may_edit(session, record)) diff --git a/api/routes_customers.py b/api/routes_customers.py index 7f0bbc80f9985f6706abb1673f8961a50bb7ea60..0a80909d64aaaecdf94e10bc7f6b5787874a1a7f 100644 --- a/api/routes_customers.py +++ b/api/routes_customers.py @@ -1374,83 +1374,6 @@ def _ensure_route_migration(session): _ROUTE_MIGRATED_TENANTS.add(tenant) -#: (tenant, user, Routing App) triples with no no-App Route left to adopt in this process. -_ROUTE_ADOPTED_USERS: set = set() - - -def _home_route_application(session): - """The id of the caller's own Routing App, or None.""" - import application_registry as registry - me = str(getattr(session, "uname", "") or "").strip().lower() - if not me: - return None - try: - doc = registry._read_strict(session.runtime, registry._KEY) or {} - apps = doc.get("applications", {}) if isinstance(doc, dict) else None - if not isinstance(apps, dict): - return None - candidates = [] - for app_id, app in apps.items(): - if (not isinstance(app, dict) or app.get("retired") is True - or str(app.get("createdBy") or "").strip().lower() != me - or app.get("tenant") != session.tenant - or not (app.get("templateKind") == "route" - or (app.get("type") == "route" and not app.get("templateKind"))) - or not registry.may_read(app, session.uname, is_admin=session.admin)): - continue - source = app.get("sourceDatabase") - if source not in (MODULE, "ut_odoo_customers") and MODULE not in (app.get("compatibleDatabases") or ()): - continue - starred = me in {str(user or "").strip().lower() for user in app.get("starredBy") or ()} - candidates.append((not starred, str(app.get("createdAt") or ""), str(app_id))) - except Exception: - # An unreadable registry falls back to the no-App partition, which still works. - return None - return min(candidates)[2] if candidates else None - - -def _adopt_unscoped_routes(session, application): - """Move the caller's post-R20 no-App Routes into their own Routing App, once per process.""" - from core import route_system - key = (str(getattr(session, "tenant", "") or ""), str(session.uname or "").strip().lower(), application) - if key in _ROUTE_ADOPTED_USERS: - return - try: - route_system.adopt_unscoped_routes(session.runtime, owner=session.uname, application=application) - except Exception: - # A failed adoption leaves the Routes where they are and retries on the next request. - return - _ROUTE_ADOPTED_USERS.add(key) - - -def _entry_route_application(session, application, *, write=False): - """A Route request that names no Routing App uses the caller's own one, when they have one.""" - if application is not None: - return _route_application(session, application, write=write) - home = _home_route_application(session) - if home is None: - return _route_application(session, None, write=write) - resolved = _route_application(session, home, write=write) - _adopt_unscoped_routes(session, resolved) - return resolved - - -def _entry_route_zone(session, raw, requested, application): - """A no-App request may name a legacy Zone id. Map it to that Zone's R20 copy in the home App.""" - from core import route_system - key = "zoneId" if "zoneId" in raw else "zone_id" if "zone_id" in raw else None - wanted = str(raw.get(key) or "").strip() if key else "" - if requested is not None or application is None or not wanted: - return raw - if wanted in route_system.zone_ids(session.runtime, table_key=route_system.DEFAULT_ZONE_TABLE, - application=application): - return raw - receipt = route_system.read(session.runtime).get(route_system.APPLICATION_MIGRATION) or {} - zones = ((receipt.get("applications") or {}).get(application) or {}).get("zones") or {} - mapped = zones.get(f"{route_system.DEFAULT_ZONE_TABLE}:{wanted}") - return dict(raw, **{key: mapped}) if mapped else raw - - def _route_application_body(raw, application): if "application" in raw and raw["application"] != application: raise err(400, "route_application_mismatch", "the body must name the same Routing App as the query") @@ -1736,7 +1659,7 @@ def routes(session: Session = Depends(module_gate(MODULE)), application: str = N 50,000 visible records sends exactly the same request as an empty grid; a future annotation door may accept a compact saved-View/filter reference without reopening that row-sized wire. """ - application = _entry_route_application(session, application) + application = _route_application(session, application) allowed = allowed_pids(session) records = _route_share_records(session) navigation = _route_navigation(session, _visible_routes(session, records, application), application) @@ -1749,7 +1672,7 @@ def routes(session: Session = Depends(module_gate(MODULE)), application: str = N @router.get("/customers/routes/delivery-recipients") def route_delivery_recipients(session: Session = Depends(module_gate(MODULE)), application: str = None): - _entry_route_application(session, application) + _route_application(session, application) return {"people": _route_delivery_people(session)} @@ -1764,9 +1687,8 @@ def create_route(body: dict = Body(default=None), session: Session = Depends(mod application: str = None): from core import route_system raw = dict(body or {}) - requested, application = application, _entry_route_application(session, application, write=True) + application = _route_application(session, application, write=True) _route_application_body(raw, application) - raw = _entry_route_zone(session, raw, requested, application) _reject_client_create_id(raw, "Route") source_view = raw.get("sourceViewId", raw.get("source_view_id")) if source_view and not _route_view_can_edit(session, source_view): @@ -1787,7 +1709,7 @@ def create_route(body: dict = Body(default=None), session: Session = Depends(mod def duplicate_route(route_id: str, body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): from core import route_system - application = _entry_route_application(session, application, write=True) + application = _route_application(session, application, write=True) _route_application_body(body or {}, application) prior = _route_or_404(session, route_id, application) if prior.get("sourceViewId"): @@ -1806,7 +1728,6 @@ def duplicate_route(route_id: str, body: dict = Body(default=None), @router.post("/customers/routes/{route_id}/share-ready") def route_share_ready(route_id: str, session: Session = Depends(module_gate(MODULE)), application: str = None): - application = _entry_route_application(session, application) route = _route_or_404(session, route_id, application) _route_mutation_allowed(session, route) key = _ensure_route_share_bridge(session, route) @@ -1817,11 +1738,9 @@ def route_share_ready(route_id: str, session: Session = Depends(module_gate(MODU def update_route(route_id: str, body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): from core import route_system - requested, application = application, _entry_route_application(session, application, write=True) prior = _route_or_404(session, route_id, application) raw = dict(body or {}) _route_application_body(raw, application) - raw = _entry_route_zone(session, raw, requested, application) _route_mutation_allowed(session, prior, raw.get("sourceViewId", prior.get("sourceViewId"))) raw["id"] = prior["id"] if "stops" in raw and any(pid not in allowed_pids(session) for pid in _route_input_pids(raw)): @@ -1839,7 +1758,6 @@ def update_route(route_id: str, body: dict = Body(default=None), def move_route_stop(route_id: str, body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): from core import route_system - application = _entry_route_application(session, application, write=True) prior = _route_or_404(session, route_id, application) _route_mutation_allowed(session, prior) raw = dict(body or {}) @@ -1864,7 +1782,6 @@ def set_route_stop_membership(route_id: str, body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): """Add or remove one permitted customer while preserving every other ordered stop.""" from core import route_system - application = _entry_route_application(session, application, write=True) prior = _route_or_404(session, route_id, application) _route_mutation_allowed(session, prior) raw = dict(body or {}) @@ -1893,7 +1810,6 @@ def set_route_stop_membership(route_id: str, body: dict = Body(default=None), @router.delete("/customers/routes/{route_id}") def remove_route(route_id: str, session: Session = Depends(module_gate(MODULE)), application: str = None): from core import route_system - application = _entry_route_application(session, application, write=True) route = _route_or_404(session, route_id, application) _route_mutation_allowed(session, route) linked_delivery_ids = _linked_route_delivery_ids(session.runtime, route_id) @@ -1919,7 +1835,6 @@ def remove_route(route_id: str, session: Session = Depends(module_gate(MODULE)), def move_route_to_folder(route_id: str, body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): from core import route_system - application = _entry_route_application(session, application, write=True) _route_application_body(body or {}, application) routes, legacy_folders, legacy_placements = _route_nav_mutation_context(session, **_route_scope(application)) if str(route_id) not in {str(route.get("id")) for route in routes}: @@ -1942,7 +1857,6 @@ def move_route_to_folder(route_id: str, body: dict = Body(default=None), def reorder_route_navigation(body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): from core import route_system - application = _entry_route_application(session, application, write=True) _route_application_body(body or {}, application) routes, legacy_folders, legacy_placements = _route_nav_mutation_context(session, **_route_scope(application)) order = (body or {}).get("order") @@ -1960,7 +1874,6 @@ def reorder_route_navigation(body: dict = Body(default=None), def create_route_folder(body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): from core import route_system - application = _entry_route_application(session, application, write=True) _route_application_body(body or {}, application) _reject_client_create_id(body or {}, "Route folder") routes, legacy_folders, legacy_placements = _route_nav_mutation_context(session, **_route_scope(application)) @@ -1976,7 +1889,6 @@ def create_route_folder(body: dict = Body(default=None), def update_route_folder(folder_id: str, body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): from core import route_system - application = _entry_route_application(session, application, write=True) _route_application_body(body or {}, application) routes, legacy_folders, legacy_placements = _route_nav_mutation_context(session, **_route_scope(application)) current = next((folder for folder in _route_navigation(session, routes, **_route_scope(application))["folders"] @@ -1997,7 +1909,6 @@ def update_route_folder(folder_id: str, body: dict = Body(default=None), @router.delete("/customers/route-folders/{folder_id}") def remove_route_folder(folder_id: str, session: Session = Depends(module_gate(MODULE)), application: str = None): from core import route_system - application = _entry_route_application(session, application, write=True) routes, legacy_folders, legacy_placements = _route_nav_mutation_context(session, **_route_scope(application)) if not route_system.delete_route_folder( session.runtime, session.uname, folder_id, routes, @@ -2010,7 +1921,6 @@ def remove_route_folder(folder_id: str, session: Session = Depends(module_gate(M def reorder_route_folder_navigation(body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): from core import route_system - application = _entry_route_application(session, application, write=True) _route_application_body(body or {}, application) routes, legacy_folders, legacy_placements = _route_nav_mutation_context(session, **_route_scope(application)) order = (body or {}).get("order") @@ -2030,7 +1940,7 @@ def route_selection(body: dict = Body(default=None), session: Session = Depends( """Dedicated managed-OR mutation; user filter clauses are returned byte-for-byte intact.""" from core import route_system raw = dict(body or {}) - application = _entry_route_application(session, application) + application = _route_application(session, application) _route_application_body(raw, application) chosen = [str(item) for item in raw.get("routeIds") or ()] compatible = _visible_routes(session, application=application) @@ -2280,8 +2190,6 @@ def patch_customer(pid: int, body: dict = Body(default=None), # refused key or a truncated value must not be reported as accepted. stored = (grid_events.table_workspace(_ctx_for(session, pool), allowed_pids=None) .get("overlays") or {}).get(str(pid)) or {} - # A tenant-wide column saves to the shared stratum, so read it back from there too. - stored = {**stored, **(shared_cells([pid], st=session.runtime).get(str(pid)) or {})} accepted = {k: stored.get(k) for k in updates if k in stored} refused = sorted(k for k in updates if k not in accepted or stored.get(k) != str(updates[k])) # No cache to patch: the overlay stratum is re-read from the store on every `_payload`, so @@ -2538,7 +2446,6 @@ def _linked_route_delivery_ids(runtime, route_id): @router.get("/customers/route-deliveries") def route_delivery_list(session: Session = Depends(module_gate(MODULE)), application: str = None): """Saved C2 route deliveries owned by the caller (or all for an administrator).""" - application = _entry_route_application(session, application) return {"deliveries": _route_delivery_definitions(session, application=application)} @@ -2546,7 +2453,6 @@ def route_delivery_list(session: Session = Depends(module_gate(MODULE)), applica def normalized_route_delivery_list(route_id: str, session: Session = Depends(module_gate(MODULE)), application: str = None): """Deliveries for the one normalized Route whose detail is open.""" - application = _entry_route_application(session, application) _route_or_404(session, route_id, application) return {"deliveries": _route_delivery_definitions(session, route_id, application)} @@ -2586,7 +2492,6 @@ def _create_route_delivery_for_route(session, route, body): def normalized_route_delivery_create(route_id: str, body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): """Schedule a named normalized Route from its server-resolved View and stop relation.""" - application = _entry_route_application(session, application, write=True) _route_application_body(body or {}, application) return _create_route_delivery_for_route( session, _route_or_404(session, route_id, application), body if isinstance(body, dict) else {}) @@ -2597,7 +2502,6 @@ def route_delivery_create(body: dict = Body(default=None), session: Session = Depends(module_gate(MODULE)), application: str = None): """Compatibility door: a normalized Route id is still mandatory and server-resolved.""" body = body if isinstance(body, dict) else {} - application = _entry_route_application(session, application, write=True) _route_application_body(body, application) route_id = str(body.get("routeId") or "").strip() if not route_id: @@ -2634,7 +2538,6 @@ def _delete_route_delivery(session, delivery_id, route_id=None, application=None def normalized_route_delivery_delete(route_id: str, delivery_id: str, session: Session = Depends(module_gate(MODULE)), application: str = None): """Remove one delivery from the normalized Route detail that owns it.""" - application = _entry_route_application(session, application, write=True) route = _route_or_404(session, route_id, application) _route_mutation_allowed(session, route) return _delete_route_delivery(session, delivery_id, route_id, application) @@ -2644,5 +2547,4 @@ def normalized_route_delivery_delete(route_id: str, delivery_id: str, def route_delivery_delete(delivery_id: str, session: Session = Depends(module_gate(MODULE)), application: str = None): """Compatibility removal for existing schedule links.""" - application = _entry_route_application(session, application, write=True) return _delete_route_delivery(session, delivery_id, application=application) diff --git a/api/routes_external.py b/api/routes_external.py index 5dd132c98058d51b078f39b0b9e5be298dc17188..fbef8ff941271e8ad6f07682bf7a8fcff5206872 100644 --- a/api/routes_external.py +++ b/api/routes_external.py @@ -79,7 +79,13 @@ def _json_digest(value) -> str: return hashlib.sha256(encoded.encode("utf-8")).hexdigest() -def _canonical_json(value) -> bytes: +def _fits_token(value, raw) -> bool: + from tool_groups.apps import code_transport_fits + return len(raw) <= _CONFIRMATION_MAX_BYTES or code_transport_fits(value, _CONFIRMATION_MAX_BYTES) + + +def _canonical_json(value, *, capped=True) -> bytes: + """Encode a value canonically. The byte cap binds a signed token only, never a write (rule 1).""" try: encoded = json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=True, allow_nan=False) @@ -87,8 +93,7 @@ def _canonical_json(value) -> bytes: raise err(400, "tool_arguments_invalid", "The tool arguments contain a value that cannot be represented safely.") from exc raw = encoded.encode("utf-8") - from tool_groups.apps import code_transport_fits - if len(raw) > _CONFIRMATION_MAX_BYTES and not code_transport_fits(value, _CONFIRMATION_MAX_BYTES): + if capped and not _fits_token(value, raw): raise err(413, "confirmation_too_large", "This change is too large for one preview. Split it into smaller changes.") if _contains_credential(encoded): @@ -344,7 +349,9 @@ class _ConfirmationRuntime: def _diff_documents(documents, *, limit=250): + """List changed leaves. limit=None lists every change.""" changes = [] + limit = float("inf") if limit is None else limit def walk(before, after, path): if len(changes) >= limit or before == after: @@ -372,6 +379,70 @@ def _diff_documents(documents, *, limit=250): return changes, len(changes) >= limit +_PREVIEW_DIFF_LIMIT = 250 +_PREVIEW_SAMPLE_CHANGES = 20 +_PREVIEW_SAMPLE_ROW_BYTES = 4096 +_CELL_ADDRESS = re.compile(r"([A-Z]{1,3})([0-9]{1,7})") + + +def _column_number(letters): + number = 0 + for letter in letters: + number = number * 26 + ord(letter) - 64 + return number + + +def _column_letters(number): + letters = "" + while number: + number, rest = divmod(number - 1, 26) + letters = chr(65 + rest) + letters + return letters + + +def _summarise_diff(diff, documents): + """Return the diff a preview shows and its summary. A diff that fits shows whole, with no summary.""" + if (len(diff) < _PREVIEW_DIFF_LIMIT + and len(_canonical_json(diff, capped=False)) <= _CONFIRMATION_MAX_BYTES): + return diff, None + cells, cell_rows, other_rows = {}, [], [] + for row in diff: + parts = [part.replace("~1", "/").replace("~0", "~") for part in row["path"][1:].split("/")] + if not (len(parts) >= 6 and parts[0] == "workbooks" and parts[2] == "sheets" and parts[4] == "cells"): + other_rows.append(row) + continue + cell_rows.append(row) + kind = "changed" + if len(parts) == 6 and row["before"] is None: + kind = "added" + elif len(parts) == 6 and row["after"] is None: + kind = "removed" + tab = cells.setdefault((parts[1], parts[3]), {}) + if tab.get(parts[5]) in (None, "changed"): + tab[parts[5]] = kind + counts = {"added": 0, "changed": 0, "removed": 0} + ranges = [] + for (workbook_id, index), addresses in sorted(cells.items()): + for kind in addresses.values(): + counts[kind] += 1 + pair = documents.get("workbooks/" + workbook_id) or {} + sheets_list = ((pair.get("after") or pair.get("before") or {}).get("sheets") or []) + sheet = sheets_list[int(index)] if index.isdigit() and int(index) < len(sheets_list) else {} + found = [_CELL_ADDRESS.fullmatch(address) for address in addresses] + found = [(_column_number(match[1]), int(match[2])) for match in found if match] + span = (f"{_column_letters(min(c for c, _ in found))}{min(r for _, r in found)}:" + f"{_column_letters(max(c for c, _ in found))}{max(r for _, r in found)}") if found else "" + ranges.append({"workbook_id": workbook_id, "tab": (sheet or {}).get("name") or (sheet or {}).get("id"), + "tab_id": (sheet or {}).get("id"), "range": span, "cells": len(addresses)}) + sample = [] + for row in (cell_rows + other_rows)[:_PREVIEW_SAMPLE_CHANGES]: + size = len(_canonical_json(row, capped=False)) + sample.append(row if size <= _PREVIEW_SAMPLE_ROW_BYTES + else {"path": row["path"], "omitted": True, "bytes": size}) + return sample, {"changes": len(diff), "cells": {**counts, "total": sum(counts.values())}, + "other_changes": len(other_rows), "ranges": ranges, "sample": len(sample), "complete": False} + + def _database_preview_documents(session, before, after): """Project changed records and fields through the caller's current database permissions.""" import routes_tables @@ -468,13 +539,29 @@ def _credential_binding(session: Session) -> str: return str(claims.get("external_credential_id") or claims.get("external_key_id") or "") +def _is_save_code(payload): + return payload.get("tool") == "apps" and payload.get("arguments", {}).get("action") == "save_code" + + +def _arguments_outside_token(payload): + """True when a payload's arguments are too large for a token, so v3 applies.""" + return not _is_save_code(payload) and not _fits_token(payload, _canonical_json(payload, capped=False)) + + +def _token_wire(payload): + """Return what the token signs. Arguments too large for a token stay in the stored operation.""" + if _is_save_code(payload): + return {"v": 2, "operation_id": payload["operation_id"], + "payload_digest": _json_digest(payload), + **{key: payload[key] for key in ("tenant", "actor", "credential", "authentication")}} + if not _arguments_outside_token(payload): + return copy.deepcopy(payload) + # v3 signs every field but the arguments. arguments_digest binds them to the stored operation. + return {**{key: copy.deepcopy(value) for key, value in payload.items() if key != "arguments"}, "v": 3} + + def _confirmation_token(payload) -> str: - wire_payload = copy.deepcopy(payload) - if payload.get("tool") == "apps" and payload.get("arguments", {}).get("action") == "save_code": - wire_payload = {"v": 2, "operation_id": payload["operation_id"], - "payload_digest": _json_digest(payload), - **{key: payload[key] for key in ("tenant", "actor", "credential", "authentication")}} - raw = _canonical_json(wire_payload) + raw = _canonical_json(_token_wire(payload)) signature = aios_session._sign(b"external-confirm:v1:" + raw) return f"{_CONFIRMATION_PREFIX}.{_b64(raw)}.{signature}" @@ -491,8 +578,20 @@ def _read_confirmation_token(token, session=None) -> dict: payload = json.loads(raw.decode("utf-8")) except (ValueError, TypeError, UnicodeError, json.JSONDecodeError) as exc: raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.") from exc - if not isinstance(payload, dict) or payload.get("v") not in (1, 2): + if not isinstance(payload, dict) or payload.get("v") not in (1, 2, 3): raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.") + if payload["v"] == 3: + if ("arguments" in payload or not isinstance(payload.get("operation_id"), str) + or not re.fullmatch(r"[a-f0-9]{64}", payload["operation_id"]) + or not isinstance(payload.get("arguments_digest"), str)): + raise err(400, "confirmation_token_invalid", "The confirmation token is invalid.") + if session is None: + raise err(400, "confirmation_session_required", "Read this confirmation through its authenticated connection.") + if any(payload.get(key) != value for key, value in _operation_bindings(session).items()): + raise err(403, "confirmation_principal_changed", "This confirmation belongs to another account or connection.") + _, item = _load_operation(session, payload["operation_id"], confirming=True) + # The stored arguments return unchecked here; confirm_change refuses them as stale unless their digest matches. + return {**payload, "v": 1, "arguments": copy.deepcopy(item["payload"].get("arguments"))} if payload["v"] == 2: if (set(payload) != {"v", "operation_id", "payload_digest", "tenant", "actor", "credential", "authentication"} or not isinstance(payload.get("operation_id"), str) @@ -1045,7 +1144,12 @@ def _assert_write_scope(session, tool_name, arguments=None): _, args = tool_groups.resolve(tool_name, arguments) _assert_workbook_scope(session, args["workbook_id"]) elif not external_access.may_write(access): - raise err(403, "key_read_only", f"{tool_name} writes, and this credential is read only.") + import tool_groups + # A group tool names the fix. A legacy tool keeps its published sentence. + fix = (", so connect with a Read and write key and call again" + if isinstance(tool_name, str) and tool_groups.canonical_name(tool_name) in tool_groups.groups() + else "") + raise err(403, "key_read_only", f"{tool_name} writes, and this credential is read only{fix}.") code = _test_environment_refusal(session) if code: raise err(403, code, MCP_RESULT_CODES[code]) @@ -3511,7 +3615,7 @@ def _execute_database_object(session, args, *, plan=None): flush="sync", expect_rev=expected_rev) except (view_audit.ViewRevisionStale, view_audit.RevisionUnverifiable) as exc: raise err(409, "confirmation_stale", - "The database changed after preview. Preview the change again.") from exc + "The database changed after the preview, " + _STALE_FIX) from exc return {"database": database, **copy.deepcopy(plan.get("planned") or {}), "affected": copy.deepcopy(plan.get("affected") or {})} @@ -3959,7 +4063,24 @@ def _build_purpose(value): return wiki_log.purpose_line(value) +def _build_about_normalised(value): + """Trim and lower-case tags, collapse repeats in first-seen order, and rewrite dashes in title and description.""" + if not isinstance(value, dict): + return value + value = dict(value) + for key in ("title", "description"): + if isinstance(value.get(key), str): + # A dash at either end has no clause to join, so it goes before the rewrite. + trimmed = value[key].strip().strip(dash_norm._DASH_CHARS).strip() + value[key] = dash_norm.plain(trimmed).strip() if trimmed else trimmed + if isinstance(value.get("tags"), list): + tags = [tag.strip().lower() if isinstance(tag, str) else tag for tag in value["tags"]] + value["tags"] = list(dict.fromkeys(tags)) if all(isinstance(tag, str) for tag in tags) else tags + return value + + def _build_about(value): + value = _build_about_normalised(value) missing = ["about." + key for key in ("title", "description", "tags") if not isinstance(value, dict) or key not in value] example = {"about": {"title": "Weekly sales review", "description": "Review weekly sales before the team meeting.", @@ -4000,7 +4121,9 @@ def _build_about(value): else "[unknown]") for key in value if key not in rules] invalid = invalid or ["about"] details = {"missing": missing, "invalid": invalid, "example": example} - message = ("Send about with title, description and tags." if missing + message = ("Send about with title, description and tags." if value is None + else "Send about as an object with title, description and tags." if not isinstance(value, dict) + else "Add " + ", ".join(missing) + " to about." if missing else "Fix " + ", ".join(invalid) + ".") if not isinstance(value, dict) or "about.tags" in invalid or "about.tags" in missing: details["allowed"] = tags @@ -4073,7 +4196,7 @@ def _preview_external_write(session, name, arguments, *, issue_token=True, execu about = _build_about(about) build_kind = _external_build_kind(name, arguments) environment = _write_environment() - normalised = json.loads(_canonical_json(arguments).decode("utf-8")) + normalised = json.loads(_canonical_json(arguments, capped=False).decode("utf-8")) _assert_tool_database_scope(session, name, normalised) preview_runtime = _PreviewRuntime(session.runtime) @@ -4105,9 +4228,9 @@ def _preview_external_write(session, name, arguments, *, issue_token=True, execu projected = _request_preview_documents(session, normalised["arguments"]["id"], documents) else: projected = _public_preview_documents(session, documents) - diff, truncated = _diff_documents(projected) - warnings = (["The preview diff reached its display cap; split this change."] - if truncated else []) + # The whole diff is computed and digested. Only its display is summarised (rule 1). + diff, _ = _diff_documents(projected, limit=None) + warnings = [] affected = _affected_for(name, normalised, diff, result) current_revision = { key: revision for key, revision in preview_runtime.base_revisions().items() @@ -4127,8 +4250,15 @@ def _preview_external_write(session, name, arguments, *, issue_token=True, execu {"user": row["user"], "role": row["role"]} for row in result["current_entries"]] - _canonical_json(diff) + _canonical_json(diff, capped=False) diff_digest = _json_digest(diff) + shown_diff, diff_summary = (diff, None) if plan is not None else _summarise_diff(diff, projected) + if diff_summary is not None: + warnings.append(f"The preview shows {diff_summary['sample']} of {diff_summary['changes']} changes. " + "confirm_change applies all of them.") + if len(affected.get("ranges") or []) > _PREVIEW_SAMPLE_CHANGES: + affected = {**affected, "ranges": [ + {key: row[key] for key in ("workbook_id", "tab", "range")} for row in diff_summary["ranges"]]} revision_digest = plan["source_fingerprint"] if remote_note else _json_digest(current_revision) expires_at = int(time.time()) + _CONFIRMATION_TTL_SECONDS payload = { @@ -4158,13 +4288,15 @@ def _preview_external_write(session, name, arguments, *, issue_token=True, execu "operation_id": payload["operation_id"], "state": "not_committed", "preview": True, "mutated": False, "tool": name, "environment": environment, - "diff": diff, "diff_digest": diff_digest, + "diff": shown_diff, "diff_digest": diff_digest, "affected": affected, "warnings": warnings, "current_revision": current_revision, "expires_at": expires_at, "requires_confirmation": True, "strong_confirmation_required": payload["strong"], "about": copy.deepcopy(about), "buildKind": build_kind, } + if diff_summary is not None: + response["diff_summary"] = diff_summary if purpose is not None: response["purpose"] = purpose if remote_note: @@ -4197,15 +4329,49 @@ def _refresh_authority(session): session.claims.get("external_workbooks", [])]) -def _prune_operations(raw): +def _prune_operations(raw, removed=None): + """Drop expired claims. The argument documents of dropped claims are added to removed.""" doc = raw if isinstance(raw, dict) else {} now = time.time() - doc["claims"] = {key: value for key, value in (doc.get("claims") or {}).items() - if isinstance(value, dict) and - float(value.get("retain_until", value.get("expiry", 0))) >= now} + kept = {} + for key, value in (doc.get("claims") or {}).items(): + if isinstance(value, dict) and float(value.get("retain_until", value.get("expiry", 0))) >= now: + kept[key] = value + elif removed is not None and isinstance(value, dict) and isinstance(value.get("arguments_ref"), dict): + removed.append(value["arguments_ref"].get("key")) + doc["claims"] = kept return doc +_OPERATION_ARGUMENTS_PREFIX = "external_confirmation_arguments_" +_OPERATION_ARGUMENT_STUB_BYTES = 4096 + + +def _drop_operation_documents(session, keys): + """Remove argument documents, best effort. Call it after the bucket update, never inside it.""" + for key in keys: + if not isinstance(key, str) or not key.startswith(_OPERATION_ARGUMENTS_PREFIX): + continue + try: + session.runtime.delete_document(key) + except Exception: + try: + session.runtime.put(key, {}) + except Exception: + pass + + +def _holds_arguments_apart(payload): + """True when a stored operation keeps its arguments in their own document.""" + return _arguments_outside_token(payload) + + +def _receipt_arguments(arguments): + """Keep the small top-level arguments a receipt check reads after the full document is removed.""" + return {key: copy.deepcopy(value) for key, value in arguments.items() + if len(_canonical_json(value, capped=False)) <= _OPERATION_ARGUMENT_STUB_BYTES} + + def _store_operation(session, payload, preview, staged, intent=None): """Keep the preview identity outside every affected object's revision.""" operation_id = payload["operation_id"] @@ -4235,17 +4401,38 @@ def _store_operation(session, payload, preview, staged, intent=None): if isinstance(intent, str): # Intent stays outside the signed payload, redacted, so confirm_change can reuse it. item["intent"] = audit.redact_intent(intent) + stored_arguments = copy.deepcopy(payload["arguments"]) + if payload["tool"] == "apps": + from tool_groups.apps import pending_arguments + stored_arguments = pending_arguments(payload["arguments"], encode=True) + ref = None + if _holds_arguments_apart(payload): + # Large arguments get their own document, so every other preview keeps a small bucket. + ref = {"key": _OPERATION_ARGUMENTS_PREFIX + operation_id, "digest": _json_digest(stored_arguments)} + session.runtime.put(ref["key"], {"operation_id": operation_id, "arguments": stored_arguments}) + item["arguments_ref"] = ref + item["receipt_arguments"] = _receipt_arguments(stored_arguments) + removed = [] + def save(raw): - doc = _prune_operations(raw) + removed.clear() + doc = _prune_operations(raw, removed) if operation_id in doc["claims"]: - raise err(409, "confirmation_stale", "Preview the change again.") + raise err(409, "confirmation_stale", "This preview was already saved, " + _STALE_FIX) stored_item = copy.deepcopy(item) - if payload["tool"] == "apps": - from tool_groups.apps import pending_arguments - stored_item["payload"]["arguments"] = pending_arguments(payload["arguments"], encode=True) + if ref is None: + stored_item["payload"]["arguments"] = copy.deepcopy(stored_arguments) + else: + stored_item["payload"].pop("arguments", None) doc["claims"][operation_id] = stored_item return doc - session.runtime.update(_CONFIRMATION_BUCKET, save, flush="sync") + try: + session.runtime.update(_CONFIRMATION_BUCKET, save, flush="sync") + except Exception: + if ref is not None: + _drop_operation_documents(session, [ref["key"]]) + raise + _drop_operation_documents(session, removed) def _operation_access(session, item, *, confirming=False): @@ -4369,6 +4556,19 @@ def _load_operation(session, operation_id, *, confirming=False): if (not isinstance(item, dict) or "payload" not in item or item.get("retain_until", 0) < time.time()): raise err(404, "operation_unavailable", _OPERATION_UNAVAILABLE_MESSAGE) + if isinstance(item.get("arguments_ref"), dict): + item = copy.deepcopy(item) + ref = item["arguments_ref"] + held = session.runtime.get(ref.get("key"), fresh=True) if str(ref.get("key", "")).startswith( + _OPERATION_ARGUMENTS_PREFIX) else None + if (isinstance(held, dict) and held.get("operation_id") == operation_id + and isinstance(held.get("arguments"), dict) and _json_digest(held["arguments"]) == ref.get("digest")): + item["payload"]["arguments"] = held["arguments"] + elif item.get("status") != "preview" and isinstance(item.get("receipt_arguments"), dict): + # A confirmed operation removed its document. Its receipt reads the small arguments kept in the claim. + item["payload"]["arguments"] = item["receipt_arguments"] + else: + raise err(409, "confirmation_stale", "This preview's saved arguments are gone, " + _STALE_FIX) if item["payload"]["tool"] == "apps": from tool_groups.apps import pending_arguments item = copy.deepcopy(item) @@ -4402,6 +4602,11 @@ def _operation_result(operation_id, item): def _operation_status(session, args): """Read a retained result without changing any object or operation revision.""" + import catalog_store + # A long Catalog PDF export is an operation too. Only its own caller can read it. + export = catalog_store.export_status(session, args["operation_id"]) + if export is not None: + return export _, item = _load_operation(session, args["operation_id"]) return _operation_result(args["operation_id"], item) @@ -4538,6 +4743,23 @@ def _finish_website_note(session, payload, plan, result): return response +_STALE_FIX = "so repeat the original call for a new preview and confirm with its new confirmation_token." + + +def _confirm_fix(args): + """Name what a confirm_change call got wrong and the shape that succeeds.""" + allowed = ("confirmation_token", "confirmation_phrase", "intent") + extra = sorted(str(key) for key in args if key not in allowed) if isinstance(args, dict) else [] + phrase = args.get("confirmation_phrase", "") if isinstance(args, dict) else "" + cause = (extra[0] + " is not a confirm_change field" if len(extra) == 1 + else ", ".join(extra) + " are not confirm_change fields" if extra + else "confirmation_phrase is not text" if isinstance(args, dict) + and isinstance(args.get("confirmation_token"), str) and not isinstance(phrase, str) + else "confirmation_token is missing or not text") + return (cause + ", so send only confirmation_token from the preview, plus confirmation_phrase " + "when the preview asks for it.") + + def _confirm_change(session, args): from core import view_audit, write_execution @@ -4554,7 +4776,7 @@ def _confirm_change(session, args): return _operation_result(operation_id, stored) _assert_write_scope(fresh_session, payload.get("tool"), payload.get("arguments")) if payload.get("environment") != _write_environment(): - raise err(409, "confirmation_stale", "The environment changed. Preview the change again.") + raise err(409, "confirmation_stale", "The environment changed after the preview, " + _STALE_FIX) if int(payload.get("expiry") or 0) <= int(time.time()): raise err(409, "confirmation_expired", "This confirmation token expired. Preview the change again.") @@ -4567,8 +4789,7 @@ def _confirm_change(session, args): execution = payload.get('execution') if (not isinstance(execution, dict) or not isinstance(execution.get('seed'), str) or not execution['seed'] or type(execution.get('timestamp')) not in (int, float)): - raise err(409, 'confirmation_stale', - 'This preview is no longer current. Preview the change again.') + raise err(409, 'confirmation_stale', 'This preview is no longer current, ' + _STALE_FIX) try: _confirmation_claim(fresh_session, operation_id) except _OperationAlreadyClaimed: @@ -4589,7 +4810,7 @@ def _confirm_change(session, args): or any((key in payload, payload.get(key)) != (key in current, current.get(key)) for key in ("purpose", "purpose_digest", "about", "about_digest", "buildKind"))): raise err(409, "confirmation_stale", - "The data, permissions, or resulting diff changed. Preview the change again.") + "The data, permissions, or resulting diff changed after the preview, " + _STALE_FIX) if remote_note: import website_tools ctx = _tool_context(fresh_session) @@ -4618,7 +4839,7 @@ def _confirm_change(session, args): {"state": "unknown", "applied": None, "reason": "remote_response_lost"}) _confirmation_status(fresh_session, operation_id, "failed") raise err(409, "confirmation_stale", - "The data changed while confirmation was committing. Preview it again.") from exc + "The data changed while this change was being saved, " + _STALE_FIX) from exc except Exception: if remote_note and capability is not None and capability.transport_started: return _finish_website_note(fresh_session, payload, recomputed["_plan"], @@ -4664,6 +4885,8 @@ def _confirm_change(session, args): if tool == "apps" and arguments.get("action") == "catalog_build": response.update({key: copy.deepcopy(result[key]) for key in _CATALOG_BUILD_RESULT_FIELDS}) _confirmation_status(fresh_session, operation_id, "committed", response) + if isinstance(stored.get("arguments_ref"), dict): + _drop_operation_documents(fresh_session, [stored["arguments_ref"].get("key")]) if tool != "database_object" and receipt.get("persisted") is True: try: targets, native_ids = _committed_build_evidence(fresh_session, guarded_session.runtime, arguments, @@ -4753,6 +4976,20 @@ _MCP_CODE_GROUPS = ( ("Check the View's sharing settings before changing it.", """ view_is_shared view_not_personal share_owner_redundant share_role_unknown share_who_required """), + # Catalog refusals keep their own code. An unpublished code collapsed to tool_refused. + ("The catalog selection or design is invalid. Correct the field the refusal names and preview again.", """ + catalog_selection_invalid catalog_design_invalid catalog_view_invalid catalog_skus_invalid + catalog_skus_missing catalog_sku_ambiguous catalog_key_ambiguous catalog_no_matches catalog_empty + catalog_source_mismatch invalid_catalog invalid_catalog_paper invalid_catalog_cursor invalid_catalog_limit + """), + ("The Catalog App or its fields are not available to this account.", """ + catalog_app_unavailable catalog_app_not_found catalog_forbidden catalog_fields_forbidden + catalog_source_forbidden catalog_tenant_forbidden + """), + ("Catalog storage did not finish this request. Read the catalog again, then retry.", """ + catalog_query_incomplete catalog_id_conflict catalog_migration_invalid catalog_save_failed + catalog_store_invalid catalog_export_failed + """), ) MCP_RESULT_CODES = { code: reason for reason, codes in _MCP_CODE_GROUPS for code in codes.split() @@ -4768,6 +5005,7 @@ MCP_RESULT_CODES.update({ "unknown_people": "Choose active people in this workspace.", "grant_exceeds_role": "You cannot grant a role above your own access.", "revoke_not_yours": "You can remove only people you added.", + "field_not_shared": "A field in this View is private to its creator. Hide it in the View or ask its creator to share it.", "key_refresh_only": "This connection can only refresh source cells in its granted workbooks.", "key_workbook_out_of_scope": "This connection cannot refresh this workbook.", "external_workbooks_invalid": "Choose up to 100 workbook ids. All workbooks is not a valid choice.", @@ -4785,11 +5023,15 @@ MCP_RESULT_CODES.update({ "operation_pending": "The change is still being saved. Ask again shortly.", "operation_not_committed": "This change has not been committed.", "operation_unavailable": _OPERATION_UNAVAILABLE_MESSAGE, + "catalog_not_found": ("No stored catalog has this catalog_id. Use the catalog_id that apps.catalog_build " + "returned, after confirm_change committed that build."), "credential_in_arguments": "Remove credential values from the arguments.", - "build_about_required": "Send about with title, description of 20 to 240 characters, and tags from the guide. See missing and example.", + "build_about_required": ("Send about with title, description of 20 to 240 characters, and tags from the guide, " + "beside action and arguments or inside arguments. See missing and example."), "build_about_invalid": ("Fix the fields in invalid. Use a nonempty title, a description of 20 to 240 characters, " "and tags from allowed, each once: " + ", ".join(external_tools.BUILD_TAGS) + "."), - "call_intent_required": "Send intent explaining why you are making this write. See example.", + "call_intent_required": ("Send intent explaining why you are making this write, beside action and arguments " + "or inside arguments. See example."), "call_intent_invalid": "Send intent as a string of 1 to 500 characters. See example.", "invalid_external_credential": "The credential is invalid, expired, or revoked.", "credential_in_output": "The result contained a credential value and was withheld.", @@ -4891,7 +5133,7 @@ _MCP_RESULT_OPAQUE = frozenset({ "revision_before", "revision_after", "filters", "sorts", "config", "binding", "bindings", }) _MCP_RESULT_IDENTIFIERS = frozenset({ - "resourceUri", "deepLink", "pdfLink", "src", "websiteUrl", "fileId", "link", "href", + "resourceUri", "deepLink", "pdfLink", "downloadUrl", "src", "websiteUrl", "fileId", "link", "href", "id", "ids", "operation_id", "key", "keys", "pid", "pids", "database", "databases", "sourceDatabase", "compatibleDatabases", "field", "fields", "field_key", "colId", "metric_key", "groupBy", "workbook_id", "worksheet", "tab", "view_id", "app_id", "catalog_id", "chart_id", "record_id", "contextId", @@ -5052,6 +5294,26 @@ def _mcp_protocol_error(rid, code, message): return {"jsonrpc": "2.0", "id": rid, "error": {"code": code, "message": out["reason"], "data": out}} +def _mcp_file_resource(rid, session, uri): + """Answer resources/read for a caller's own file with its bytes as a base64 blob.""" + import assistant_files + from loopy_catalog_tools import FILE_RESOURCE_PREFIX + try: + item = _external_file(session, uri[len(FILE_RESOURCE_PREFIX):]) + data = assistant_files.read_bytes(session.runtime, item) + except HTTPException as exc: + if exc.status_code == 401: + _auth_refusal() + data = None + except Exception: + data = None + if data is None: + return _mcp_protocol_error(rid, -32602, "That resource is not available.") + return {"jsonrpc": "2.0", "id": rid, "result": {"contents": [{ + "uri": uri, "mimeType": item["mime"], "name": item["filename"], + "blob": base64.b64encode(data).decode("ascii")}]}} + + def _mcp_protocol_result(rid, data): out = _shape_mcp_result({"ok": True, "data": data}) return {"jsonrpc": "2.0", "id": rid, "result": { @@ -5065,8 +5327,7 @@ def _mcp_protocol_result(rid, data): # `loopable_guide` tool (every client can call a tool). ⛔ Model-facing text is user-facing copy: # no em or en dashes here. MCP_GUIDE_TOOL = "loopable_guide" -_MCP_INTENT_SCHEMA = {"type": "string", "minLength": 1, "maxLength": 500, - "description": "State why you are making this call. Required for every write, including confirmation."} +from tool_groups import INTENT_SCHEMA as _MCP_INTENT_SCHEMA # noqa: E402 - one schema for base and served specs MCP_INSTRUCTIONS = "\n".join([ "1. Loopable is a governed business data workspace: Table databases, saved Views, Sheet workbooks and " "Apps. Every call runs as the signed-in user, with that user's own permissions. " @@ -5088,6 +5349,7 @@ MCP_INSTRUCTIONS = "\n".join([ "intelligence.create_wiki publishes a new wiki/ page at once without preview, confirmation or owner approval. " "It cannot replace a committed page. Existing wiki edits use intelligence.propose with confirmation and file approval. " "Send intent beside action and arguments on every write, including confirm_change. " + "If your client sends only action and arguments, put intent and about inside arguments. " "Reads may send intent. Use 1 to 500 characters explaining why you are making this call." " Use requests.list_team for teammate names and last-active times, requests.send_message for questions or change proposals, " "requests.check_inbox for incoming requests, and requests.ask_user for recipient Approve or Deny decisions. " @@ -5116,7 +5378,8 @@ _MCP_GUIDE_SPEC = { "name": MCP_GUIDE_TOOL, "description": "Read workspace connections, your skills, databases and the group index, " "or choose a group for its actions and argument schemas.", - "inputSchema": {"type": "object", "properties": {"group": {"type": "string"}}, "required": [], + "inputSchema": {"type": "object", "properties": {"action": {"type": "string", "enum": ["read"]}, + "group": {"type": "string"}}, "required": [], "additionalProperties": False}, "annotations": {"title": "Loopable guide", "readOnlyHint": True, "destructiveHint": False, "openWorldHint": False}, @@ -5473,11 +5736,17 @@ def _guide_unread_count(session: Session) -> int: def _mcp_guide(session: Session, group=None) -> str: """Return the group index or one group's action schemas without reading records.""" import tool_groups + groups = tool_groups.groups() + if isinstance(group, str) and group not in groups and group != "data": + # A cased, plural or dotted name (Sheets, sheet.write_cells) reads the one group it names. + key = group.strip().lower().partition(".")[0] + group = next((name for name in (key, key[:-1] if key.endswith("s") else None) + if name in groups or name == "data"), group) alias = group == "data" group = tool_groups.canonical_name(group) - groups = tool_groups.groups() if group is not None and group not in groups: - raise sheets.SheetRefusal("tool_not_found", "The requested group is not available.") + raise sheets.SheetRefusal("tool_not_found", "Set group to one of: " + ", ".join(groups) + + ". Omit group to read the index.") # One rows-free definitions read per guide: a live runtime pays one pool checkout per database. # The guide reads schemas only, so no reader here needs `rows`. On a failed read, keep the old path. try: @@ -5602,6 +5871,7 @@ def _mcp_tool_specs() -> list[dict]: group_names = set(groups) for spec in specs: spec["inputSchema"]["properties"]["intent"] = copy.deepcopy(_MCP_INTENT_SCHEMA) + # Every write-capable tool requires intent. Groups carry it from tool_groups.tool_specs. if spec["name"] == "confirm_change": spec["inputSchema"]["required"] = [*spec["inputSchema"].get("required", []), "intent"] if spec["name"] in group_names: @@ -5784,7 +6054,7 @@ def _mcp_call_intent(arguments): def _mcp_saved_intent(session, arguments): """Return the intent saved with the preview the signed token names, or None.""" try: - payload = _read_confirmation_token(arguments.get("confirmation_token")) + payload = _read_confirmation_token(arguments.get("confirmation_token"), session=session) _, stored = _load_operation(session, payload.get("operation_id", ""), confirming=True) if stored["payload"] != payload: return None @@ -5794,12 +6064,48 @@ def _mcp_saved_intent(session, arguments): return intent if isinstance(intent, str) and intent.strip() else None +def _mcp_decode_about(arguments): + """Return arguments with an about sent as JSON object text decoded, or the same object unchanged.""" + if not isinstance(arguments.get("about"), str): + return arguments + try: + decoded = json.loads(arguments["about"]) + except ValueError: + return arguments + return {**arguments, "about": decoded} if isinstance(decoded, dict) else arguments + + def _mcp_hoist_call_metadata(name, arguments): """Return group call arguments with intent, about and purpose lifted out of the inner arguments.""" import tool_groups keys = ("intent", "about", "purpose") + if isinstance(arguments, dict) and isinstance(arguments.get("arguments"), str): + # Some clients send arguments as a JSON object string. Only an object decodes. + try: + decoded = json.loads(arguments["arguments"]) + except ValueError: + decoded = None + if isinstance(decoded, dict): + arguments = {**arguments, "arguments": decoded} + if isinstance(arguments, dict): + # about sent as JSON object text decodes the same way, beside action and arguments or inside arguments. + arguments = _mcp_decode_about(arguments) + if isinstance(arguments.get("arguments"), dict): + inner = _mcp_decode_about(arguments["arguments"]) + if inner is not arguments["arguments"]: + arguments = {**arguments, "arguments": inner} + canonical = tool_groups.canonical_call(name, arguments)[0] if isinstance(name, str) else None + if canonical in {"confirm_change", MCP_GUIDE_TOOL} and isinstance(arguments, dict): + # A flat tool called in the group shape unwraps: its fields beside action and arguments win. + flat = {key: value for key, value in arguments.items() if key != "arguments"} + # "call" and "confirm" name the same step as confirm_change. + if canonical == "confirm_change" and flat.get("action") in ("confirm_change", "call", "confirm"): + flat.pop("action") + if isinstance(arguments.get("arguments"), dict): + return {**arguments["arguments"], **flat} + return flat if "arguments" not in arguments else arguments # The Table group's `data` alias hoists like `table` (C4). - if (not isinstance(name, str) or tool_groups.canonical_call(name, arguments)[0] not in tool_groups.groups() + if (canonical not in tool_groups.groups() or not isinstance(arguments, dict) or not isinstance(arguments.get("arguments"), dict) or not any(key in arguments["arguments"] for key in keys)): @@ -5813,6 +6119,27 @@ def _mcp_hoist_call_metadata(name, arguments): return hoisted +def _mcp_intent_fix(name): + """Name the intent field and where this tool takes it.""" + import tool_groups + if name == "confirm_change": + return ("Send intent beside confirmation_token: 1 to 500 characters on why you are confirming this change. " + "A client that sends only action and arguments may put both inside arguments.") + if tool_groups.canonical_call(name, {})[0] in tool_groups.groups(): + return ("Send intent beside action and arguments: 1 to 500 characters on why you are making this write. " + "If your client sends only action and arguments, put intent inside arguments.") + return "Send intent beside this tool's other arguments: 1 to 500 characters on why you are making this write." + + +def _mcp_about_fix(name): + """Name where a group call takes about, or nothing for a flat tool.""" + import tool_groups + if isinstance(name, str) and tool_groups.canonical_call(name, {})[0] in tool_groups.groups(): + return (" Put about beside action and arguments. " + "If your client sends only action and arguments, put about inside arguments.") + return "" + + def _mcp_requires_intent(name, arguments): """Classify declared writes before argument validation can mask the intent requirement.""" import tool_groups @@ -5834,9 +6161,10 @@ def _dispatch_mcp(req, session: Session, base_url: str = ""): if isinstance(req, dict) and req.get("method") == "tools/call" and isinstance(req.get("params"), dict): params = req["params"] original = params.get("name") - name, arguments = tool_groups.canonical_call(original, params.get("arguments", {})) + raw = params.get("arguments", {}) + name, arguments = tool_groups.canonical_call(original, raw) alias = isinstance(original, str) and (original == "data" or original.startswith("data.")) and name == "table" - if name != original: + if name != original or arguments is not raw: req = {**req, "params": {**params, "name": name, "arguments": arguments}} reply = _dispatch_mcp_call(req, session, base_url) if alias and isinstance(reply, dict) and "content" in reply.get("result", {}): @@ -5865,6 +6193,8 @@ def _dispatch_mcp_call(req, session: Session, base_url: str = ""): if not isinstance(params, dict): return protocol_error(rid, -32602, "MCP params must be an object.") name = params.get("name") + called = name + arguments = params.get("arguments", {}) try: # Same AI apps wall as the read doors, inside the dispatcher so a grant refusal # remains MCP content. Only the unchanged bearer dependency produces an HTTP 401. @@ -5902,6 +6232,10 @@ def _dispatch_mcp_call(req, session: Session, base_url: str = ""): "_meta": {"ui": {"csp": {"connectDomains": [], "resourceDomains": [], "frameDomains": []}}}} if method == "resources/list": return {"jsonrpc": "2.0", "id": rid, "result": {"resources": [resource]}} + from loopy_catalog_tools import FILE_RESOURCE_PREFIX + uri = params.get("uri") + if isinstance(uri, str) and uri.startswith(FILE_RESOURCE_PREFIX): + return _mcp_file_resource(rid, session, uri) if params.get("uri") != WORKBOOK_RESOURCE_URI: return protocol_error(rid, -32602, "That resource is not available.") resource["text"] = (Path(__file__).parent / "side_view" / "workbook.html").read_text(encoding="utf-8") @@ -5915,7 +6249,7 @@ def _dispatch_mcp_call(req, session: Session, base_url: str = ""): if intent is not None: external_activity.note_saved_intent(intent) if intent is None and _mcp_requires_intent(name, arguments): - raise err(400, "call_intent_required", "Send intent explaining why you are making this write.", + raise err(400, "call_intent_required", _mcp_intent_fix(name), {"example": {"intent": "Prepare the weekly sales review"}}) if isinstance(arguments, dict): arguments = {key: value for key, value in arguments.items() if key != "intent"} @@ -5949,6 +6283,10 @@ def _dispatch_mcp_call(req, session: Session, base_url: str = ""): or action in {"describe_database", "query_database"}): from tool_groups.data import READ_ARGUMENT_HELP raise sheets.SheetRefusal(exc.code, exc.message + " " + READ_ARGUMENT_HELP) from exc + if (tool_groups.canonical_name(name) == "views" and exc.code == "tool_arguments_invalid" + and isinstance(arguments, dict) and arguments.get("action") == "share"): + from tool_groups.views import share_arguments_fix + raise sheets.SheetRefusal(exc.code, share_arguments_fix(arguments)) from exc raise confirm = True scoped_arguments = arguments @@ -6013,6 +6351,8 @@ def _dispatch_mcp_call(req, session: Session, base_url: str = ""): out = _tool_refusal(name, "external_access_unavailable", "The AI API is unavailable right now.", 503) except sheets.SheetRefusal as exc: import routes_sheet + if exc.code == "tool_arguments_invalid" and name == "confirm_change": + exc = sheets.SheetRefusal(exc.code, _confirm_fix(arguments)) translated = routes_sheet._refuse(exc) info = translated.detail["error"] out = _tool_refusal(name, info["code"], info["message"], translated.status_code, @@ -6034,12 +6374,15 @@ def _dispatch_mcp_call(req, session: Session, base_url: str = ""): retry["suggestions"] = details.get("suggestions", []) if info.get("code") == "workbook_name_ambiguous" and isinstance(details, dict): retry["candidates"] = details.get("candidates", []) - out = _tool_refusal(name, info.get("code", "tool_refused"), - info.get("message", "The app refused this operation."), exc.status_code, **retry) + message = info.get("message", "The app refused this operation.") + if info.get("code") == "build_about_required" and isinstance(message, str): + message += _mcp_about_fix(called) + out = _tool_refusal(name, info.get("code", "tool_refused"), message, exc.status_code, **retry) except (TypeError, ValueError, KeyError, AttributeError): out = _tool_refusal(name, "tool_arguments_invalid", "The arguments could not be read. Use the published input schema.") except Exception: - out = _tool_refusal(name, "tool_unavailable", "The tool could not complete this operation.", 503) + out = _tool_refusal(name, "tool_unavailable", + "The tool could not complete this operation, so try the same call again shortly.", 503) return _mcp_content(rid, out, session) @@ -6050,10 +6393,37 @@ async def external_mcp(request: Request, session: Session = Depends(require_exte req = await request.json() except (ValueError, UnicodeError): return _mcp_protocol_error(None, -32700, "The request body must be valid JSON.") - reply = await run_in_threadpool(_handle_mcp, req, session, str(request.base_url)) + reply = await run_in_threadpool(_handle_mcp, req, session, _mcp_public_origin(request)) return Response(status_code=204) if reply is None else reply +def _mcp_public_origin(request: Request) -> str: + """C3: MCP links use the deployment's public base; only an unconfigured local run uses the request.""" + import loopy_links + return loopy_links.public_base() or str(request.base_url) + + +def _external_file(session: Session, file_id): + """Return this caller's own file metadata when every source is inside the credential scope.""" + import assistant_files + require_external_analyst(session) + item = assistant_files.owned_metadata(session.runtime, session.uname, file_id) + sources = (item or {}).get("sources") + if item is None or session.tenant != item["tenant"] or not isinstance(sources, list) or not sources: + raise err(404, "unknown_file", "That file does not exist.") + for database in sources: + _assert_database_scope(session, database) + _external_read_scope(session, database, fields=[], mode="schema") + return item + + +@router.get("/files/{file_id}") +def external_file(file_id: str, session: Session = Depends(require_external_session)): + """Download a file this credential's own tool call produced, with the same bearer credential.""" + import routes_files + return routes_files._response(session.runtime, _external_file(session, file_id)) + + # R8 (SDLC 61) revokes the read-only surface. Every write must be named here deliberately. # A new write in the registry without a matching literal raises at import, before serving. _WRITE_TOOLS = frozenset({ diff --git a/api/routes_forms.py b/api/routes_forms.py index 8bcab13ef04e69ea054bdb7f0141898ee63daba1..f0939f0fcd1189be1ec9d68aeb6a663feeff3de6 100644 --- a/api/routes_forms.py +++ b/api/routes_forms.py @@ -410,11 +410,12 @@ def _clean_values(fields: list, raw: dict): def _public_base() -> str: """The origin a share link is built on. Empty = the client builds it from its own location. - `AIOS_PUBLIC_BASE` is the same variable the OAuth callback resolves against, so a domain move - takes the form links with it. Absent, we return a PATH rather than guessing a hostname: + `loopy_links.public_base()` reads `AIOS_PUBLIC_BASE` like the OAuth callback, and live never + answers *.hf.space (C3). With no base it returns a PATH rather than guessing a hostname: a link with the wrong origin looks right, gets pasted into an email and 404s for the reader. """ - return (os.environ.get("AIOS_PUBLIC_BASE") or os.environ.get("APP_BASE_URL") or "").rstrip("/") + import loopy_links + return loopy_links.public_base() or "" def _link_of(token: str) -> str: diff --git a/api/routes_nav.py b/api/routes_nav.py index 1e44da884199676c99a5836a4fc5d878b8822878..6ecd171d3fc9719c5c59acc01296dc3e5c2b119b 100644 --- a/api/routes_nav.py +++ b/api/routes_nav.py @@ -725,6 +725,8 @@ def nav(session: Session = Depends(require_session)): # marked as such. `degraded` is that mark; the shell renders it in the affected slot. _degraded.append("databases") pass + import external_freshness + external_freshness.amazon_nav(session, pages) # S73-T64: every viewer is admitted to the activity log, which then gates each event. Alone it # would turn "no access" and "no databases yet" into a rail holding one always-empty database, # so it rides only beside another page this session may open. @@ -1479,7 +1481,7 @@ def update_schema_map_setup(key: str, body: dict = Body(default=None), @router.get("/nav/application-templates") def application_templates(session: Session = Depends(require_session)): """The trusted template contracts the App creation flow may offer.""" - return {"templates": application_registry.template_contracts()} + return {"templates": application_registry.template_contracts(session=session)} @router.post("/nav/applications", status_code=201) @@ -1493,7 +1495,7 @@ def create_nav_application(body: dict = Body(default=None), """ payload = body if isinstance(body, dict) else {} template_kind = str(payload.get("templateKind") or "").strip().lower() - wizard_kinds = {row["kind"] for row in application_registry.template_contracts()} + wizard_kinds = {row["kind"] for row in application_registry.template_contracts(session=session)} if template_kind not in wizard_kinds: raise err(400, "bad_application", "application template kind is not recognized") source = str(payload.get("sourceDatabase") or "").strip() diff --git a/api/routes_odoo_attachments.py b/api/routes_odoo_attachments.py new file mode 100644 index 0000000000000000000000000000000000000000..a5a57121c22be7a812c693255d83997341fb932e --- /dev/null +++ b/api/routes_odoo_attachments.py @@ -0,0 +1,31 @@ +"""Contract C5 (S78-T14): the delivery photo list and the photo bytes, behind the delivery read wall. + +The work lives in `odoo_attachments.py`. This router decides who may ask and shapes the response. +Mounted by `main.py` through `_mount_router("routes_odoo_attachments")`. +""" +from fastapi import APIRouter, Depends, Path +from fastapi.responses import Response + +import odoo_attachments +from deps import Session, require_session + +router = APIRouter(prefix="/api/v1") + +_MAX_ID = 2 ** 31 - 1 + + +@router.get("/odoo/deliveries/{picking_id}/photos") +def delivery_photos(picking_id: int = Path(..., ge=1, le=_MAX_ID), session: Session = Depends(require_session)): + """`[{id, name, mimetype, url}]`: every photo attachment of one delivery.""" + return odoo_attachments.list_photos(session, picking_id) + + +@router.get("/odoo/attachments/{attachment_id}") +def delivery_photo(attachment_id: int = Path(..., ge=1, le=_MAX_ID), session: Session = Depends(require_session)): + """The photo bytes, streamed through and never stored.""" + body, mimetype, _name = odoo_attachments.read_photo(session, attachment_id) + return Response(content=body, media_type=mimetype, headers={ + "Cache-Control": "private, max-age=300", + "Content-Disposition": "inline", + "X-Content-Type-Options": "nosniff", + }) diff --git a/api/routes_publish.py b/api/routes_publish.py index 65410864e1999b0e944fc6a0876a38816535641e..092700f6dfac7c00ad3399de917f3dcf07faeecd 100644 --- a/api/routes_publish.py +++ b/api/routes_publish.py @@ -186,7 +186,9 @@ def _equalise_pw_cost(entry) -> None: def _public_base() -> str: - return (os.environ.get("AIOS_PUBLIC_BASE") or os.environ.get("APP_BASE_URL") or "").rstrip("/") + """The origin a published link is built on; live never links *.hf.space (C3).""" + import loopy_links + return loopy_links.public_base() or "" def _link_of(token: str) -> str: diff --git a/api/routes_templates.py b/api/routes_templates.py index da6000f06c2a673861ff8b2a6f536f072ec16ec8..df2d7c99539ad36f21bc347f41047bd3557ed708 100644 --- a/api/routes_templates.py +++ b/api/routes_templates.py @@ -36,9 +36,10 @@ router = APIRouter(prefix="/api/v1") _TEMPLATE_CATEGORIES = { "sheet": "Sheet", "data": "Table", "app": "App", "agents": "Automation", "saved": "Saved", - "route": "Operations", "catalog": "Sales", + "route": "Operations", "catalog": "Sales", "competitor_watch": "Sales", } -_TEMPLATE_PREVIEW_LABELS = {**_TEMPLATE_CATEGORIES, "route": "Routing", "catalog": "Catalog"} +_TEMPLATE_PREVIEW_LABELS = {**_TEMPLATE_CATEGORIES, "route": "Routing", "catalog": "Catalog", + "competitor_watch": "Competitor watch"} @router.get("/templates/previews/{module}.svg") @@ -74,6 +75,16 @@ M92 224h88 M240 224h38 M354 224h54"/> +''' + elif module == "competitor_watch": + # S78-T17: a ranked list beside a trend line, the App's own two panels. + drawing = ''' + + + + + + ''' elif module == "catalog": drawing = ''' @@ -349,6 +360,8 @@ def _catalog_row(key, name, description, requires, *, module="sheet", creation_m blocked = next((row for row in requires if row["state"] == "disconnected"), None) blocked = blocked or next((row for row in requires if row["state"] != "ready"), None) preview = key if module == "app" and key in ("route", "catalog") else module + if module == "app" and key == "competitor_watch": + preview = key # S78-T17: the gated template draws its own preview. return {"key": key, "name": dash_norm.plain(str(name)), "description": dash_norm.plain(str(description)), "image": f"/api/v1/templates/previews/{preview}.svg", @@ -608,7 +621,9 @@ def create_module_template(module: str, key: str, body: dict = Body(default=None if module == "agents": # A direct call to create_automation does not run its FastAPI dependency. session.require("automation") - if module == "app" and key not in {"route", "catalog"}: + if module == "app" and key == "competitor_watch": + pass # S78-T17: gated by application_registry.template_contracts(session=...). + elif module == "app" and key not in {"route", "catalog"}: raise err(404, "unknown_template", "That template does not exist.") section = next((row for row in _module_templates(session)["sections"] if row["module"] == module), None) template = next((row for row in section["templates"] if row["key"] == key), None) if section else None diff --git a/api/tool_groups/__init__.py b/api/tool_groups/__init__.py index 503b049b31c0bd0b0866a95bab219f63e623256a..17c9d2322378751fe879462dc7732fea572ca4c3 100644 --- a/api/tool_groups/__init__.py +++ b/api/tool_groups/__init__.py @@ -8,6 +8,10 @@ from core import external_tools, sheets _GROUP_MODULE_NAMES = {"data": "table"} TABLE_ALIAS_NOTE = "The data tool is deprecated for one release. Use table for the Table module." +GUIDE_TOOL = "loopable_guide" +# One intent schema for the base specs and the served tools/list, so the two cannot drift. +INTENT_SCHEMA = {"type": "string", "minLength": 1, "maxLength": 500, + "description": "State why you are making this call. Required for every write, including confirmation."} def canonical_name(name): @@ -15,14 +19,36 @@ def canonical_name(name): return "table" if name == "data" else name +def _prefixed_action(name, arguments): + """Strip a repeated group prefix from an action, as in sheet_write_cells, when the rest is exact.""" + group = groups().get(canonical_name(name)) + action = arguments.get("action") if isinstance(arguments, dict) else None + if group is None or not isinstance(action, str) or action in group["actions"]: + return arguments + inner = arguments.get("arguments") + if action == group["name"] and isinstance(inner, dict) and inner.get("action") in group["actions"]: + # The group name sent as the action, with the real action inside arguments, unwraps one level. + rest = {key: value for key, value in inner.items() if key != "action"} + return {**arguments, "action": inner["action"], "arguments": rest} + for prefix in {name, canonical_name(name)}: + for joint in ("_", "."): + if action.startswith(prefix + joint) and action[len(prefix) + 1:] in group["actions"]: + return {**arguments, "action": action[len(prefix) + 1:]} + return arguments + + def canonical_call(name, arguments): """Adapt Table group aliases and dotted actions before normal admission.""" + if name == GUIDE_TOOL + ".read": + return GUIDE_TOOL, arguments if name == "data": - return "table", arguments + return "table", _prefixed_action("data", arguments) if not isinstance(name, str): return name, arguments prefix, separator, action = name.partition(".") - if not separator or prefix not in {"table", "data"}: + if not separator: + return name, _prefixed_action(name, arguments) + if prefix not in {"table", "data"}: return name, arguments entry = groups().get("table", {}).get("actions", {}).get(action) if entry is None: @@ -92,6 +118,14 @@ def resolve(name, arguments): group = groups().get(canonical_name(name)) if isinstance(name, str) else None if group is None: return None + action = arguments.get("action") if isinstance(arguments, dict) else None + if isinstance(arguments, dict) and (not isinstance(action, str) or action not in group["actions"]): + # A bare group name as the action gets one clause naming the mix-up before the action list. + named = group["name"] + " is the tool name, not an action. " if action == group["name"] else "" + raise sheets.SheetRefusal("tool_arguments_invalid", ( + named + "Tool arguments must match the published input schema. Set action to one of the " + + group["name"] + " actions: " + ", ".join(group["actions"]) + ". Put that action's inputs inside " + "arguments, and intent, about and purpose beside action and arguments.")) external_tools.validate_arguments(arguments, { "type": "object", "properties": { "action": {"type": "string", "enum": list(group["actions"])}, @@ -141,13 +175,21 @@ def action_grants(entry, group=""): return tuple(dict.fromkeys((*grants, *entry.get("grants", ())))) +def writes(group): + """Tell whether any action in a group writes.""" + return any(not entry["read"] for entry in group["actions"].values()) + + def tool_specs(): - """Publish compact group summaries with conservative effect hints.""" + """Publish compact group summaries with call metadata and conservative effect hints.""" + import wiki_log return [{ "name": name, "description": group["guide"], "inputSchema": {"type": "object", "properties": { "action": {"type": "string"}, "arguments": {"type": "object"}, - }, "required": ["action"], "additionalProperties": False}, + "intent": copy.deepcopy(INTENT_SCHEMA), "about": copy.deepcopy(external_tools.ABOUT_SCHEMA), + "purpose": copy.deepcopy(wiki_log.PURPOSE_SCHEMA), + }, "required": ["action", "intent"] if writes(group) else ["action"], "additionalProperties": False}, "annotations": { "title": group["title"], "readOnlyHint": all(entry["read"] for entry in group["actions"].values()), diff --git a/api/tool_groups/apps.py b/api/tool_groups/apps.py index 3c1fed3dc51a00ba9bf40b6b209b5122e84de34b..55e673c37efb99fa5b458fbaf614c21df793262e 100644 --- a/api/tool_groups/apps.py +++ b/api/tool_groups/apps.py @@ -78,13 +78,29 @@ def list_apps(ctx, args): _refuse(exc) +CATALOG_ID_FIX = ("No stored catalog has this catalog_id. Use the catalog_id that apps.catalog_build returned, " + "after confirm_change committed that build. A preview saves no catalog until it is confirmed.") + + def catalog_pdf(ctx, args): session = _admit(ctx) import catalog_store import loopy_catalog_tools + from fastapi import HTTPException # The stored record admits its actual App source and credential scope. - catalog_store._record(session, args["catalog_id"]) + try: + catalog_store._record(session, args["catalog_id"]) + except HTTPException as exc: + code = ((exc.detail or {}).get("error") or {}).get("code") if isinstance(exc.detail, dict) else None + if code == "catalog_not_found": + from deps import err + raise err(404, "catalog_not_found", CATALOG_ID_FIX) from exc + raise + if (session.claims or {}).get("external") is True: + # An MCP call has a time budget. A longer render answers with an operation_id. + return loopy_catalog_tools.catalog_pdf_within_budget( + session, args, render=lambda caller, values: loopy_catalog_tools.catalog_pdf(caller, values)) return loopy_catalog_tools.catalog_pdf(session, args) @@ -127,6 +143,7 @@ def apply_template(ctx, args): payload = copy.deepcopy(args) if payload["templateKind"] not in {row["kind"] for row in applications.template_contracts()}: raise applications.ApplicationError("Choose a template from apps.templates.") + _template_inputs(payload) share = payload.pop("share", None) source = payload.get("sourceDatabase") databases = [source] if source else [] @@ -147,6 +164,28 @@ def apply_template(ctx, args): _refuse(exc) +def _template_inputs(payload): + """Name the missing template input and where it goes before the registry answers generically.""" + contract = next(row for row in applications.template_contracts() if row["kind"] == payload["templateKind"]) + required = [role["key"] for role in contract["fieldRoles"] if role["required"]] + example = json.dumps({role: "" for role in required}) + if not isinstance(payload.get("sourceDatabase"), str) or not payload["sourceDatabase"].strip(): + raise applications.ApplicationError( + "apply_template is missing sourceDatabase. Put sourceDatabase inside arguments: the database key " + "this App reads, as table.list_databases returns it.") + field_map = payload.get("fieldMap") + if not isinstance(field_map, dict): + raise applications.ApplicationError( + "apply_template is missing fieldMap. Put fieldMap inside arguments: an object that maps the required " + "roles " + ", ".join(required) + " to field keys of sourceDatabase, for example " + example + + ". apps.templates lists the optional roles.") + missing = [role for role in required if not str(field_map.get(role) or "").strip()] + if missing: + raise applications.ApplicationError( + "fieldMap is missing the required role " + ", ".join(missing) + ". Map each to a field key of " + "sourceDatabase inside arguments.fieldMap, for example " + example + ".") + + def _result(app): result = {"application": _catalog_link(app)} if app.get("shareApproval"): diff --git a/api/tool_groups/data.py b/api/tool_groups/data.py index 73167f422905244f28b661dd807b778ed259fd1e..0e56cc7bea6914771890182f4366da3d4411a8f5 100644 --- a/api/tool_groups/data.py +++ b/api/tool_groups/data.py @@ -109,9 +109,23 @@ def _operation_status(ctx, args): return read_status(ctx.session, args) +def _delivery_photos(ctx, args): + """List one Odoo delivery's photos when this credential and account may read that delivery.""" + import odoo_attachments + from loopy_links import public_base + from routes_external import _assert_database_scope + + def key_allowed(key): + _assert_database_scope(ctx.session, key) + + photos = odoo_attachments.list_photos(ctx.session, args["picking_id"], key_allowed=key_allowed, + base=public_base() or "") + return {"picking_id": args["picking_id"], "photos": photos} + + GROUP = { "name": "table", "title": "Table", - "guide": "Table: list databases, read fields, group and page records, propose changes or report a limitation. Read loopable_guide(group='table') for actions and a grouping example.", + "guide": "Table: list databases, read fields, group and page records, propose changes, list an Odoo delivery's photos with delivery_photos, or report a limitation. Read loopable_guide(group='table') for actions and a grouping example.", "actions": { "operation_status": { "read": True, "destructive": False, "confirm": False, @@ -120,6 +134,14 @@ GROUP = { }, "required": ["operation_id"], "additionalProperties": False}, "handler": _operation_status, }, + "delivery_photos": { + "read": True, "destructive": False, "confirm": False, + "schema": {"type": "object", "properties": { + "picking_id": {"type": "integer", "minimum": 1, "maximum": 2147483647, + "description": "The stock.picking id: odoo_id in Odoo shipments."}, + }, "required": ["picking_id"], "additionalProperties": False}, + "handler": _delivery_photos, + }, "report_limitation": { "read": True, "destructive": False, "confirm": False, "schema": {"type": "object", "properties": { diff --git a/api/tool_groups/sheet.py b/api/tool_groups/sheet.py index 9cba83ddd7a51ac848410ce971028474a9fc385a..f4fc6d05ea0684647bd861dfcdef257c17c878fc 100644 --- a/api/tool_groups/sheet.py +++ b/api/tool_groups/sheet.py @@ -208,7 +208,7 @@ GROUP["guide"] += ' Under MCP rule 9, ask "Do you want me to build a Sheet workb _BATCH_GUIDE = ( " A batch has 1 to 50 operations for one workbook. Use name and arguments for each operation. " "Use sheet_add_tab, sheet_write_cells, sheet_set_binding, sheet_set_styles or sheet_set_dimensions " - "with their published arguments. One sheet_write_cells operation can write 200 cells. " + "with their published arguments. A sheet_write_cells preview of 250 or more changes shows counts, ranges and a sample. confirm_change writes every cell. " "Range actions use name='range_object', with action, workbook_id, worksheet and an A1 range. " "Put values inside arguments for write_values, write_formulas, write_styles or number_format. " "For worksheet_object.add, put name inside arguments. For worksheet_object.geometry, " diff --git a/api/tool_groups/views.py b/api/tool_groups/views.py index 54789a0c86ce26c1f16e671bdb102873436f4e3b..c936c58069bdd06aa25e4477e8cc2344b53191d9 100644 --- a/api/tool_groups/views.py +++ b/api/tool_groups/views.py @@ -1,11 +1,95 @@ """Saved View actions through the existing permission checks.""" import copy +import re from fastapi import HTTPException from core import external_tools from . import legacy_action +# One line per refusal: why it was refused, then the call that would succeed. +SHARE_REFUSALS = { + "not_owner": "You cannot change who sees this View, so ask its owner or an Administrator to share it.", + "audience_not_allowed": ("Sharing with everyone is a choice a person makes on screen, " + "so name each person in entries instead."), + "blank_user": "An entry has an empty user, so set user to the username of an active person in this workspace.", + "grants_changed": ("This View's audience changed after it was read, " + "so call views share again without expect and confirm the new preview."), + "grant_exceeds_role": ("Can edit is above your own access to this View, " + "so set role to view for each new editor or ask its owner to grant edit."), + "grant_exceeds_audience": ("Only this View's owner or an Administrator can share it with everyone, " + "so name each person in entries instead."), + "revoke_not_yours": ("You can remove only people you added to this View, " + "so keep everyone else in entries and share again."), + "field_not_shared": ("This View shows a field that is still private to its creator, " + "so hide that field in the View or ask its creator to share it, then share again."), + "unknown_people": ("A person in entries has no active account in this workspace, " + "so set user to a username, not a display name or email."), + "forbidden": "Your account cannot share this View, so ask its owner or an Administrator to share it.", + "view_share_failed": "This View's access could not be saved, so send the same share call again.", +} +_SHARE_FIELDS = ("database", "view_id", "entries", "expect") +_CALL_FIELDS = ("action", "arguments", "intent", "about", "purpose") + + +def _share_refusal(code, status=403): + """Raise the one-line refusal for a share code.""" + import routes_shares + raise routes_shares.err(status, code, SHARE_REFUSALS[code]) + + +def _listed(names): + names = list(names) + if len(names) < 2: + return "".join(names) + return ", ".join(names[:-1]) + " and " + names[-1] + + +def share_arguments_fix(call): + """Name the first wrong part of a views share call and the shape that succeeds.""" + shape = "each entry is user (a username) and role (view or edit)" + if not isinstance(call, dict): + return "The call is not an object, so send action share with arguments database, view_id and entries." + stray = [str(key) for key in call if key not in _CALL_FIELDS] + if stray: + return (_listed(stray) + (" sit" if len(stray) > 1 else " sits") + " beside action, so move " + + ("them" if len(stray) > 1 else "it") + " inside arguments, which holds database, view_id, entries and optional expect.") + args = call.get("arguments") + if not isinstance(args, dict): + return "arguments is missing or not an object, so send arguments with database, view_id and entries." + unknown = [key for key in args if key not in _SHARE_FIELDS] + if unknown: + return (_listed(map(str, unknown)) + (" are not share arguments" if len(unknown) > 1 else " is not a share argument") + ", so send only database, view_id, " + "entries and optional expect, where " + shape + ".") + missing = [key for key in ("database", "view_id", "entries") if key not in args] + if missing: + return ("arguments has no " + " or ".join(missing) + ", so add " + ("them" if len(missing) > 1 else "it") + ": database and view_id are text, " + "and entries is a list where " + shape + ".") + for key in ("database", "view_id"): + if not isinstance(args[key], str) or not args[key].strip() or key == "view_id" and len(args[key]) > 120: + return key + " is not a usable id, so set it to the text id that the View list returns." + for key in ("entries", "expect"): + rows = args.get(key) + if key not in args: + continue + if not isinstance(rows, list): + return key + " is not a list, so send a list where " + shape + ", or [] to make the View private." + if len(rows) > 200: + return key + " names more than 200 people, so share with at most 200 people in one call." + for row in rows: + if not isinstance(row, dict): + return key + " holds a plain value, so send objects where " + shape + "." + extra = [name for name in row if name not in ("user", "role")] + if extra or "user" not in row or "role" not in row: + return (key + " has an entry with " + (_listed(map(str, extra)) or "a missing field") + + ", so give every entry exactly user (a username) and role (view or edit).") + if not isinstance(row["user"], str) or not 1 <= len(row["user"]) <= 120: + return key + " has a user that is not a username, so set user to a username of 1 to 120 characters." + if row["role"] not in ("view", "edit"): + return key + " has a role other than view or edit, so set role to view for Can view or edit for Can edit." + return "The share arguments do not match, so send database, view_id and entries, where " + shape + "." + + def _share_source(session, args): """Resolve source ownership on a detached canonical workspace before revealing the audience.""" import routes_external @@ -17,8 +101,9 @@ def _share_source(session, args): topic, source_owner, view = routes_shares._find_view_record( session, args["view_id"], _snapshot=snapshot) workspace = routes_shares._field_storage_keys(args["database"])[0] + # A missing View and another person's View read the same, so the refusal reveals nothing. if not view or not source_owner or f"{topic}_table_workspace" != workspace: - raise routes_shares.err(403, "not_owner", "You cannot administer this View's audience.") + _share_refusal("not_owner") rec = shares.grants("view", args["view_id"], st=session.runtime) if rec["owner"]: allowed = shares.may_administer( @@ -26,7 +111,7 @@ def _share_source(session, args): else: allowed = session.admin or source_owner == session.uname if not allowed: - raise routes_shares.err(403, "not_owner", "You cannot administer this View's audience.") + _share_refusal("not_owner") return snapshot, topic, source_owner, view, rec @@ -36,8 +121,40 @@ def _receipt_access(ctx, args): return True +def _unknown_people_refusal(unknown, people): + """Name each unknown username and, for an exact display name match, the username to use.""" + import routes_shares + names = {} + for username, row in people.items(): + label = str(row.get("name") or "").strip().lower() + if label: + names.setdefault(label, []).append(username) + hints = [names[who][0] + " for " + str(people[names[who][0]].get("name")) + for who in unknown if len(names.get(who, ())) == 1] + fix = ("set user to " + ", ".join(hints) + + (", and to a username for the rest" if len(hints) < len(unknown) else "") + if hints else "set user to a username, not a display name or email") + raise routes_shares.err(400, "unknown_people", "No active person in this workspace has the username " + + ", ".join(unknown) + ", so " + fix + ".") + + def share(ctx, args): """Replace a named audience through the on-screen share writer and its permission ceiling.""" + import routes_external + import routes_shares + try: + return _share(ctx, args) + except HTTPException as exc: + error = (exc.detail.get("error") or {}) if isinstance(exc.detail, dict) else {} + code = error.get("code") + # A 403 without a published code reaches the AI as forbidden. It names the share fix. + if code == "forbidden" or (exc.status_code == 403 and code not in SHARE_REFUSALS + and code not in routes_external.MCP_RESULT_CODES): + raise routes_shares.err(403, "forbidden", SHARE_REFUSALS["forbidden"]) from exc + raise + + +def _share(ctx, args): import routes_external import routes_shares from core import shares @@ -47,15 +164,13 @@ def share(ctx, args): snapshot, topic, source_owner, view, rec = _share_source(session, args) entries = copy.deepcopy(args["entries"]) if any(row["user"].strip().lower() in {shares.EVERYONE, "everyone"} for row in entries): - raise routes_shares.err( - 400, "audience_not_allowed", "A person must choose sharing with everyone on screen.") + _share_refusal("audience_not_allowed", 400) if any(not row["user"].strip() for row in entries): - raise routes_shares.err(400, "unknown_people", "Choose an active person in this workspace.") + raise routes_shares.err(400, "unknown_people", SHARE_REFUSALS["blank_user"]) people = {row["username"].strip().lower(): row for row in routes_shares._people(session.tenant)} unknown = sorted({row["user"].strip().lower() for row in entries} - people.keys()) if unknown: - raise routes_shares.err( - 400, "unknown_people", "These people have no active account in this workspace: " + ", ".join(unknown) + ".") + _unknown_people_refusal(unknown, people) keys = routes_shares._view_field_keys(view) definitions = routes_shares._view_field_definitions( session, f"{topic}_table_workspace", keys, _snapshot=snapshot) @@ -70,18 +185,12 @@ def share(ctx, args): _notify=False) except HTTPException as exc: code = (exc.detail.get("error") or {}).get("code") - messages = { - "not_owner": "You cannot administer this View's audience.", - "unknown_people": "Choose active people in this workspace.", - "grants_changed": "This View's audience changed, so preview the share again.", - "grant_exceeds_role": "You can grant Can view because Can edit exceeds your own access.", - "grant_exceeds_audience": "A person must choose sharing with everyone on screen.", - "revoke_not_yours": "You can remove only people you added to this View.", - "field_not_shared": "A private field must be shared by its owner before sharing this View.", - "view_share_failed": "This View's access could not be saved, so try again.", - } - if code in messages: - raise routes_shares.err(exc.status_code, code, messages[code]) from exc + if code == "view_share_failed": + reference = re.search(r"\bVS-[0-9A-F]{8}\b", str((exc.detail.get("error") or {}).get("message") or "")) + raise routes_shares.err(exc.status_code, code, SHARE_REFUSALS[code] + ( + " If it fails again, report reference " + reference[0] + "." if reference else "")) from exc + if code in SHARE_REFUSALS: + raise routes_shares.err(exc.status_code, code, SHARE_REFUSALS[code]) from exc raise name = str(view.get("name") or "A View") audience = [{"user": row["user"], "name": people[row["user"]].get("name") or row["user"], diff --git a/platform/core/connector_spec.py b/platform/core/connector_spec.py index 7d837eace8c9a7fb1877f986e4cbf86bfd87033f..512953342f88167ef014fde97c8e330693f65899 100644 --- a/platform/core/connector_spec.py +++ b/platform/core/connector_spec.py @@ -278,6 +278,16 @@ def edges_for(key): return out +def _link_field_label(label): + """An edge label with each repeated word dropped: "Amazon orders by order ID" (S78-T01).""" + seen, kept = set(), [] + for word in label.split(): + if word.lower() not in seen: + seen.add(word.lower()) + kept.append(word) + return " ".join(kept) + + def system_link_fields(key, live_columns): """Generate fields per live source table using only declared, live join endpoints.""" key = CONNECTOR_SPEC_KEYS.get(key, key) @@ -295,7 +305,7 @@ def system_link_fields(key, live_columns): # Field keys are unique within their source table, never across a connector. seen[source].add(edge["key"]) out[source].append({ - "key": edge["key"], "label": edge["label"], "type": "link", + "key": edge["key"], "label": _link_field_label(edge["label"]), "type": "link", "source": "overlay", "default": True, "link": {"table": target, "on": edge["to_col"], "from": edge["from_col"], "single": edge["cardinality"] == "many_to_one"}, @@ -402,7 +412,8 @@ AMAZON_TABLES = ( source="amzn_brand_analytics", label="Amazon brand analytics", pinned="search_term", - identity=("search_term",), + identity=("department_name", "search_term", "clicked_asin", "click_share_rank", + "_period_start", "_period_end"), # ⚠ keyed by SEARCH TERM, not by product, so it joins to nothing by default. Its ASIN # columns are the top-clicked ASINs for a term, a different relationship from "this row is # about this product", and a link would be misleading. @@ -796,6 +807,2543 @@ AMAZON_FIELDS = { ), } +#: S78-T01: the written label of every measured Amazon column, `{ut_key: {column: label}}`. +#: Generated once from the mirror's own headers, then edited by hand: at most 40 characters, +#: sentence case, no repeated word, no em or en dash. A label names a measured column and never +#: creates one, so this is NOT a column registry: `AMAZON_FIELDS` is, and it stays separate because +#: `amazon_schema._table_spec` extends a table's columns from it. A column absent here falls back to +#: `_label`. Labels are preset: no user rename path reads or writes this map. +AMAZON_LABELS = { + _AMZN + "orders": { + "amazon_order_id": "Amazon order ID", + "merchant_order_id": "Merchant order ID", + "purchase_date": "Purchase date", + "last_updated_date": "Last updated date", + "order_status": "Order status", + "fulfillment_channel": "Fulfillment channel", + "sales_channel": "Sales channel", + "order_channel": "Order channel", + "ship_service_level": "Ship service level", + "product_name": "Product name", + "sku": "SKU", + "asin": "ASIN", + "item_status": "Item status", + "quantity": "Quantity", + "currency": "Currency", + "item_price": "Item price", + "item_tax": "Item tax", + "shipping_price": "Shipping price", + "shipping_tax": "Shipping tax", + "gift_wrap_price": "Gift wrap price", + "gift_wrap_tax": "Gift wrap tax", + "item_promotion_discount": "Item promotion discount", + "ship_promotion_discount": "Ship promotion discount", + "ship_city": "Ship city", + "ship_state": "Ship state", + "ship_postal_code": "Ship postal code", + "ship_country": "Ship country", + "promotion_ids": "Promotion ids", + "cpf": "CPF", + "is_business_order": "Is business order", + "purchase_order_number": "Purchase order number", + "price_designation": "Price designation", + "signature_confirmation_recommended": "Signature confirmation recommended", + "order_item_id": "Order item ID", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + }, + _AMZN + "order_items": { + "amazon_order_id": "Amazon order ID", + "merchant_order_id": "Merchant order ID", + "purchase_date": "Purchase date", + "last_updated_date": "Last updated date", + "order_status": "Order status", + "fulfillment_channel": "Fulfillment channel", + "sales_channel": "Sales channel", + "order_channel": "Order channel", + "ship_service_level": "Ship service level", + "product_name": "Product name", + "sku": "SKU", + "asin": "ASIN", + "item_status": "Item status", + "quantity": "Quantity", + "currency": "Currency", + "item_price": "Item price", + "item_tax": "Item tax", + "shipping_price": "Shipping price", + "shipping_tax": "Shipping tax", + "gift_wrap_price": "Gift wrap price", + "gift_wrap_tax": "Gift wrap tax", + "item_promotion_discount": "Item promotion discount", + "ship_promotion_discount": "Ship promotion discount", + "ship_city": "Ship city", + "ship_state": "Ship state", + "ship_postal_code": "Ship postal code", + "ship_country": "Ship country", + "promotion_ids": "Promotion ids", + "cpf": "CPF", + "is_business_order": "Is business order", + "purchase_order_number": "Purchase order number", + "price_designation": "Price designation", + "signature_confirmation_recommended": "Signature confirmation recommended", + "order_item_id": "Order item ID", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + }, + _AMZN + "listings": { + "item_name": "Item name", + "item_description": "Item description", + "listing_id": "Listing ID", + "seller_sku": "Seller SKU", + "price": "Price", + "quantity": "Quantity", + "open_date": "Open date", + "image_url": "Image URL", + "item_is_marketplace": "Item is marketplace", + "product_id_type": "Product ID type", + "zshop_shipping_fee": "Zshop shipping fee", + "item_note": "Item note", + "item_condition": "Item condition", + "zshop_category1": "Zshop category", + "zshop_browse_path": "Zshop browse path", + "zshop_storefront_feature": "Zshop storefront feature", + "asin1": "ASIN", + "asin2": "ASIN 2", + "asin3": "ASIN 3", + "will_ship_internationally": "Will ship internationally", + "expedited_shipping": "Expedited shipping", + "zshop_boldface": "Zshop boldface", + "product_id": "Product ID", + "bid_for_featured_placement": "Bid for featured placement", + "add_delete": "Add delete", + "pending_quantity": "Pending quantity", + "fulfillment_channel": "Fulfillment channel", + "merchant_shipping_group": "Merchant shipping group", + "status": "Status", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + }, + _AMZN + "settlements": { + "settlement_id": "Settlement ID", + "settlement_start_date": "Settlement start date", + "settlement_end_date": "Settlement end date", + "deposit_date": "Deposit date", + "total_amount": "Total amount", + "currency": "Currency", + "transaction_type": "Transaction type", + "order_id": "Order ID", + "merchant_order_id": "Merchant order ID", + "adjustment_id": "Adjustment ID", + "shipment_id": "Shipment ID", + "marketplace_name": "Marketplace name", + "amount_type": "Amount type", + "amount_description": "Amount description", + "amount": "Amount", + "fulfillment_id": "Fulfillment ID", + "posted_date": "Posted date", + "posted_date_time": "Posted date time", + "order_item_code": "Order item code", + "merchant_order_item_id": "Merchant order item ID", + "merchant_adjustment_item_id": "Merchant adjustment item ID", + "sku": "SKU", + "quantity_purchased": "Quantity purchased", + "promotion_id": "Promotion ID", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + }, + _AMZN + "inventory": { + "_snapshot_date": "Synced on", + "inventory_source": "Inventory source", + }, + _AMZN + "pricing": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "parent_asin": "Parent ASIN", + "sales_by_asin_ordered_product_sales_amount": "Ordered product sales", + "sales_by_asin_ordered_product_sales_b2_b_amount": "Ordered product sales B2B", + "sales_by_asin_ordered_product_sales_b2_b_currency_code": "Ordered sales B2B currency", + "sales_by_asin_ordered_product_sales_currency_code": "Ordered sales currency", + "sales_by_asin_orders_shipped": "Orders shipped", + "sales_by_asin_orders_shipped_b2_b": "Orders shipped B2B", + "sales_by_asin_refund_rate": "Refund rate", + "sales_by_asin_refund_rate_b2_b": "Refund rate B2B", + "sales_by_asin_shipped_product_sales_amount": "Shipped product sales", + "sales_by_asin_shipped_product_sales_b2_b_amount": "Shipped product sales B2B", + "sales_by_asin_shipped_product_sales_b2_b_currency_code": "Shipped sales B2B currency", + "sales_by_asin_shipped_product_sales_currency_code": "Shipped sales currency", + "sales_by_asin_total_order_items": "Total order items", + "sales_by_asin_total_order_items_b2_b": "Total order items B2B", + "sales_by_asin_units_ordered": "Units ordered", + "sales_by_asin_units_ordered_b2_b": "Units ordered B2B", + "sales_by_asin_units_refunded": "Units refunded", + "sales_by_asin_units_refunded_b2_b": "Units refunded B2B", + "sales_by_asin_units_shipped": "Units shipped", + "sales_by_asin_units_shipped_b2_b": "Units shipped B2B", + "traffic_by_asin_browser_page_views": "Browser page views", + "traffic_by_asin_browser_page_views_b2_b": "Browser page views B2B", + "traffic_by_asin_browser_page_views_percentage": "Browser page views percentage", + "traffic_by_asin_browser_page_views_percentage_b2_b": "Browser page views percentage B2B", + "traffic_by_asin_browser_session_percentage": "Browser session percentage", + "traffic_by_asin_browser_session_percentage_b2_b": "Browser session percentage B2B", + "traffic_by_asin_browser_sessions": "Browser sessions", + "traffic_by_asin_browser_sessions_b2_b": "Browser sessions B2B", + "traffic_by_asin_buy_box_percentage": "Buy Box percentage", + "traffic_by_asin_buy_box_percentage_b2_b": "Buy Box percentage B2B", + "traffic_by_asin_mobile_app_page_views": "Mobile app page views", + "traffic_by_asin_mobile_app_page_views_b2_b": "Mobile app page views B2B", + "traffic_by_asin_mobile_app_page_views_percentage": "Mobile app page views percentage", + "traffic_by_asin_mobile_app_page_views_percentage_b2_b": "Mobile app page views percentage B2B", + "traffic_by_asin_mobile_app_session_percentage": "Mobile app session percentage", + "traffic_by_asin_mobile_app_session_percentage_b2_b": "Mobile app session percentage B2B", + "traffic_by_asin_mobile_app_sessions": "Mobile app sessions", + "traffic_by_asin_mobile_app_sessions_b2_b": "Mobile app sessions B2B", + "traffic_by_asin_page_views": "Page views", + "traffic_by_asin_page_views_b2_b": "Page views B2B", + "traffic_by_asin_page_views_percentage": "Page views percentage", + "traffic_by_asin_page_views_percentage_b2_b": "Page views percentage B2B", + "traffic_by_asin_session_percentage": "Session percentage", + "traffic_by_asin_session_percentage_b2_b": "Session percentage B2B", + "traffic_by_asin_sessions": "Sessions", + "traffic_by_asin_sessions_b2_b": "Sessions B2B", + "traffic_by_asin_unit_session_percentage": "Unit session percentage", + "traffic_by_asin_unit_session_percentage_b2_b": "Unit session percentage B2B", + "competitive_body_asin": "Competitive ASIN", + "competitive_body_featured_buying_options": "Featured buying options", + "competitive_body_marketplace_id": "Competitive marketplace ID", + "competitive_body_reference_prices": "Reference prices", + "competitive_status_reason_phrase": "Competitive status reason", + "competitive_status_status_code": "Competitive status code", + "child_asin": "Child ASIN", + "sku": "SKU", + }, + _AMZN + "brand_analytics": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "click_share": "Click share", + "click_share_rank": "Click share rank", + "clicked_asin": "Clicked ASIN", + "clicked_item_name": "Clicked item name", + "conversion_share": "Conversion share", + "department_name": "Department name", + "search_frequency_rank": "Search frequency rank", + "search_term": "Search term", + }, + _AMZN + "economics": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "ads_sponsored_product_fee_amount": "Sponsored products fee", + "ads_sponsored_product_fee_charge_1_amount_amount": "Sponsored products charge", + "ads_sponsored_product_fee_charge_1_amount_currency_code": "Sponsored products currency", + "ads_sponsored_product_fee_charge_1_amount_per_unit": "Sponsored products unit detail", + "ads_sponsored_product_fee_charge_1_amount_per_unit_amount": "Sponsored products per unit", + "ads_sponsored_product_fee_charge_1_amount_per_unit_currency_code": "Sponsored products unit currency", + "ads_sponsored_product_fee_charge_1_promotion_amount_amount": "Sponsored products promo", + "ads_sponsored_product_fee_charge_1_promotion_amount_currency_code": "Sponsored products promo currency", + "ads_sponsored_product_fee_charge_1_quantity": "Sponsored products quantity", + "ads_sponsored_product_fee_charge_1_tax_amount_amount": "Sponsored products tax", + "ads_sponsored_product_fee_charge_1_tax_amount_currency_code": "Sponsored products tax currency", + "ads_sponsored_product_fee_charge_1_total_amount_amount": "Sponsored products total", + "ads_sponsored_product_fee_charge_1_total_amount_currency_code": "Sponsored products total currency", + "ads_sponsored_product_fee_type_name": "Sponsored products fee type", + "child_asin": "Child ASIN", + "cost_cost_of_goods_sold": "Cost of goods sold", + "cost_fba_cost": "FBA cost", + "cost_mfn_cost": "MFN cost", + "cost_miscellaneous_cost": "Miscellaneous cost", + "currency_code": "Currency", + "end_date": "End date", + "fee_customer_return_hrrunit_fee_amount": "HRR return fee", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_amount_amount": "HRR return fee charge", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_amount_currency_code": "HRR return fee currency", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_amount_per_unit_amount": "HRR return fee per unit", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "HRR return fee unit currency", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_amount_per_unit_delta": "HRR return fee unit change", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_promotion_amount_amount": "HRR return fee promo", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "HRR return fee promo currency", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_quantity": "HRR return fee quantity", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_tax_amount_amount": "HRR return fee tax", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_tax_amount_currency_code": "HRR return fee tax currency", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_total_amount_amount": "HRR return fee total", + "fee_customer_return_hrrunit_fee_charge_1_aggregated_detail_total_amount_currency_code": "HRR return fee total currency", + "fee_customer_return_hrrunit_fee_charge_1_end_date": "HRR return fee end date", + "fee_customer_return_hrrunit_fee_charge_1_identifier": "HRR return fee charge ID", + "fee_customer_return_hrrunit_fee_charge_1_start_date": "HRR return fee start date", + "fee_customer_return_hrrunit_fee_type_name": "HRR return fee type", + "fee_disposal_fee_amount": "Disposal fee", + "fee_disposal_fee_charge_1_aggregated_detail_amount_amount": "Disposal fee charge", + "fee_disposal_fee_charge_1_aggregated_detail_amount_currency_code": "Disposal fee currency", + "fee_disposal_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Disposal fee per unit", + "fee_disposal_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Disposal fee unit currency", + "fee_disposal_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Disposal fee unit change", + "fee_disposal_fee_charge_1_aggregated_detail_promotion_amount_amount": "Disposal fee promo", + "fee_disposal_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Disposal fee promo currency", + "fee_disposal_fee_charge_1_aggregated_detail_quantity": "Disposal fee quantity", + "fee_disposal_fee_charge_1_aggregated_detail_tax_amount_amount": "Disposal fee tax", + "fee_disposal_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Disposal fee tax currency", + "fee_disposal_fee_charge_1_aggregated_detail_total_amount_amount": "Disposal fee total", + "fee_disposal_fee_charge_1_aggregated_detail_total_amount_currency_code": "Disposal fee total currency", + "fee_disposal_fee_charge_1_end_date": "Disposal fee end date", + "fee_disposal_fee_charge_1_identifier": "Disposal fee charge ID", + "fee_disposal_fee_charge_1_start_date": "Disposal fee start date", + "fee_disposal_fee_type_name": "Disposal fee type", + "fee_fba_fulfilment_fee_amount": "FBA fulfillment fee", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_amount_amount": "FBA fulfillment fee charge", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_amount_currency_code": "FBA fulfillment fee currency", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_amount_per_unit_amount": "FBA fulfillment fee per unit", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "FBA fulfillment fee unit currency", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_amount_per_unit_delta": "FBA fulfillment fee unit change", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_promotion_amount_amount": "FBA fulfillment fee promo", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "FBA fulfillment fee promo currency", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_quantity": "FBA fulfillment fee quantity", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_tax_amount_amount": "FBA fulfillment fee tax", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_tax_amount_currency_code": "FBA fulfillment fee tax currency", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_total_amount_amount": "FBA fulfillment fee total", + "fee_fba_fulfilment_fee_charge_1_aggregated_detail_total_amount_currency_code": "FBA fulfillment fee total currency", + "fee_fba_fulfilment_fee_charge_1_end_date": "FBA fulfillment fee end date", + "fee_fba_fulfilment_fee_charge_1_identifier": "FBA fulfillment fee charge ID", + "fee_fba_fulfilment_fee_charge_1_start_date": "FBA fulfillment fee start date", + "fee_fba_fulfilment_fee_type_name": "FBA fulfillment fee type", + "fee_fba_inbound_convenience_fee_amount": "Inbound placement fee", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_amount_amount": "Inbound placement fee charge", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_amount_currency_code": "Inbound placement fee currency", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Inbound placement fee per unit", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Inbound placement fee unit currency", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Inbound placement fee unit change", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_promotion_amount_amount": "Inbound placement fee promo", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Inbound placement fee promo currency", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_quantity": "Inbound placement fee quantity", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_tax_amount_amount": "Inbound placement fee tax", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Inbound placement fee tax currency", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_total_amount_amount": "Inbound placement fee total", + "fee_fba_inbound_convenience_fee_charge_1_aggregated_detail_total_amount_currency_code": "Inbound placement fee total currency", + "fee_fba_inbound_convenience_fee_charge_1_end_date": "Inbound placement fee end date", + "fee_fba_inbound_convenience_fee_charge_1_identifier": "Inbound placement fee charge ID", + "fee_fba_inbound_convenience_fee_charge_1_start_date": "Inbound placement fee start date", + "fee_fba_inbound_convenience_fee_type_name": "Inbound placement fee type", + "fee_fba_storage_fee_amount": "FBA storage fee", + "fee_fba_storage_fee_charge_1_aggregated_detail_amount_amount": "FBA storage fee charge", + "fee_fba_storage_fee_charge_1_aggregated_detail_amount_currency_code": "FBA storage fee currency", + "fee_fba_storage_fee_charge_1_aggregated_detail_amount_per_unit_amount": "FBA storage fee per unit", + "fee_fba_storage_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "FBA storage fee unit currency", + "fee_fba_storage_fee_charge_1_aggregated_detail_amount_per_unit_delta": "FBA storage fee unit change", + "fee_fba_storage_fee_charge_1_aggregated_detail_promotion_amount_amount": "FBA storage fee promo", + "fee_fba_storage_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "FBA storage fee promo currency", + "fee_fba_storage_fee_charge_1_aggregated_detail_quantity": "FBA storage fee quantity", + "fee_fba_storage_fee_charge_1_aggregated_detail_tax_amount_amount": "FBA storage fee tax", + "fee_fba_storage_fee_charge_1_aggregated_detail_tax_amount_currency_code": "FBA storage fee tax currency", + "fee_fba_storage_fee_charge_1_aggregated_detail_total_amount_amount": "FBA storage fee total", + "fee_fba_storage_fee_charge_1_aggregated_detail_total_amount_currency_code": "FBA storage fee total currency", + "fee_fba_storage_fee_charge_1_end_date": "FBA storage fee end date", + "fee_fba_storage_fee_charge_1_identifier": "FBA storage fee charge ID", + "fee_fba_storage_fee_charge_1_start_date": "FBA storage fee start date", + "fee_fba_storage_fee_type_name": "FBA storage fee type", + "fee_fbainventory_reimbursement_amount": "Inventory reimbursement", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_amount_amount": "Inventory reimbursement charge", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_amount_currency_code": "Inventory reimbursement currency", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_amount_per_unit_amount": "Inventory reimbursement per unit", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_amount_per_unit_currency_code": "Inventory reimbursement unit currency", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_amount_per_unit_delta": "Inventory reimbursement unit change", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_promotion_amount_amount": "Inventory reimbursement promo", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_promotion_amount_currency_code": "Inventory reimbursement promo currency", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_quantity": "Inventory reimbursement quantity", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_tax_amount_amount": "Inventory reimbursement tax", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_tax_amount_currency_code": "Inventory reimbursement tax currency", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_total_amount_amount": "Inventory reimbursement total", + "fee_fbainventory_reimbursement_charge_1_aggregated_detail_total_amount_currency_code": "Inventory reimbursement total currency", + "fee_fbainventory_reimbursement_charge_1_end_date": "Inventory reimbursement end date", + "fee_fbainventory_reimbursement_charge_1_identifier": "Inventory reimbursement charge ID", + "fee_fbainventory_reimbursement_charge_1_start_date": "Inventory reimbursement start date", + "fee_fbainventory_reimbursement_type_name": "Inventory reimbursement type", + "fee_liquidation_processing_fee_amount": "Liquidation fee", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_amount_amount": "Liquidation fee charge", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_amount_currency_code": "Liquidation fee currency", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Liquidation fee per unit", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Liquidation fee unit currency", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Liquidation fee unit change", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_promotion_amount_amount": "Liquidation fee promo", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Liquidation fee promo currency", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_quantity": "Liquidation fee quantity", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_tax_amount_amount": "Liquidation fee tax", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Liquidation fee tax currency", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_total_amount_amount": "Liquidation fee total", + "fee_liquidation_processing_fee_charge_1_aggregated_detail_total_amount_currency_code": "Liquidation fee total currency", + "fee_liquidation_processing_fee_charge_1_end_date": "Liquidation fee end date", + "fee_liquidation_processing_fee_charge_1_identifier": "Liquidation fee charge ID", + "fee_liquidation_processing_fee_charge_1_start_date": "Liquidation fee start date", + "fee_liquidation_processing_fee_type_name": "Liquidation fee type", + "fee_liquidation_referral_fee_amount": "Liquidation referral fee", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_amount_amount": "Liquidation referral fee charge", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_amount_currency_code": "Liquidation referral fee currency", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Liquidation referral fee per unit", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Liquidation referral fee unit currency", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Liquidation referral fee unit change", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_promotion_amount_amount": "Liquidation referral fee promo", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Liquidation referral fee promo currency", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_quantity": "Liquidation referral fee quantity", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_tax_amount_amount": "Liquidation referral fee tax", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Liquidation referral fee tax currency", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_total_amount_amount": "Liquidation referral fee total", + "fee_liquidation_referral_fee_charge_1_aggregated_detail_total_amount_currency_code": "Liquidation referral fee total currency", + "fee_liquidation_referral_fee_charge_1_end_date": "Liquidation referral fee end date", + "fee_liquidation_referral_fee_charge_1_identifier": "Liquidation referral fee charge ID", + "fee_liquidation_referral_fee_charge_1_start_date": "Liquidation referral fee start date", + "fee_liquidation_referral_fee_type_name": "Liquidation referral fee type", + "fee_long_term_storage_fee_amount": "Long term storage fee", + "fee_long_term_storage_fee_charge_1_aggregated_detail_amount_amount": "Long term storage fee charge", + "fee_long_term_storage_fee_charge_1_aggregated_detail_amount_currency_code": "Long term storage fee currency", + "fee_long_term_storage_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Long term storage fee per unit", + "fee_long_term_storage_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Long term storage fee unit currency", + "fee_long_term_storage_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Long term storage fee unit change", + "fee_long_term_storage_fee_charge_1_aggregated_detail_promotion_amount_amount": "Long term storage fee promo", + "fee_long_term_storage_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Long term storage fee promo currency", + "fee_long_term_storage_fee_charge_1_aggregated_detail_quantity": "Long term storage fee quantity", + "fee_long_term_storage_fee_charge_1_aggregated_detail_tax_amount_amount": "Long term storage fee tax", + "fee_long_term_storage_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Long term storage fee tax currency", + "fee_long_term_storage_fee_charge_1_aggregated_detail_total_amount_amount": "Long term storage fee total", + "fee_long_term_storage_fee_charge_1_aggregated_detail_total_amount_currency_code": "Long term storage fee total currency", + "fee_long_term_storage_fee_charge_1_end_date": "Long term storage fee end date", + "fee_long_term_storage_fee_charge_1_identifier": "Long term storage fee charge ID", + "fee_long_term_storage_fee_charge_1_start_date": "Long term storage fee start date", + "fee_long_term_storage_fee_type_name": "Long term storage fee type", + "fee_referral_fee_amount": "Referral fee", + "fee_referral_fee_charge_1_aggregated_detail_amount_amount": "Referral fee charge", + "fee_referral_fee_charge_1_aggregated_detail_amount_currency_code": "Referral fee currency", + "fee_referral_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Referral fee per unit", + "fee_referral_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Referral fee unit currency", + "fee_referral_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Referral fee unit change", + "fee_referral_fee_charge_1_aggregated_detail_promotion_amount_amount": "Referral fee promo", + "fee_referral_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Referral fee promo currency", + "fee_referral_fee_charge_1_aggregated_detail_quantity": "Referral fee quantity", + "fee_referral_fee_charge_1_aggregated_detail_tax_amount_amount": "Referral fee tax", + "fee_referral_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Referral fee tax currency", + "fee_referral_fee_charge_1_aggregated_detail_total_amount_amount": "Referral fee total", + "fee_referral_fee_charge_1_aggregated_detail_total_amount_currency_code": "Referral fee total currency", + "fee_referral_fee_charge_1_end_date": "Referral fee end date", + "fee_referral_fee_charge_1_identifier": "Referral fee charge ID", + "fee_referral_fee_charge_1_start_date": "Referral fee start date", + "fee_referral_fee_type_name": "Referral fee type", + "fee_refund_commission_fee_amount": "Refund commission fee", + "fee_refund_commission_fee_charge_1_aggregated_detail_amount_amount": "Refund commission fee charge", + "fee_refund_commission_fee_charge_1_aggregated_detail_amount_currency_code": "Refund commission fee currency", + "fee_refund_commission_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Refund commission fee per unit", + "fee_refund_commission_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Refund commission fee unit currency", + "fee_refund_commission_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Refund commission fee unit change", + "fee_refund_commission_fee_charge_1_aggregated_detail_promotion_amount_amount": "Refund commission fee promo", + "fee_refund_commission_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Refund commission fee promo currency", + "fee_refund_commission_fee_charge_1_aggregated_detail_quantity": "Refund commission fee quantity", + "fee_refund_commission_fee_charge_1_aggregated_detail_tax_amount_amount": "Refund commission fee tax", + "fee_refund_commission_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Refund commission fee tax currency", + "fee_refund_commission_fee_charge_1_aggregated_detail_total_amount_amount": "Refund commission fee total", + "fee_refund_commission_fee_charge_1_aggregated_detail_total_amount_currency_code": "Refund commission fee total currency", + "fee_refund_commission_fee_charge_1_end_date": "Refund commission fee end date", + "fee_refund_commission_fee_charge_1_identifier": "Refund commission fee charge ID", + "fee_refund_commission_fee_charge_1_start_date": "Refund commission fee start date", + "fee_refund_commission_fee_type_name": "Refund commission fee type", + "fee_refunded_referral_fee_amount": "Refunded referral fee", + "fee_refunded_referral_fee_charge_1_aggregated_detail_amount_amount": "Refunded referral fee charge", + "fee_refunded_referral_fee_charge_1_aggregated_detail_amount_currency_code": "Refunded referral fee currency", + "fee_refunded_referral_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Refunded referral fee per unit", + "fee_refunded_referral_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Refunded referral fee unit currency", + "fee_refunded_referral_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Refunded referral fee unit change", + "fee_refunded_referral_fee_charge_1_aggregated_detail_promotion_amount_amount": "Refunded referral fee promo", + "fee_refunded_referral_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Refunded referral fee promo currency", + "fee_refunded_referral_fee_charge_1_aggregated_detail_quantity": "Refunded referral fee quantity", + "fee_refunded_referral_fee_charge_1_aggregated_detail_tax_amount_amount": "Refunded referral fee tax", + "fee_refunded_referral_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Refunded referral fee tax currency", + "fee_refunded_referral_fee_charge_1_aggregated_detail_total_amount_amount": "Refunded referral fee total", + "fee_refunded_referral_fee_charge_1_aggregated_detail_total_amount_currency_code": "Refunded referral fee total currency", + "fee_refunded_referral_fee_charge_1_end_date": "Refunded referral fee end date", + "fee_refunded_referral_fee_charge_1_identifier": "Refunded referral fee charge ID", + "fee_refunded_referral_fee_charge_1_start_date": "Refunded referral fee start date", + "fee_refunded_referral_fee_type_name": "Refunded referral fee type", + "fee_removal_fee_amount": "Removal fee", + "fee_removal_fee_charge_1_aggregated_detail_amount_amount": "Removal fee charge", + "fee_removal_fee_charge_1_aggregated_detail_amount_currency_code": "Removal fee currency", + "fee_removal_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Removal fee per unit", + "fee_removal_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Removal fee unit currency", + "fee_removal_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Removal fee unit change", + "fee_removal_fee_charge_1_aggregated_detail_promotion_amount_amount": "Removal fee promo", + "fee_removal_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Removal fee promo currency", + "fee_removal_fee_charge_1_aggregated_detail_quantity": "Removal fee quantity", + "fee_removal_fee_charge_1_aggregated_detail_tax_amount_amount": "Removal fee tax", + "fee_removal_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Removal fee tax currency", + "fee_removal_fee_charge_1_aggregated_detail_total_amount_amount": "Removal fee total", + "fee_removal_fee_charge_1_aggregated_detail_total_amount_currency_code": "Removal fee total currency", + "fee_removal_fee_charge_1_end_date": "Removal fee end date", + "fee_removal_fee_charge_1_identifier": "Removal fee charge ID", + "fee_removal_fee_charge_1_start_date": "Removal fee start date", + "fee_removal_fee_type_name": "Removal fee type", + "fees_total_amount": "Total fees", + "fnsku": "FNSKU", + "marketplace_id": "Marketplace ID", + "msku": "MSKU", + "net_proceeds_per_unit": "Net proceeds unit detail", + "net_proceeds_per_unit_amount": "Net proceeds per unit", + "net_proceeds_per_unit_currency_code": "Net proceeds unit currency", + "net_proceeds_total_amount": "Net proceeds", + "net_proceeds_total_currency_code": "Net proceeds currency", + "parent_asin": "Parent ASIN", + "sales_average_selling_price_amount": "Average selling price", + "sales_average_selling_price_currency_code": "Average selling price currency", + "sales_net_product_sales_amount": "Net product sales", + "sales_net_product_sales_currency_code": "Net product sales currency", + "sales_net_units_sold": "Net units sold", + "sales_ordered_product_sales_amount": "Ordered product sales", + "sales_ordered_product_sales_currency_code": "Ordered product sales currency", + "sales_refunded_product_sales_amount": "Refunded product sales", + "sales_refunded_product_sales_currency_code": "Refunded product sales currency", + "sales_units_ordered": "Units ordered", + "sales_units_refunded": "Units refunded", + "start_date": "Start date", + "fee_deal_participation_fee_amount": "Deal participation fee", + "fee_deal_participation_fee_charge_1_aggregated_detail_amount_amount": "Deal participation fee charge", + "fee_deal_participation_fee_charge_1_aggregated_detail_amount_currency_code": "Deal participation fee currency", + "fee_deal_participation_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Deal participation fee per unit", + "fee_deal_participation_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Deal participation fee unit currency", + "fee_deal_participation_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Deal participation fee unit change", + "fee_deal_participation_fee_charge_1_aggregated_detail_promotion_amount_amount": "Deal participation fee promo", + "fee_deal_participation_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Deal participation fee promo currency", + "fee_deal_participation_fee_charge_1_aggregated_detail_quantity": "Deal participation fee quantity", + "fee_deal_participation_fee_charge_1_aggregated_detail_tax_amount_amount": "Deal participation fee tax", + "fee_deal_participation_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Deal participation fee tax currency", + "fee_deal_participation_fee_charge_1_aggregated_detail_total_amount_amount": "Deal participation fee total", + "fee_deal_participation_fee_charge_1_aggregated_detail_total_amount_currency_code": "Deal participation fee total currency", + "fee_deal_participation_fee_charge_1_end_date": "Deal participation fee end date", + "fee_deal_participation_fee_charge_1_identifier": "Deal participation fee charge ID", + "fee_deal_participation_fee_charge_1_start_date": "Deal participation fee start date", + "fee_deal_participation_fee_type_name": "Deal participation fee type", + "fee_deal_performance_fee_amount": "Deal performance fee", + "fee_deal_performance_fee_charge_1_aggregated_detail_amount_amount": "Deal performance fee charge", + "fee_deal_performance_fee_charge_1_aggregated_detail_amount_currency_code": "Deal performance fee currency", + "fee_deal_performance_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Deal performance fee per unit", + "fee_deal_performance_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Deal performance fee unit currency", + "fee_deal_performance_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Deal performance fee unit change", + "fee_deal_performance_fee_charge_1_aggregated_detail_promotion_amount_amount": "Deal performance fee promo", + "fee_deal_performance_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Deal performance fee promo currency", + "fee_deal_performance_fee_charge_1_aggregated_detail_quantity": "Deal performance fee quantity", + "fee_deal_performance_fee_charge_1_aggregated_detail_tax_amount_amount": "Deal performance fee tax", + "fee_deal_performance_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Deal performance fee tax currency", + "fee_deal_performance_fee_charge_1_aggregated_detail_total_amount_amount": "Deal performance fee total", + "fee_deal_performance_fee_charge_1_aggregated_detail_total_amount_currency_code": "Deal performance fee total currency", + "fee_deal_performance_fee_charge_1_end_date": "Deal performance fee end date", + "fee_deal_performance_fee_charge_1_identifier": "Deal performance fee charge ID", + "fee_deal_performance_fee_charge_1_start_date": "Deal performance fee start date", + "fee_deal_performance_fee_type_name": "Deal performance fee type", + "fee_fba_storage_fee_charge_1_aggregated_detail_amount_per_unit": "FBA storage fee unit detail", + "fee_fba_inbound_transportation_fee_amount": "Inbound transport fee", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_amount_amount": "Inbound transport fee charge", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_amount_currency_code": "Inbound transport fee currency", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Inbound transport fee per unit", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Inbound transport fee unit currency", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Inbound transport fee unit change", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_promotion_amount_amount": "Inbound transport fee promo", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Inbound transport fee promo currency", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_quantity": "Inbound transport fee quantity", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_tax_amount_amount": "Inbound transport fee tax", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Inbound transport fee tax currency", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_total_amount_amount": "Inbound transport fee total", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_total_amount_currency_code": "Inbound transport fee total currency", + "fee_fba_inbound_transportation_fee_charge_1_end_date": "Inbound transport fee end date", + "fee_fba_inbound_transportation_fee_charge_1_identifier": "Inbound transport fee charge ID", + "fee_fba_inbound_transportation_fee_charge_1_start_date": "Inbound transport fee start date", + "fee_fba_inbound_transportation_fee_type_name": "Inbound transport fee type", + "fee_coupon_participation_fee_amount": "Coupon participation fee", + "fee_coupon_participation_fee_charge_1_aggregated_detail_amount_amount": "Coupon participation fee charge", + "fee_coupon_participation_fee_charge_1_aggregated_detail_amount_currency_code": "Coupon participation fee currency", + "fee_coupon_participation_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Coupon participation fee per unit", + "fee_coupon_participation_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Coupon participation fee unit currency", + "fee_coupon_participation_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Coupon participation fee unit change", + "fee_coupon_participation_fee_charge_1_aggregated_detail_promotion_amount_amount": "Coupon participation fee promo", + "fee_coupon_participation_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Coupon participation fee promo currency", + "fee_coupon_participation_fee_charge_1_aggregated_detail_quantity": "Coupon participation fee quantity", + "fee_coupon_participation_fee_charge_1_aggregated_detail_tax_amount_amount": "Coupon participation fee tax", + "fee_coupon_participation_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Coupon participation fee tax currency", + "fee_coupon_participation_fee_charge_1_aggregated_detail_total_amount_amount": "Coupon participation fee total", + "fee_coupon_participation_fee_charge_1_aggregated_detail_total_amount_currency_code": "Coupon participation fee total currency", + "fee_coupon_participation_fee_charge_1_end_date": "Coupon participation fee end date", + "fee_coupon_participation_fee_charge_1_identifier": "Coupon participation fee charge ID", + "fee_coupon_participation_fee_charge_1_start_date": "Coupon participation fee start date", + "fee_coupon_participation_fee_type_name": "Coupon participation fee type", + "fee_coupon_performance_fee_amount": "Coupon performance fee", + "fee_coupon_performance_fee_charge_1_aggregated_detail_amount_amount": "Coupon performance fee charge", + "fee_coupon_performance_fee_charge_1_aggregated_detail_amount_currency_code": "Coupon performance fee currency", + "fee_coupon_performance_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Coupon performance fee per unit", + "fee_coupon_performance_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Coupon performance fee unit currency", + "fee_coupon_performance_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Coupon performance fee unit change", + "fee_coupon_performance_fee_charge_1_aggregated_detail_promotion_amount_amount": "Coupon performance fee promo", + "fee_coupon_performance_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Coupon performance fee promo currency", + "fee_coupon_performance_fee_charge_1_aggregated_detail_quantity": "Coupon performance fee quantity", + "fee_coupon_performance_fee_charge_1_aggregated_detail_tax_amount_amount": "Coupon performance fee tax", + "fee_coupon_performance_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Coupon performance fee tax currency", + "fee_coupon_performance_fee_charge_1_aggregated_detail_total_amount_amount": "Coupon performance fee total", + "fee_coupon_performance_fee_charge_1_aggregated_detail_total_amount_currency_code": "Coupon performance fee total currency", + "fee_coupon_performance_fee_charge_1_end_date": "Coupon performance fee end date", + "fee_coupon_performance_fee_charge_1_identifier": "Coupon performance fee charge ID", + "fee_coupon_performance_fee_charge_1_start_date": "Coupon performance fee start date", + "fee_coupon_performance_fee_type_name": "Coupon performance fee type", + "fee_coupon_redemption_fee_amount": "Coupon redemption fee", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_amount_amount": "Coupon redemption fee charge", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_amount_currency_code": "Coupon redemption fee currency", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Coupon redemption fee per unit", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Coupon redemption fee unit currency", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Coupon redemption fee unit change", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_promotion_amount_amount": "Coupon redemption fee promo", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Coupon redemption fee promo currency", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_quantity": "Coupon redemption fee quantity", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_tax_amount_amount": "Coupon redemption fee tax", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Coupon redemption fee tax currency", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_total_amount_amount": "Coupon redemption fee total", + "fee_coupon_redemption_fee_charge_1_aggregated_detail_total_amount_currency_code": "Coupon redemption fee total currency", + "fee_coupon_redemption_fee_charge_1_end_date": "Coupon redemption fee end date", + "fee_coupon_redemption_fee_charge_1_identifier": "Coupon redemption fee charge ID", + "fee_coupon_redemption_fee_charge_1_start_date": "Coupon redemption fee start date", + "fee_coupon_redemption_fee_type_name": "Coupon redemption fee type", + "fee_run_lightning_deal_fee_amount": "Lightning deal fee", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_amount_amount": "Lightning deal fee charge", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_amount_currency_code": "Lightning deal fee currency", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_amount_per_unit_amount": "Lightning deal fee per unit", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_amount_per_unit_currency_code": "Lightning deal fee unit currency", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_amount_per_unit_delta": "Lightning deal fee unit change", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_promotion_amount_amount": "Lightning deal fee promo", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_promotion_amount_currency_code": "Lightning deal fee promo currency", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_quantity": "Lightning deal fee quantity", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_tax_amount_amount": "Lightning deal fee tax", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_tax_amount_currency_code": "Lightning deal fee tax currency", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_total_amount_amount": "Lightning deal fee total", + "fee_run_lightning_deal_fee_charge_1_aggregated_detail_total_amount_currency_code": "Lightning deal fee total currency", + "fee_run_lightning_deal_fee_charge_1_end_date": "Lightning deal fee end date", + "fee_run_lightning_deal_fee_charge_1_identifier": "Lightning deal fee charge ID", + "fee_run_lightning_deal_fee_charge_1_start_date": "Lightning deal fee start date", + "fee_run_lightning_deal_fee_type_name": "Lightning deal fee type", + "fee_fba_inbound_transportation_fee_charge_1_aggregated_detail_amount_per_unit": "Inbound transport fee unit detail", + }, + _AMZN + "transactions": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "amazon_order_id": "Amazon order ID", + "asin": "ASIN", + "contexts": "Contexts", + "currency_code": "Currency", + "description": "Description", + "item_breakdown_amazon_fees_amount": "Item Amazon fees", + "item_breakdown_amazon_fees_amount_currency_code": "Item Amazon fees currency", + "item_breakdown_base_amount": "Item base", + "item_breakdown_base_amount_currency_code": "Item base currency", + "item_breakdown_fbafees_amount": "Item FBA fees", + "item_breakdown_fbafees_amount_currency_code": "Item FBA fees currency", + "item_breakdown_fbainventory_reimbursement_amount": "Item inventory reimbursement", + "item_breakdown_fbainventory_reimbursement_amount_currency_code": "Item inventory reimbursement currency", + "item_breakdown_fbareversed_reimbursement_amount": "Item FBA reversed reimbursement", + "item_breakdown_fbareversed_reimbursement_amount_currency_code": "Item FBA reversed reimbursement currency", + "item_breakdown_other_amount": "Item other", + "item_breakdown_other_amount_currency_code": "Item other currency", + "item_breakdown_product_charges_amount": "Item product charges", + "item_breakdown_product_charges_amount_currency_code": "Item product charges currency", + "item_breakdown_promo_rebates_amount": "Item promo rebates", + "item_breakdown_promo_rebates_amount_currency_code": "Item promo rebates currency", + "item_breakdown_recommerce_liquidation_amount": "Item recommerce liquidation", + "item_breakdown_recommerce_liquidation_amount_currency_code": "Item recommerce liquidation currency", + "item_breakdown_retrocharge_amount": "Item retrocharge", + "item_breakdown_retrocharge_amount_currency_code": "Item retrocharge currency", + "item_breakdown_shipping_amount": "Item shipping", + "item_breakdown_shipping_amount_currency_code": "Item shipping currency", + "item_breakdown_tax_amount": "Item tax", + "item_breakdown_tax_amount_currency_code": "Item tax currency", + "item_contexts": "Item contexts", + "item_description": "Item description", + "item_position": "Item position", + "item_related_identifiers": "Item related identifiers", + "item_sub_breakdown_amazon_fees__commission_amount": "Item commission", + "item_sub_breakdown_amazon_fees__commission_amount_currency_code": "Item commission currency", + "item_sub_breakdown_amazon_fees__fbaper_unit_fulfillment_fee_amount": "Item FBA fulfillment fee", + "item_sub_breakdown_amazon_fees__fbaper_unit_fulfillment_fee_amount_currency_code": "Item FBA fulfillment fee currency", + "item_sub_breakdown_amazon_fees__giftwrap_chargeback_amount": "Item giftwrap chargeback", + "item_sub_breakdown_amazon_fees__giftwrap_chargeback_amount_currency_code": "Item giftwrap chargeback currency", + "item_sub_breakdown_amazon_fees__refund_commission_amount": "Item refund commission", + "item_sub_breakdown_amazon_fees__refund_commission_amount_currency_code": "Item refund commission currency", + "item_sub_breakdown_amazon_fees__shipping_chargeback_amount": "Item shipping chargeback", + "item_sub_breakdown_amazon_fees__shipping_chargeback_amount_currency_code": "Item shipping chargeback currency", + "item_sub_breakdown_amazon_fees__subscription_amount": "Item subscription", + "item_sub_breakdown_amazon_fees__subscription_amount_currency_code": "Item subscription currency", + "item_sub_breakdown_amazon_fees__variable_closing_fee_amount": "Item variable closing fee", + "item_sub_breakdown_amazon_fees__variable_closing_fee_amount_currency_code": "Item variable closing fee currency", + "item_sub_breakdown_fbafees__fbadisposal_fee_amount": "Item FBA fees disposal", + "item_sub_breakdown_fbafees__fbadisposal_fee_amount_currency_code": "Item FBA fees disposal currency", + "item_sub_breakdown_fbafees__fbaper_unit_fulfillment_fee_amount": "Item FBA fees fulfillment", + "item_sub_breakdown_fbafees__fbaper_unit_fulfillment_fee_amount_currency_code": "Item FBA fees fulfillment currency", + "item_sub_breakdown_fbafees__fbaremoval_fee_amount": "Item FBA fees removal", + "item_sub_breakdown_fbafees__fbaremoval_fee_amount_currency_code": "Item FBA fees removal currency", + "item_sub_breakdown_fbafees__hrrnon_apparel_rollup_amount": "Item FBA fees HRR non apparel", + "item_sub_breakdown_fbafees__hrrnon_apparel_rollup_amount_currency_code": "Item FBA fees HRR non apparel currency", + "item_sub_breakdown_fbafees__inbound_convenience_charge_amount": "Item FBA fees inbound placement", + "item_sub_breakdown_fbafees__inbound_convenience_charge_amount_currency_code": "Item FBA fees inbound placement currency", + "item_sub_breakdown_fbafees__long_term_storage_fee_amount": "Item FBA fees long term storage", + "item_sub_breakdown_fbafees__long_term_storage_fee_amount_currency_code": "Item FBA fees long term storage currency", + "item_sub_breakdown_fbafees__storage_billing_fee_amount": "Item FBA fees storage billing", + "item_sub_breakdown_fbafees__storage_billing_fee_amount_currency_code": "Item FBA fees storage billing currency", + "item_sub_breakdown_other__giftwrap_principal_amount": "Item giftwrap principal", + "item_sub_breakdown_other__giftwrap_principal_amount_currency_code": "Item giftwrap principal currency", + "item_sub_breakdown_other__restocking_deduction_principal_amount": "Item restocking deduction", + "item_sub_breakdown_other__restocking_deduction_principal_amount_currency_code": "Item restocking deduction currency", + "item_sub_breakdown_product_charges__our_price_principal_amount": "Item our price principal", + "item_sub_breakdown_product_charges__our_price_principal_amount_currency_code": "Item our price principal currency", + "item_sub_breakdown_product_charges__principal_amount": "Item principal", + "item_sub_breakdown_product_charges__principal_amount_currency_code": "Item principal currency", + "item_sub_breakdown_promo_rebates__our_price_discount_amount": "Item our price discount", + "item_sub_breakdown_promo_rebates__our_price_discount_amount_currency_code": "Item our price discount currency", + "item_sub_breakdown_promo_rebates__shipping_discount_amount": "Item shipping discount", + "item_sub_breakdown_promo_rebates__shipping_discount_amount_currency_code": "Item shipping discount currency", + "item_sub_breakdown_retrocharge__base_tax_amount": "Item retrocharge base tax", + "item_sub_breakdown_retrocharge__base_tax_amount_currency_code": "Item retrocharge base tax currency", + "item_sub_breakdown_retrocharge__shipping_tax_amount": "Item retrocharge shipping tax", + "item_sub_breakdown_retrocharge__shipping_tax_amount_currency_code": "Item retrocharge shipping tax currency", + "item_sub_breakdown_shipping__shipping_principal_amount": "Item shipping principal", + "item_sub_breakdown_shipping__shipping_principal_amount_currency_code": "Item shipping principal currency", + "item_sub_breakdown_tax__giftwrap_tax_amount": "Item giftwrap tax", + "item_sub_breakdown_tax__giftwrap_tax_amount_currency_code": "Item giftwrap tax currency", + "item_sub_breakdown_tax__low_value_goods_tax_principal_amount": "Item low value goods tax", + "item_sub_breakdown_tax__low_value_goods_tax_principal_amount_currency_code": "Item low value goods tax currency", + "item_sub_breakdown_tax__low_value_goods_tax_shipping_amount": "Item low value goods ship tax", + "item_sub_breakdown_tax__low_value_goods_tax_shipping_amount_currency_code": "Item low value goods ship tax currency", + "item_sub_breakdown_tax__marketplace_facilitator_tax_other_amount": "Item facilitator tax other", + "item_sub_breakdown_tax__marketplace_facilitator_tax_other_amount_currency_code": "Item facilitator tax other currency", + "item_sub_breakdown_tax__marketplace_facilitator_tax_principal_amount": "Item facilitator tax", + "item_sub_breakdown_tax__marketplace_facilitator_tax_principal_amount_currency_code": "Item facilitator tax currency", + "item_sub_breakdown_tax__marketplace_facilitator_tax_shipping_amount": "Item facilitator ship tax", + "item_sub_breakdown_tax__marketplace_facilitator_tax_shipping_amount_currency_code": "Item facilitator ship tax currency", + "item_sub_breakdown_tax__marketplace_facilitator_vat_principal_amount": "Item facilitator VAT", + "item_sub_breakdown_tax__marketplace_facilitator_vat_principal_amount_currency_code": "Item facilitator VAT currency", + "item_sub_breakdown_tax__marketplace_facilitator_vat_shipping_amount": "Item facilitator ship VAT", + "item_sub_breakdown_tax__marketplace_facilitator_vat_shipping_amount_currency_code": "Item facilitator ship VAT currency", + "item_sub_breakdown_tax__our_price_tax_amount": "Item our price tax", + "item_sub_breakdown_tax__our_price_tax_amount_currency_code": "Item our price tax currency", + "item_sub_breakdown_tax__restocking_deduction_tax_amount": "Item restocking tax", + "item_sub_breakdown_tax__restocking_deduction_tax_amount_currency_code": "Item restocking tax currency", + "item_sub_breakdown_tax__shipping_tax_amount": "Item shipping tax", + "item_sub_breakdown_tax__shipping_tax_amount_currency_code": "Item shipping tax currency", + "item_total_amount_currency_amount": "Item total", + "item_total_amount_currency_code": "Item total currency", + "marketplace_details_marketplace_id": "Marketplace ID", + "marketplace_details_marketplace_name": "Marketplace", + "posted_date": "Posted date", + "related_identifiers": "Related identifiers", + "selling_partner_metadata_account_type": "Account type", + "selling_partner_metadata_marketplace_id": "Seller marketplace ID", + "selling_partner_metadata_selling_partner_id": "Seller ID", + "sku": "SKU", + "total_amount": "Total", + "transaction_breakdown_expenses_amount": "Expenses total", + "transaction_breakdown_expenses_amount_currency_code": "Expenses total currency", + "transaction_breakdown_refunded_expenses_amount": "Refunded expenses total", + "transaction_breakdown_refunded_expenses_amount_currency_code": "Refunded expenses total currency", + "transaction_breakdown_refunded_sales_amount": "Refunded sales total", + "transaction_breakdown_refunded_sales_amount_currency_code": "Refunded sales total currency", + "transaction_breakdown_sales_amount": "Sales total", + "transaction_breakdown_sales_amount_currency_code": "Sales total currency", + "transaction_id": "Transaction ID", + "transaction_item_count": "Item count", + "transaction_status": "Transaction status", + "transaction_sub_breakdown_expenses__advertising_fee_amount": "Expense advertising fee", + "transaction_sub_breakdown_expenses__advertising_fee_amount_currency_code": "Expense advertising fee currency", + "transaction_sub_breakdown_expenses__amazon_fees_amount": "Expense Amazon fees", + "transaction_sub_breakdown_expenses__amazon_fees_amount_currency_code": "Expense Amazon fees currency", + "transaction_sub_breakdown_expenses__fbafees_amount": "Expense FBA fees", + "transaction_sub_breakdown_expenses__fbafees_amount_currency_code": "Expense FBA fees currency", + "transaction_sub_breakdown_expenses__fbareversed_reimbursement_amount": "Expense FBA reversal", + "transaction_sub_breakdown_expenses__fbareversed_reimbursement_amount_currency_code": "Expense FBA reversal currency", + "transaction_sub_breakdown_expenses__hrrnon_apparel_rollup_amount": "Expense HRR non apparel", + "transaction_sub_breakdown_expenses__hrrnon_apparel_rollup_amount_currency_code": "Expense HRR non apparel currency", + "transaction_sub_breakdown_expenses__inbound_convenience_charge_amount": "Expense inbound placement", + "transaction_sub_breakdown_expenses__inbound_convenience_charge_amount_currency_code": "Expense inbound placement currency", + "transaction_sub_breakdown_expenses__long_term_storage_fee_amount": "Expense long term storage fee", + "transaction_sub_breakdown_expenses__long_term_storage_fee_amount_currency_code": "Expense long term storage fee currency", + "transaction_sub_breakdown_expenses__other_amount": "Expense other", + "transaction_sub_breakdown_expenses__other_amount_currency_code": "Expense other currency", + "transaction_sub_breakdown_expenses__promo_rebates_amount": "Expense promo rebates", + "transaction_sub_breakdown_expenses__promo_rebates_amount_currency_code": "Expense promo rebates currency", + "transaction_sub_breakdown_expenses__retrocharge_reversal_amount": "Expense retrocharge reversal", + "transaction_sub_breakdown_expenses__retrocharge_reversal_amount_currency_code": "Expense retrocharge reversal currency", + "transaction_sub_breakdown_expenses__storage_billing_fee_amount": "Expense storage billing fee", + "transaction_sub_breakdown_expenses__storage_billing_fee_amount_currency_code": "Expense storage billing fee currency", + "transaction_sub_breakdown_expenses__subscription_fee_amount": "Expense subscription fee", + "transaction_sub_breakdown_expenses__subscription_fee_amount_currency_code": "Expense subscription fee currency", + "transaction_sub_breakdown_refunded_expenses__amazon_fees_amount": "Refunded expense Amazon fees", + "transaction_sub_breakdown_refunded_expenses__amazon_fees_amount_currency_code": "Refunded expense Amazon fees currency", + "transaction_sub_breakdown_refunded_expenses__other_amount": "Refunded expense other", + "transaction_sub_breakdown_refunded_expenses__other_amount_currency_code": "Refunded expense other currency", + "transaction_sub_breakdown_refunded_expenses__promo_rebates_amount": "Refunded expense promo rebates", + "transaction_sub_breakdown_refunded_expenses__promo_rebates_amount_currency_code": "Refunded expense promo rebates currency", + "transaction_sub_breakdown_refunded_expenses__tax_amount": "Refunded expense tax", + "transaction_sub_breakdown_refunded_expenses__tax_amount_currency_code": "Refunded expense tax currency", + "transaction_sub_breakdown_refunded_sales__product_charges_amount": "Refunded sales product charges", + "transaction_sub_breakdown_refunded_sales__product_charges_amount_currency_code": "Refunded sales product charges currency", + "transaction_sub_breakdown_refunded_sales__shipping_amount": "Refunded sales shipping", + "transaction_sub_breakdown_refunded_sales__shipping_amount_currency_code": "Refunded sales shipping currency", + "transaction_sub_breakdown_sales__fbainventory_reimbursement_amount": "Sales inventory reimbursement", + "transaction_sub_breakdown_sales__fbainventory_reimbursement_amount_currency_code": "Sales inventory reimbursement currency", + "transaction_sub_breakdown_sales__fund_transfer_amount": "Sales fund transfer", + "transaction_sub_breakdown_sales__fund_transfer_amount_currency_code": "Sales fund transfer currency", + "transaction_sub_breakdown_sales__other_amount": "Sales other", + "transaction_sub_breakdown_sales__other_amount_currency_code": "Sales other currency", + "transaction_sub_breakdown_sales__product_charges_amount": "Sales product charges", + "transaction_sub_breakdown_sales__product_charges_amount_currency_code": "Sales product charges currency", + "transaction_sub_breakdown_sales__recommerce_liquidation_amount": "Sales recommerce liquidation", + "transaction_sub_breakdown_sales__recommerce_liquidation_amount_currency_code": "Sales recommerce liquidation currency", + "transaction_sub_breakdown_sales__retrocharge_amount": "Sales retrocharge", + "transaction_sub_breakdown_sales__retrocharge_amount_currency_code": "Sales retrocharge currency", + "transaction_sub_breakdown_sales__shipping_amount": "Sales shipping", + "transaction_sub_breakdown_sales__shipping_amount_currency_code": "Sales shipping currency", + "transaction_type": "Transaction type", + "item_sub_breakdown_amazon_fees__fbainbound_defect_fee_amount": "Item inbound defect fee", + "item_sub_breakdown_amazon_fees__fbainbound_defect_fee_amount_currency_code": "Item inbound defect fee currency", + "transaction_sub_breakdown_expenses__unplanned_service_charge_amount": "Expense unplanned service", + "transaction_sub_breakdown_expenses__unplanned_service_charge_amount_currency_code": "Expense unplanned service currency", + "item_sub_breakdown_amazon_fees__fbastorage_fee_amount": "Item FBA storage fee", + "item_sub_breakdown_amazon_fees__fbastorage_fee_amount_currency_code": "Item FBA storage fee currency", + "transaction_sub_breakdown_sales__storage_billing_fee_amount": "Sales storage billing fee", + "transaction_sub_breakdown_sales__storage_billing_fee_amount_currency_code": "Sales storage billing fee currency", + "item_sub_breakdown_other__goodwill_principal_amount": "Item goodwill principal", + "item_sub_breakdown_other__goodwill_principal_amount_currency_code": "Item goodwill principal currency", + "transaction_sub_breakdown_refunded_sales__other_amount": "Refunded sales other", + "transaction_sub_breakdown_refunded_sales__other_amount_currency_code": "Refunded sales other currency", + "item_breakdown_shipping_service_chargebacks_amount": "Item shipping chargebacks", + "item_breakdown_shipping_service_chargebacks_amount_currency_code": "Item shipping chargebacks currency", + "item_sub_breakdown_amazon_fees__deal_participation_fee_rollup_amount": "Item deal participation fee", + "item_sub_breakdown_amazon_fees__deal_participation_fee_rollup_amount_currency_code": "Item deal participation fee currency", + "item_sub_breakdown_amazon_fees__deal_performance_fee_rollup_amount": "Item deal performance fee", + "item_sub_breakdown_amazon_fees__deal_performance_fee_rollup_amount_currency_code": "Item deal performance fee currency", + "item_sub_breakdown_amazon_fees__fbadisposal_fee_amount": "Item FBA disposal fee", + "item_sub_breakdown_amazon_fees__fbadisposal_fee_amount_currency_code": "Item FBA disposal fee currency", + "item_sub_breakdown_amazon_fees__fbainbound_convenience_fee_amount": "Item inbound placement fee", + "item_sub_breakdown_amazon_fees__fbainbound_convenience_fee_amount_currency_code": "Item inbound placement fee currency", + "item_sub_breakdown_amazon_fees__fbalong_term_storage_fee_amount": "Item long term storage fee", + "item_sub_breakdown_amazon_fees__fbalong_term_storage_fee_amount_currency_code": "Item long term storage fee currency", + "item_sub_breakdown_amazon_fees__fbaremoval_fee_amount": "Item FBA removal fee", + "item_sub_breakdown_amazon_fees__fbaremoval_fee_amount_currency_code": "Item FBA removal fee currency", + "item_sub_breakdown_shipping_service_chargebacks__carrier_packaging_charge_amount": "Item carrier packaging charge", + "item_sub_breakdown_shipping_service_chargebacks__carrier_packaging_charge_amount_currency_code": "Item carrier packaging charge currency", + "item_sub_breakdown_shipping_service_chargebacks__customer_packaging_charge_amount": "Item customer packaging charge", + "item_sub_breakdown_shipping_service_chargebacks__customer_packaging_charge_amount_currency_code": "Item customer packaging charge currency", + "transaction_sub_breakdown_expenses__deal_participation_fee_rollup_amount": "Expense deal participation fee", + "transaction_sub_breakdown_expenses__deal_participation_fee_rollup_amount_currency_code": "Expense deal participation fee currency", + "transaction_sub_breakdown_expenses__deal_performance_fee_rollup_amount": "Expense deal performance fee", + "transaction_sub_breakdown_expenses__deal_performance_fee_rollup_amount_currency_code": "Expense deal performance fee currency", + "transaction_sub_breakdown_refunded_sales__amazon_fees_amount": "Refunded sales Amazon fees", + "transaction_sub_breakdown_refunded_sales__amazon_fees_amount_currency_code": "Refunded sales Amazon fees currency", + "transaction_sub_breakdown_sales__shipping_service_chargebacks_amount": "Sales shipping chargebacks", + "transaction_sub_breakdown_sales__shipping_service_chargebacks_amount_currency_code": "Sales shipping chargebacks currency", + "transaction_sub_breakdown_sales__advertising_fee_refund_amount": "Sales advertising fee refund", + "transaction_sub_breakdown_sales__advertising_fee_refund_amount_currency_code": "Sales advertising fee refund currency", + "item_breakdown_reserve_credit_amount": "Item reserve credit", + "item_breakdown_reserve_credit_amount_currency_code": "Item reserve credit currency", + "item_breakdown_reserve_debit_amount": "Item reserve debit", + "item_breakdown_reserve_debit_amount_currency_code": "Item reserve debit currency", + "transaction_sub_breakdown_expenses__reserve_debit_amount": "Expense reserve debit", + "transaction_sub_breakdown_expenses__reserve_debit_amount_currency_code": "Expense reserve debit currency", + "transaction_sub_breakdown_sales__reserve_credit_amount": "Sales reserve credit", + "transaction_sub_breakdown_sales__reserve_credit_amount_currency_code": "Sales reserve credit currency", + "transaction_sub_breakdown_expenses__shipping_service_chargebacks_amount": "Expense shipping chargebacks", + "transaction_sub_breakdown_expenses__shipping_service_chargebacks_amount_currency_code": "Expense shipping chargebacks currency", + "item_sub_breakdown_amazon_fees__fbainbound_transportation_fee_amount": "Item inbound transport fee", + "item_sub_breakdown_amazon_fees__fbainbound_transportation_fee_amount_currency_code": "Item inbound transport fee currency", + "item_sub_breakdown_amazon_fees__paid_services_fee_amount": "Item paid services fee", + "item_sub_breakdown_amazon_fees__paid_services_fee_amount_currency_code": "Item paid services fee currency", + "transaction_sub_breakdown_expenses__inbound_transportation_fee_amount": "Expense inbound transport fee", + "transaction_sub_breakdown_expenses__inbound_transportation_fee_amount_currency_code": "Expense inbound transport fee currency", + "transaction_sub_breakdown_expenses__paid_services_charge_amount": "Expense paid services charge", + "transaction_sub_breakdown_expenses__paid_services_charge_amount_currency_code": "Expense paid services charge currency", + "item_sub_breakdown_amazon_fees__run_lightning_deal_fee_amount": "Item run lightning deal fee", + "item_sub_breakdown_amazon_fees__run_lightning_deal_fee_amount_currency_code": "Item run lightning deal fee currency", + "transaction_sub_breakdown_expenses__deal_fee_amount": "Expense deal fee", + "transaction_sub_breakdown_expenses__deal_fee_amount_currency_code": "Expense deal fee currency", + "transaction_sub_breakdown_sales__fbafees_amount": "Sales FBA fees", + "transaction_sub_breakdown_sales__fbafees_amount_currency_code": "Sales FBA fees currency", + "item_sub_breakdown_amazon_fees__coupon_participation_fee_rollup_amount": "Item coupon participation fee", + "item_sub_breakdown_amazon_fees__coupon_participation_fee_rollup_amount_currency_code": "Item coupon participation fee currency", + "item_sub_breakdown_amazon_fees__coupon_performance_fee_rollup_amount": "Item coupon performance fee", + "item_sub_breakdown_amazon_fees__coupon_performance_fee_rollup_amount_currency_code": "Item coupon performance fee currency", + "transaction_sub_breakdown_expenses__coupon_participation_fee_rollup_amount": "Expense coupon participation fee", + "transaction_sub_breakdown_expenses__coupon_participation_fee_rollup_amount_currency_code": "Expense coupon participation currency", + "transaction_sub_breakdown_expenses__coupon_performance_fee_rollup_amount": "Expense coupon performance fee", + "transaction_sub_breakdown_expenses__coupon_performance_fee_rollup_amount_currency_code": "Expense coupon performance fee currency", + "item_sub_breakdown_amazon_fees__coupon_redemption_fee_amount": "Item coupon redemption fee", + "item_sub_breakdown_amazon_fees__coupon_redemption_fee_amount_currency_code": "Item coupon redemption fee currency", + "transaction_sub_breakdown_expenses__coupon_fee_amount": "Expense coupon fee", + "transaction_sub_breakdown_expenses__coupon_fee_amount_currency_code": "Expense coupon fee currency", + "item_sub_breakdown_other__supply_chain_credit_roll_up_amount": "Item supply chain credit", + "item_sub_breakdown_other__supply_chain_credit_roll_up_amount_currency_code": "Item supply chain credit currency", + "transaction_sub_breakdown_sales__supply_chain_credit_roll_up_amount": "Sales supply chain credit", + "transaction_sub_breakdown_sales__supply_chain_credit_roll_up_amount_currency_code": "Sales supply chain credit currency", + }, + _AMZN + "fba_fee_estimates": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "amazon_store": "Amazon store", + "asin": "ASIN", + "brand": "Brand", + "currency": "Currency", + "estimated_fee_total": "Estimated fee total", + "estimated_future_fee_current_selling_on_amazon_future_fulfillment_fees": "Future fulfillment fees", + "estimated_future_order_handling_fee_per_order": "Future order handling fee", + "estimated_future_pick_pack_fee_per_unit": "Future pick and pack fee per unit", + "estimated_future_weight_handling_fee_per_unit": "Future weight handling fee per unit", + "estimated_order_handling_fee_per_order": "Order handling fee", + "estimated_pick_pack_fee_per_unit": "Pick and pack fee per unit", + "estimated_referral_fee_per_unit": "Referral fee per unit", + "estimated_variable_closing_fee": "Variable closing fee", + "estimated_weight_handling_fee_per_unit": "Weight handling fee per unit", + "expected_fulfillment_fee_per_unit": "Fulfillment fee per unit", + "expected_future_fulfillment_fee_per_unit": "Future fulfillment fee per unit", + "fnsku": "FNSKU", + "fulfilled_by": "Fulfilled by", + "item_package_weight": "Item package weight", + "length_and_girth": "Length and girth", + "longest_side": "Longest side", + "median_side": "Median side", + "product_group": "Product group", + "product_name": "Product name", + "product_size_tier": "Product size tier", + "sales_price": "Sales price", + "shortest_side": "Shortest side", + "sku": "SKU", + "unit_of_dimension": "Unit of dimension", + "unit_of_weight": "Unit of weight", + "your_price": "Your price", + }, + _AMZN + "referral_fees": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "asin": "ASIN", + "estimated_referral_fee_per_item": "Estimated referral fee per item", + "item_name": "Item name", + "price": "Price", + "seller_sku": "Seller SKU", + }, + _AMZN + "storage_fees": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "asin": "ASIN", + "average_quantity_customer_orders": "Average quantity customer orders", + "average_quantity_on_hand": "Average quantity on hand", + "average_quantity_pending_removal": "Average quantity pending removal", + "avg_qty_for_sus": "Average units for surcharge", + "base_rate": "Base fee per cubic foot", + "breakdown_incentive_fee_amount": "Incentive fee breakdown", + "country_code": "Country code", + "currency": "Currency", + "dangerous_goods_storage_type": "Dangerous goods storage type", + "eligible_for_inventory_discount": "Eligible for inventory discount", + "est_base_msf": "Estimated base storage fee", + "est_sus": "Estimated utilization surcharge", + "est_vol_for_sus": "Estimated volume for surcharge", + "estimated_monthly_storage_fee": "Estimated monthly storage fee", + "estimated_total_item_volume": "Estimated total item volume", + "fnsku": "FNSKU", + "fulfillment_center": "Fulfillment center", + "item_volume": "Item volume", + "longest_side": "Longest side", + "measurement_units": "Measurement units", + "median_side": "Median side", + "month": "Month", + "month_of_charge": "Month of charge", + "product_name": "Product name", + "product_size_tier": "Product size tier", + "qualifies_for_inventory_discount": "Qualifies for inventory discount", + "shortest_side": "Shortest side", + "storage_utilization_ratio": "Storage utilization ratio", + "storage_utilization_ratio_units": "Utilization ratio unit", + "total_incentive_fee_amount": "Total incentive fee", + "utilization_surcharge_rate": "Utilization surcharge per cubic foot", + "volume_units": "Volume units", + "weight": "Weight", + "weight_units": "Weight units", + }, + _AMZN + "search_query_performance": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "asin": "ASIN", + "cart_add_data_asin_cart_add_count": "ASIN cart adds", + "cart_add_data_asin_cart_add_share": "ASIN cart add share", + "cart_add_data_asin_median_cart_add_price": "ASIN median cart add price detail", + "cart_add_data_asin_median_cart_add_price_amount": "ASIN median cart add price", + "cart_add_data_asin_median_cart_add_price_currency_code": "ASIN cart add price currency", + "cart_add_data_total_cart_add_count": "Query cart adds", + "cart_add_data_total_cart_add_rate": "Query cart add rate", + "cart_add_data_total_median_cart_add_price": "Query median cart add price detail", + "cart_add_data_total_median_cart_add_price_amount": "Query median cart add price", + "cart_add_data_total_median_cart_add_price_currency_code": "Query cart add price currency", + "cart_add_data_total_one_day_shipping_cart_add_count": "One day shipping cart adds", + "cart_add_data_total_same_day_shipping_cart_add_count": "Same day shipping cart adds", + "cart_add_data_total_two_day_shipping_cart_add_count": "Two day shipping cart adds", + "click_data_asin_click_count": "ASIN clicks", + "click_data_asin_click_share": "ASIN click share", + "click_data_asin_median_click_price": "ASIN median click price detail", + "click_data_asin_median_click_price_amount": "ASIN median click price", + "click_data_asin_median_click_price_currency_code": "ASIN click price currency", + "click_data_total_click_count": "Query clicks", + "click_data_total_click_rate": "Query click rate", + "click_data_total_median_click_price": "Query median click price detail", + "click_data_total_median_click_price_amount": "Query median click price", + "click_data_total_median_click_price_currency_code": "Query click price currency", + "click_data_total_one_day_shipping_click_count": "One day shipping clicks", + "click_data_total_same_day_shipping_click_count": "Same day shipping clicks", + "click_data_total_two_day_shipping_click_count": "Two day shipping clicks", + "end_date": "End date", + "impression_data_asin_impression_count": "ASIN impressions", + "impression_data_asin_impression_share": "ASIN impression share", + "impression_data_total_query_impression_count": "Query impressions", + "purchase_data_asin_median_purchase_price": "ASIN median purchase price detail", + "purchase_data_asin_median_purchase_price_amount": "ASIN median purchase price", + "purchase_data_asin_median_purchase_price_currency_code": "ASIN purchase price currency", + "purchase_data_asin_purchase_count": "ASIN purchases", + "purchase_data_asin_purchase_share": "ASIN purchase share", + "purchase_data_total_median_purchase_price": "Query median purchase price detail", + "purchase_data_total_median_purchase_price_amount": "Query median purchase price", + "purchase_data_total_median_purchase_price_currency_code": "Query purchase price currency", + "purchase_data_total_one_day_shipping_purchase_count": "One day shipping purchases", + "purchase_data_total_purchase_count": "Query purchases", + "purchase_data_total_purchase_rate": "Query purchase rate", + "purchase_data_total_same_day_shipping_purchase_count": "Same day shipping purchases", + "purchase_data_total_two_day_shipping_purchase_count": "Two day shipping purchases", + "search_query_data_search_query": "Search query", + "search_query_data_search_query_score": "Search query score", + "search_query_data_search_query_volume": "Search query volume", + "start_date": "Start date", + "_report_period": "Report period", + }, + _AMZN + "long_term_storage_fees": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "amount_charged": "Amount charged", + "asin": "ASIN", + "condition": "Condition", + "country": "Country", + "currency": "Currency", + "fnsku": "FNSKU", + "per_unit_volume": "Per unit volume", + "product_name": "Product name", + "qty_charged": "Quantity charged", + "rate_surcharge": "Surcharge per cubic foot", + "sku": "SKU", + "snapshot_date": "Snapshot date", + "surcharge_age_tier": "Surcharge age tier", + "volume_unit": "Volume unit", + }, + _AMZN + "storage_overage_fees": { + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + }, + _AMZN + "reimbursements": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "amazon_order_id": "Amazon order ID", + "amount_per_unit": "Amount per unit", + "amount_total": "Amount total", + "approval_date": "Approval date", + "asin": "ASIN", + "case_id": "Case ID", + "condition": "Condition", + "currency_unit": "Currency", + "fnsku": "FNSKU", + "original_reimbursement_id": "Original reimbursement ID", + "original_reimbursement_type": "Original reimbursement type", + "product_name": "Product name", + "quantity_reimbursed_cash": "Quantity reimbursed cash", + "quantity_reimbursed_inventory": "Quantity reimbursed inventory", + "quantity_reimbursed_total": "Quantity reimbursed total", + "reason": "Reason", + "reimbursement_id": "Reimbursement ID", + "sku": "SKU", + }, + _AMZN + "fba_customer_returns": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "asin": "ASIN", + "customer_comments": "Customer comments", + "detailed_disposition": "Detailed disposition", + "fnsku": "FNSKU", + "fulfillment_center_id": "Fulfillment center ID", + "license_plate_number": "License plate number", + "order_id": "Order ID", + "product_name": "Product name", + "quantity": "Quantity", + "reason": "Reason", + "return_date": "Return date", + "sku": "SKU", + "status": "Status", + }, + _AMZN + "replacements": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "asin": "ASIN", + "fulfillment_center_id": "Fulfillment center ID", + "original_amazon_order_id": "Original Amazon order ID", + "original_fulfillment_center_id": "Original fulfillment center ID", + "quantity": "Quantity", + "replacement_amazon_order_id": "Replacement Amazon order ID", + "replacement_reason_code": "Replacement reason code", + "shipment_date": "Shipment date", + "sku": "SKU", + }, + _AMZN + "fba_shipped_sales": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "amazon_order_id": "Amazon order ID", + "asin": "ASIN", + "currency": "Currency", + "fnsku": "FNSKU", + "fulfillment_center_id": "Fulfillment center ID", + "gift_wrap_price": "Gift wrap price", + "item_price_per_unit": "Item price per unit", + "quantity": "Quantity", + "ship_city": "Ship city", + "ship_postal_code": "Ship postal code", + "ship_state": "Ship state", + "shipment_date": "Shipment date", + "shipping_price": "Shipping price", + "sku": "SKU", + }, + _AMZN + "fba_shipment_promotions": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "amazon_order_id": "Amazon order ID", + "currency": "Currency", + "description": "Description", + "item_promotion_discount": "Item promotion discount", + "item_promotion_id": "Item promotion ID", + "promotion_rule_value": "Promotion rule value", + "shipment_date": "Shipment date", + "shipment_id": "Shipment ID", + "shipment_item_id": "Shipment item ID", + }, + _AMZN + "inventory_ledger_detail": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "asin": "ASIN", + "country": "Country", + "date": "Date", + "date_and_time": "Date and time", + "disposition": "Disposition", + "event_type": "Event type", + "fnsku": "FNSKU", + "fulfillment_center": "Fulfillment center", + "msku": "MSKU", + "quantity": "Quantity", + "reason": "Reason", + "reconciled_quantity": "Reconciled quantity", + "reference_id": "Reference ID", + "store": "Store", + "title": "Title", + "unreconciled_quantity": "Unreconciled quantity", + }, + _AMZN + "inventory_planning": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "alert": "Alert", + "asin": "ASIN", + "available": "Available", + "condition": "Condition", + "currency": "Currency", + "days_of_supply": "Days of supply", + "deprecated_healthy_inventory_level": "Healthy inventory level (old)", + "estimated_ais_181_210_days": "Estimated AIS 181 to 210 days", + "estimated_ais_211_240_days": "Estimated AIS 211 to 240 days", + "estimated_ais_241_270_days": "Estimated AIS 241 to 270 days", + "estimated_ais_271_300_days": "Estimated AIS 271 to 300 days", + "estimated_ais_301_330_days": "Estimated AIS 301 to 330 days", + "estimated_ais_331_365_days": "Estimated AIS 331 to 365 days", + "estimated_ais_366_455_days": "Estimated AIS 366 to 455 days", + "estimated_ais_456_plus_days": "Estimated AIS 456 plus days", + "estimated_cost_savings_of_recommended_actions": "Savings from recommended actions", + "estimated_excess_quantity": "Estimated excess quantity", + "estimated_storage_cost_next_month": "Estimated storage cost next month", + "exempted_from_low_inventory_level_fee": "Exempt from low inventory fee", + "fba_inventory_level_health_status": "Inventory level health", + "fba_minimum_inventory_level": "FBA minimum inventory level", + "fc_transfer": "FC transfer", + "featuredoffer_price": "Featured offer price", + "fnsku": "FNSKU", + "fulfillment_service_and_programs": "Fulfillment service and programs", + "historical_days_of_supply": "Historical days of supply", + "inbound_quantity": "Inbound quantity", + "inbound_received": "Inbound received", + "inbound_shipped": "Inbound shipped", + "inbound_working": "Inbound working", + "inv_age_0_to_30_days": "Inventory age 0 to 30 days", + "inv_age_0_to_90_days": "Inventory age 0 to 90 days", + "inv_age_181_to_270_days": "Inventory age 181 to 270 days", + "inv_age_181_to_330_days": "Inventory age 181 to 330 days", + "inv_age_271_to_365_days": "Inventory age 271 to 365 days", + "inv_age_31_to_60_days": "Inventory age 31 to 60 days", + "inv_age_331_to_365_days": "Inventory age 331 to 365 days", + "inv_age_366_to_455_days": "Inventory age 366 to 455 days", + "inv_age_456_plus_days": "Inventory age 456 plus days", + "inv_age_61_to_90_days": "Inventory age 61 to 90 days", + "inv_age_91_to_180_days": "Inventory age 91 to 180 days", + "inventory_age_snapshot_date": "Inventory age as of", + "inventory_supply_at_fba": "Inventory supply at FBA", + "is_seasonal_in_next_3_months": "Seasonal in next 3 months", + "item_volume": "Item volume", + "last_updated_date_for_historical_days_of_supply": "Historical supply updated on", + "long_term_historical_days_of_supply": "Long term historical days of supply", + "low_inventory_level_fee_applied_in_current_week": "Low inventory fee this week", + "lowest_price_new_plus_shipping": "Lowest new price with shipping", + "lowest_price_used": "Lowest price used", + "marketplace": "Marketplace", + "no_sale_last_6_months": "No sale last 6 months", + "pending_removal_quantity": "Pending removal quantity", + "product_group": "Product group", + "product_name": "Product name", + "quantity_to_be_charged_ais_181_210_days": "AIS units 181 to 210 days", + "quantity_to_be_charged_ais_211_240_days": "AIS units 211 to 240 days", + "quantity_to_be_charged_ais_241_270_days": "AIS units 241 to 270 days", + "quantity_to_be_charged_ais_271_300_days": "AIS units 271 to 300 days", + "quantity_to_be_charged_ais_301_330_days": "AIS units 301 to 330 days", + "quantity_to_be_charged_ais_331_365_days": "AIS units 331 to 365 days", + "quantity_to_be_charged_ais_366_455_days": "AIS units 366 to 455 days", + "quantity_to_be_charged_ais_456_plus_days": "AIS units 456 plus days", + "recommended_action": "Recommended action", + "recommended_removal_quantity": "Recommended removal quantity", + "recommended_sale_duration_days": "Recommended sale duration days", + "recommended_sales_price": "Recommended sales price", + "recommended_ship_in_date": "Recommended ship in date", + "recommended_ship_in_quantity": "Recommended ship in quantity", + "reserved_customer_order": "Reserved customer order", + "reserved_fc_processing": "Reserved FC processing", + "reserved_staging": "Reserved staging", + "sales_price": "Sales price", + "sales_rank": "Sales rank", + "sales_shipped_last_30_days": "Sales shipped last 30 days", + "sales_shipped_last_60_days": "Sales shipped last 60 days", + "sales_shipped_last_7_days": "Sales shipped last 7 days", + "sales_shipped_last_90_days": "Sales shipped last 90 days", + "season_end_date": "Season end date", + "season_name": "Season name", + "season_start_date": "Season start date", + "sell_through": "Sell through", + "short_term_historical_days_of_supply": "Short term historical days of supply", + "sku": "SKU", + "snapshot_date": "Snapshot date", + "storage_type": "Storage type", + "storage_volume": "Storage volume", + "supplier": "Supplier", + "total_days_of_supply_including_units_from_open_shipments": "Days of supply with open shipments", + "total_reserved_quantity": "Total reserved quantity", + "unfulfillable_quantity": "Unfulfillable quantity", + "units_shipped_t30": "Units shipped last 30 days", + "units_shipped_t60": "Units shipped last 60 days", + "units_shipped_t7": "Units shipped last 7 days", + "units_shipped_t90": "Units shipped last 90 days", + "volume_unit_measurement": "Volume unit measurement", + "weeks_of_cover_t30": "Weeks of cover (30 days)", + "weeks_of_cover_t90": "Weeks of cover (90 days)", + "your_price": "Your price", + "estimated_ais_365_plus_days": "Estimated AIS 365 plus days", + "inv_age_365_plus_days": "Inventory age 365 plus days", + "quantity_to_be_charged_ais_365_plus_days": "AIS units 365 plus days", + }, + _AMZN + "manage_inventory": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "afn_fc_transfer_quantity": "AFN FC transfer quantity", + "afn_fulfillable_quantity": "AFN fulfillable quantity", + "afn_future_supply_buyable": "AFN future supply buyable", + "afn_inbound_receiving_quantity": "AFN inbound receiving quantity", + "afn_inbound_shipped_quantity": "AFN inbound shipped quantity", + "afn_inbound_working_quantity": "AFN inbound working quantity", + "afn_listing_exists": "AFN listing exists", + "afn_onhand_buyable_quantity": "AFN onhand buyable quantity", + "afn_researching_quantity": "AFN researching quantity", + "afn_reserved_future_supply": "AFN reserved future supply", + "afn_reserved_quantity": "AFN reserved quantity", + "afn_total_quantity": "AFN total quantity", + "afn_unsellable_quantity": "AFN unsellable quantity", + "afn_warehouse_quantity": "AFN warehouse quantity", + "asin": "ASIN", + "condition": "Condition", + "fnsku": "FNSKU", + "mfn_fulfillable_quantity": "MFN fulfillable quantity", + "mfn_listing_exists": "MFN listing exists", + "per_unit_volume": "Per unit volume", + "product_name": "Product name", + "sku": "SKU", + "store": "Store", + "your_price": "Your price", + }, + _AMZN + "reserved_inventory": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "asin": "ASIN", + "fnsku": "FNSKU", + "product_name": "Product name", + "program": "Program", + "reserved_customerorders": "Reserved customer orders", + "reserved_fc_processing": "Reserved FC processing", + "reserved_qty": "Reserved quantity", + "reserved_staging": "Reserved staging", + "sku": "SKU", + }, + _AMZN + "restock_recommendations": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "alert": "Alert", + "asin": "ASIN", + "available": "Available", + "condition": "Condition", + "country": "Country", + "currency_code": "Currency", + "customer_order": "Customer order", + "days_of_supply_at_amazon_fulfillment_network": "Days of supply at Amazon", + "fc_processing": "FC processing", + "fc_transfer": "FC transfer", + "fnsku": "FNSKU", + "fulfilled_by": "Fulfilled by", + "inbound": "Inbound", + "merchant_sku": "Merchant SKU", + "price": "Price", + "product_name": "Product name", + "receiving": "Receiving", + "recommended_action": "Recommended action", + "recommended_replenishment_qty": "Recommended replenishment quantity", + "recommended_ship_date": "Recommended ship date", + "sales_last_30_days": "Sales last 30 days", + "shipped": "Shipped", + "supplier": "Supplier", + "supplier_part_no": "Supplier part number", + "total_days_of_supply_including_units_from_open_shipments": "Days of supply with open shipments", + "total_units": "Total units", + "unfulfillable": "Unfulfillable", + "unit_storage_size": "Unit storage size", + "units_sold_last_30_days": "Units sold last 30 days", + "working": "Working", + }, + _AMZN + "stranded_inventory": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "asin": "ASIN", + "condition": "Condition", + "date_stranded": "Date stranded", + "date_to_take_auto_removal": "Date to take auto removal", + "error_message": "Error message", + "fnsku": "FNSKU", + "fulfillable_qty": "Fulfillable quantity", + "fulfilled_by": "Fulfilled by", + "inbound_shipped_qty": "Inbound shipped quantity", + "primary_action": "Primary action", + "product_name": "Product name", + "program": "Program", + "reserved_quantity": "Reserved quantity", + "sku": "SKU", + "status_primary": "Status primary", + "status_secondary": "Status secondary", + "stranded_reason": "Stranded reason", + "unfulfillable_qty": "Unfulfillable quantity", + "your_price": "Your price", + }, + _AMZN + "inbound_noncompliance": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "alert_status": "Alert status", + "asin": "ASIN", + "coaching_level": "Coaching level", + "currency": "Currency", + "expected_quantity": "Expected quantity", + "fba_carton_id": "FBA carton ID", + "fba_shipment_id": "FBA shipment ID", + "fee_total": "Fee total", + "fee_type": "Fee type", + "fnsku": "FNSKU", + "fulfillment_center_id": "Fulfillment center ID", + "issue_reported_date": "Issue reported date", + "performance_measurement_unit": "Performance measurement unit", + "problem_level": "Problem level", + "problem_quantity": "Problem quantity", + "problem_type": "Problem type", + "product_name": "Product name", + "received_quantity": "Received quantity", + "shipment_creation_date": "Shipment creation date", + "sku": "SKU", + }, + _AMZN + "inventory_expiration_dates": { + "_snapshot_date": "Synced on", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + }, + _AMZN + "recommended_removals": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "asin": "ASIN", + "condition": "Condition", + "fnsku": "FNSKU", + "product_name": "Product name", + "sellable_121_180_days": "Sellable 121 180 days", + "sellable_181_270_days": "Sellable 181 270 days", + "sellable_271_365_days": "Sellable 271 365 days", + "sellable_365_days": "Sellable 365 days", + "sellable_quantity": "Sellable quantity", + "sellable_removal_quantity": "Sellable removal quantity", + "sku": "SKU", + "snapshot_date": "Snapshot date", + "unsellable_0_7_days": "Unsellable 0 7 days", + "unsellable_61_90_days": "Unsellable 61 90 days", + "unsellable_8_60_days": "Unsellable 8 60 days", + "unsellable_quantity": "Unsellable quantity", + }, + _AMZN + "removal_orders": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "cancelled_quantity": "Cancelled quantity", + "currency": "Currency", + "disposed_quantity": "Disposed quantity", + "disposition": "Disposition", + "fnsku": "FNSKU", + "in_process_quantity": "In process quantity", + "last_updated_date": "Last updated date", + "order_id": "Order ID", + "order_source": "Order source", + "order_status": "Order status", + "order_type": "Order type", + "removal_fee": "Removal fee", + "request_date": "Request date", + "requested_quantity": "Requested quantity", + "shipped_quantity": "Shipped quantity", + "sku": "SKU", + }, + _AMZN + "removal_shipments": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "carrier": "Carrier", + "disposition": "Disposition", + "fnsku": "FNSKU", + "order_id": "Order ID", + "removal_order_type": "Removal order type", + "request_date": "Request date", + "shipment_date": "Shipment date", + "shipped_quantity": "Shipped quantity", + "sku": "SKU", + "tracking_number": "Tracking number", + }, + _AMZN + "subscribe_save_forecast": { + "_snapshot_date": "Synced on", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + }, + _AMZN + "subscribe_save_performance": { + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + "asin": "ASIN", + "country": "Country", + "fnsku": "FNSKU", + "offer_state": "Offer state", + "oos_rate_week_1": "Out of stock rate week 1", + "oos_rate_week_2": "Out of stock rate week 2", + "oos_rate_week_3": "Out of stock rate week 3", + "oos_rate_week_4": "Out of stock rate week 4", + "product_name": "Product name", + "sku": "SKU", + "snapshot_date": "Snapshot date", + "sns_discount_week_1": "S&S discount week 1", + "sns_discount_week_2": "S&S discount week 2", + "sns_discount_week_3": "S&S discount week 3", + "sns_discount_week_4": "S&S discount week 4", + "sns_sale_price_week_1": "S&S sale price week 1", + "sns_sale_price_week_2": "S&S sale price week 2", + "sns_sale_price_week_3": "S&S sale price week 3", + "sns_sale_price_week_4": "S&S sale price week 4", + "sns_units_shipped_week_1": "S&S units shipped week 1", + "sns_units_shipped_week_2": "S&S units shipped week 2", + "sns_units_shipped_week_3": "S&S units shipped week 3", + "sns_units_shipped_week_4": "S&S units shipped week 4", + "week_1_start_date": "Week 1 start date", + }, + _AMZN + "remote_fulfillment_eligibility": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "managing_remote_fulfillment": "Managing remote fulfillment", + }, + _AMZN + "search_catalog_performance": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "asin": "ASIN", + "cart_add_data_cart_add_count": "Cart adds", + "cart_add_data_cart_added_median_price": "Median cart add price detail", + "cart_add_data_cart_added_median_price_amount": "Median cart add price", + "cart_add_data_cart_added_median_price_currency_code": "Cart add price currency", + "cart_add_data_one_day_shipping_cart_add_count": "One day shipping cart adds", + "cart_add_data_same_day_shipping_cart_add_count": "Same day shipping cart adds", + "cart_add_data_two_day_shipping_cart_add_count": "Two day shipping cart adds", + "click_data_click_count": "Clicks", + "click_data_click_rate": "Click rate", + "click_data_clicked_median_price": "Median click price detail", + "click_data_clicked_median_price_amount": "Median click price", + "click_data_clicked_median_price_currency_code": "Click price currency", + "click_data_one_day_shipping_click_count": "One day shipping clicks", + "click_data_same_day_shipping_click_count": "Same day shipping clicks", + "click_data_two_day_shipping_click_count": "Two day shipping clicks", + "end_date": "End date", + "impression_data_impression_count": "Impressions", + "impression_data_impression_median_price": "Median impression price detail", + "impression_data_impression_median_price_amount": "Median impression price", + "impression_data_impression_median_price_currency_code": "Impression price currency", + "impression_data_one_day_shipping_impression_count": "One day shipping impressions", + "impression_data_same_day_shipping_impression_count": "Same day shipping impressions", + "impression_data_two_day_shipping_impression_count": "Two day shipping impressions", + "purchase_data_conversion_rate": "Conversion rate", + "purchase_data_one_day_shipping_purchase_count": "One day shipping purchases", + "purchase_data_purchase_count": "Purchases", + "purchase_data_purchase_median_price": "Median purchase price detail", + "purchase_data_purchase_median_price_amount": "Median purchase price", + "purchase_data_purchase_median_price_currency_code": "Purchase price currency", + "purchase_data_same_day_shipping_purchase_count": "Same day shipping purchases", + "purchase_data_search_traffic_sales_amount": "Search traffic sales", + "purchase_data_search_traffic_sales_currency_code": "Search traffic sales currency", + "purchase_data_two_day_shipping_purchase_count": "Two day shipping purchases", + "start_date": "Start date", + }, + _AMZN + "market_basket": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "asin": "ASIN", + "combination_pct": "Combination pct", + "end_date": "End date", + "purchased_with_asin": "Purchased with ASIN", + "purchased_with_rank": "Purchased with rank", + "start_date": "Start date", + }, + _AMZN + "promotion_performance": { + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + }, + _AMZN + "coupon_performance": { + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + }, + _AMZN + "account_health": { + "_data_origin": "Data origin", + "_json_root": "Report section", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "account_health_rating_ahr_score": "Account health score", + "account_health_rating_ahr_status": "Account health status", + "account_health_rating_reporting_date_range_reporting_date_from": "Account health rating from", + "account_health_rating_reporting_date_range_reporting_date_to": "Account health rating to", + "customer_product_reviews_policy_violations_defects_count": "Review policy violations count", + "customer_product_reviews_policy_violations_reporting_date_range_reporting_date_from": "Review policy violations from", + "customer_product_reviews_policy_violations_reporting_date_range_reporting_date_to": "Review policy violations to", + "customer_product_reviews_policy_violations_status": "Review policy violations status", + "customer_product_reviews_policy_violations_target_condition": "Review policy violations target rule", + "customer_product_reviews_policy_violations_target_value": "Review policy violations target", + "document_requests_defects_count": "Document requests count", + "document_requests_reporting_date_range_reporting_date_from": "Document requests from", + "document_requests_reporting_date_range_reporting_date_to": "Document requests to", + "document_requests_status": "Document requests status", + "document_requests_target_condition": "Document requests target rule", + "document_requests_target_value": "Document requests target", + "food_and_product_safety_issues_defects_count": "Food safety issues count", + "food_and_product_safety_issues_reporting_date_range_reporting_date_from": "Food safety issues from", + "food_and_product_safety_issues_reporting_date_range_reporting_date_to": "Food safety issues to", + "food_and_product_safety_issues_status": "Food safety issues status", + "food_and_product_safety_issues_target_condition": "Food safety issues target rule", + "food_and_product_safety_issues_target_value": "Food safety issues target", + "invoice_defect_rate_invoice_defect_count": "Invoice defects", + "invoice_defect_rate_invoice_defect_status": "Invoice defects status", + "invoice_defect_rate_late_invoice_count": "Late invoices", + "invoice_defect_rate_late_invoice_status": "Late invoices status", + "invoice_defect_rate_missing_invoice_count": "Missing invoices", + "invoice_defect_rate_missing_invoice_status": "Missing invoices status", + "invoice_defect_rate_order_count": "Invoice defect rate orders", + "invoice_defect_rate_rate": "Invoice defect rate", + "invoice_defect_rate_reporting_date_range_reporting_date_from": "Invoice defect rate from", + "invoice_defect_rate_reporting_date_range_reporting_date_to": "Invoice defect rate to", + "invoice_defect_rate_status": "Invoice defect rate status", + "invoice_defect_rate_target_condition": "Invoice defect rate target rule", + "invoice_defect_rate_target_value": "Invoice defect rate target", + "late_shipment_rate_late_shipment_count": "Late shipments", + "late_shipment_rate_list": "Late shipment history", + "late_shipment_rate_order_count": "Late shipment rate orders", + "late_shipment_rate_rate": "Late shipment rate", + "late_shipment_rate_reporting_date_range_reporting_date_from": "Late shipment rate from", + "late_shipment_rate_reporting_date_range_reporting_date_to": "Late shipment rate to", + "late_shipment_rate_status": "Late shipment rate status", + "late_shipment_rate_target_condition": "Late shipment rate target rule", + "late_shipment_rate_target_value": "Late shipment rate target", + "listing_policy_violations_defects_count": "Listing policy violations count", + "listing_policy_violations_reporting_date_range_reporting_date_from": "Listing policy violations from", + "listing_policy_violations_reporting_date_range_reporting_date_to": "Listing policy violations to", + "listing_policy_violations_status": "Listing policy violations status", + "listing_policy_violations_target_condition": "Listing policy violations target rule", + "listing_policy_violations_target_value": "Listing policy violations target", + "marketplace_id": "Marketplace ID", + "on_time_delivery_rate_on_time_delivery_count": "On time deliveries", + "on_time_delivery_rate_rate": "On time delivery rate", + "on_time_delivery_rate_reporting_date_range_reporting_date_from": "On time delivery rate from", + "on_time_delivery_rate_reporting_date_range_reporting_date_to": "On time delivery rate to", + "on_time_delivery_rate_shipment_count_with_valid_tracking": "On time delivery tracked shipments", + "on_time_delivery_rate_status": "On time delivery rate status", + "on_time_delivery_rate_target_condition": "On time delivery rate target rule", + "on_time_delivery_rate_target_value": "On time delivery rate target", + "order_defect_rate_afn_chargebacks_count": "FBA ODR chargebacks", + "order_defect_rate_afn_chargebacks_status": "FBA ODR chargebacks status", + "order_defect_rate_afn_claims_count": "FBA ODR A-to-z claims", + "order_defect_rate_afn_claims_status": "FBA ODR claims status", + "order_defect_rate_afn_fulfillment_type": "FBA ODR channel", + "order_defect_rate_afn_negative_feedback_count": "FBA ODR negative feedback", + "order_defect_rate_afn_negative_feedback_status": "FBA ODR feedback status", + "order_defect_rate_afn_order_count": "FBA order defect rate orders", + "order_defect_rate_afn_order_with_defects_count": "FBA ODR defective orders", + "order_defect_rate_afn_order_with_defects_status": "FBA ODR defective orders status", + "order_defect_rate_afn_rate": "FBA order defect rate", + "order_defect_rate_afn_reporting_date_range_reporting_date_from": "FBA order defect rate from", + "order_defect_rate_afn_reporting_date_range_reporting_date_to": "FBA order defect rate to", + "order_defect_rate_afn_status": "FBA order defect rate status", + "order_defect_rate_afn_target_condition": "FBA order defect rate target rule", + "order_defect_rate_afn_target_value": "FBA order defect rate target", + "order_defect_rate_mfn_chargebacks_count": "MFN ODR chargebacks", + "order_defect_rate_mfn_chargebacks_status": "MFN ODR chargebacks status", + "order_defect_rate_mfn_claims_count": "MFN ODR A-to-z claims", + "order_defect_rate_mfn_claims_status": "MFN ODR claims status", + "order_defect_rate_mfn_fulfillment_type": "MFN ODR channel", + "order_defect_rate_mfn_negative_feedback_count": "MFN ODR negative feedback", + "order_defect_rate_mfn_negative_feedback_status": "MFN ODR feedback status", + "order_defect_rate_mfn_order_count": "MFN order defect rate orders", + "order_defect_rate_mfn_order_with_defects_count": "MFN ODR defective orders", + "order_defect_rate_mfn_order_with_defects_status": "MFN ODR defective orders status", + "order_defect_rate_mfn_rate": "MFN order defect rate", + "order_defect_rate_mfn_reporting_date_range_reporting_date_from": "MFN order defect rate from", + "order_defect_rate_mfn_reporting_date_range_reporting_date_to": "MFN order defect rate to", + "order_defect_rate_mfn_status": "MFN order defect rate status", + "order_defect_rate_mfn_target_condition": "MFN order defect rate target rule", + "order_defect_rate_mfn_target_value": "MFN order defect rate target", + "other_policy_violations_defects_count": "Other policy violations count", + "other_policy_violations_reporting_date_range_reporting_date_from": "Other policy violations from", + "other_policy_violations_reporting_date_range_reporting_date_to": "Other policy violations to", + "other_policy_violations_status": "Other policy violations status", + "other_policy_violations_target_condition": "Other policy violations target rule", + "other_policy_violations_target_value": "Other policy violations target", + "policy_violation_warnings_reporting_date_range_reporting_date_from": "Policy warnings from", + "policy_violation_warnings_reporting_date_range_reporting_date_to": "Policy warnings to", + "policy_violation_warnings_warnings_count": "Policy warnings", + "pre_fulfillment_cancellation_rate_cancellation_count": "Cancelled orders", + "pre_fulfillment_cancellation_rate_order_count": "Cancellation rate orders", + "pre_fulfillment_cancellation_rate_rate": "Cancellation rate", + "pre_fulfillment_cancellation_rate_reporting_date_range_reporting_date_from": "Cancellation rate from", + "pre_fulfillment_cancellation_rate_reporting_date_range_reporting_date_to": "Cancellation rate to", + "pre_fulfillment_cancellation_rate_status": "Cancellation rate status", + "pre_fulfillment_cancellation_rate_target_condition": "Cancellation rate target rule", + "pre_fulfillment_cancellation_rate_target_value": "Cancellation rate target", + "product_authenticity_customer_complaints_defects_count": "Authenticity complaints count", + "product_authenticity_customer_complaints_reporting_date_range_reporting_date_from": "Authenticity complaints from", + "product_authenticity_customer_complaints_reporting_date_range_reporting_date_to": "Authenticity complaints to", + "product_authenticity_customer_complaints_status": "Authenticity complaints status", + "product_authenticity_customer_complaints_target_condition": "Authenticity complaints target rule", + "product_authenticity_customer_complaints_target_value": "Authenticity complaints target", + "product_condition_customer_complaints_defects_count": "Condition complaints count", + "product_condition_customer_complaints_reporting_date_range_reporting_date_from": "Condition complaints from", + "product_condition_customer_complaints_reporting_date_range_reporting_date_to": "Condition complaints to", + "product_condition_customer_complaints_status": "Condition complaints status", + "product_condition_customer_complaints_target_condition": "Condition complaints target rule", + "product_condition_customer_complaints_target_value": "Condition complaints target", + "product_safety_customer_complaints_defects_count": "Safety complaints count", + "product_safety_customer_complaints_reporting_date_range_reporting_date_from": "Safety complaints from", + "product_safety_customer_complaints_reporting_date_range_reporting_date_to": "Safety complaints to", + "product_safety_customer_complaints_status": "Safety complaints status", + "product_safety_customer_complaints_target_condition": "Safety complaints target rule", + "product_safety_customer_complaints_target_value": "Safety complaints target", + "received_intellectual_property_complaints_defects_count": "IP complaints count", + "received_intellectual_property_complaints_reporting_date_range_reporting_date_from": "IP complaints from", + "received_intellectual_property_complaints_reporting_date_range_reporting_date_to": "IP complaints to", + "received_intellectual_property_complaints_status": "IP complaints status", + "received_intellectual_property_complaints_target_condition": "IP complaints target rule", + "received_intellectual_property_complaints_target_value": "IP complaints target", + "restricted_product_policy_violations_defects_count": "Restricted product issues count", + "restricted_product_policy_violations_reporting_date_range_reporting_date_from": "Restricted product issues from", + "restricted_product_policy_violations_reporting_date_range_reporting_date_to": "Restricted product issues to", + "restricted_product_policy_violations_status": "Restricted product issues status", + "restricted_product_policy_violations_target_condition": "Restricted product issues target rule", + "restricted_product_policy_violations_target_value": "Restricted product issues target", + "status": "Status", + "suspected_intellectual_property_violations_defects_count": "Suspected IP violations count", + "suspected_intellectual_property_violations_reporting_date_range_reporting_date_from": "Suspected IP violations from", + "suspected_intellectual_property_violations_reporting_date_range_reporting_date_to": "Suspected IP violations to", + "suspected_intellectual_property_violations_status": "Suspected IP violations status", + "suspected_intellectual_property_violations_target_condition": "Suspected IP violations target rule", + "suspected_intellectual_property_violations_target_value": "Suspected IP violations target", + "unit_on_time_delivery_rate_rate": "Unit on time delivery rate", + "unit_on_time_delivery_rate_reporting_date_range_reporting_date_from": "Unit on time delivery rate from", + "unit_on_time_delivery_rate_reporting_date_range_reporting_date_to": "Unit on time delivery rate to", + "unit_on_time_delivery_rate_status": "Unit on time delivery rate status", + "unit_on_time_delivery_rate_target_condition": "Unit on time delivery rate target rule", + "unit_on_time_delivery_rate_target_value": "Unit on time delivery rate target", + "unit_on_time_delivery_rate_total_unit_count": "Unit on time delivery units", + "unit_on_time_delivery_rate_unit_on_time_delivery_count": "Units delivered on time", + "valid_tracking_rate_rate": "Valid tracking rate", + "valid_tracking_rate_reporting_date_range_reporting_date_from": "Valid tracking rate from", + "valid_tracking_rate_reporting_date_range_reporting_date_to": "Valid tracking rate to", + "valid_tracking_rate_shipment_count": "Valid tracking rate shipments", + "valid_tracking_rate_status": "Valid tracking rate status", + "valid_tracking_rate_target_condition": "Valid tracking rate target rule", + "valid_tracking_rate_target_value": "Valid tracking rate target", + "valid_tracking_rate_valid_tracking_count": "Shipments with valid tracking", + "warning_states": "Warning states", + }, + _AMZN + "inactive_listings": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "add_delete": "Add delete", + "asin1": "ASIN", + "asin2": "ASIN 2", + "asin3": "ASIN 3", + "bid_for_featured_placement": "Bid for featured placement", + "expedited_shipping": "Expedited shipping", + "fulfillment_channel": "Fulfillment channel", + "image_url": "Image URL", + "item_condition": "Item condition", + "item_description": "Item description", + "item_is_marketplace": "Item is marketplace", + "item_name": "Item name", + "item_note": "Item note", + "listing_id": "Listing ID", + "merchant_shipping_group": "Merchant shipping group", + "open_date": "Open date", + "pending_quantity": "Pending quantity", + "price": "Price", + "product_id": "Product ID", + "product_id_type": "Product ID type", + "quantity": "Quantity", + "seller_sku": "Seller SKU", + "will_ship_internationally": "Will ship internationally", + "zshop_boldface": "Zshop boldface", + "zshop_browse_path": "Zshop browse path", + "zshop_category1": "Zshop category", + "zshop_shipping_fee": "Zshop shipping fee", + "zshop_storefront_feature": "Zshop storefront feature", + }, + _AMZN + "cancelled_listings": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "add_delete": "Add delete", + "asin1": "ASIN", + "asin2": "ASIN 2", + "asin3": "ASIN 3", + "business_price": "Business price", + "expedited_shipping": "Expedited shipping", + "image_url": "Image URL", + "item_condition": "Item condition", + "item_description": "Item description", + "item_is_marketplace": "Item is marketplace", + "item_name": "Item name", + "item_note": "Item note", + "merchant_shipping_group": "Merchant shipping group", + "price": "Price", + "product_id": "Product ID", + "product_id_type": "Product ID type", + "progressive_lower_bound_1": "Progressive lower bound 1", + "progressive_lower_bound_2": "Progressive lower bound 2", + "progressive_lower_bound_3": "Progressive lower bound 3", + "progressive_price_1": "Progressive price 1", + "progressive_price_2": "Progressive price 2", + "progressive_price_3": "Progressive price 3", + "progressive_price_type": "Progressive price type", + "quantity": "Quantity", + "quantity_lower_bound_1": "Quantity lower bound 1", + "quantity_lower_bound_2": "Quantity lower bound 2", + "quantity_lower_bound_3": "Quantity lower bound 3", + "quantity_lower_bound_4": "Quantity lower bound 4", + "quantity_lower_bound_5": "Quantity lower bound 5", + "quantity_price_1": "Quantity price 1", + "quantity_price_2": "Quantity price 2", + "quantity_price_3": "Quantity price 3", + "quantity_price_4": "Quantity price 4", + "quantity_price_5": "Quantity price 5", + "quantity_price_type": "Quantity price type", + "seller_sku": "Seller SKU", + "will_ship_internationally": "Will ship internationally", + "zshop_boldface": "Zshop boldface", + "zshop_browse_path": "Zshop browse path", + "zshop_category1": "Zshop category", + "zshop_shipping_fee": "Zshop shipping fee", + "zshop_storefront_feature": "Zshop storefront feature", + "_snapshot_date": "Synced on", + }, + _AMZN + "suppressed_listings": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "asin": "ASIN", + "condition": "Condition", + "issue_description": "Issue description", + "product_name": "Product name", + "reason": "Reason", + "sku": "SKU", + "status": "Status", + "status_change_date": "Status change date", + }, + _AMZN + "mfn_returns": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "a_to_z_claim": "A-to-z claim", + "amazon_rma_id": "Amazon RMA ID", + "asin": "ASIN", + "currency_code": "Currency", + "in_policy": "In policy", + "invoice_number": "Invoice number", + "is_prime": "Prime order", + "item_name": "Item name", + "label_cost": "Label cost", + "label_to_be_paid_by": "Label to be paid by", + "label_type": "Label type", + "merchant_rma_id": "Merchant RMA ID", + "merchant_sku": "Merchant SKU", + "order_amount": "Order amount", + "order_date": "Order date", + "order_id": "Order ID", + "order_item_id": "Order item ID", + "order_quantity": "Order quantity", + "refunded_amount": "Refunded amount", + "resolution": "Resolution", + "return_carrier": "Return carrier", + "return_delivery_date": "Return delivery date", + "return_quantity": "Return quantity", + "return_reason": "Return reason", + "return_request_date": "Return request date", + "return_request_status": "Return request status", + "return_type": "Return type", + "safet_action_reason": "SAFE-T action reason", + "safet_claim_creation_time": "SAFE-T claim created", + "safet_claim_id": "SAFE-T claim ID", + "safet_claim_reimbursement_amount": "SAFE-T reimbursement", + "safet_claim_state": "SAFE-T claim state", + "tracking_id": "Tracking ID", + "_period_start": "Report period start", + "_period_end": "Report period end", + }, + _AMZN + "fee_discounts": { + "_snapshot_date": "Synced on", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_data_origin": "Data origin", + "_report_type": "Report type", + }, + _AMZN + "order_metrics": { + "_data_origin": "Data origin", + "_period_end": "Report period end", + "_period_start": "Report period start", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "average_unit_price_amount": "Average unit price amount", + "average_unit_price_currency_code": "Average unit price currency", + "interval": "Interval", + "order_count": "Order count", + "order_item_count": "Order item count", + "total_sales_amount": "Total sales amount", + "total_sales_currency_code": "Total sales currency", + "unit_count": "Unit count", + }, + _AMZN + "fba_inventory": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "asin": "ASIN", + "condition": "Condition", + "fn_sku": "FNSKU", + "inventory_details_fulfillable_quantity": "Fulfillable quantity", + "inventory_details_future_supply_quantity_future_supply_buyable_quantity": "Future supply buyable", + "inventory_details_future_supply_quantity_reserved_future_supply_quantity": "Future supply reserved", + "inventory_details_inbound_receiving_quantity": "Inbound receiving quantity", + "inventory_details_inbound_shipped_quantity": "Inbound shipped quantity", + "inventory_details_inbound_working_quantity": "Inbound working quantity", + "inventory_details_researching_quantity_researching_quantity_breakdown": "Researching breakdown", + "inventory_details_researching_quantity_total_researching_quantity": "Researching quantity", + "inventory_details_reserved_quantity_fc_processing_quantity": "Reserved FC processing", + "inventory_details_reserved_quantity_pending_customer_order_quantity": "Reserved customer orders", + "inventory_details_reserved_quantity_pending_transshipment_quantity": "Reserved transshipment", + "inventory_details_reserved_quantity_total_reserved_quantity": "Reserved quantity", + "inventory_details_unfulfillable_quantity_carrier_damaged_quantity": "Damaged by carrier", + "inventory_details_unfulfillable_quantity_customer_damaged_quantity": "Damaged by customer", + "inventory_details_unfulfillable_quantity_defective_quantity": "Defective units", + "inventory_details_unfulfillable_quantity_distributor_damaged_quantity": "Damaged by distributor", + "inventory_details_unfulfillable_quantity_expired_quantity": "Expired units", + "inventory_details_unfulfillable_quantity_total_unfulfillable_quantity": "Unfulfillable quantity", + "inventory_details_unfulfillable_quantity_warehouse_damaged_quantity": "Damaged in warehouse", + "last_updated_time": "Last updated time", + "product_name": "Product name", + "seller_sku": "Seller SKU", + "stores": "Stores", + "total_quantity": "Total quantity", + }, + _AMZN + "listing_items": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "attributes_age_range_description": "Age range description", + "attributes_animal_theme": "Animal theme", + "attributes_batteries_included": "Batteries included", + "attributes_batteries_required": "Batteries required", + "attributes_binding": "Binding", + "attributes_bowl": "Bowl", + "attributes_bracelet_form": "Bracelet form", + "attributes_brand": "Brand", + "attributes_bullet_point": "Bullet point", + "attributes_candle_holder_form": "Candle holder form", + "attributes_candle_type": "Candle type", + "attributes_care_instructions": "Care instructions", + "attributes_child_parent_sku_relationship": "Parent SKU relationship", + "attributes_closure": "Closure", + "attributes_collection": "Collection", + "attributes_collection_item": "Collection item", + "attributes_color": "Color", + "attributes_compatible_devices": "Compatible devices", + "attributes_condition_note": "Condition note", + "attributes_condition_type": "Condition type", + "attributes_container": "Container", + "attributes_contains_liquid_contents": "Contains liquid contents", + "attributes_country_as_labeled": "Country as labeled", + "attributes_country_of_origin": "Country of origin", + "attributes_country_string": "Country string", + "attributes_cpsia_cautionary_statement": "CPSIA cautionary statement", + "attributes_customer_package_type": "Customer package type", + "attributes_department": "Department", + "attributes_each_unit_count": "Each unit count", + "attributes_externally_assigned_product_identifier": "External product identifier", + "attributes_fabric_type": "Fabric type", + "attributes_fabric_weight": "Fabric weight", + "attributes_finish_type": "Finish type", + "attributes_frame": "Frame", + "attributes_fulfillment_availability": "Fulfillment availability attribute", + "attributes_generic_keyword": "Generic keyword", + "attributes_gift_options": "Gift options", + "attributes_handle": "Handle", + "attributes_import_designation": "Import designation", + "attributes_included_components": "Included components", + "attributes_includes_ac_adapter": "Includes AC adapter", + "attributes_includes_rechargable_battery": "Includes rechargable battery", + "attributes_indoor_outdoor_usage": "Indoor outdoor usage", + "attributes_ink": "Ink", + "attributes_is_adult_product": "Is adult product", + "attributes_is_assembly_required": "Is assembly required", + "attributes_is_autographed": "Is autographed", + "attributes_is_customizable": "Is customizable", + "attributes_is_dishwasher_safe": "Is dishwasher safe", + "attributes_is_electric": "Is electric", + "attributes_is_expiration_dated_product": "Is expiration dated product", + "attributes_is_fragile": "Is fragile", + "attributes_is_stain_resistant": "Is stain resistant", + "attributes_is_this_product_subject_to_buyer_age_restrictions": "Buyer age restricted", + "attributes_item_depth_width_height": "Item depth width height", + "attributes_item_diameter": "Item diameter", + "attributes_item_dimensions": "Item dimensions", + "attributes_item_dimensions_fraction": "Item dimensions fraction", + "attributes_item_display_dimensions": "Item display dimensions", + "attributes_item_display_weight": "Item display weight", + "attributes_item_form": "Item form", + "attributes_item_length": "Item length", + "attributes_item_length_width": "Item length width", + "attributes_item_length_width_height": "Item length width height", + "attributes_item_length_width_thickness": "Item length width thickness", + "attributes_item_name": "Item name", + "attributes_item_package_dimensions": "Item package dimensions", + "attributes_item_package_quantity": "Item package quantity", + "attributes_item_package_weight": "Item package weight", + "attributes_item_shape": "Item shape", + "attributes_item_thickness": "Item thickness", + "attributes_item_type_keyword": "Item type keyword", + "attributes_item_type_name": "Item type name", + "attributes_item_volume": "Item volume", + "attributes_item_weight": "Item weight", + "attributes_item_width_height": "Item width height", + "attributes_light_source": "Light source", + "attributes_list_price": "List price", + "attributes_main_product_image_locator": "Main image", + "attributes_manufacturer": "Manufacturer", + "attributes_manufacturer_minimum_age": "Manufacturer minimum age", + "attributes_material": "Material", + "attributes_material_composition": "Material composition", + "attributes_material_feature": "Material feature", + "attributes_max_order_quantity": "Max order quantity", + "attributes_merchant_release_date": "Merchant release date", + "attributes_merchant_shipping_group": "Merchant shipping group", + "attributes_merchant_suggested_asin": "Merchant suggested ASIN", + "attributes_metal_type": "Metal type", + "attributes_model_name": "Model name", + "attributes_model_number": "Model number", + "attributes_mounting_type": "Mounting type", + "attributes_number_of_boxes": "Number of boxes", + "attributes_number_of_compartments": "Number of compartments", + "attributes_number_of_items": "Number of items", + "attributes_number_of_packs": "Number of packs", + "attributes_number_of_pieces": "Number of pieces", + "attributes_number_of_sets": "Number of sets", + "attributes_occasion": "Occasion", + "attributes_occasion_type": "Occasion type", + "attributes_other_product_image_locator_1": "Other image 1", + "attributes_other_product_image_locator_2": "Other image 2", + "attributes_other_product_image_locator_3": "Other image 3", + "attributes_other_product_image_locator_4": "Other image 4", + "attributes_other_product_image_locator_5": "Other image 5", + "attributes_other_product_image_locator_6": "Other image 6", + "attributes_other_product_image_locator_7": "Other image 7", + "attributes_other_product_image_locator_8": "Other image 8", + "attributes_package_contains_sku": "Package contains SKU", + "attributes_package_level": "Package level", + "attributes_packaging_type": "Packaging type", + "attributes_parentage_level": "Parentage level", + "attributes_part_number": "Part number", + "attributes_pattern": "Pattern", + "attributes_pesticide_marking": "Pesticide marking", + "attributes_plant_or_animal_product_type": "Plant or animal product type", + "attributes_planter_form": "Planter form", + "attributes_power_source_type": "Power source type", + "attributes_product_description": "Product description", + "attributes_product_expiration_type": "Product expiration type", + "attributes_product_site_launch_date": "Product site launch date", + "attributes_product_tax_code": "Product tax code", + "attributes_purchasable_offer": "Purchasable offer", + "attributes_recommended_browse_nodes": "Recommended browse nodes", + "attributes_recommended_uses_for_product": "Recommended uses for product", + "attributes_required_product_compliance_certificate": "Required compliance certificate", + "attributes_room_type": "Room type", + "attributes_rtip_safety_warning": "RTIP safety warning", + "attributes_safety_warning": "Safety warning", + "attributes_scent": "Scent", + "attributes_seasons": "Seasons", + "attributes_set_name": "Set name", + "attributes_size": "Size", + "attributes_size_map": "Size map", + "attributes_skip_offer": "Skip offer", + "attributes_special_feature": "Special feature", + "attributes_specific_uses_for_product": "Specific uses for product", + "attributes_specific_uses_keyword": "Specific uses keyword", + "attributes_street_date": "Street date", + "attributes_style": "Style", + "attributes_supplemental_condition_information": "Supplemental condition info", + "attributes_supplier_declared_dg_hz_regulation": "Dangerous goods regulation", + "attributes_supplier_declared_has_product_identifier_exemption": "Product identifier exemption", + "attributes_supplier_declared_material_regulation": "Material regulation", + "attributes_swatch_product_image_locator": "Swatch image", + "attributes_target_audience": "Target audience", + "attributes_target_audience_keyword": "Target audience keyword", + "attributes_target_gender": "Target gender", + "attributes_theme": "Theme", + "attributes_title_differentiation": "Title differentiation", + "attributes_total_eaches": "Total eaches", + "attributes_unit_count": "Unit count", + "attributes_variation_theme": "Variation theme", + "attributes_warranty_description": "Warranty description", + "attributes_website_shipping_weight": "Website shipping weight", + "attributes_wick_quantity": "Wick quantity", + "fulfillment_availability": "Fulfillment availability", + "issues": "Issues", + "offers": "Offers", + "sku": "SKU", + "summaries": "Summaries", + "attributes_package_size_name": "Package size name", + "attributes_product_benefit": "Product benefit", + }, + _AMZN + "catalog_items": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "asin": "ASIN", + "attributes_accent_color": "Accent color", + "attributes_adhesive_format": "Adhesive format", + "attributes_adhesive_location": "Adhesive location", + "attributes_adhesive_material": "Adhesive material", + "attributes_adhesive_side_count": "Adhesive side count", + "attributes_age_range_description": "Age range description", + "attributes_animal_theme": "Animal theme", + "attributes_art_craft_kit_type": "Art craft kit type", + "attributes_assembly_instructions": "Assembly instructions", + "attributes_assembly_instructions_description": "Assembly instructions detail", + "attributes_band": "Band", + "attributes_base": "Base", + "attributes_base_type": "Base type", + "attributes_batteries_included": "Batteries included", + "attributes_batteries_required": "Batteries required", + "attributes_bowl": "Bowl", + "attributes_bracelet_form": "Bracelet form", + "attributes_brand": "Brand", + "attributes_brightness": "Brightness", + "attributes_bullet_point": "Bullet point", + "attributes_cable": "Cable", + "attributes_candle_holder_form": "Candle holder form", + "attributes_candle_type": "Candle type", + "attributes_capacity": "Capacity", + "attributes_care_instructions": "Care instructions", + "attributes_chain_length": "Chain length", + "attributes_chain_type": "Chain type", + "attributes_clasp_type": "Clasp type", + "attributes_closure": "Closure", + "attributes_collection": "Collection", + "attributes_collection_item": "Collection item", + "attributes_color": "Color", + "attributes_compatible_material": "Compatible material", + "attributes_configuration": "Configuration", + "attributes_container": "Container", + "attributes_contains_liquid_contents": "Contains liquid contents", + "attributes_corner_style": "Corner style", + "attributes_cpsia_cautionary_statement": "CPSIA cautionary statement", + "attributes_customer_package_type": "Customer package type", + "attributes_department": "Department", + "attributes_dispenser_core_diameter": "Dispenser core diameter", + "attributes_embellishment_feature": "Embellishment feature", + "attributes_externally_assigned_product_identifier": "External product identifier", + "attributes_fabric_type": "Fabric type", + "attributes_fc_shelf_life": "FC shelf life", + "attributes_finish_type": "Finish type", + "attributes_flower_count": "Flower count", + "attributes_foil_thickness": "Foil thickness", + "attributes_gem_birthstone_month": "Gem birthstone month", + "attributes_gem_type": "Gem type", + "attributes_ghs": "GHS", + "attributes_gpsr_safety_attestation": "GPSR safety attestation", + "attributes_handle": "Handle", + "attributes_hardware_color": "Hardware color", + "attributes_has_builtin_light": "Has builtin light", + "attributes_has_crackle_wick": "Has crackle wick", + "attributes_has_drainage": "Has drainage", + "attributes_has_lid": "Has lid", + "attributes_hazmat": "Hazmat", + "attributes_included_components": "Included components", + "attributes_indoor_outdoor_usage": "Indoor outdoor usage", + "attributes_initial_character": "Initial character", + "attributes_ink": "Ink", + "attributes_inside": "Inside", + "attributes_is_assembly_required": "Is assembly required", + "attributes_is_customizable": "Is customizable", + "attributes_is_dishwasher_safe": "Is dishwasher safe", + "attributes_is_expiration_dated_product": "Is expiration dated product", + "attributes_is_fragile": "Is fragile", + "attributes_is_microwaveable": "Is microwaveable", + "attributes_is_this_product_subject_to_buyer_age_restrictions": "Buyer age restricted", + "attributes_item_depth_width_height": "Item depth width height", + "attributes_item_diameter": "Item diameter", + "attributes_item_dimensions": "Item dimensions", + "attributes_item_dimensions_fraction": "Item dimensions fraction", + "attributes_item_display_dimensions": "Item display dimensions", + "attributes_item_display_weight": "Item display weight", + "attributes_item_form": "Item form", + "attributes_item_length": "Item length", + "attributes_item_length_width": "Item length width", + "attributes_item_length_width_height": "Item length width height", + "attributes_item_length_width_thickness": "Item length width thickness", + "attributes_item_name": "Item name", + "attributes_item_package_dimensions": "Item package dimensions", + "attributes_item_package_quantity": "Item package quantity", + "attributes_item_package_weight": "Item package weight", + "attributes_item_resistance": "Item resistance", + "attributes_item_shape": "Item shape", + "attributes_item_thickness": "Item thickness", + "attributes_item_type_keyword": "Item type keyword", + "attributes_item_type_name": "Item type name", + "attributes_item_volume": "Item volume", + "attributes_item_weight": "Item weight", + "attributes_item_width": "Item width", + "attributes_item_width_height": "Item width height", + "attributes_legal_compliance_certifications": "Legal compliance certifications", + "attributes_life_event": "Life event", + "attributes_light_color": "Light color", + "attributes_line_weight": "Line weight", + "attributes_list_price": "List price", + "attributes_lock_type": "Lock type", + "attributes_manufacturer": "Manufacturer", + "attributes_material": "Material", + "attributes_material_feature": "Material feature", + "attributes_material_type_free": "Material type free", + "attributes_metal_finish": "Metal finish", + "attributes_metal_stamp": "Metal stamp", + "attributes_metal_type": "Metal type", + "attributes_model_name": "Model name", + "attributes_model_number": "Model number", + "attributes_mounting_type": "Mounting type", + "attributes_net_content_count": "Net content count", + "attributes_net_content_weight": "Net content weight", + "attributes_number_of_boxes": "Number of boxes", + "attributes_number_of_compartments": "Number of compartments", + "attributes_number_of_items": "Number of items", + "attributes_number_of_levels": "Number of levels", + "attributes_number_of_packs": "Number of packs", + "attributes_number_of_pieces": "Number of pieces", + "attributes_number_of_sets": "Number of sets", + "attributes_numeric_theme": "Numeric theme", + "attributes_occasion": "Occasion", + "attributes_occasion_type": "Occasion type", + "attributes_opening_mechanism": "Opening mechanism", + "attributes_operation_mode": "Operation mode", + "attributes_package_level": "Package level", + "attributes_package_size_name": "Package size name", + "attributes_packaging_type": "Packaging type", + "attributes_paint_type": "Paint type", + "attributes_paper_finish": "Paper finish", + "attributes_part_number": "Part number", + "attributes_pattern": "Pattern", + "attributes_pesticide_marking": "Pesticide marking", + "attributes_plant_or_animal_product_type": "Plant or animal product type", + "attributes_plant_style": "Plant style", + "attributes_planter_form": "Planter form", + "attributes_power_source_type": "Power source type", + "attributes_primary_stone": "Primary stone", + "attributes_product_description": "Product description", + "attributes_product_expiration_type": "Product expiration type", + "attributes_product_site_launch_date": "Product site launch date", + "attributes_recommended_uses_for_product": "Recommended uses for product", + "attributes_required_product_compliance_certificate": "Required compliance certificate", + "attributes_reusability": "Reusability", + "attributes_ring": "Ring", + "attributes_roll": "Roll", + "attributes_room_type": "Room type", + "attributes_safety_data_sheet_url": "Safety data sheet URL", + "attributes_safety_warning": "Safety warning", + "attributes_scent": "Scent", + "attributes_seasons": "Seasons", + "attributes_set_name": "Set name", + "attributes_setting_type": "Setting type", + "attributes_sheet_count": "Sheet count", + "attributes_size": "Size", + "attributes_size_per_pearl": "Size per pearl", + "attributes_skip_offer": "Skip offer", + "attributes_special_feature": "Special feature", + "attributes_specific_uses_for_product": "Specific uses for product", + "attributes_specification_met": "Specification met", + "attributes_sport_type": "Sport type", + "attributes_stone": "Stone", + "attributes_storage_volume": "Storage volume", + "attributes_street_date": "Street date", + "attributes_style": "Style", + "attributes_subject_character": "Subject character", + "attributes_supplemental_condition_information": "Supplemental condition info", + "attributes_supplier_declared_dg_hz_regulation": "Dangerous goods regulation", + "attributes_supplier_declared_has_product_identifier_exemption": "Product identifier exemption", + "attributes_surface_recommendation": "Surface recommendation", + "attributes_tank_volume": "Tank volume", + "attributes_target_audience": "Target audience", + "attributes_target_audience_keyword": "Target audience keyword", + "attributes_target_gender": "Target gender", + "attributes_tensile_strength": "Tensile strength", + "attributes_theme": "Theme", + "attributes_tip_count": "Tip count", + "attributes_title_differentiation": "Title differentiation", + "attributes_total_diamond_weight": "Total diamond weight", + "attributes_total_eaches": "Total eaches", + "attributes_total_gem_weight": "Total gem weight", + "attributes_tree_type": "Tree type", + "attributes_unit_count": "Unit count", + "attributes_unspsc_code": "UNSPSC code", + "attributes_variation_theme": "Variation theme", + "attributes_vivarium_type": "Vivarium type", + "attributes_warranty_description": "Warranty description", + "attributes_water_resistance_level": "Water resistance level", + "attributes_weave_type": "Weave type", + "attributes_wick_quantity": "Wick quantity", + "attributes_wire_diameter": "Wire diameter", + "dimensions": "Dimensions", + "sales_ranks": "Sales ranks", + "summaries": "Summaries", + "parent_asin": "Parent ASIN", + "parent_asin_source": "Parent ASIN source", + "relationships": "Relationships", + }, + _AMZN + "replenishment": { + "_data_origin": "Data origin", + "_report_document_id": "Report document ID", + "_report_row_number": "Report row", + "_report_type": "Report type", + "_snapshot_date": "Synced on", + "active_subscriptions": "Active subscriptions", + "asin": "ASIN", + "brand_name": "Brand name", + "coupons_revenue_penetration": "Coupon revenue penetration", + "currency_code": "Currency", + "established_subscriber_life_time_value_from_otp": "Established subscriber LTV (one time)", + "established_subscriber_life_time_value_from_sns": "Established subscriber LTV (S&S)", + "fulfillment_channel_type": "Fulfillment channel type", + "growing_subscriber_life_time_value_from_otp": "Growing subscriber LTV (one time)", + "growing_subscriber_life_time_value_from_sns": "Growing subscriber LTV (S&S)", + "lost_revenue_due_to_oos": "Revenue lost to stockouts", + "lost_subscriber_life_time_value_from_otp": "Lost subscriber LTV (one time)", + "lost_subscriber_life_time_value_from_sns": "Lost subscriber LTV (S&S)", + "marketplace_id": "Marketplace ID", + "metric_id": "Metric ID", + "metric_scope": "Metric scope", + "non_subscriber_average_reorders": "Non subscriber average reorders", + "non_subscriber_average_revenue": "Non subscriber average revenue", + "non_subscriber_life_time_value_from_otp": "Non subscriber LTV (one time)", + "not_delivered_due_to_oos": "Not delivered due to stockouts", + "program_type": "Program type", + "revenue_from_active_subscriptions_with_single_delivery": "Revenue from single delivery actives", + "revenue_from_cancelled_subscriptions_after_single_delivery": "Revenue lost after single delivery", + "revenue_penetration": "Revenue penetration", + "revenue_penetration_for0_percent_seller_funding": "Revenue penetration at 0% funding", + "revenue_penetration_for10_percent_seller_funding": "Revenue penetration at 10% funding", + "revenue_penetration_for5_percent_seller_funding": "Revenue penetration at 5% funding", + "share_of_coupon_subscriptions": "Share of coupon subscriptions", + "shipped_subscription_units": "Shipped subscription units", + "signup_conversion_for0_percent_seller_funding": "Signup conversion at 0% funding", + "signup_conversion_for10_percent_seller_funding": "Signup conversion at 10% funding", + "signup_conversion_for5_percent_seller_funding": "Signup conversion at 5% funding", + "sku": "SKU", + "subscriber_average_reorders": "Subscriber average reorders", + "subscriber_average_revenue": "Subscriber average revenue", + "subscriber_retention_for30_days": "Subscriber retention (30 days)", + "subscriber_retention_for90_days": "Subscriber retention (90 days)", + "time_interval_end_date": "Interval end", + "time_interval_start_date": "Interval start", + "time_period_type": "Time period type", + "total_subscriptions_revenue": "Total subscriptions revenue", + }, +} + +#: S78-T01: the columns each Amazon table opens on, `{ut_key: (column, ...)}`, beside the ones +#: `amazon_relational._fields_for` always shows (the pinned column, the product columns, the S67-T30 +#: lookups and the generated link fields). Together they are at most 12 per table; every other +#: field stays listed in Fields with `default: False`. +AMAZON_DEFAULT_COLUMNS = { + _AMZN + "orders": ("purchase_date", "order_status", "fulfillment_channel", "product_name", + "asin", "quantity", "item_price", "currency"), + _AMZN + "order_items": ("purchase_date", "order_status", "item_status", "product_name", "asin", + "quantity", "item_price"), + _AMZN + "listings": ("item_name", "price", "quantity", "status", "fulfillment_channel", + "open_date", "item_condition", "product_id", "listing_id"), + _AMZN + "settlements": ("posted_date", "transaction_type", "amount_type", "amount_description", + "amount", "quantity_purchased"), + _AMZN + "inventory": ("inventory_source", "_snapshot_date"), + _AMZN + "pricing": ("sales_by_asin_units_ordered", + "sales_by_asin_ordered_product_sales_amount", "traffic_by_asin_sessions", + "traffic_by_asin_page_views", "traffic_by_asin_unit_session_percentage", + "traffic_by_asin_buy_box_percentage", "sales_by_asin_total_order_items"), + _AMZN + "brand_analytics": ("search_frequency_rank", "department_name", "clicked_asin", + "clicked_item_name", "click_share_rank", "click_share", "conversion_share"), + _AMZN + "economics": ("start_date", "end_date", "sales_net_units_sold", + "sales_ordered_product_sales_amount", "sales_net_product_sales_amount", + "fees_total_amount", "net_proceeds_total_amount"), + _AMZN + "transactions": ("posted_date", "transaction_type", "transaction_status", + "description", "total_amount"), + _AMZN + "fba_fee_estimates": ("product_name", "your_price", "estimated_fee_total", + "estimated_referral_fee_per_unit", "expected_fulfillment_fee_per_unit", + "product_size_tier", "_snapshot_date"), + _AMZN + "referral_fees": ("item_name", "price", "estimated_referral_fee_per_item", + "_snapshot_date"), + _AMZN + "storage_fees": ("month", "product_name", "fulfillment_center", "product_size_tier", + "average_quantity_on_hand", "estimated_total_item_volume", "estimated_monthly_storage_fee", + "currency"), + _AMZN + "search_query_performance": ("start_date", "end_date", + "search_query_data_search_query_volume", "impression_data_asin_impression_count", + "click_data_asin_click_count", "purchase_data_asin_purchase_count", + "purchase_data_asin_purchase_share"), + _AMZN + "long_term_storage_fees": ("product_name", "snapshot_date", "surcharge_age_tier", + "qty_charged", "amount_charged", "currency", "condition"), + _AMZN + "storage_overage_fees": ("_period_start",), + _AMZN + "reimbursements": ("approval_date", "product_name", "reason", + "quantity_reimbursed_total", "amount_total"), + _AMZN + "fba_customer_returns": ("return_date", "product_name", "quantity", "reason", + "detailed_disposition"), + _AMZN + "replacements": ("shipment_date", "original_amazon_order_id", "quantity", + "replacement_reason_code", "fulfillment_center_id"), + _AMZN + "fba_shipped_sales": ("shipment_date", "quantity", "item_price_per_unit", "currency", + "ship_state"), + _AMZN + "fba_shipment_promotions": ("shipment_date", "item_promotion_id", "description", + "item_promotion_discount", "currency", "shipment_id", "promotion_rule_value"), + _AMZN + "inventory_ledger_detail": ("date", "title", "event_type", "quantity", "disposition", + "fulfillment_center", "reason"), + _AMZN + "inventory_planning": ("product_name", "available", "inbound_quantity", + "units_shipped_t30", "days_of_supply", "recommended_action", + "recommended_ship_in_quantity"), + _AMZN + "manage_inventory": ("product_name", "afn_fulfillable_quantity", + "afn_inbound_shipped_quantity", "afn_reserved_quantity", "afn_unsellable_quantity", + "afn_total_quantity", "your_price"), + _AMZN + "reserved_inventory": ("product_name", "reserved_qty", "reserved_customerorders", + "reserved_fc_processing", "reserved_staging", "_snapshot_date"), + _AMZN + "restock_recommendations": ("product_name", "available", "inbound", + "units_sold_last_30_days", "days_of_supply_at_amazon_fulfillment_network", + "recommended_replenishment_qty", "recommended_ship_date"), + _AMZN + "stranded_inventory": ("product_name", "date_stranded", "stranded_reason", + "primary_action", "fulfillable_qty", "unfulfillable_qty", "your_price"), + _AMZN + "inbound_noncompliance": ("fba_shipment_id", "issue_reported_date", "product_name", + "problem_type", "problem_quantity", "fee_type", "fee_total"), + _AMZN + "inventory_expiration_dates": ("_snapshot_date",), + _AMZN + "recommended_removals": ("product_name", "sellable_quantity", "unsellable_quantity", + "sellable_removal_quantity", "condition", "snapshot_date"), + _AMZN + "removal_orders": ("request_date", "order_type", "order_status", "requested_quantity", + "shipped_quantity", "disposition", "removal_fee"), + _AMZN + "removal_shipments": ("request_date", "shipment_date", "removal_order_type", + "shipped_quantity", "carrier", "tracking_number", "disposition"), + _AMZN + "subscribe_save_forecast": ("_snapshot_date",), + _AMZN + "subscribe_save_performance": ("product_name", "offer_state", "week_1_start_date", + "sns_units_shipped_week_1", "sns_sale_price_week_1", "sns_discount_week_1"), + _AMZN + "remote_fulfillment_eligibility": ("managing_remote_fulfillment", "_snapshot_date"), + _AMZN + "search_catalog_performance": ("start_date", "end_date", + "impression_data_impression_count", "click_data_click_count", "click_data_click_rate", + "cart_add_data_cart_add_count", "purchase_data_purchase_count", + "purchase_data_conversion_rate"), + _AMZN + "market_basket": ("start_date", "end_date", "purchased_with_asin", + "purchased_with_rank", "combination_pct"), + _AMZN + "promotion_performance": ("_period_start",), + _AMZN + "coupon_performance": ("_period_start",), + _AMZN + "account_health": ("account_health_rating_ahr_score", + "account_health_rating_ahr_status", "order_defect_rate_afn_rate", + "order_defect_rate_afn_status", "late_shipment_rate_rate", + "pre_fulfillment_cancellation_rate_rate", "valid_tracking_rate_rate", + "on_time_delivery_rate_rate", "invoice_defect_rate_rate", + "policy_violation_warnings_warnings_count", "_snapshot_date"), + _AMZN + "inactive_listings": ("item_name", "price", "quantity", "open_date", + "fulfillment_channel", "item_condition", "_snapshot_date"), + _AMZN + "cancelled_listings": ("item_name", "price", "quantity", "item_condition", + "_snapshot_date"), + _AMZN + "suppressed_listings": ("product_name", "status", "reason", "issue_description", + "status_change_date", "condition"), + _AMZN + "mfn_returns": ("item_name", "return_request_date", "return_reason", "return_quantity", + "refunded_amount"), + _AMZN + "fee_discounts": ("_snapshot_date",), + _AMZN + "order_metrics": ("_period_start", "order_count", "order_item_count", "unit_count", + "total_sales_amount", "average_unit_price_amount", "total_sales_currency_code"), + _AMZN + "fba_inventory": ("product_name", "condition", "total_quantity", + "inventory_details_fulfillable_quantity", + "inventory_details_reserved_quantity_total_reserved_quantity", + "inventory_details_unfulfillable_quantity_total_unfulfillable_quantity", + "last_updated_time"), + _AMZN + "listing_items": ("attributes_item_name", "attributes_brand", "attributes_list_price", + "attributes_color", "attributes_size", "issues", "backend_keywords", "_snapshot_date"), + _AMZN + "catalog_items": ("parent_asin", "attributes_item_name", "attributes_brand", + "attributes_color", "attributes_size", "attributes_item_type_keyword", + "attributes_list_price", "sales_ranks", "_snapshot_date"), + _AMZN + "replenishment": ("asin", "time_interval_start_date", "time_interval_end_date", + "active_subscriptions", "shipped_subscription_units", "total_subscriptions_revenue", + "revenue_penetration", "subscriber_retention_for30_days", "program_type"), + _AMZN + "sku_keywords": ("sku", "asin", "month", "search_query", "impressions", "clicks", + "cart_adds", "purchases", "backend_keywords", "source_basis"), + _AMZN + "keyword_ranks": ("keyword", "date", "organic_rank", "sponsored_rank", + "sponsored_rank_basis", "sku", "asin"), +} + # ═════════════════════════════════════════════════════════════════════════════════════════════ # META ADS — transcribed from `aios-web/api/meta_relational.py` @@ -889,6 +3437,46 @@ META_TABLES = ( ) +#: S78-T02 (C1): the rail order of the Amazon groups. ⛔ Shown to a person: sentence case, no dash. +AMAZON_GROUP_ORDER = ("Sales", "Fees and payouts", "Inventory", "Listings", "Advertising") + +#: S78-T02 (C1): `{ut_key: group}` for every Amazon spec. A literal beside the specs, because +#: `ConnectorTableSpec` stays exactly seven fields. `verify_connectors` asserts full coverage. +AMAZON_GROUPS = { + **{_AMZN + t: "Sales" for t in ( + "orders", "order_items", "order_metrics", "fba_shipped_sales", "fba_shipment_promotions", + "fba_customer_returns", "replacements", "mfn_returns", "subscribe_save_performance", + "account_health")}, + **{_AMZN + t: "Fees and payouts" for t in ( + "settlements", "economics", "transactions", "fba_fee_estimates", "referral_fees", + "storage_fees", "long_term_storage_fees", "storage_overage_fees", "reimbursements", + "fee_discounts", "fee_estimates_api")}, + **{_AMZN + t: "Inventory" for t in ( + "inventory", "inventory_ledger_detail", "inventory_planning", "manage_inventory", + "reserved_inventory", "restock_recommendations", "stranded_inventory", + "inbound_noncompliance", "inventory_expiration_dates", "recommended_removals", + "removal_orders", "removal_shipments", "fba_inventory", "inbound_shipments", + "outbound_fulfillment", "replenishment", "remote_fulfillment_eligibility", + "out_of_stock_mfn", "subscribe_save_forecast")}, + **{_AMZN + t: "Listings" for t in ( + "listings", "pricing", "inactive_listings", "cancelled_listings", "suppressed_listings", + "listing_defects", "sold_listings", "listing_items", "catalog_items", "b2b_recommended", + "b2b_not_yet_on_amazon", "geo_opportunities", "b2b_quotes")}, + **{_AMZN + t: "Advertising" for t in ( + "brand_analytics", "search_query_performance", "search_catalog_performance", + "market_basket", "repeat_purchase", "promotion_performance", "coupon_performance", + "sku_keywords", "keyword_ranks")}, +} + + +def table_group(key): + """The rail group of one Amazon table, or None off Amazon. Never raises.""" + try: + return AMAZON_GROUPS.get(str(key or "")) + except Exception: # noqa: BLE001 + return None + + register("amazon", AMAZON_TABLES, mirror=ConnectorMirrorSpec(tuple(s.source for s in AMAZON_TABLES if not s.native.startswith("derived:")) @@ -896,9 +3484,11 @@ register("amazon", AMAZON_TABLES, register("meta", META_TABLES, mirror=ConnectorMirrorSpec(tuple(s.source for s in META_TABLES), state_entities=("meta",))) -__all__ = ["CARDINALITIES", "ConnectorTableSpec", "SPECS", "CONNECTOR_SPEC_KEYS", "AMAZON_TABLES", "AMAZON_FIELDS", "META_TABLES", +__all__ = ["CARDINALITIES", "ConnectorTableSpec", "SPECS", "CONNECTOR_SPEC_KEYS", "AMAZON_TABLES", "AMAZON_FIELDS", + "AMAZON_LABELS", "AMAZON_DEFAULT_COLUMNS", "META_TABLES", "ConnectorMirrorSpec", "MIRRORS", "mirror_for", "ODOO_MODEL_TABLE_PREFIX", "odoo_model_table_key", "odoo_model_label", "register", "connectors", "tables_for", "all_specs", "spec_for", "connector_of", "register_scoped_row_decoder", "decode_scoped_rows", "edges_for", "system_link_fields"] +__all__ += ["AMAZON_GROUP_ORDER", "AMAZON_GROUPS", "table_group"] diff --git a/platform/core/external_tools.py b/platform/core/external_tools.py index 5fffd1244b7b94613937d6a291bb8c8014bcc300..82148e48076c9a0f79f829269a4e01297e72600c 100644 --- a/platform/core/external_tools.py +++ b/platform/core/external_tools.py @@ -248,7 +248,7 @@ _APPLICATION = { "type": {"enum": ["chart", "route", "map", "code"]}, "compatibleDatabases": {"type": "array", "items": _STRING}, "sourceDatabase": {"type": "string", "description": "Source for a template Application only."}, - "templateKind": {"enum": ["route", "catalog"]}, + "templateKind": {"enum": ["route", "catalog", "competitor_watch"]}, "fieldMap": {"type": "object", "additionalProperties": _STRING}, "navPlacement": _object({"section": {"const": "app"}, "order": {"type": "integer", "minimum": 0, "maximum": 999}}, diff --git a/platform/core/registry.py b/platform/core/registry.py index 143be5f5f5fd294d1275d9eec1783d11bc61b51b..fdc7cd11233d50a4d86b157d4e54f2a50835a0f4 100644 --- a/platform/core/registry.py +++ b/platform/core/registry.py @@ -225,6 +225,11 @@ REGISTRY = [ {'key': 'customer_card', 'label': 'Customer Card', 'brand': True, 'hq': False, 'validate': True, 'validate_only': True, 'nav': False, 'source': 'Odoo', 'note': 'Template App customer receivables independently tied to res.partner.credit.'}, + {'key': 'amazon_profit', 'label': 'Amazon SKU profit', 'brand': False, 'hq': True, + 'validate': True, 'validate_only': True, 'nav': False, 'source': 'Amazon', + 'note': 'Per SKU Amazon net profit, margin and band in the module table ' + 'ut_amazon_sku_profit (Odoo cost times units). The row keeps validate.py tying ' + 'monthly net proceeds to settlements within 2%; the table is the surface.'}, {'key': 'intelligence', 'label': 'Brain', 'brand': False, 'hq': True, 'validate': False, 'nav': False, 'note': 'Workspace Markdown files: playbooks, agent prompts, company facts, dictionary and ' diff --git a/platform/core/route_system.py b/platform/core/route_system.py index a0ee822cd57c30e0f2eb70131282c6edf928e90a..e19ff7ef9a577feffc983457e32ddfd77793dd12 100644 --- a/platform/core/route_system.py +++ b/platform/core/route_system.py @@ -553,64 +553,6 @@ def migrate_application_routes(st, applications, *, legacy_navigation=None): return _persist(st, mutate) -def adopt_unscoped_routes(st, *, owner, application): - """Move one owner's no-App Routes made after the R20 receipt into that owner's Routing App. - - A moved Route keeps its id, stops, number, createdAt and createdBy. Returns the moved ids. - """ - owner = str(owner or "").strip().lower() - application = _application(application) - - def when(value): - try: - stamp = _dt.datetime.fromisoformat(str(value or "")) - except (TypeError, ValueError): - return None - return stamp if stamp.tzinfo else stamp.replace(tzinfo=_dt.timezone.utc) - - def signature(route): - return (str(route.get("createdBy") or "").strip().lower(), _name(route.get("name")), - tuple(_pid(stop.get("customerPid")) for stop in ordered_stops(route))) - - def plan(state): - receipt = state.get(APPLICATION_MIGRATION) - cutoff = when(receipt.get("at")) if isinstance(receipt, dict) else None - if not owner or application is None or cutoff is None: - return [] - taken = {signature(item) for item in state.get("routes") or [] if _in_application(item, application)} - moving = [] - for index, item in enumerate(state.get("routes") or []): - if not _in_application(item, None) or str(item.get("createdBy") or "").strip().lower() != owner: - continue - made = when(item.get("createdAt")) - # A Route made before the receipt already has its R20 copy, and an identical one is a duplicate. - if made is None or made <= cutoff or signature(item) in taken: - continue - taken.add(signature(item)) - moving.append(index) - return moving - - # The read-only plan keeps the common request from writing the Route document. - if not plan(_store(st)): - return [] - - def mutate(state): - zones = (((state.get(APPLICATION_MIGRATION) or {}).get("applications") or {}) - .get(application) or {}).get("zones") or {} - moved = [] - for index in plan(state): - route = dict(state["routes"][index]) - route.update(application=application, shareKey=route_share_key(route.get("id")), - zoneId=zones.get(f"{DEFAULT_ZONE_TABLE}:{route.get('zoneId')}")) - state["routes"][index] = route - moved.append(str(route.get("id"))) - if moved: - _reconcile_inbox_state(state, _inbox_day(None, st=st)) - return state, moved - - return _persist(st, mutate) or [] - - def route_share_key(route_id): """Stable generic-field bridge id for the existing share registry. diff --git a/platform/core/view_templates.py b/platform/core/view_templates.py index 51c15132a96e73ea5fa16764254c7b0e1e7d20ee..d816f14f54068b77f9047f793a5d74e4512c780f 100644 --- a/platform/core/view_templates.py +++ b/platform/core/view_templates.py @@ -36,7 +36,7 @@ their shape without a store. #: The topic a template is FOR. A coarse grouping label used to filter the picker's list; the #: real gate is `missing_columns` below (a table whose contract has the columns can take the #: template, whatever we called its source). -SOURCES = ('odoo_customer', 'odoo_product', 'instagram', 'any') +SOURCES = ('odoo_customer', 'odoo_product', 'instagram', 'amazon', 'any') #: The workspace bucket a topic's views live in. `_table_workspace` for user tables #: (`core/user_tables.py:211` builds the same string when it deletes them), and the two built-ins @@ -236,6 +236,44 @@ TEMPLATES = [ ), ], }, + { + # SDLC 78 (R7, R12). `modules/amazon_profit.py` writes these onto its own table when it + # creates or adopts it and grants them to everyone who can open it, so nobody applies them. + 'key': 'amazon_sku_profit', + 'label': 'Amazon money losers', + 'desc': 'Amazon SKUs that lose money, run thin, or have no cost to measure.', + 'source': 'amazon', + 'views': [ + _view( + 'tpl_amazon_sku_profit_losing', 'Losing', + 'SKUs whose net profit is below 0 in this period, worst first.', + ['sku', 'title', 'period', 'net_profit', 'margin', 'net_sales', 'net_proceeds', + 'cogs_total', 'units_sold', 'owner', 'status', 'due', 'status_set'], + sorts=[{'colId': 'net_profit', 'dir': 'asc'}], + filters=[{'colId': 'net_profit', 'op': 'lt', 'value': '0'}], + ), + _view( + 'tpl_amazon_sku_profit_thin', 'Thin', + 'SKUs that make money below the thin margin threshold, thinnest first.', + ['sku', 'title', 'period', 'margin', 'net_profit', 'net_sales', 'net_proceeds', + 'cogs_total', 'units_sold', 'owner', 'status', 'due', 'status_set'], + sorts=[{'colId': 'margin', 'dir': 'asc'}], + filters=[{'colId': 'band', 'op': 'eq', 'value': 'Thin'}], + ), + _view( + 'tpl_amazon_sku_profit_cogs_missing', 'COGS missing', + 'SKUs with no Odoo product linked or no Odoo cost, so profit stays blank. ' + 'Confirm or reject the suggested product to measure them.', + ['sku', 'title', 'suggested_product', 'suggested_units', 'match_rule', + 'match_state', 'odoo_product', 'units_per_sku', 'cogs_per_unit', 'net_sales', + 'net_proceeds', 'units_sold', 'asin', 'period'], + sorts=[{'colId': 'net_sales', 'dir': 'desc'}], + filters=[{'colId': 'odoo_product', 'op': 'isEmpty'}, + {'colId': 'cogs_per_unit', 'op': 'isEmpty'}], + conj='or', + ), + ], + }, ] diff --git a/platform/harness/amazon_client.py b/platform/harness/amazon_client.py index f51a10cc2d22aa110fa5252a7d7f1bc6021f9d4a..572ec2c6ef56b2c03f5e6f0c180cce111df0ca3d 100644 --- a/platform/harness/amazon_client.py +++ b/platform/harness/amazon_client.py @@ -364,8 +364,10 @@ REPORT_TYPES = { # ⚠ Returns data only for Brand Registry brand owners (owner ruling R5). Carried as the # wave's one UNPROVEN ticket with its exit condition written in advance: returns >0 rows # for any registered brand, else demote to a declared-but-unsynced stub. + # R18 (SDLC 78): one completed Sunday to Saturday WEEK per window. A first load takes the + # latest week only; older weeks keep watched terms only (amazon_store.write_week). "report": "GET_BRAND_ANALYTICS_SEARCH_TERMS_REPORT", - "monthly": True, "period": "MONTH", + "weekly": True, "period": "WEEK", "json_root": "dataByDepartmentAndSearchTerm", "validates_against": "report period totals vs our search-term rows", "role": "Brand Analytics", @@ -2883,3 +2885,53 @@ def marketplace_participations(fields, timeout=_TIMEOUT): got = _request("sellers/v1/marketplaceParticipations", token, region=region, timeout=timeout, sandbox=is_sandbox(fields)) return got.get("payload") or [] + + +def _sales_rank(item, marketplace): + """`{"rank", "group"}` from one catalog item: the display group rank, else the best classification rank.""" + for entry in (item or {}).get("salesRanks") or (): + if not isinstance(entry, dict) or entry.get("marketplaceId") not in (None, marketplace): + continue + display = [r for r in entry.get("displayGroupRanks") or () if isinstance((r or {}).get("rank"), int)] + if display: + return {"rank": display[0]["rank"], "group": display[0].get("title")} + ranks = [r for r in entry.get("classificationRanks") or () if isinstance((r or {}).get("rank"), int)] + if ranks: + best = min(ranks, key=lambda r: r["rank"]) + return {"rank": best["rank"], "group": best.get("title")} + return {"rank": None, "group": None} + + +def catalog_sales_ranks(fields, asins, timeout=_TIMEOUT, *, sleep=time.sleep, now=time.monotonic): + """Read sales ranks for any ASINs through `searchCatalogItems`, 20 per call. -> {asin: {"rank", "group"}}. + + An ASIN Amazon does not return is absent from the result; the caller names it. + """ + unique = sorted({str(a or "").strip() for a in asins or () if str(a or "").strip()}) + if not unique: + return {} + if any(not re.fullmatch(r"[A-Z0-9]{10}", a) for a in unique): + raise AmazonError("A sales rank read needs 10 character ASINs") + f = fields or {} + marketplace = str(f.get("marketplace_id") or US_MARKETPLACE_ID) + region = str(f.get("region") or DEFAULT_REGION) + path = OPERATIONS["searchCatalogItems"]["path"] + token, issued = access_token(f, timeout=timeout), now() + out = {} + for offset in range(0, len(unique), 20): + _pace_entity("searchCatalogItems", ENTITY_INTERVALS["searchCatalogItems"], sleep=sleep, now=now) + if now() - issued >= 3000: + token, issued = access_token(f, timeout=timeout), now() + batch = unique[offset:offset + 20] + got = _request(path, token, region=region, sandbox=is_sandbox(f), timeout=timeout, + marketplaceIds=marketplace, identifiersType="ASIN", identifiers=",".join(batch), + includedData="summaries,salesRanks", pageSize=20, + throttle_fix="read sales ranks again after the next weekly pull") + items = got.get("items") if isinstance(got, dict) else None + if not isinstance(items, list): + raise AmazonError("searchCatalogItems returned no item list; retry after Amazon restores its response") + for item in items: + asin = str((item or {}).get("asin") or "") + if asin in batch: + out[asin] = _sales_rank(item, marketplace) + return out diff --git a/platform/harness/amazon_schema.py b/platform/harness/amazon_schema.py index 8d32d4da92c18387ed88a66e5dcd73ff48361c07..1fe5898c1028f2970d6d0bf8a3ba2e22eb499d4a 100644 --- a/platform/harness/amazon_schema.py +++ b/platform/harness/amazon_schema.py @@ -415,8 +415,10 @@ TABLE_SPECS = { ("child_asin", "listings", "asin1", "many_to_one"), ("sku", "listings", "seller_sku", "many_to_one"), )), + # D-2675: the report period is part of the identity, so each weekly pull of a term keeps its own rows. "brand_analytics": _table_spec("brand_analytics", ( "department_name", "search_term", "clicked_asin", "click_share_rank", + "_period_start", "_period_end", )), "economics": _table_spec("economics", ("start_date", "msku", "marketplace_id"), ( ("msku", "listings", "seller_sku", "many_to_one"), diff --git a/platform/harness/amazon_store.py b/platform/harness/amazon_store.py index f3e9c8411e879f3cb24fb2e44ac1444c293e761a..285b029a26b9075da8eb7918d034ac9713bc420a 100644 --- a/platform/harness/amazon_store.py +++ b/platform/harness/amazon_store.py @@ -88,6 +88,20 @@ LEGACY_SNAPSHOT_TABLES = ("listings", "inventory") #: Head -> lines: both tables read the same document; a second report buys no new rows. SHARED_REPORT_SIBLINGS = {"orders": "order_items"} +#: Contract C2: called as `fn(tenant, synced_tables)` after a pass publishes. Each import is lazy and a +#: failure is one log line, never a failed sync. +AFTER_SYNC = ("modules.amazon_profit:on_amazon_sync", "modules.competitor_watch:on_amazon_sync") + +#: R18: `fn(tenant) -> iterable of str`. Older brand analytics weeks keep these search terms only. +#: A missing module is an empty set; a reader that raises prunes nothing that pass. +WATCHED_TERMS = "modules.competitor_watch:watched_terms" + +#: Standing rule 1 copy for the brand analytics limit (o7-brand-analytics-week.md). It reaches a screen. +BRAND_ANALYTICS_LIMIT = ( + "Brand analytics keeps the latest full week of search terms. Older weeks keep only your watched " + "search terms, because one week is about 9.6 million rows and 3.5 GB. " + "To keep a term's history, add it to the competitor watch.") + # C1 is a tenant-store document, not an API dependency. verify_amazon gates reader parity. ENABLEMENT_KEY = "amazon_connector_enablement" MIRROR_PREFIX = "amazon_mirror/" @@ -1260,8 +1274,11 @@ def replace_keys_for(table): if c not in _ORIGIN_TYPES) -def replace_window(con, table, cols, types, rows, keys): - """Replace incoming key groups atomically, or report why only insertion is possible.""" +def replace_window(con, table, cols, types, rows, keys, after=None): + """Replace incoming key groups atomically, or report why only insertion is possible. + + `after(con)` runs inside the same transaction, after the insert and before the commit. + """ missing = [c for c in keys if c not in cols] if missing: note = (f"{table}: window replacement unavailable; missing identity columns: " @@ -1284,6 +1301,8 @@ def replace_window(con, table, cols, types, rows, keys): count = _replace(con, temporary, cols, types, rows, source_table=table) con.execute(f'DELETE FROM {target} WHERE EXISTS (SELECT 1 FROM {temporary} AS "k" WHERE {predicate})') con.execute(f"INSERT INTO {target} ({columns}) SELECT {columns} FROM {temporary}") + if after is not None: + after(con) con.execute("COMMIT") except Exception: con.execute("ROLLBACK") @@ -1315,6 +1334,94 @@ def _month_windows(start, end): return out +def _week_start(value): + """The Sunday 00:00 UTC on or before `value`: Amazon's WEEK report runs Sunday to Saturday.""" + day = _utc(value).replace(hour=0, minute=0, second=0, microsecond=0) + return day - timedelta(days=(day.weekday() + 1) % 7) + + +def _week_windows(start, end): + """Completed Sunday to Saturday weeks only, as half-open UTC windows.""" + current, stop, out = _week_start(start), _week_start(end), [] + while current < stop: + out.append((current, current + timedelta(days=7))) + current += timedelta(days=7) + return out + + +def _periodic(spec): + """A report that lands one completed calendar period, a month or a week, per window.""" + return bool((spec or {}).get("monthly") or (spec or {}).get("weekly")) + + +def _period_windows(spec, start, end): + return _week_windows(start, end) if spec.get("weekly") else _month_windows(start, end) + + +def stamp_week(cols, rows, w0, w1): + """Stamp a window's report period on every row, for a table whose identity names the period.""" + period = {"_period_start": _iso(w0), "_period_end": _iso(w1)} + return [*cols, *(c for c in period if c not in cols)], _stamp_rows(rows, period) + + +def watched_terms(tenant_key): + """R18's watched set -> `(terms, None)`, or `(None, problem)` when the reader fails.""" + module_name, _, name = WATCHED_TERMS.partition(":") + try: + import importlib + module = importlib.import_module(module_name) + except ModuleNotFoundError as exc: + if exc.name == module_name or module_name.startswith(f"{exc.name}."): + return set(), None + return None, _watched_problem(exc) + except Exception as exc: # noqa: BLE001 + return None, _watched_problem(exc) + try: + return {str(term).strip().lower() for term in getattr(module, name)(tenant_key) or () + if str(term or "").strip()}, None + except Exception as exc: # noqa: BLE001 + return None, _watched_problem(exc) + + +def _watched_problem(exc): + return (f"brand_analytics: watched search terms unreadable ({type(exc).__name__}); older weeks " + "are kept in full this pass and pruned when the list reads again.") + + +def _retain_weekly(con, mirror, watched): + """R18: the newest week stays in full; every older period keeps watched search terms only.""" + quote = lambda name: '"' + str(name).replace('"', '""') + '"' + terms = quote("amazon_watched_terms_" + uuid.uuid4().hex) + target = quote(mirror) + con.execute(f"CREATE TEMP TABLE {terms} (term VARCHAR)") + try: + if watched: + con.executemany(f"INSERT INTO {terms} VALUES (?)", [[term] for term in sorted(watched)]) + row = con.execute( + f'DELETE FROM {target} WHERE "_period_start" IS DISTINCT FROM ' + f'(SELECT MAX("_period_start") FROM {target}) ' + f"AND (search_term IS NULL OR lower(trim(search_term)) NOT IN (SELECT term FROM {terms}))").fetchone() + return int(row[0]) if row else 0 + finally: + con.execute(f"DROP TABLE IF EXISTS {terms}") + + +def write_week(con, table, cols, types, rows, tenant_key, report, log=print): + """Land one week by its period identity and apply R18's retention in the same transaction.""" + watched, problem = watched_terms(tenant_key) + if problem: + report.setdefault("problems", []).append(problem) + log(problem) + mirror = table_name(table) + after = None if watched is None else (lambda c: _retain_weekly(c, mirror, watched)) + return replace_window(con, mirror, cols, types, rows, replace_keys_for(table), after=after) + + +def _record_brand_analytics_limit(con): + _record_state(con, entity_key("limit_brand_analytics"), "done", + detail=json.dumps({"reason": BRAND_ANALYTICS_LIMIT})) + + def sqp_batches(asins, max_chars=200): """Stable, lossless space-joined options within Amazon's character ceiling.""" if type(max_chars) is not int or max_chars < 1: @@ -1926,6 +2033,9 @@ def first_load_plan(tables, start, end, *, recent_days=None, size=MAX_WINDOW_DAY continue if spec.get("snapshot"): recent.append((table, None, None)) + elif spec.get("weekly"): + # R18: a first load takes the latest completed week only; no weekly history is owed. + recent.extend((table, w0, w1) for w0, w1 in _week_windows(start, end)[-1:]) elif spec.get("monthly"): months = list(reversed(_month_windows(start, end))) if not months: @@ -2753,6 +2863,11 @@ class _AmazonMirrorStore(_mirrors.MirrorStore): def part_max_bytes(self): return MIRROR_PART_MAX_BYTES + def _install_snapshot(self, con, manifest, rows, paths): + result = super()._install_snapshot(con, manifest, rows, paths) + _note_published() # C2: AFTER_SYNC runs only for a pass that published. + return result + def _valid_table(self, name): # Preserve older Amazon tables even when they are no longer advertised. return isinstance(name, str) and bool(re.fullmatch(r"amzn_[a-z0-9_]+", name)) @@ -3405,8 +3520,8 @@ def record_backfill_floor(con, table, when): def _undocumented_month(con, table): - """Repair a legacy completion that never parsed a monthly document.""" - if table == SQP_TABLE or not (_client().REPORT_TYPES.get(table) or {}).get("monthly"): + """Repair a legacy completion that never parsed a monthly or weekly document.""" + if table == SQP_TABLE or not _periodic(_client().REPORT_TYPES.get(table)): return False if not read_watermark(con, table) or _read_state(con, entity_key("reconcile_" + table)): return False @@ -3656,8 +3771,8 @@ def needs_first_load(tenant_key, rt=None, *, completed_tables=None, repair_table return (refusals.get(table, {}).get("state") in ("not_offered", "failed") or read_watermark(con, table) is not None) spec = _client().REPORT_TYPES.get(table) or {} - proven = not (spec.get("monthly") or spec.get("snapshot")) or table in documented_tables - if not proven and spec.get("monthly") and table != SQP_TABLE and read_watermark(con, table): + proven = not (_periodic(spec) or spec.get("snapshot")) or table in documented_tables + if not proven and _periodic(spec) and table != SQP_TABLE and read_watermark(con, table): mirror = table_name(table) if not _columns_of(con, mirror) or con.execute(f"SELECT COUNT(*) FROM {mirror}").fetchone()[0] == 0: repairs.add(table) @@ -3756,6 +3871,14 @@ def sync_window(table, mark, *, now, since=None, backfill_days=None): start = start.replace(day=1, hour=0, minute=0, second=0, microsecond=0) end = end.replace(day=1, hour=0, minute=0, second=0, microsecond=0) cause += "; completed calendar months only" + if spec.get("weekly"): + end = _week_start(end) + if incremental or since is not None: + start = _week_start(start) + cause += "; completed Sunday to Saturday weeks only" + else: + start = end - timedelta(days=7) + cause = "first load: the latest completed Sunday to Saturday week only (R18)" if table == "settlements": # The watermark filters issued reports by creation time; it never requests one. rtype = spec.get("report_discover") @@ -4673,16 +4796,46 @@ def sync(tenant_key="royal-imports", tables=None, since=None, log=print, rt=None expected = [name for name in (TABLES if tables is None else tables) if name in TABLES] if scheduled and not first_load and expected and "settlements" not in expected: expected.append("settlements") + _PUBLISHED.flag = False with _mirrors.sync_mirror("amazon", tenant_key, rt, report=report, log=log, expected_tables=expected): report.update(_sync_candidate(tenant_key, **options)) _refuse_unreconciled(tenant_key, rt, report, log) + if getattr(_PUBLISHED, "flag", False): + _after_sync(tenant_key, report, log) return report +#: Set when this thread's sync pass installs a published snapshot. +_PUBLISHED = threading.local() + + +def _note_published(): + _PUBLISHED.flag = True + + +def _synced_tables(report): + """The tables this pass landed: state `done` with a row count.""" + return [table for table, row in (report.get("tables") or {}).items() + if isinstance(row, dict) and row.get("state", "done") == "done" and "rows" in row] + + +def _after_sync(tenant_key, report, log): + """Contract C2: each callback is imported lazily; any failure is one log line and changes nothing.""" + synced = tuple(_synced_tables(report)) + if not synced: + return + for target in AFTER_SYNC: + try: + import importlib + module_name, _, name = target.partition(":") + getattr(importlib.import_module(module_name), name)(tenant_key, synced) + except Exception as exc: # noqa: BLE001 + log(f"Amazon after-sync callback {target} failed ({type(exc).__name__}); the sync result is unchanged.") + + def _refuse_unreconciled(tenant_key, rt, report, log): """Standing rule 8: a table synced in this pass whose reconciliation refuses keeps its previous rows.""" - synced = [table for table, row in (report.get("tables") or {}).items() - if isinstance(row, dict) and row.get("state", "done") == "done" and "rows" in row] + synced = _synced_tables(report) if not synced or report.get("state") in {"partial", "failed", "done"}: return try: @@ -4864,8 +5017,11 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri "resync of each snapshot table replaces its earlier copies.") def write_window(table, cols, types, rows): - count, note = replace_window(con, table_name(table), cols, types, rows, - replace_keys_for(table)) + if cx.REPORT_TYPES[table].get("weekly"): + count, note = write_week(con, table, cols, types, rows, tenant_key, report, log) + else: + count, note = replace_window(con, table_name(table), cols, types, rows, + replace_keys_for(table)) if note and table not in noted: noted.add(table) report["problems"].append(note) @@ -5165,9 +5321,18 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri record_backfill_floor(con, table, w0) floors[table] = w0 elif table != "settlements": + period_spec = cx.REPORT_TYPES[table] windows = (([(task_start, task_end)] if task_end is not None else []) if first_load - else _month_windows(plan["start"], plan["end"]) if cx.REPORT_TYPES[table].get("monthly") + else _period_windows(period_spec, plan["start"], plan["end"]) if _periodic(period_spec) else _windows(plan["start"], plan["end"])) + if period_spec.get("weekly"): + _record_brand_analytics_limit(con) + if not first_load and len(windows) > 1: + # One week per pass, oldest first. The watermark stops at its end, so no week is asked twice. + log(f"{table}: {len(windows) - 1} more completed weeks follow on later passes") + windows = windows[:1] + plan["end"] = windows[0][1] + weekly = bool(table != "settlements" and cx.REPORT_TYPES[table].get("weekly")) for w0, w1 in (() if table == "settlements" else windows): # W59-T43 / C2 has no amazon.access kind. Record each credential-backed # vendor attempt as a sync ACCESS phase, distinct from the completion event. @@ -5176,7 +5341,7 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri probe = _DocumentProbe(table, rtype) with _report_resume( con, table, rtype, start=_iso(w0), - end=_iso(w1 - timedelta(seconds=1)) if cx.REPORT_TYPES[table].get("monthly") else _iso(w1), max_wait=max_wait, + end=_iso(w1 - timedelta(seconds=1)) if _periodic(cx.REPORT_TYPES[table]) else _iso(w1), max_wait=max_wait, log=lambda message: log(f"{table}: {message}"), **({"options": {**REPORT_OPTIONS.get(table, {}), "reportPeriod": cx.REPORT_TYPES[table]["period"]}} if cx.REPORT_TYPES[table].get("period") else @@ -5219,11 +5384,14 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri cols, rows = parse_flat_file( text, document_id=meta.get("reportDocumentId"), report_type=rtype) declared = {} + if cols and (weekly or "_period_start" in replace_keys_for(table)): + cols, rows = stamp_week(cols, rows, w0, w1) if not cols: if first_load: - # Even an empty recent window clears old rows exactly once. + # Even an empty recent window clears old rows exactly once. A weekly + # table never clears: R18's retention keeps watched history. for empty_table in (table, sibling): - if empty_table and floors[empty_table] is None: + if empty_table and floors[empty_table] is None and not weekly: if con.execute("SELECT 1 FROM information_schema.tables WHERE table_name=?", [table_name(empty_table)]).fetchone(): con.execute(f"DELETE FROM {table_name(empty_table)} WHERE 1=1") @@ -5252,12 +5420,13 @@ def _sync_candidate(tenant_key="royal-imports", tables=None, since=None, log=pri types = _ensure(con, mirror, cols, rows, declared=declared, preserve_rows=True) if cols else {} # Explicit windows and first loads replace the mirror; incremental passes keep history. - if not cleared and not plan["incremental"]: + # A weekly table replaces by its period identity instead (D-2675). + if not cleared and not plan["incremental"] and not weekly: con.execute(f"DELETE FROM {mirror} WHERE 1=1") cleared = True if cols: written += write_window(table, cols, types, rows) - if cx.REPORT_TYPES[table].get("monthly"): + if _periodic(cx.REPORT_TYPES[table]): _record_reconciliation(con, table, {"start": _iso(w0), "end": _iso(w1), "count": len(rows)}) probe.finish(len(rows) if cols else 0) @@ -8039,6 +8208,7 @@ def _bounded_document_selftest(rows=24_000): try: cols, parsed = parse_json_report(document, "dataByDepartmentAndSearchTerm", document_id="selftest-document", report_type="selftest") + cols, parsed = stamp_week(cols, parsed, _utc("2026-10-11T00:00:00Z"), _utc("2026-10-18T00:00:00Z")) cols = merge_columns([c for c, _t in amazon_schema.columns_for("brand_analytics")], cols) types = _ensure(con, table, cols, parsed, declared=dict(amazon_schema.columns_for("brand_analytics"))) first, _note = replace_window(con, table, cols, types, parsed, replace_keys_for("brand_analytics")) diff --git a/platform/harness/semantic.py b/platform/harness/semantic.py index fc2b5074905250fde8c900be1d5e8034a913bbf9..c3e29bf24d9db72aaf6b00116732511a935a56da 100644 --- a/platform/harness/semantic.py +++ b/platform/harness/semantic.py @@ -1120,6 +1120,8 @@ _VALIDATORS.update({key: _v_customer_rollup(key) for key in ( 'customer_oldest_due', 'customer_invoiced_all_time', 'customer_days_since_invoice')}) from modules.finance_spine import FINANCE_VALIDATORS _VALIDATORS.update(FINANCE_VALIDATORS) +from modules.amazon_profit import PROFIT_VALIDATORS +_VALIDATORS.update(PROFIT_VALIDATORS) _VALIDATORS.update({'standard_' + key: _v_standard_metric(key) for key in ('booked', 'shipped', 'collected', 'margin')}) _VALIDATORS['invoiced_sales'] = _v_standard_metric('invoiced_sales') diff --git a/platform/model/metrics/amazon_profit.yml b/platform/model/metrics/amazon_profit.yml new file mode 100644 index 0000000000000000000000000000000000000000..0e6049d1cc9ed9d6a881f4e48be28feac2cfd11e --- /dev/null +++ b/platform/model/metrics/amazon_profit.yml @@ -0,0 +1,56 @@ +# Amazon SKU profit. The module (platform/modules/amazon_profit.py) owns the per SKU table +# ut_amazon_sku_profit and registers the amazon_sku_net_profit validator when it loads. +# harness/semantic.py imports that validator, so the topic declares its measure at model load. +topics: + - key: amazon_sku_profit + label: "Amazon SKU profit" + entity: amazon.sku_profit + grain: "one row per Amazon seller SKU for the chosen period" + semantic: + approved: [] + date_field: "" + date_col_note: "amzn_economics.start_date, one economics row per SKU per day" + scope: "Every Amazon seller SKU in the economics mirror for one period: the last 30 days, the last 90 days or a calendar month. Net proceeds are net sales minus Amazon fees minus sponsored ads. COGS is the linked Odoo product cost times units per SKU times units sold. A SKU without an Odoo link has a blank net profit, never zero." + measure: {key: amazon_sku_net_profit, validate: amazon_sku_net_profit} + fields: + - key: sku + label: "SKU" + type: text + kind: data + - key: net_proceeds + label: "Net proceeds" + type: currency + kind: data + - key: net_profit + label: "Net profit" + type: currency + kind: data + - key: margin + label: "Margin" + type: pct + kind: data + - key: band + label: "Band" + type: text + kind: data +topic: amazon_sku_profit +metrics: + - key: amazon_sku_net_profit + label: Amazon SKU net profit + agg: sum + field: net_profit + format: usd + empty: blank + description: Net proceeds minus Odoo COGS for each Amazon SKU over the chosen period. + ai_context: > + Amazon SKU net profit is net proceeds minus COGS for one seller SKU over the chosen period. + Net proceeds are net sales minus Amazon fees minus sponsored ads, from Amazon economics. + COGS is the linked Odoo product cost times units per SKU times net units sold. + A SKU with no Odoo link has a blank net profit and belongs in the COGS missing view; never treat it as zero. + Losing means net profit below zero. Thin means margin below the thin threshold, 10% of net sales by default. + Read the figures from the Amazon SKU profit database. + validate: + method: amazon_sku_net_profit + independent: > + Amazon's reported netProceeds total for the same window; the module validate() also ties + each month's net proceeds to settlements within 2%, excluding loan repayment and withheld tax. diff --git a/platform/modules/amazon_profit.py b/platform/modules/amazon_profit.py new file mode 100644 index 0000000000000000000000000000000000000000..a5a9264656b5d7ffc94307ec432f8752addb9340 --- /dev/null +++ b/platform/modules/amazon_profit.py @@ -0,0 +1,1424 @@ +"""Amazon SKU profit: one row per Amazon SKU with Odoo cost, net profit, margin and band. + +Net proceeds = net sales minus fees minus sponsored ads, from the `amzn_economics` mirror. +COGS = Odoo `standard_price` of the linked product times units per SKU times units sold. +The module owns its table (`ut_amazon_sku_profit`). It is not a connector table, so people +edit the owned columns (Odoo product, units per SKU, owner, status, due date). A refresh +rewrites only the computed columns, keyed by SKU, so owned cells belong to the SKU and +survive every period switch. + +`validate()` ties a month's net proceeds to Amazon settlements within 2%, excluding loan +repayments and withheld tax, and proves a negative control that drops fees goes red. +A refresh runs it for the last complete month and writes no figures when it fails. + +Owned items (R10, R31): a refresh opens one item on a Losing or Thin SKU with no status, for the +default owner, due in 14 days. It reopens a failed fix 30 days after the status was set. The +`status_set` column records the status and the date a refresh first saw it, because the +activity log is delivered async and pruned, so a refresh cannot read a status date from it. +""" +from __future__ import annotations + +import atexit +import datetime as dt +import hashlib +import json +import math +import re +import shutil +import tempfile +import threading +from pathlib import Path + +TABLE_KEY = "ut_amazon_sku_profit" +TABLE_LABEL = "Amazon SKU profit" +SETTINGS_KEY = "amazon_profit_settings" +VIA = "amazon_profit" +WRITER = "automation" +TENANT_ZERO = "royal-imports" + +PERIOD_DEFAULT = "90d" +PERIOD_DAYS = {"30d": 30, "90d": 90} +THIN_DEFAULT = 0.10 +STATUSES = ("Open", "Repriced", "Ads cut", "Delisted", "Keep", "COGS wrong") +#: A fix that reopens when the SKU still loses money or runs thin 30 days on. Keep never reopens. +FIX_STATUSES = ("Repriced", "Ads cut", "Delisted", "COGS wrong") +BANDS = ("Losing", "Thin") +NEEDS_REVIEW, CONFIRMED, REJECTED = "Needs review", "Confirmed", "Rejected" +MATCH_STATES = (NEEDS_REVIEW, CONFIRMED, REJECTED) +DUE_DAYS = 14 +REOPEN_DAYS = 30 +TEMPLATE_KEY = "amazon_sku_profit" +PICKER_MONTHS = 12 +SETTLEMENT_TOLERANCE = 0.02 + +ECONOMICS = "amzn_economics" +SETTLEMENTS = "amzn_settlements" +LISTINGS = "amzn_listings" +LOAN_REPAYMENT = "Automated Loan Repayment" +WITHHELD_TAX = "ItemWithheldTax" +_ECONOMICS_REQUIRED = ("msku", "start_date", "sales_net_product_sales_amount", + "sales_net_units_sold", "fees_total_amount") +_SETTLEMENT_REQUIRED = ("amount", "posted_date", "transaction_type", "amount_type") +_ADS_TOTAL = re.compile(r"ads_[a-z0-9_]+_amount") +_MONTH = re.compile(r"(\d{4})-(\d{2})") +_LOCK = threading.Lock() + + +class SourceUnavailable(RuntimeError): + """The Amazon mirror lacks a table or column this module reads.""" + + +class TableRefused(RuntimeError): + """The store refused the module table; the message names the cause and the fix.""" + + +def _field(key, label, ftype, description, *, computed, **extra): + out = {"key": key, "label": label, "type": ftype, "origin": "loopable", + "valueAuthority": "computed" if computed else "user-entered", + "scope": "tenant", "description": description} + out.update(extra) + return out + + +#: Computed columns: a refresh owns them and the pre-set lock keeps people out of them. +COMPUTED_FIELDS = ( + _field("sku", "SKU", "text", "Amazon seller SKU.", computed=True), + _field("asin", "ASIN", "text", "Amazon product id.", computed=True), + _field("title", "Title", "text", "Listing title.", computed=True), + _field("period", "Period", "text", "Dates these figures cover.", computed=True), + _field("units_sold", "Units sold", "int", "Net units sold in the period.", computed=True), + _field("net_sales", "Net sales", "currency", "Sales after refunds.", computed=True), + _field("fees", "Fees", "currency", "Amazon fees.", computed=True), + _field("sponsored_ads", "Sponsored ads", "currency", "Ad spend.", computed=True), + _field("net_proceeds", "Net proceeds", "currency", "Net sales minus fees and ads.", + computed=True), + _field("cogs_per_unit", "COGS per unit", "currency", "Odoo cost times units per SKU.", + computed=True), + _field("cogs_total", "COGS total", "currency", "COGS per unit times units sold.", + computed=True), + _field("net_profit", "Net profit", "currency", "Net proceeds minus COGS.", computed=True), + _field("margin", "Margin", "pct", "Net profit over net sales.", computed=True), + _field("band", "Band", "select", "Losing or thin.", computed=True, options=list(BANDS)), +) +#: Owned columns: people edit them and a refresh never writes them. +OWNED_FIELDS = ( + _field("odoo_product", "Odoo product", "text", "Odoo internal reference.", computed=False), + _field("units_per_sku", "Units per SKU", "int", "Odoo units in one Amazon unit.", + computed=False, format={"decimals": 4}), + _field("owner", "Owner", "user", "Who acts on this SKU.", computed=False), + _field("status", "Status", "select", "Where the action stands.", computed=False, + options=list(STATUSES)), + _field("due", "Due date", "date", "When the action is due.", computed=False), + _field("match_state", "Match state", "select", "Confirm or reject the suggested product.", + computed=False, options=list(MATCH_STATES)), +) +#: Tracking columns: a refresh writes them, people read them, and a period switch keeps them. +TRACK_FIELDS = ( + _field("status_set", "Status set", "text", "The status and the day a refresh first saw it.", + computed=True), + _field("suggested_product", "Suggested product", "text", "Odoo product the matcher suggests.", + computed=True), + _field("suggested_units", "Suggested units", "int", "Odoo units in one Amazon unit.", + computed=True, format={"decimals": 4}), + _field("match_rule", "Match rule", "text", "How the matcher found the product.", computed=True), + _field("match_rejected", "Rejected matches", "text", "Odoo codes a person rejected.", + computed=True), +) +COMPUTED_KEYS = tuple(f["key"] for f in COMPUTED_FIELDS) +OWNED_KEYS = tuple(f["key"] for f in OWNED_FIELDS) +TRACK_KEYS = tuple(f["key"] for f in TRACK_FIELDS) +ALL_KEYS = COMPUTED_KEYS + OWNED_KEYS + TRACK_KEYS +SUGGESTION_KEYS = ("suggested_product", "suggested_units", "match_rule") +#: Period figures a failed validate() blanks. COGS per unit is Odoo cost, not a period figure. +_UNVERIFIED_KEYS = ("units_sold", "net_sales", "fees", "sponsored_ads", "net_proceeds", + "cogs_total", "net_profit", "margin", "band") +_PERIOD_KEYS = tuple(k for k in COMPUTED_KEYS if k not in ("sku", "asin", "title")) + + +# ---------------------------------------------------------------- period and threshold + +def _today(): + return dt.datetime.now(dt.timezone.utc).date() + + +def period_bounds(period=None, today=None): + """`{key, start, end, label}` for 30d, 90d or YYYY-MM; ISO dates, end inclusive.""" + key = str(period or PERIOD_DEFAULT).strip().lower() + today = today or _today() + if key in PERIOD_DAYS: + end = today - dt.timedelta(days=1) + start = end - dt.timedelta(days=PERIOD_DAYS[key] - 1) + label = f"Last {PERIOD_DAYS[key]} days" + else: + match = _MONTH.fullmatch(key) + if not match or not 1 <= int(match.group(2)) <= 12: + raise ValueError("Choose 30 days, 90 days or a calendar month.") + start = dt.date(int(match.group(1)), int(match.group(2)), 1) + if start > today: + raise ValueError("Choose a month that has started.") + following = (start.replace(day=28) + dt.timedelta(days=4)).replace(day=1) + end = following - dt.timedelta(days=1) + label = start.strftime("%B %Y") + return {"key": key, "start": start.isoformat(), "end": end.isoformat(), + "label": f"{label} ({start.isoformat()} to {end.isoformat()})"} + + +def period_choices(today=None, months=PICKER_MONTHS): + """`[{key, label}]`: 30 days, 90 days, then the last `months` complete calendar months.""" + out = [{"key": key, "label": f"Last {days} days"} for key, days in PERIOD_DAYS.items()] + first = (today or _today()).replace(day=1) + for _ in range(months): + first = (first - dt.timedelta(days=1)).replace(day=1) + out.append({"key": first.strftime("%Y-%m"), "label": first.strftime("%B %Y")}) + return out + + +def clean_thin(value): + """The Thin threshold as a fraction of net sales, 0 to under 1.""" + if value is None or value == "": + return THIN_DEFAULT + try: + number = float(str(value).strip().rstrip("%")) + except (TypeError, ValueError): + raise ValueError("Enter the thin margin as a number.") from None + if str(value).strip().endswith("%"): + number /= 100.0 + if not math.isfinite(number) or not 0 <= number < 1: + raise ValueError("Enter the thin margin between 0% and 99%.") + return number + + +def last_complete_month(today=None): + first = (today or _today()).replace(day=1) + return (first - dt.timedelta(days=1)).strftime("%Y-%m") + + +def verification_month(period=None, today=None): + """The month a refresh ties to settlements: a picked month once it has ended, else the last one.""" + key = str(period or PERIOD_DEFAULT).strip().lower() + last = last_complete_month(today) + return key if _MONTH.fullmatch(key) and key <= last else last + + +# ---------------------------------------------------------------- pure arithmetic + +def _num(value): + try: + number = float(value) + except (TypeError, ValueError): + return 0.0 + return number if math.isfinite(number) else 0.0 + + +def band_for(net_profit, margin, thin=THIN_DEFAULT): + """Losing below 0 profit, Thin below the threshold margin, else no band.""" + if net_profit is None: + return None + if net_profit < 0: + return "Losing" + if margin is not None and margin < thin: + return "Thin" + return None + + +def compute_row(econ, cost=None, units_per_sku=1, thin=THIN_DEFAULT): + """One SKU's figures. `cost` None means unlinked: COGS, profit, margin and band stay blank.""" + net_sales = _num(econ.get("net_sales")) + fees = _num(econ.get("fees")) + ads = _num(econ.get("ads")) + units = int(_num(econ.get("units"))) + net_proceeds = net_sales - fees - ads + row = {"sku": econ.get("sku"), "asin": econ.get("asin") or None, + "title": econ.get("title") or None, "units_sold": units, "net_sales": net_sales, + "fees": fees, "sponsored_ads": ads, "net_proceeds": net_proceeds, + "cogs_per_unit": None, "cogs_total": None, "net_profit": None, "margin": None, + "band": None} + if cost is None: + return row + cogs_per_unit = _num(cost) * (units_per_sku or 1) + cogs_total = cogs_per_unit * units + net_profit = net_proceeds - cogs_total + margin = (net_profit / net_sales) if net_sales else None + row.update(cogs_per_unit=cogs_per_unit, cogs_total=cogs_total, net_profit=net_profit, + margin=margin, band=band_for(net_profit, margin, thin)) + return row + + +def cells(row, period_label=""): + """Computed cells as stored strings; None means the cell is blank.""" + out = {} + for key in COMPUTED_KEYS: + value = period_label if key == "period" else row.get(key) + if value is None or value == "": + out[key] = None + elif key == "margin": + out[key] = f"{value * 100:.2f}" + elif key == "units_sold": + out[key] = str(int(value)) + elif isinstance(value, float): + out[key] = f"{value:.2f}" + else: + out[key] = str(value) + return out + + +def odoo_code(value): + """The Odoo internal reference a cell names: `RI-1` or `[RI-1] Name`.""" + text = str(value or "").strip() + match = re.match(r"\[([^\]]+)\]", text) + return (match.group(1) if match else text).strip() + + +UNITS_DECIMALS = 4 + + +def units_per_sku(value): + """Odoo units in one Amazon unit, a positive number that may be a fraction (0.1 of a + 1000-piece box). Blank, unreadable or not above 0 reads 1.""" + try: + number = round(float(str(value).strip()), UNITS_DECIMALS) + except (TypeError, ValueError): + return 1.0 + return number if math.isfinite(number) and number > 0 else 1.0 + + +def units_text(value): + """The stored cell for a units value: at most 4 decimals, no trailing zeros.""" + return f"{units_per_sku(value):.{UNITS_DECIMALS}f}".rstrip("0").rstrip(".") + + +# ---------------------------------------------------------------- owned items (pure) + +_STATUS_SET = re.compile(r"(.+) since (\d{4}-\d{2}-\d{2})") + + +def status_set_cell(status, day): + return f"{status} since {day.isoformat()}" + + +def parse_status_set(value): + """`(status, date)` from a `status_set` cell, or `(None, None)`.""" + match = _STATUS_SET.fullmatch(str(value or "").strip()) + if not match: + return None, None + try: + return match.group(1), dt.date.fromisoformat(match.group(2)) + except ValueError: + return None, None + + +def owned_changes(row, band, *, today, owner=None): + """Cells a refresh writes on one SKU's owned item. It never overwrites a person's choice, + except the reopen: a fix 30 days old on a SKU still Losing or Thin goes back to Open.""" + status = str(row.get("status") or "").strip() + seen, since = parse_status_set(row.get("status_set")) + due = (today + dt.timedelta(days=DUE_DAYS)).isoformat() + if not status: + if band in BANDS: + out = {"status": "Open", "status_set": status_set_cell("Open", today)} + if owner and not str(row.get("owner") or "").strip(): + out["owner"] = owner + if not str(row.get("due") or "").strip(): + out["due"] = due + return out + return {"status_set": None} if row.get("status_set") else {} + if seen != status or since is None: + return {"status_set": status_set_cell(status, today)} + if status in FIX_STATUSES and band in BANDS and (today - since).days >= REOPEN_DAYS: + return {"status": "Open", "due": due, "status_set": status_set_cell("Open", today)} + return {} + + +# ---------------------------------------------------------------- mirror reads (SQL only) + +def _columns(con, table): + rows = con.execute("SELECT column_name FROM information_schema.columns WHERE table_name = ?", + [table]).fetchall() + return {r[0] for r in rows} + + +def _require(con, table, required): + cols = _columns(con, table) + if not cols: + raise SourceUnavailable(f"The Amazon mirror has no {table} table. Sync Amazon first.") + missing = [c for c in required if c not in cols] + if missing: + raise SourceUnavailable(f"{table} lacks {', '.join(missing)}. Resync Amazon economics.") + return cols + + +def ads_columns(cols): + """Per ad type totals; charge detail columns are parts of these totals.""" + return sorted(c for c in cols if _ADS_TOTAL.fullmatch(c) and "_charge_" not in c) + + +def economics_by_sku(con, start, end): + """`[{sku, asin, units, net_sales, fees, ads}]`: every SKU in the mirror, period sums.""" + cols = _require(con, ECONOMICS, _ECONOMICS_REQUIRED) + if "currency_code" in cols: + currencies = con.execute(f"SELECT count(DISTINCT currency_code) FROM {ECONOMICS}").fetchone()[0] + if currencies > 1: + raise SourceUnavailable("Amazon economics mixes currencies; profit needs one currency.") + ads = " + ".join(f'COALESCE("{c}", 0)' for c in ads_columns(cols)) or "0" + asin = "arg_max(child_asin, start_date)" if "child_asin" in cols else "NULL" + window = "FILTER (WHERE left(start_date, 10) BETWEEN ? AND ?)" + sql = (f"SELECT msku, {asin}, " + f"COALESCE(SUM(sales_net_units_sold) {window}, 0), " + f"COALESCE(SUM(sales_net_product_sales_amount) {window}, 0), " + f"COALESCE(SUM(fees_total_amount) {window}, 0), " + f"COALESCE(SUM({ads}) {window}, 0) " + f"FROM {ECONOMICS} WHERE NULLIF(trim(msku), '') IS NOT NULL GROUP BY msku ORDER BY msku") + rows = con.execute(sql, [start, end] * 4).fetchall() + return [{"sku": r[0], "asin": r[1], "units": r[2], "net_sales": r[3], "fees": r[4], + "ads": r[5]} for r in rows] + + +def titles(con): + """`{seller_sku: item_name}` from the newest listing snapshot; empty without listings.""" + cols = _columns(con, LISTINGS) + if not {"seller_sku", "item_name"} <= cols: + return {} + order = "COALESCE(_snapshot_date, '')" if "_snapshot_date" in cols else "1" + rows = con.execute(f"SELECT seller_sku, arg_max(item_name, {order}) FROM {LISTINGS} " + "WHERE seller_sku IS NOT NULL GROUP BY seller_sku").fetchall() + return {r[0]: r[1] for r in rows if r[1]} + + +def net_proceeds_total(con, start, end, *, drop_fees=False): + """Net sales minus fees and ads over the window; `drop_fees` is the negative control.""" + cols = _require(con, ECONOMICS, _ECONOMICS_REQUIRED) + ads = " + ".join(f'COALESCE("{c}", 0)' for c in ads_columns(cols)) or "0" + fees = "0" if drop_fees else "COALESCE(fees_total_amount, 0)" + return float(con.execute( + f"SELECT COALESCE(SUM(COALESCE(sales_net_product_sales_amount, 0) - {fees} - ({ads})), 0) " + f"FROM {ECONOMICS} WHERE left(start_date, 10) BETWEEN ? AND ?", [start, end]).fetchone()[0]) + + +def reported_net_proceeds(con, start, end): + """Amazon's own netProceeds total over the window, or None if the column is absent.""" + if "net_proceeds_total_amount" not in _columns(con, ECONOMICS): + return None + return float(con.execute( + f"SELECT COALESCE(SUM(net_proceeds_total_amount), 0) FROM {ECONOMICS} " + "WHERE left(start_date, 10) BETWEEN ? AND ?", [start, end]).fetchone()[0]) + + +def economics_currency(con): + if "currency_code" not in _columns(con, ECONOMICS): + return None + return con.execute(f"SELECT any_value(currency_code) FROM {ECONOMICS}").fetchone()[0] + + +def settlements_total(con, start, end, currency=None): + """Settlements posted in the window, excluding loan repayment and withheld tax. + + `currency` keeps settlements whose header row names it: Amazon.ca and Amazon.com.mx settle + in their own currency, outside the economics marketplace. Their reports write DD.MM.YYYY. + """ + cols = _require(con, SETTLEMENTS, _SETTLEMENT_REQUIRED) + scoped = currency is not None and {"currency", "settlement_id"} <= cols + stream = ("JOIN (SELECT settlement_id FROM {t} WHERE currency IS NOT NULL GROUP BY settlement_id " + "HAVING any_value(currency) = ?) h USING (settlement_id)").format(t=SETTLEMENTS) if scoped else "" + total, rows, undated = con.execute( + "SELECT COALESCE(SUM(amount) FILTER (WHERE d BETWEEN CAST(? AS DATE) AND CAST(? AS DATE)), 0), " + "count(*) FILTER (WHERE d BETWEEN CAST(? AS DATE) AND CAST(? AS DATE)), " + "count(*) FILTER (WHERE d IS NULL AND posted_date IS NOT NULL) " + "FROM (SELECT amount, posted_date, COALESCE(TRY_CAST(left(posted_date, 10) AS DATE), " + "CAST(try_strptime(left(posted_date, 10), '%d.%m.%Y') AS DATE)) AS d " + f"FROM {SETTLEMENTS} s {stream} WHERE COALESCE(transaction_type, '') <> ? " + "AND COALESCE(amount_type, '') <> ?)", + [start, end, start, end] + ([currency] if scoped else []) + [LOAN_REPAYMENT, WITHHELD_TAX]).fetchone() + return {"total": float(total), "rows": int(rows), "undated": int(undated), + "currency": currency if scoped else None} + + +# ---------------------------------------------------------------- Odoo cost (read only) + +def odoo_costs(codes): + """`({code: {id, name, cost}}, problems)` for linked references; search_read only (rule 4).""" + codes = sorted({c for c in codes if c}) + if not codes: + return {}, [] + import core.odoo as O + domain = [("default_code", "in", codes), ("active", "in", [True, False])] + rows = O.search_read("product.product", domain, + ["id", "default_code", "name", "standard_price"], limit=len(codes) * 4 + 50) + expected = O.get_odoo().search_count("product.product", domain) + if len(rows) != expected: + raise SourceUnavailable(f"Odoo returned {len(rows)} of {expected} linked products; " + "the cost read is incomplete.") + found, problems = {}, [] + by_code = {} + for row in rows: + by_code.setdefault(str(row.get("default_code") or ""), []).append(row) + for code in codes: + matches = by_code.get(code) or [] + if len(matches) == 1: + row = matches[0] + found[code] = {"id": row["id"], "name": row.get("name") or "", + "cost": _num(row.get("standard_price"))} + elif matches: + problems.append(f"{code}: {len(matches)} Odoo products share this reference") + else: + problems.append(f"{code}: no Odoo product has this reference") + return found, problems + + +def bound_cost_reader(runtime): + """`odoo_costs` inside this tenant's own Odoo binding; no binding means no cost.""" + def read(codes): + source = runtime.odoo_source() if hasattr(runtime, "odoo_source") else None + if source is None: + return {}, [f"{code}: this workspace has no Odoo connection" for code in sorted(codes)] + with source._bound(): + return odoo_costs(codes) + return read + + +def _odoo_binding(runtime): + source = runtime.odoo_source() if hasattr(runtime, "odoo_source") else None + if source is None: + raise SourceUnavailable("This workspace has no Odoo connection.") + return source._bound() + + +# ---------------------------------------------------------------- suggested matches (R13) + +#: Rule labels a `match_rule` cell stores, in the order the matcher tries them. +RULE_CODE = "Odoo code" +RULE_PREFIX = "Odoo code without prefix" +RULE_PACK = "Odoo code for the pack" +RULE_AMAZON = "Amazon Odoo code" +RULE_BARCODE = "Barcode" +RULE_NAME = "Name" +RULES = (RULE_CODE, RULE_PREFIX, RULE_PACK, RULE_AMAZON, RULE_BARCODE, RULE_NAME) +#: Listing report product id types that carry a barcode: 3 is UPC, 4 is EAN. +BARCODE_ID_TYPES = ("3", "4") +_NAME_STOP = frozenset("a an and the of for with per in x h to by or on inch inches set pack " + "piece pieces box bag roll count pcs".split()) +_PACK = re.compile(r"(.+?)_(\d{1,4})") +_SELLER_PREFIX = re.compile(r"[A-Za-z]{2,3}[-_ ](?=.)") +_ODOO_PACK = re.compile(r"(\d+)\s*-?\s*pieces?\s+per\s+(?:box|pack|case|bag)", re.I) +_TITLE_PACK = re.compile(r"\b(?:set|pack)\s+of\s+(\d+)\b|\b(\d+)\s*-?\s*(?:pack|pcs|pieces|count)\b", re.I) + + +def title_pack(title): + """Pieces in one Amazon unit as the listing title states them ("Set of 12", "100 Pack"), or None.""" + match = _TITLE_PACK.search(str(title or "")) + n = int(match.group(1) or match.group(2)) if match else 0 + return n if n > 0 else None + + +def odoo_pack(name): + """Pieces in one Odoo unit from its name (`24-Piece Per Box` is 24), or None.""" + match = _ODOO_PACK.search(str(name or "")) + return int(match.group(1)) if match and int(match.group(1)) > 0 else None + + +def odoo_units(n, name): + """Odoo units in one Amazon unit of `n` pieces: n / k when the Odoo name states k pieces, + to 4 decimals and never 0; n when it states none.""" + n = max(int(n or 1), 1) + k = odoo_pack(name) + if k is None: + return float(n) + return max(round(n / k, UNITS_DECIMALS), 10.0 ** -UNITS_DECIMALS) + + +def _norm_code(value): + return re.sub(r"[^A-Z0-9]", "", str(value or "").upper()) + + +def _norm_barcode(value): + return re.sub(r"\D", "", str(value or "")).lstrip("0") + + +def _name_tokens(text, brand=()): + text = str(text or "").lower().split("|")[0] + return {w for w in re.findall(r"[a-z0-9]+(?:\.[0-9]+)?", text) + if w not in _NAME_STOP and w not in brand and (len(w) > 1 or w.isdigit())} + + +def odoo_catalogue(): + """Every Odoo product, active and archived, in one read; search_read and search_count only.""" + import core.odoo as O + domain = [("active", "in", [True, False])] + expected = O.get_odoo().search_count("product.product", domain) + rows = O.search_read("product.product", domain, + ["id", "default_code", "barcode", "name"], limit=expected + 50) + if len(rows) != expected: + raise SourceUnavailable(f"Odoo returned {len(rows)} of {expected} products; " + "the product read is incomplete.") + return rows + + +def listing_barcodes(con): + """`{seller_sku: barcode}` from the newest listing snapshot, UPC and EAN ids only.""" + cols = _columns(con, LISTINGS) + if not {"seller_sku", "product_id", "product_id_type"} <= cols: + return {} + order = "COALESCE(_snapshot_date, '')" if "_snapshot_date" in cols else "1" + rows = con.execute(f"SELECT seller_sku, arg_max(product_id, {order}), " + f"arg_max(CAST(TRY_CAST(product_id_type AS BIGINT) AS VARCHAR), {order}) FROM {LISTINGS} " + "WHERE seller_sku IS NOT NULL GROUP BY seller_sku").fetchall() + return {r[0]: r[1] for r in rows if r[1] and str(r[2]).strip() in BARCODE_ID_TYPES} + + +class Matcher: + """Suggests one Odoo product per Amazon SKU. Only a product whose code is unique can link.""" + + def __init__(self, products, brand=("royal", "imports")): + codes = {} + for p in products: + code = str(p.get("default_code") or "").strip() + if code and code != "False": + codes.setdefault(code, []).append(p) + self.by_code = {code: rows[0] for code, rows in codes.items() if len(rows) == 1} + self.by_norm, self.by_barcode = {}, {} + for code, p in self.by_code.items(): + self.by_norm.setdefault(_norm_code(code), []).append(code) + barcode = p.get("barcode") + if barcode and barcode is not False and _norm_barcode(barcode): + self.by_barcode.setdefault(_norm_barcode(barcode), []).append(code) + self.brand = frozenset(brand) + self.tokens = {code: _name_tokens(p.get("name"), self.brand) for code, p in self.by_code.items()} + df = {} + for toks in self.tokens.values(): + for w in toks: + df[w] = df.get(w, 0) + 1 + total = max(len(self.tokens), 1) + self.idf = {w: math.log(total / (1 + n)) + 1.0 for w, n in df.items()} + self.index = {} + for code, toks in self.tokens.items(): + for w in toks: + self.index.setdefault(w, []).append(code) + + def label(self, code): + return f"[{code}] {self.by_code[code].get('name') or ''}".strip() + + def _one(self, codes, rejected): + codes = [c for c in codes or () if c not in rejected] + return codes[0] if len(codes) == 1 else None + + def _by_code(self, candidate, rejected): + exact = [c for c in self.by_norm.get(_norm_code(candidate), ()) if c.upper() == candidate.upper()] + return self._one(exact, rejected) or self._one(self.by_norm.get(_norm_code(candidate)), rejected) + + def code_candidates(self, sku): + """`[(candidate code, pieces, rule)]` in the order the code rules try them. Pieces is + the `_n` count for the pack stem, 0 for a pack code (one Odoo unit), None otherwise.""" + raw = str(sku or "").strip() + out = [(raw, None, RULE_CODE)] + plain = re.sub(r"\s*\(#?[^)]*\)\s*$", "", raw).lstrip(".#-_ ") + base = _SELLER_PREFIX.sub("", plain, count=1) + out += [(plain, None, RULE_PREFIX), (base, None, RULE_PREFIX)] + pack = _PACK.fullmatch(base) + if pack: + stem, n = pack.group(1), int(pack.group(2)) + out += [(f"{stem}-{n:02d}", 0, RULE_PACK), (stem, max(n, 1), RULE_PACK)] + out += [("AMZ" + c, u, RULE_AMAZON) for c, u, _ in list(out)] + return [item for item in out if item[0]] + + def pack_units(self, sku): + base = _SELLER_PREFIX.sub("", str(sku or "").strip().lstrip(".#-_ "), count=1) + pack = _PACK.fullmatch(re.sub(r"\s*\(#?[^)]*\)\s*$", "", base)) + return max(int(pack.group(2)), 1) if pack else None + + def by_name(self, title, rejected): + """The product whose every name word (three or more) is in the title, with a clear lead + on the next. A short generic name such as `WEDDING ARCH` matches too many titles.""" + words = _name_tokens(title, self.brand) + if not words: + return None + seen, scored = set(), [] + for w in words: + for code in self.index.get(w, ()): + if code in seen or code in rejected: + continue + seen.add(code) + toks = self.tokens[code] + if len(toks) >= 3 and sum(not t[0].isdigit() for t in toks) >= 2 and toks <= words: + scored.append((sum(self.idf[t] for t in toks), code)) + scored.sort(reverse=True) + if not scored or (len(scored) > 1 and scored[0][0] - scored[1][0] < 1.0): + return None + return scored[0][1] + + def _units(self, code, n): + """Pieces in the Amazon unit become Odoo units; 0 (a pack code) or unknown stays 1.""" + return odoo_units(n, self.by_code[code].get("name")) if n else 1.0 + + def suggest(self, sku, *, title=None, barcode=None, rejected=()): + """`{code, product, units, rule}` or None.""" + rejected = set(rejected or ()) + suffix = self.pack_units(sku) + pieces = suffix or title_pack(title) + for candidate, n, rule in self.code_candidates(sku): + code = self._by_code(candidate, rejected) + if code: + if n is None: + n = None if suffix else pieces + return {"code": code, "product": self.label(code), "rule": rule, + "units": self._units(code, n)} + if barcode and _norm_barcode(barcode): + code = self._one(self.by_barcode.get(_norm_barcode(barcode)), rejected) + if code: + return {"code": code, "product": self.label(code), "units": self._units(code, pieces), + "rule": RULE_BARCODE} + code = self.by_name(title, rejected) if title else None + if code: + return {"code": code, "product": self.label(code), "units": self._units(code, pieces), + "rule": RULE_NAME} + return None + + +def _rejected_codes(row): + return [c for c in (s.strip() for s in str(row.get("match_rejected") or "").split(",")) if c] + + +def decision_changes(row, state): + """Cells a person's Confirm or Reject writes on one row; {} when there is nothing to apply.""" + if str(row.get("odoo_product") or "").strip(): + return {} + suggested = str(row.get("suggested_product") or "").strip() + if state == CONFIRMED and suggested: + return {"odoo_product": suggested, + "units_per_sku": units_text(row.get("suggested_units")), + "match_state": CONFIRMED} + if state == REJECTED and suggested: + rejected = _rejected_codes(row) + code = odoo_code(suggested) + if code and code not in rejected: + rejected.append(code) + return {"match_state": REJECTED, "match_rejected": ", ".join(rejected), + **{k: None for k in SUGGESTION_KEYS}} + return {} + + +def _apply_cells(row, changes): + for key, value in changes.items(): + if value is None: + row.pop(key, None) + else: + row[key] = value + + +def apply_decisions(st, *, by=WRITER, sku=None, state=None): + """Apply every Confirmed or Rejected match state a person set, or one door's decision on + `sku`. Returns `{confirmed, rejected}` and, for a door, the row after the change.""" + import core.user_tables as ut + counts, after = {}, {} + + def _set(cur): + counts.clear() + after.clear() + counts.update(confirmed=0, rejected=0) + table = (cur or {}).get(TABLE_KEY) + if table is None: + return cur + for row in (table.get("rows") or {}).values(): + row_sku = str(row.get("sku") or "").strip() + if sku is not None and row_sku != sku: + continue + want = state if sku is not None else str(row.get("match_state") or "").strip() + changes = decision_changes(row, want) + if changes: + _apply_cells(row, changes) + counts["confirmed" if want == CONFIRMED else "rejected"] += 1 + if sku is not None: + after.update(row) + return cur + + ut.update_with_activity(TABLE_KEY, _set, st=st, by=by or WRITER, via=VIA, row_ids=None) + return {**counts, "row": dict(after) if sku is not None else None} + + +def suggest_matches(st, con, products, *, by=WRITER): + """Suggest an Odoo product for every unlinked SKU, no cap, in one store write. A SKU whose + Odoo product is set is never touched. Suggestions go to this table only, never to Odoo.""" + import core.user_tables as ut + matcher = Matcher(products) + names, barcodes = titles(con), listing_barcodes(con) + counts = {} + + def _set(cur): + counts.clear() + counts.update(considered=0, suggested=0, unmatched=0, **{rule: 0 for rule in RULES}) + table = (cur or {}).get(TABLE_KEY) + if table is None: + return cur + for row in (table.get("rows") or {}).values(): + sku = str(row.get("sku") or "").strip() + if not sku or str(row.get("odoo_product") or "").strip(): + continue + counts["considered"] += 1 + state = str(row.get("match_state") or "").strip() + found = matcher.suggest(sku, title=names.get(sku) or row.get("title"), + barcode=barcodes.get(sku), rejected=_rejected_codes(row)) + if found is None: + counts["unmatched"] += 1 + changes = {k: None for k in SUGGESTION_KEYS} + if state in (NEEDS_REVIEW, CONFIRMED): + changes["match_state"] = None + else: + counts["suggested"] += 1 + counts[found["rule"]] += 1 + changes = {"suggested_product": found["product"], "match_state": NEEDS_REVIEW, + "suggested_units": units_text(found["units"]), "match_rule": found["rule"]} + _apply_cells(row, {k: v for k, v in changes.items() + if v != row.get(k) and not (v is None and k not in row)}) + return cur + + ut.update_with_activity(TABLE_KEY, _set, st=st, by=by or WRITER, via=VIA, row_ids=None) + return dict(counts) + + +def _log(line): + try: + print(f"amazon_profit: {line}", flush=True) + except Exception: # noqa: BLE001 + pass + + +def _sync_cursor(tenant_key, rt): + """`(cursor, close)` on the tenant mirror the sync just wrote, else a fresh stored snapshot.""" + from harness import amazon_store + path = Path(amazon_store._tenant_mirror_path(tenant_key, rt)) + if path.is_file(): + from harness import datastore + try: + shared = Path(datastore.DB_PATH).resolve() == path.resolve() + except Exception: # noqa: BLE001 + shared = False + if shared: + cur = datastore.ro_cursor() + else: + import duckdb + cur = duckdb.connect(str(path), read_only=True) + if _mirror_has_economics(cur): + return cur, cur.close + cur.close() + for key in [k for k in _SNAPSHOT if k[0] == tenant_key]: + _SNAPSHOT.pop(key, None) + cur = snapshot_cursor(tenant_key) + return cur, cur.close + + +def sync_matches(tenant_key, synced_tables, *, st=None, con=None, catalogue=None, today=None): + """Refresh the current period, apply decisions, then suggest for every unlinked SKU. + Returns the report. It raises on failure; `on_amazon_sync` is the never-raise wrapper.""" + synced = {str(t or "").strip().lower() for t in synced_tables or ()} + if not synced & {"economics", "listings"}: + return {"state": "skipped", "synced": sorted(synced)} + if st is None: + from harness import amazon_store + st = amazon_store._runtime_for(tenant_key) + if st is None: + raise SourceUnavailable(f"The store for {tenant_key} could not be opened.") + close = None + if con is None: + con, close = _sync_cursor(tenant_key, st) + try: + report = {"state": "done"} + try: + refreshed = refresh(st, con, by=WRITER, cost_reader=bound_cost_reader(st), today=today, + tenant=tenant_key) + report["refresh"] = {k: refreshed.get(k) for k in ("skus", "linked", "refusal")} + except Exception as exc: # noqa: BLE001 + report["refreshError"] = f"{type(exc).__name__}: {exc}" + _log(f"{tenant_key}: the profit refresh failed ({report['refreshError']}); " + "suggesting on the rows already stored.") + with _LOCK: + ensure_table(st, WRITER) + report["decisions"] = apply_decisions(st) + if catalogue is None: + with _odoo_binding(st): + products = odoo_catalogue() + else: + products = catalogue() + with _LOCK: + report["matches"] = suggest_matches(st, con, products) + write_settings(st, matchedAt=dt.datetime.now(dt.timezone.utc).isoformat(timespec="seconds"), + matches=report["matches"], matchError=None) + return report + finally: + if close is not None: + try: + close() + except Exception: # noqa: BLE001 + pass + + +def on_amazon_sync(tenant_key, synced_tables): + """Contract C2 callback after an Amazon sync. Never raises: a failure logs one line naming + the cause, links nothing and returns.""" + try: + return sync_matches(tenant_key, synced_tables) + except Exception as exc: # noqa: BLE001 + reason = f"{type(exc).__name__}: {exc}" + _log(f"{tenant_key}: no Odoo matches suggested; every SKU stays unlinked. Cause: {reason}") + try: + from harness import amazon_store + st = amazon_store._runtime_for(tenant_key) + if st is not None: + write_settings(st, matchError=reason) + except Exception: # noqa: BLE001 + pass + return {"state": "failed", "reason": reason} + + +# ---------------------------------------------------------------- module-owned table + +def _definition_fields(): + import core.user_tables as ut + cleaned = ut.clean_fields([dict(f) for f in COMPUTED_FIELDS + OWNED_FIELDS + TRACK_FIELDS]) + by_key = {f["key"]: f for f in cleaned} + if set(by_key) != set(ALL_KEYS): + raise TableRefused("The profit columns failed the table field check.") + out = [] + for key in ALL_KEYS: + field = dict(by_key[key]) + # editRole governs the column definition only. Cell values on owned columns follow the + # database edit grant (`may_edit_database`), so editors edit them and viewers cannot. + field["editRole"] = "admins" + if key not in OWNED_KEYS: + field["automation"] = {"preset": True, "flowId": VIA} + out.append(field) + return out + + +def _format_repairs(fields, wanted): + """`{key: format}` for module fields an adopted table holds without their format.""" + want = {f["key"]: f["format"] for f in wanted if f.get("format")} + return {f.get("key"): want[f.get("key")] for f in fields or () + if isinstance(f, dict) and f.get("key") in want and not f.get("format")} + + +def ensure_table(st, username): + """Create or adopt the module table; add missing columns, never retype one. Returns its key.""" + import core.user_tables as ut + wanted = _definition_fields() + have = ut.get_one(TABLE_KEY, st) + if (have is not None and {f["key"] for f in wanted} <= {f.get("key") for f in have.get("fields") or []} + and not _format_repairs(have.get("fields"), wanted)): + return TABLE_KEY + created = dt.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") + refused = [] + + def _up(cur): + cur = cur if isinstance(cur, dict) else {} + table = cur.get(TABLE_KEY) + if table is None: + if ut.capped_table_count(cur) >= ut.MAX_TABLES: + refused.append("cap") + return cur + cur[TABLE_KEY] = {"key": TABLE_KEY, "label": TABLE_LABEL, "source": ut.AUTOMATION_SOURCE, + "createdBy": username or WRITER, "created": created, + "fields": [dict(f) for f in wanted], "rows": {}} + return cur + present = {f.get("key") for f in table.get("fields") or []} + repairs = _format_repairs(table.get("fields"), wanted) + for field in table.get("fields") or []: + if field.get("key") in repairs: + field["format"] = dict(repairs[field["key"]]) + table.setdefault("fields", []).extend(dict(f) for f in wanted if f["key"] not in present) + return cur + + st.update(ut.STORE_KEY, _up, flush="sync") + if refused: + raise TableRefused(f"This workspace already holds {ut.MAX_TABLES} databases, so " + f"{TABLE_LABEL} cannot be created. Delete an unused database, then refresh.") + if ut.get_one(TABLE_KEY, st) is None: + raise TableRefused(f"The store did not keep {TABLE_LABEL}. Refresh again.") + return TABLE_KEY + + +def table_rows(st): + import core.user_tables as ut + return dict((ut.get_one(TABLE_KEY, st) or {}).get("rows") or {}) + + +def links(rows): + """`{sku: {code, units_per_sku}}` from the owned columns.""" + out = {} + for row in rows.values(): + sku = str(row.get("sku") or "").strip() + if sku: + out[sku] = {"code": odoo_code(row.get("odoo_product")), + "units_per_sku": units_per_sku(row.get("units_per_sku"))} + return out + + +def _rid_order(rid): + text = str(rid) + return (0, int(text), text) if text.isdigit() else (1, 0, text) + + +def merge_duplicates(st): + """Keep one row per SKU (R10): the oldest row takes empty owned cells from its copies, + then the copies go. Returns how many rows were removed.""" + import core.user_tables as ut + groups = {} + for rid, row in table_rows(st).items(): + sku = str(row.get("sku") or "").strip() + if sku: + groups.setdefault(sku, []).append(str(rid)) + extra = {} + for rids in groups.values(): + if len(rids) > 1: + rids.sort(key=_rid_order) + extra[rids[0]] = rids[1:] + if not extra: + return 0 + dropped = [rid for rids in extra.values() for rid in rids] + + def _merge(cur): + table = cur.get(TABLE_KEY) + if table is None: + return cur + rows = table.get("rows") or {} + order = table.get("_rowCreationOrder") or {} + for keep, copies in extra.items(): + row = rows.get(keep) + if row is None: + continue + for rid in copies: + copy = rows.pop(rid, None) or {} + order.pop(rid, None) + for key in OWNED_KEYS + TRACK_KEYS: + if not str(row.get(key) or "").strip() and str(copy.get(key) or "").strip(): + row[key] = copy[key] + return cur + + ut.update_with_activity(TABLE_KEY, _merge, st=st, by=WRITER, via=VIA, + row_ids=list(extra) + dropped, flush="sync") + return len(dropped) + + +def write_rows(st, computed): + """Upsert computed cells by SKU in two store writes; owned cells are never touched.""" + import core.user_tables as ut + existing = table_rows(st) + seen = {str(r.get("sku") or "").strip() for r in existing.values()} + new = [{k: v for k, v in values.items() if v is not None} + for sku, values in computed.items() if sku not in seen] + limit = ut.row_limit(TABLE_KEY, st) + if limit and len(existing) + len(new) > limit: + raise TableRefused( + f"{TABLE_LABEL} holds at most {limit:,} rows and Amazon has " + f"{len(existing) + len(new):,} SKUs. Cause: a user database is capped at {limit:,} " + "rows. Fix: read this table through the mirror instead of storing rows.") + created = 0 + if new: + made = ut.add_rows(TABLE_KEY, new, username=WRITER, st=st, by=WRITER, via=VIA) + if made is None: + raise TableRefused(f"The store refused new rows for {TABLE_LABEL}.") + created = len(made) + updated = [] + + def _set(cur): + updated.clear() + table = cur.get(TABLE_KEY) + if table is None: + return cur + for rid, row in (table.get("rows") or {}).items(): + sku = str(row.get("sku") or "").strip() + values = computed.get(sku) + if values is None: + values = {k: None for k in _PERIOD_KEYS} + changed = False + for key, value in values.items(): + if value is None: + changed = row.pop(key, None) is not None or changed + elif row.get(key) != value: + row[key] = value + changed = True + if changed: + updated.append(rid) + return cur + + ut.update_with_activity(TABLE_KEY, _set, st=st, by=WRITER, via=VIA, row_ids=None) + return {"created": created, "updated": len(updated)} + + +# ---------------------------------------------------------------- settings + +def read_settings(st): + raw = {} + try: + raw = st.get(SETTINGS_KEY) or {} + except Exception: # noqa: BLE001 + raw = {} + raw = raw if isinstance(raw, dict) else {} + period = raw.get("period") or PERIOD_DEFAULT + try: + period_bounds(period) + except ValueError: + period = PERIOD_DEFAULT + try: + thin = clean_thin(raw.get("thin")) + except ValueError: + thin = THIN_DEFAULT + owner = str(raw.get("defaultOwner") or "").strip().lower() or None + return {**raw, "period": period, "thin": thin, "defaultOwner": owner} + + +def write_settings(st, **values): + def _up(cur): + cur = dict(cur) if isinstance(cur, dict) else {} + cur.update(values) + return cur + st.update(SETTINGS_KEY, _up, flush="sync") + + +# ---------------------------------------------------------------- default owner (R31) + +#: Accounts that act for the platform, never for the business. +SERVICE_ACCOUNTS = ("admin", "automation", "scheduler", "qa-runner", "leadership") + + +def _accounts(tenant): + """`[(username, record)]`: the tenant's active accounts, through the one tenant filter.""" + from core import users + return users._tenant_accounts(tenant) + + +def _is_service(username, record): + name = str(username or "").strip().lower() + return (not name or name.startswith("qa") or name in SERVICE_ACCOUNTS + or name.endswith("-admin") or bool(record.get("platform_admin"))) + + +def owner_account(tenant=TENANT_ZERO): + """The tenant's owner account: no record names one, so the earliest active Administrator + that is a person. A person signs in with an email; earliest is the oldest password stamp.""" + people = [(name, rec) for name, rec in _accounts(tenant) + if isinstance(rec, dict) and rec.get("role") == "admin" and not _is_service(name, rec)] + if not people: + return None + people.sort(key=lambda item: ("@" not in item[0], not item[1].get("password_changed_at"), + str(item[1].get("password_changed_at") or ""), item[0])) + return str(people[0][0]).strip().lower() + + +def account_name(username, tenant=TENANT_ZERO): + """The display name a `user` cell stores for `username`, or None if no active account.""" + want = str(username or "").strip().lower() + for name, rec in _accounts(tenant): + if str(name).strip().lower() == want and isinstance(rec, dict): + return str(rec.get("name") or name) + return None + + +def set_default_owner(st, username, tenant=TENANT_ZERO, by=""): + """Point new owned items at `username`; refuses an account this tenant does not hold.""" + want = str(username or "").strip().lower() + if not want or account_name(want, tenant) is None: + raise ValueError("Choose an active account in this workspace.") + write_settings(st, defaultOwner=want, defaultOwnerSetBy=by or WRITER) + return want + + +def default_owner(st, tenant=TENANT_ZERO): + """`(username, display name)` new owned items get; resolves and stores it on first use.""" + username = read_settings(st)["defaultOwner"] + if not username: + username = owner_account(tenant) + if username: + write_settings(st, defaultOwner=username, defaultOwnerSetBy=WRITER) + return username, (account_name(username, tenant) if username else None) + + +# ---------------------------------------------------------------- seeded views + +def seeded_views(): + from core import view_templates as vt + return list((vt.get(TEMPLATE_KEY) or {}).get("views") or ()) + + +def ensure_views(st, owner): + """Write the seeded views once, owned by `owner`, granted to everyone who can open the table. + + A view lives in one personal stratum and reaches others through a `*` view grant, the same + residency every shared view has. A view a person deleted stays deleted: `viewsSeeded` in the + settings records what was written.""" + from core import shares + from core import view_templates as vt + from core.table_store import TableStore + done = set(read_settings(st).get("viewsSeeded") or ()) + ops = TableStore(vt.workspace_key(TABLE_KEY), st) + owner = str(owner or WRITER).strip().lower() + made = [] + for view in seeded_views(): + if view["id"] in done: + continue + found = ops.find_view(view["id"]) + holder = found[0] if found else owner + if not found: + ops.save_view(owner, dict(view, createdBy=owner), actor=WRITER, origin={"client": VIA}) + made.append(view["id"]) + held = shares.grants("view", view["id"], st=st) + if not any(e["user"] == shares.EVERYONE for e in held["entries"]): + shares.set_grants("view", view["id"], + held["entries"] + [{"user": shares.EVERYONE, "role": "view"}], + owner=held["owner"] or holder, st=st) + done.add(view["id"]) + write_settings(st, viewsSeeded=sorted(done)) + return made + + +# ---------------------------------------------------------------- refresh + +def build_rows(con, period, row_links, thin=THIN_DEFAULT, cost_reader=odoo_costs, today=None): + """`(computed cells by SKU, report)` for one period. Every SKU in the mirror is present.""" + bounds = period_bounds(period, today) + econ = economics_by_sku(con, bounds["start"], bounds["end"]) + names = titles(con) + codes = {link["code"] for link in row_links.values() if link.get("code")} + costs, problems = cost_reader(codes) if codes else ({}, []) + computed, linked = {}, 0 + for item in econ: + item = dict(item, title=names.get(item["sku"])) + link = row_links.get(item["sku"]) or {} + found = costs.get(link.get("code") or "") + if found is not None: + linked += 1 + row = compute_row(item, None if found is None else found["cost"], + link.get("units_per_sku") or 1, thin) + computed[item["sku"]] = cells(row, bounds["label"]) + report = {"period": bounds, "thin": thin, "skus": len(computed), "linked": linked, + "unlinked": len(computed) - linked, "costProblems": problems} + return computed, report + + +def refusal(checks, month): + """One plain sentence naming the first failed validate() check, or None if all pass.""" + label = dt.date.fromisoformat(f"{month}-01").strftime("%B %Y") + sentences = ( + lambda c: (f"Not verified: Amazon net proceeds miss settlements by " + f"{c.get('gap') or 0:.1%} in {label}." if math.isfinite(c.get("gap") or 0) + else f"Not verified: Amazon has no settlements in {label} to tie to."), + lambda c: f"Not verified: the fee check cannot tell a wrong total from a right one in {label}.", + lambda c: f"Not verified: {c.get('a')} settlement rows in {label} have no readable date.", + lambda c: f"Not verified: SKU net proceeds do not add up to Amazon's total in {label}.", + lambda c: "Not verified: the SKU list is incomplete.", + ) + for index, check in enumerate(checks): + if not check.get("ok"): + if index < len(sentences): + return sentences[index](check) + return f"Not verified: the Amazon figures do not tie in {label}." + return None + + +def unverified(computed, sentence): + """Cells with every period figure blank and the refusal in the period cell. Never a 0.""" + return {sku: {**values, **{k: None for k in _UNVERIFIED_KEYS}, "period": sentence} + for sku, values in computed.items()} + + +def apply_owned(st, bands, *, today, owner=None): + """Open, reopen and stamp owned items in one store write. Returns the counts.""" + import core.user_tables as ut + counts = {} + + def _set(cur): + counts.clear() + counts.update(opened=0, reopened=0, stamped=0) + table = cur.get(TABLE_KEY) + if table is None: + return cur + for row in (table.get("rows") or {}).values(): + sku = str(row.get("sku") or "").strip() + before = str(row.get("status") or "").strip() + changes = owned_changes(row, bands.get(sku), today=today, owner=owner) + if not changes: + continue + for key, value in changes.items(): + if value is None: + row.pop(key, None) + else: + row[key] = value + if changes.get("status") == "Open": + counts["reopened" if before else "opened"] += 1 + else: + counts["stamped"] += 1 + return cur + + ut.update_with_activity(TABLE_KEY, _set, st=st, by=WRITER, via=VIA, row_ids=None) + return dict(counts) + + +def refresh(st, con, *, period=None, thin=None, by="", cost_reader=odoo_costs, today=None, + tenant=TENANT_ZERO, validator=None): + """Recompute every SKU for the period and store it; owned cells stay. Returns the report. + + It runs validate() first for `verification_month`: a picked month that has ended ties itself. + A failed tie writes no figures: the report carries `refusal`, and every period cell reads it.""" + settings = read_settings(st) + period = str(period or settings["period"]).strip().lower() + period_bounds(period, today) + thin = settings["thin"] if thin is None else clean_thin(thin) + day = today or _today() + month = verification_month(period, day) + checks = (validator or (lambda c, m: validate(month=m, con=c)))(con, month) + sentence = refusal(checks, month) + with _LOCK: + ensure_table(st, by) + merged = merge_duplicates(st) + decisions = apply_decisions(st, by=by or WRITER) + owner, owner_label = default_owner(st, tenant) + ensure_views(st, owner or by) + computed, report = build_rows(con, period, links(table_rows(st)), thin, cost_reader, today) + if sentence: + computed = unverified(computed, sentence) + report.update(write_rows(st, computed), merged=merged, + decisions={k: decisions[k] for k in ("confirmed", "rejected")}) + bands = {sku: values.get("band") for sku, values in computed.items()} + report["owned"] = apply_owned(st, bands, today=day, owner=owner_label) + stamp = dt.datetime.now(dt.timezone.utc).isoformat(timespec="seconds") + write_settings(st, period=period, thin=thin, refreshedAt=stamp, refreshedBy=by or WRITER, + lastPeriod=report["period"], skus=report["skus"], linked=report["linked"], + verifiedMonth=month, refusal=sentence) + report.update(tableKey=TABLE_KEY, refreshedAt=stamp, verifiedMonth=month, refusal=sentence, + defaultOwner=owner) + return report + + +# ---------------------------------------------------------------- validation source + +_SNAPSHOT = {} + + +def _mirror_has_economics(con): + try: + return bool(_columns(con, ECONOMICS)) + except Exception: # noqa: BLE001 + return False + + +def snapshot_cursor(tenant=TENANT_ZERO, tables=(ECONOMICS, SETTLEMENTS, LISTINGS)): + """An in-memory DuckDB over the stored mirror snapshot. Downloads only; writes nothing back.""" + key = (tenant, tuple(tables)) + if key in _SNAPSHOT: + return _SNAPSHOT[key].cursor() + import duckdb + from harness import runtime as R + rt = R.get_runtime(tenant) + raw = rt.download_bytes("amazon_mirror/manifest.json") + if raw is None: + raise SourceUnavailable(f"{tenant} has no stored Amazon mirror.") + manifest = json.loads(raw) + if manifest.get("tenant") != tenant: + raise SourceUnavailable("The stored Amazon mirror names another tenant.") + folder = Path(tempfile.mkdtemp(prefix="amazon-profit-")) + atexit.register(shutil.rmtree, folder, True) + con = duckdb.connect() + for name in tables: + meta = (manifest.get("tables") or {}).get(name) + if not isinstance(meta, dict): + continue + paths = [] + for index, part in enumerate(meta.get("parts") or [meta]): + blob_key = part.get("key") or f"{name}.parquet" + data = rt.download_bytes("amazon_mirror/" + blob_key) + if data is None or (part.get("sha256") and hashlib.sha256(data).hexdigest() != part["sha256"]): + raise SourceUnavailable(f"The stored {name} snapshot failed its checksum.") + path = folder / f"{name}.{index:04d}.parquet" + path.write_bytes(data) + paths.append(str(path)) + if paths: + listed = ", ".join("'" + p.replace("'", "''") + "'" for p in paths) + con.execute(f"CREATE VIEW {name} AS SELECT * FROM read_parquet([{listed}], union_by_name=true)") + _SNAPSHOT[key] = con + return con.cursor() + + +def validation_cursor(tenant=TENANT_ZERO): + """`(cursor, source)`: the local mirror when it holds economics, else the stored snapshot.""" + try: + from harness import datastore as DS + if Path(DS.DB_PATH).is_file(): + con = DS.ro_cursor() + if _mirror_has_economics(con): + return con, "local mirror" + except Exception: # noqa: BLE001 + pass + return snapshot_cursor(tenant), "stored snapshot" + + +# ---------------------------------------------------------------- validate + +def _tie(a, b, tolerance=SETTLEMENT_TOLERANCE): + gap = abs(a - b) / abs(b) if b else (0.0 if a == b else math.inf) + return gap <= tolerance, gap + + +def validate(pre=None, month=None, con=None): + """validate.py convention: net proceeds tie to settlements within 2% for one month.""" + month = month or last_complete_month() + bounds = period_bounds(month) + start, end = bounds["start"], bounds["end"] + source = "given cursor" + if con is None: + con, source = validation_cursor() + checks = [] + proceeds = net_proceeds_total(con, start, end) + settled = settlements_total(con, start, end, economics_currency(con)) + ok, gap = _tie(proceeds, settled["total"]) + checks.append({"check": f"amazon_profit {month}: net proceeds tie to settlements within 2% " + f"({gap:.2%} apart, {source})", + "ok": ok, "a": round(proceeds, 2), "b": round(settled["total"], 2), + "gap": round(gap, 4)}) + dropped = net_proceeds_total(con, start, end, drop_fees=True) + control_ok, control_gap = _tie(dropped, settled["total"]) + checks.append({"check": f"amazon_profit {month}: NC net proceeds without fees miss settlements " + f"({control_gap:.2%} apart)", + "ok": not control_ok, "a": round(dropped, 2), "b": round(settled["total"], 2), + "gap": round(control_gap, 4)}) + checks.append({"check": f"amazon_profit {month}: every {settled['currency'] or 'settlement'} " + "settlement row carries a readable date", + "ok": settled["undated"] == 0, "a": settled["undated"], "b": 0, + "gap": settled["undated"]}) + econ = economics_by_sku(con, start, end) + ours = sum(compute_row(item)["net_proceeds"] for item in econ) + reported = reported_net_proceeds(con, start, end) + checks.append({"check": f"amazon_profit {month}: per SKU net proceeds sum to Amazon's reported total", + "ok": reported is not None and abs(ours - reported) <= 0.01, + "a": round(ours, 2), "b": None if reported is None else round(reported, 2), + "gap": None if reported is None else round(abs(ours - reported), 4)}) + distinct = con.execute(f"SELECT count(DISTINCT msku) FROM {ECONOMICS} " + "WHERE NULLIF(trim(msku), '') IS NOT NULL").fetchone()[0] + checks.append({"check": f"amazon_profit: every SKU is present, no cap ({len(econ)} SKUs)", + "ok": len(econ) == distinct, "a": len(econ), "b": distinct, + "gap": abs(len(econ) - distinct)}) + return checks + + +# ---------------------------------------------------------------- semantic validator + +def _v_amazon_sku_net_profit(date_from, date_to, team_id=None, **_kwargs): + """Per SKU net proceeds (the profit's revenue side) against Amazon's reported total.""" + con, _source = validation_cursor() + start, end = str(date_from)[:10], str(date_to)[:10] + ours = sum(compute_row(item)["net_proceeds"] for item in economics_by_sku(con, start, end)) + reported = reported_net_proceeds(con, start, end) + if reported is None: + raise SourceUnavailable("amzn_economics lacks net_proceeds_total_amount.") + return round(ours, 2), round(reported, 2) + + +PROFIT_VALIDATORS = {"amazon_sku_net_profit": _v_amazon_sku_net_profit} + + +def register_semantic_validators(): + """Register this module's validator until harness/semantic.py imports it directly.""" + from harness import semantic + semantic._VALIDATORS.update(PROFIT_VALIDATORS) + return tuple(PROFIT_VALIDATORS) + + +try: + register_semantic_validators() +except Exception: # noqa: BLE001 + pass diff --git a/platform/modules/competitor_watch.py b/platform/modules/competitor_watch.py new file mode 100644 index 0000000000000000000000000000000000000000..aea6d4b19b290045bcadfa1b4452c3bb42ff02f8 --- /dev/null +++ b/platform/modules/competitor_watch.py @@ -0,0 +1,660 @@ +"""Competitor watch App: watched terms, the weekly top 3 comparison, and owned alerts on four triggers. + +Store documents (tenant runtime store, App owned, never the mirror and never Amazon): + competitor_watch_settings {undercut_pct, rank_surge_pct, default_owner} + competitor_watch_terms {seeded: [...], added: [...], removed: [...], seeded_at} + competitor_watch_alerts {seq, items: {id: alert}, checks: [check, ...]} + competitor_watch_weeks {last_compared, comparison: {...}, metrics: {period_start: {asin: metric}}} + +Top 3 history is read from the connector table `amzn_brand_analytics`, which the store's weekly +retention keeps for watched terms. Deleting this module and its four documents leaves the +connector intact (rule 13). + +Limit (rule 1): each document is one JSON value, and the Postgres store refuses a document over +`core.store_pg.DOCUMENT_CEILING_BYTES` (128 MB). At about 600 bytes an alert, the alerts document +reaches it near 200,000 alerts. Nothing here caps terms or alerts. The fix when it nears: split +`competitor_watch_alerts` by year. +""" +import contextlib +import datetime as dt +from pathlib import Path + +TRIGGERS = { + "new_asin": "New ASIN in top 3", + "lost_spot": "Lost spot", + "undercut": "Undercut", + "rank_surge": "Rank surge", +} +STATUSES = ("Open", "Repriced", "Ads changed", "Listing improved", "Watching", "Ignore") +#: A key (term, asin, trigger) is held while its latest alert has one of these statuses. +HOLDING = ("Open", "Watching", "Ignore") +DEFAULTS = {"undercut_pct": 5.0, "rank_surge_pct": 50.0} +DUE_DAYS = 7 +NOT_READY = "Comparison starts after the next weekly pull." +LIMIT = ("Each competitor watch document is limited to 128 MB by the store. About 200,000 alerts " + "fit in one. Split the alerts document by year when it nears that size.") + +KEY_SETTINGS = "competitor_watch_settings" +KEY_TERMS = "competitor_watch_terms" +KEY_ALERTS = "competitor_watch_alerts" +KEY_WEEKS = "competitor_watch_weeks" + +BA_TABLE = "amzn_brand_analytics" +SQP_TABLE = "amzn_search_query_performance" +LISTINGS_TABLE = "amzn_listings" + + +def _now(): + return dt.datetime.now(dt.timezone.utc) + + +def _stamp(at): + return at.strftime("%Y-%m-%dT%H:%M:%SZ") + + +def _norm(term): + return " ".join(str(term or "").split()).lower() + + +def _rt(tenant_key, st=None): + """The tenant runtime store handle (get and update), or raise when it cannot be resolved.""" + if st is not None: + return st + from harness import amazon_store + rt = amazon_store._runtime_for(tenant_key) + if rt is None: + raise RuntimeError(f"The store for tenant {tenant_key!r} could not be opened.") + return rt + + +def _doc(st, key): + value = st.get(key) + return value if isinstance(value, dict) else {} + + +# --------------------------------------------------------------------------- mirror reads + +def _mirror_path(tenant_key, st): + from harness import amazon_store + rt = st if hasattr(st, "datastore_path") else None + return amazon_store._tenant_mirror_path(tenant_key, rt) + + +@contextlib.contextmanager +def _reader(tenant_key, st): + """A read-only cursor on the tenant mirror, or None when the mirror file does not exist.""" + path = Path(_mirror_path(tenant_key, st)) + if not path.exists(): + yield None + return + from harness import datastore + try: + shared = Path(datastore.DB_PATH).resolve() == path.resolve() + except Exception: # noqa: BLE001 + shared = False + # The process holds one writer on its own mirror: a separate read-only connect would refuse. + if shared: + cur = datastore.ro_cursor() + try: + yield cur + finally: + cur.close() + return + import duckdb + con = duckdb.connect(str(path), read_only=True) + try: + yield con + finally: + con.close() + + +def _columns(cur, table): + try: + return {r[0] for r in cur.execute( + "SELECT column_name FROM information_schema.columns WHERE table_name = ?", [table]).fetchall()} + except Exception: # noqa: BLE001 + return set() + + +def _sqp(cur): + """SQP rows as (term, asin, impressions, end_date). SQP reports our ASINs only.""" + need = {"search_query_data_search_query", "asin"} + cols = _columns(cur, SQP_TABLE) if cur is not None else set() + if not need <= cols: + return [] + imp = ('TRY_CAST("impression_data_asin_impression_count" AS DOUBLE)' + if "impression_data_asin_impression_count" in cols else "0") + end = 'CAST("end_date" AS VARCHAR)' if "end_date" in cols else "''" + rows = cur.execute( + f'SELECT "search_query_data_search_query", "asin", {imp}, {end} FROM {SQP_TABLE}').fetchall() + return [(_norm(t), str(a or "").strip(), float(i or 0), str(e or "")) for t, a, i, e in rows if _norm(t)] + + +def _our_asins(cur, sqp_rows): + ours = {a for _t, a, _i, _e in sqp_rows if a} + if cur is not None and "asin1" in _columns(cur, LISTINGS_TABLE): + ours |= {str(r[0]).strip() for r in cur.execute( + f'SELECT DISTINCT "asin1" FROM {LISTINGS_TABLE} WHERE "asin1" IS NOT NULL').fetchall()} + return {a for a in ours if a} + + +def _our_asin_for(term, sqp_rows): + """R23: our ASIN with the most impressions on the term, in the term's newest SQP period.""" + rows = [r for r in sqp_rows if r[0] == term and r[1]] + if not rows: + return None + newest = max(r[3] for r in rows) + totals = {} + for _t, asin, imp, end in rows: + if end == newest: + totals[asin] = totals.get(asin, 0.0) + imp + return max(sorted(totals), key=lambda a: totals[a]) + + +def _top3(cur, terms=None): + """{term: {period_start: {"end": str, "rows": {asin: (rank, click_share, conversion_share)}}}}.""" + cols = _columns(cur, BA_TABLE) if cur is not None else set() + if not {"search_term", "clicked_asin", "click_share_rank", "_period_start"} <= cols: + return {} + cs = 'TRY_CAST("click_share" AS DOUBLE)' if "click_share" in cols else "NULL" + cv = 'TRY_CAST("conversion_share" AS DOUBLE)' if "conversion_share" in cols else "NULL" + pe = 'CAST("_period_end" AS VARCHAR)' if "_period_end" in cols else "''" + rows = cur.execute( + f'SELECT "search_term", CAST("_period_start" AS VARCHAR), {pe}, "clicked_asin", ' + f'TRY_CAST("click_share_rank" AS INTEGER), {cs}, {cv} FROM {BA_TABLE} ' + f'WHERE TRY_CAST("click_share_rank" AS INTEGER) BETWEEN 1 AND 3').fetchall() + wanted = None if terms is None else set(terms) + out = {} + for term, start, end, asin, rank, share, conv in rows: + key = _norm(term) + if wanted is not None and key not in wanted or not asin: + continue + week = out.setdefault(key, {}).setdefault(str(start), {"end": str(end or ""), "rows": {}}) + week["rows"][str(asin).strip()] = (int(rank), share, conv) + return out + + +def _periods(cur): + if cur is None or "_period_start" not in _columns(cur, BA_TABLE): + return [] + rows = cur.execute(f'SELECT DISTINCT CAST("_period_start" AS VARCHAR) FROM {BA_TABLE}').fetchall() + return sorted((str(r[0]) for r in rows if r[0] is not None), reverse=True) + + +# --------------------------------------------------------------------------- watched terms + +def watched_terms(tenant_key, *, st=None): + """Seeded SQP terms plus terms a person added, minus terms a person removed (store only).""" + doc = _doc(_rt(tenant_key, st), KEY_TERMS) + removed = {_norm(t) for t in doc.get("removed") or ()} + terms = {_norm(t) for t in [*(doc.get("seeded") or ()), *(doc.get("added") or ())]} + return sorted(t for t in terms if t and t not in removed) + + +def seed_terms(tenant_key, *, st=None, cur=None): + """Add every distinct SQP search query to the seeded set. A removed term stays removed.""" + st = _rt(tenant_key, st) + if cur is None: + with _reader(tenant_key, st) as reader: + found = {t for t, _a, _i, _e in _sqp(reader)} if reader is not None else set() + else: + found = {t for t, _a, _i, _e in _sqp(cur)} + + def _seed(doc): + doc = doc if isinstance(doc, dict) else {} + seeded = set(doc.get("seeded") or ()) | found + doc["seeded"] = sorted(seeded) + doc.setdefault("added", []) + doc.setdefault("removed", []) + doc["seeded_at"] = _stamp(_now()) + return doc + st.update(KEY_TERMS, _seed) + return watched_terms(tenant_key, st=st) + + +def _edit_terms(tenant_key, terms, add, st): + clean = sorted({_norm(t) for t in terms or () if _norm(t)}) + if not clean: + raise ValueError("Name at least one search term.") + st = _rt(tenant_key, st) + + def _apply(doc): + doc = doc if isinstance(doc, dict) else {} + added, removed = set(doc.get("added") or ()), set(doc.get("removed") or ()) + if add: + added |= set(clean) + removed -= set(clean) + else: + removed |= set(clean) + added -= set(clean) + doc.setdefault("seeded", []) + doc["added"], doc["removed"] = sorted(added), sorted(removed) + return doc + st.update(KEY_TERMS, _apply) + return watched_terms(tenant_key, st=st) + + +def add_terms(tenant_key, terms, *, st=None): + """Watch these terms. A term a person removed before comes back.""" + return _edit_terms(tenant_key, terms, True, st) + + +def remove_terms(tenant_key, terms, *, st=None): + """Stop watching these terms. A later SQP seed does not bring them back.""" + return _edit_terms(tenant_key, terms, False, st) + + +def terms_summary(tenant_key, *, st=None): + """Every watched term with its source, newest held period and open alert count. No cap.""" + st = _rt(tenant_key, st) + doc = _doc(st, KEY_TERMS) + if not doc.get("seeded_at"): + # First open before any sync: seed from the SQP terms already in the mirror. + seed_terms(tenant_key, st=st) + doc = _doc(st, KEY_TERMS) + seeded = {_norm(t) for t in doc.get("seeded") or ()} + terms = watched_terms(tenant_key, st=st) + with _reader(tenant_key, st) as cur: + held = _top3(cur, terms) + open_counts = {} + for alert in (_doc(st, KEY_ALERTS).get("items") or {}).values(): + if alert.get("status") == "Open": + open_counts[alert.get("term")] = open_counts.get(alert.get("term"), 0) + 1 + return {"terms": [{"term": t, "source": "seeded" if t in seeded else "added", + "newestPeriod": max(held.get(t, {}), default=None), + "openAlerts": open_counts.get(t, 0)} for t in terms]} + + +# --------------------------------------------------------------------------- settings and owner + +#: Accounts that sign in for a process, never a person who can own an alert (same list as amazon_profit). +SERVICE_ACCOUNTS = ("admin", "automation", "scheduler", "qa-runner", "leadership") + + +def _is_service(name, rec): + name = str(name or "").strip().lower() + return (not name or name.startswith("qa") or name in SERVICE_ACCOUNTS + or name.endswith("-admin") or bool(rec.get("platform_admin"))) + + +def owner_account(st, tenant_key=None): + """R31: the tenant's earliest active person Administrator by display name, as money-losers use. + + A tenant with no person Administrator falls back to its `admin` account, else its first Administrator.""" + try: + users = st.get("users") + except Exception: # noqa: BLE001 + users = None + want = str(tenant_key or "").strip().lower() + admins = [(str(name), rec) for name, rec in (users or {}).items() + if isinstance(rec, dict) and rec.get("active", True) + and str(rec.get("role") or "").lower() == "admin" + and (not want or str(rec.get("tenant") or "royal-imports").strip().lower() == want)] + people = sorted(((n, r) for n, r in admins if not _is_service(n, r)), + key=lambda item: ("@" not in item[0], not item[1].get("password_changed_at"), + str(item[1].get("password_changed_at") or ""), item[0])) + if people: + return str(people[0][1].get("name") or people[0][0]) + names = sorted(n for n, _r in admins) + return "admin" if "admin" in names or not names else names[0] + + +def settings(tenant_key, *, st=None): + """Thresholds and the default owner, with defaults filled in.""" + st = _rt(tenant_key, st) + doc = _doc(st, KEY_SETTINGS) + out = {k: float(doc.get(k, v)) for k, v in DEFAULTS.items()} + out["defaultOwnerSetting"] = doc.get("default_owner") or None + out["defaultOwner"] = out["defaultOwnerSetting"] or owner_account(st, tenant_key) + out["statuses"] = list(STATUSES) + out["limit"] = LIMIT + return out + + +def set_settings(tenant_key, *, undercut_pct=None, rank_surge_pct=None, default_owner=None, st=None): + """Set thresholds (percent, above 0 and below 100) or the default owner ('' resets to the owner account).""" + st = _rt(tenant_key, st) + changes = {} + for name, value in (("undercut_pct", undercut_pct), ("rank_surge_pct", rank_surge_pct)): + if value is None: + continue + try: + number = float(value) + except (TypeError, ValueError): + raise ValueError("A threshold must be a number.") from None + if not 0 < number < 100: + raise ValueError("A threshold must be above 0 and below 100 percent.") + changes[name] = number + + def _apply(doc): + doc = doc if isinstance(doc, dict) else {} + doc.update(changes) + if default_owner is not None: + doc["default_owner"] = str(default_owner).strip() or None + return doc + st.update(KEY_SETTINGS, _apply) + return settings(tenant_key, st=st) + + +# --------------------------------------------------------------------------- price and rank reads + +def _parse_price(response): + """(price, currency, seller_id, cause) from one getCompetitiveSummary item response.""" + status = ((response or {}).get("status") or {}).get("statusCode") + body = (response or {}).get("body") or {} + if status not in (None, 200): + errors = body.get("errors") or [] + reason = str((errors[0] or {}).get("message") or "") if errors else "" + return None, None, None, f"Amazon refused the price read ({status}{': ' + reason if reason else ''})." + options = body.get("featuredBuyingOptions") or [] + options = sorted(options, key=lambda o: 0 if str(o.get("buyingOptionType") or "New") == "New" else 1) + for option in options: + for offer in option.get("segmentedFeaturedOffers") or [option]: + price = (offer.get("listingPrice") or {}).get("amount") + if isinstance(price, (int, float)): + return (float(price), (offer.get("listingPrice") or {}).get("currencyCode"), + offer.get("sellerId"), None) + return None, None, None, "Amazon returned no Buy Box offer for this ASIN." + + +def _cause(kind, exc): + from harness import amazon_client as cx + name = type(exc).__name__ + if isinstance(exc, cx.AmazonThrottled): + return f"Amazon throttled the {kind} read ({name}). It runs again after the next weekly pull." + if isinstance(exc, cx.AmazonOperationRefused): + return f"The connector refused the {kind} read ({name}): {exc}" + return f"The {kind} read failed ({name}): {exc}" + + +def _read_prices(tenant_key, st, asins): + """{asin: (price, currency, seller_id, cause)}; a refused call writes its cause on every ASIN.""" + from harness import amazon_client as cx, amazon_store + if not asins: + return {} + try: + fields = amazon_store.credential(tenant_key, st if hasattr(st, "datastore_path") else None) + if not fields: + raise cx.AmazonError("no Amazon credential is saved for this tenant") + responses = cx.competitive_summaries(fields, sorted(asins)) + except Exception as exc: # noqa: BLE001 + cause = _cause("price", exc) + return {a: (None, None, None, cause) for a in asins} + by_asin = {str(((r or {}).get("body") or {}).get("asin") or ""): r for r in responses} + return {a: _parse_price(by_asin[a]) if a in by_asin + else (None, None, None, "Amazon returned no price for this ASIN.") for a in asins} + + +def _read_ranks(tenant_key, st, asins): + """{asin: (rank, cause)}; a refused call writes its cause on every ASIN.""" + from harness import amazon_client as cx, amazon_store + if not asins: + return {} + try: + fields = amazon_store.credential(tenant_key, st if hasattr(st, "datastore_path") else None) + if not fields: + raise cx.AmazonError("no Amazon credential is saved for this tenant") + ranks = cx.catalog_sales_ranks(fields, sorted(asins)) + except Exception as exc: # noqa: BLE001 + cause = _cause("sales rank", exc) + return {a: (None, cause) for a in asins} + out = {} + for a in asins: + rank = (ranks.get(a) or {}).get("rank") + out[a] = (rank, None) if isinstance(rank, int) else (None, "Amazon returned no sales rank for this ASIN.") + return out + + +# --------------------------------------------------------------------------- the weekly comparison + +def on_amazon_sync(tenant_key, synced_tables): + """Contract C2 callback: seed terms, then compare the newest brand analytics week. Never raises.""" + try: + compare(tenant_key, synced_tables) + except Exception as exc: # noqa: BLE001 + try: + def _note(doc): + doc = doc if isinstance(doc, dict) else {} + doc["last_error"] = f"The weekly comparison failed ({type(exc).__name__}): {exc}" + return doc + _rt(tenant_key).update(KEY_WEEKS, _note) + except Exception: # noqa: BLE001 + pass + + +def compare(tenant_key, synced_tables=("brand_analytics",), *, st=None, now=None): + """Compare the newest held week with the week before and raise owned alerts. Returns a summary.""" + st = _rt(tenant_key, st) + now = now or _now() + synced = set(synced_tables or ()) + with _reader(tenant_key, st) as cur: + if cur is None: + return {"state": "no_mirror"} + sqp = _sqp(cur) + seed_terms(tenant_key, st=st, cur=cur) + if "brand_analytics" not in synced: + return {"state": "seeded"} + periods = _periods(cur) + terms = watched_terms(tenant_key, st=st) + held = _top3(cur, terms) + ours = _our_asins(cur, sqp) + weeks = _doc(st, KEY_WEEKS) + if not periods: + return {"state": "no_week"} + newest = periods[0] + if weeks.get("last_compared") == newest: + return {"state": "already_compared", "period": newest} + previous = periods[1] if len(periods) > 1 else None + conf = settings(tenant_key, st=st) + + # Read price and rank for every top 3 ASIN of the newest week and our priced ASIN per term. + our_for = {t: _our_asin_for(t, sqp) for t in terms} + want = set() + for term in terms: + want |= set(((held.get(term) or {}).get(newest) or {}).get("rows", {})) + if our_for.get(term): + want.add(our_for[term]) + prices = _read_prices(tenant_key, st, want) + ranks = _read_ranks(tenant_key, st, want) + metrics = {a: {"price": prices[a][0], "currency": prices[a][1], "sellerId": prices[a][2], + "priceCause": prices[a][3], "rank": ranks[a][0], "rankCause": ranks[a][1]} + for a in sorted(want)} + prior = ((weeks.get("metrics") or {}).get(previous) or {}) if previous else {} + + raised, checks = [], [] + if previous is not None: + undercut = conf["undercut_pct"] / 100.0 + surge = conf["rank_surge_pct"] / 100.0 + for term in terms: + now_week = (held.get(term) or {}).get(newest) + was_week = (held.get(term) or {}).get(previous) + if not now_week or not was_week: + continue + top_now, top_was = now_week["rows"], was_week["rows"] + end = now_week["end"] + + def found(asin, key, detail, cause=None): + raised.append({"term": term, "asin": asin, "trigger": key, "periodStart": newest, + "periodEnd": end, "previousPeriodStart": previous, "detail": detail, + "cause": cause}) + + def asin_cause(asin): + m = metrics.get(asin) or {} + return " ".join(c for c in (m.get("priceCause"), m.get("rankCause")) if c) or None + + for asin in sorted(set(top_now) - set(top_was)): + found(asin, "new_asin", {"rank": top_now[asin][0]}, asin_cause(asin)) + for asin in sorted((set(top_was) - set(top_now)) & ours): + found(asin, "lost_spot", {"previousRank": top_was[asin][0]}) + mine = our_for.get(term) + mine_m = metrics.get(mine) or {} + if mine is None and set(top_now) - ours: + checks.append({"term": term, "asin": None, "trigger": TRIGGERS["undercut"], + "periodStart": newest, + "cause": "None of our ASINs has impressions on this term, so there is " + "no price of ours to compare."}) + for asin in sorted(set(top_now) - ours): + m = metrics.get(asin) or {} + if mine is None: + pass + elif mine_m.get("price") is None or m.get("price") is None: + cause = " ".join(c for c in (mine_m.get("priceCause") and f"Our price: {mine_m['priceCause']}", + m.get("priceCause")) if c) + checks.append({"term": term, "asin": asin, "trigger": TRIGGERS["undercut"], + "periodStart": newest, "cause": cause or "No price was read."}) + elif m["price"] <= mine_m["price"] * (1 - undercut): + found(asin, "undercut", {"ourAsin": mine, "ourPrice": mine_m["price"], + "price": m["price"], + "percentUnder": round(100 * (1 - m["price"] / mine_m["price"]), 2)}, + asin_cause(asin)) + old = (prior.get(asin) or {}) + if m.get("rank") is None: + checks.append({"term": term, "asin": asin, "trigger": TRIGGERS["rank_surge"], + "periodStart": newest, "cause": m.get("rankCause") or "No sales rank was read."}) + elif old.get("rank") is None: + if old.get("rankCause"): + checks.append({"term": term, "asin": asin, "trigger": TRIGGERS["rank_surge"], + "periodStart": newest, + "cause": f"The week before: {old['rankCause']}"}) + elif m["rank"] <= old["rank"] * (1 - surge): + found(asin, "rank_surge", {"rank": m["rank"], "previousRank": old["rank"]}, + asin_cause(asin)) + + owner = conf["defaultOwner"] + created = [] + + def _alerts(doc): + doc = doc if isinstance(doc, dict) else {} + doc.setdefault("seq", 0) + items = doc.setdefault("items", {}) + latest = {} + for alert in sorted(items.values(), key=lambda a: int(str(a.get("id", "a0"))[1:] or 0)): + latest[(alert.get("term"), alert.get("asin"), alert.get("triggerKey"))] = alert + for hit in raised: + key = (hit["term"], hit["asin"], hit["trigger"]) + if key in latest and latest[key].get("status") in HOLDING: + continue + doc["seq"] += 1 + aid = f"a{doc['seq']}" + alert = {"id": aid, "term": hit["term"], "asin": hit["asin"], "trigger": TRIGGERS[hit["trigger"]], + "triggerKey": hit["trigger"], "periodStart": hit["periodStart"], + "periodEnd": hit["periodEnd"], "previousPeriodStart": hit["previousPeriodStart"], + "detail": hit["detail"], "cause": hit["cause"], "owner": owner, "status": "Open", + "due": (now.date() + dt.timedelta(days=DUE_DAYS)).isoformat(), + "createdAt": _stamp(now), "updatedAt": _stamp(now), "updatedBy": "system", + "history": [{"at": _stamp(now), "by": "system", "event": "created"}]} + items[aid] = alert + latest[key] = alert + created.append(aid) + doc["checks"] = [c for c in doc.get("checks") or () if c.get("periodStart") != newest] + checks + return doc + if raised or checks: + st.update(KEY_ALERTS, _alerts) + + ready = previous is not None + + def _weeks(doc): + doc = doc if isinstance(doc, dict) else {} + doc.setdefault("metrics", {})[newest] = metrics + doc["last_compared"] = newest + doc["comparison"] = {"ready": ready, "message": None if ready else NOT_READY, + "periodStart": newest, "previousPeriodStart": previous, + "comparedAt": _stamp(now), "alertsRaised": len(created), + "checks": len(checks)} + doc.pop("last_error", None) + return doc + st.update(KEY_WEEKS, _weeks) + return {"state": "compared" if ready else "waiting", "period": newest, "previous": previous, + "created": created, "checks": checks} + + +# --------------------------------------------------------------------------- alerts + +def alerts(tenant_key, *, status=None, st=None): + """Every alert, newest first, plus the newest week's check rows. No cap.""" + st = _rt(tenant_key, st) + doc = _doc(st, KEY_ALERTS) + rows = [a for a in (doc.get("items") or {}).values() if status is None or a.get("status") == status] + rows.sort(key=lambda a: int(str(a.get("id", "a0"))[1:] or 0), reverse=True) + return {"alerts": rows, "checks": list(doc.get("checks") or ()), "statuses": list(STATUSES)} + + +def update_alert(tenant_key, alert_id, *, owner=None, status=None, due=None, user="system", st=None): + """Reassign, move status, or move the due date of one alert. Returns the alert.""" + if status is not None and status not in STATUSES: + raise ValueError("Status must be one of: " + ", ".join(STATUSES) + ".") + if due is not None: + try: + due = dt.date.fromisoformat(str(due)).isoformat() + except ValueError: + raise ValueError("Due must be a date such as 2026-10-27.") from None + if owner is not None and not str(owner).strip(): + raise ValueError("Owner cannot be empty.") + st = _rt(tenant_key, st) + out = {} + + def _apply(doc): + doc = doc if isinstance(doc, dict) else {} + alert = (doc.get("items") or {}).get(alert_id) + if not alert: + return doc + at = _stamp(_now()) + event = {"at": at, "by": user} + if owner is not None: + alert["owner"] = event["owner"] = str(owner).strip() + if status is not None: + alert["status"] = event["status"] = status + if due is not None: + alert["due"] = event["due"] = due + alert["updatedAt"], alert["updatedBy"] = at, user + alert.setdefault("history", []).append(event) + out["alert"] = dict(alert) + return doc + st.update(KEY_ALERTS, _apply) + if "alert" not in out: + raise KeyError(alert_id) + return out["alert"] + + +# --------------------------------------------------------------------------- term detail + +def term_detail(tenant_key, term, *, st=None): + """One term's held periods, the newest top 3 with week-over-week change, and the click share trend.""" + st = _rt(tenant_key, st) + key = _norm(term) + with _reader(tenant_key, st) as cur: + sqp = _sqp(cur) if cur is not None else [] + ours = _our_asins(cur, sqp) if cur is not None else set() + weeks_held = _top3(cur, [key]).get(key, {}) + periods = sorted(weeks_held, reverse=True) + weeks = _doc(st, KEY_WEEKS) + top3 = [] + if periods: + newest = weeks_held[periods[0]]["rows"] + before = weeks_held[periods[1]]["rows"] if len(periods) > 1 else None + metric = (weeks.get("metrics") or {}).get(periods[0]) or {} + for asin, (rank, share, conv) in sorted(newest.items(), key=lambda kv: kv[1][0]): + change = None + if before is not None: + if asin not in before: + change = {"kind": "new", "previousRank": None, "clickShareDelta": None, + "conversionShareDelta": None} + else: + r0, s0, c0 = before[asin] + change = {"kind": "up" if rank < r0 else "down" if rank > r0 else "same", + "previousRank": r0, + "clickShareDelta": None if share is None or s0 is None else share - s0, + "conversionShareDelta": None if conv is None or c0 is None else conv - c0} + m = metric.get(asin) or {} + top3.append({"asin": asin, "rank": rank, "clickShare": share, "conversionShare": conv, + "isOurs": asin in ours, "change": change, "buyBoxPrice": m.get("price"), + "currency": m.get("currency"), "salesRank": m.get("rank"), + "priceCause": m.get("priceCause"), "rankCause": m.get("rankCause")}) + every = sorted({a for p in periods for a in weeks_held[p]["rows"]}) + trend = [{"asin": a, "isOurs": a in ours, + "clickShare": [(weeks_held[p]["rows"].get(a) or (None, None, None))[1] + if a in weeks_held[p]["rows"] else None for p in periods]} for a in every] + state = weeks.get("comparison") or {} + ready = len(periods) > 1 and bool(state.get("ready", True)) + return {"term": key, "periods": periods, "top3": top3, "trend": trend, + "comparison": {"ready": ready, "message": None if ready else NOT_READY}} diff --git a/web/public/AIOS_DEPLOY_RECEIPT b/web/public/AIOS_DEPLOY_RECEIPT index aeeab71a27963e86d69a619a445f5fd6e4dfd40e..31414dc96fa6c13fd2dc20a4ac84a2d6a8a24857 100644 --- a/web/public/AIOS_DEPLOY_RECEIPT +++ b/web/public/AIOS_DEPLOY_RECEIPT @@ -1 +1 @@ -ab085f5030f792659e50e3d9b8cdcdf43af11c641476d39a9f37295134cbeb7d +26b2b98342f6ecb0f85cab3bd8844f79c1b1d0b2fc0881389ce4e0d8041bec4d diff --git a/web/public/brand/SOURCES.md b/web/public/brand/SOURCES.md index 90654b35e58d0124dd4e51cef83fb769d32528ee..31d02c0b2928edaa626bfd28c14b394bd96e3b95 100644 --- a/web/public/brand/SOURCES.md +++ b/web/public/brand/SOURCES.md @@ -48,6 +48,7 @@ hand-drawn set was built for. | `shopify` | `shopify.png` | https://www.shopify.com/brand-assets | `shopify-shopping-bag.zip` → `02 - Glyph/png/shopify_glyph.png` (600×683) | trim + fit only | | `amazon` | `amazon.svg` | the approved lowercase `a` and smile supplied for W58-T08 (owner instruction 19) | vector, no raster cut needed | none — shipped as the vendor SVG. **Its geometry is digest-pinned** in `src/connectors/_directory_render.cjs`, which asserts the served file and `brandMarks.tsx`'s inline fallback are the same artwork | | `whatsapp` | `whatsapp.svg` | https://www.meta.com/brand/resources/whatsapp/whatsapp-brand/ | Official `WhatsApp-Brand-Resource-Center.zip`, `01_Glyph/01_Digital RGB/03_SVG/Digital_Glyph_Green_RGB_2026.svg`, downloaded from the page's Meta-controlled fbcdn.net link on 2026-10-09 | unchanged vendor artwork; only a final newline added. Archive SVG SHA-256 `f7b1311db718533e671645f57cd94b92f0e006e61d7e6581a80675fc5a478fc4`; repository file `46d8d8acf2cf2bee7618db0924c6b53329cd70c919e763e7c77f0c12d03b53be` | +| `google-maps` | `google-maps.svg` | Google product branding on www.gstatic.com (S78-T09) | `https://www.gstatic.com/images/branding/productlogos/maps/v7/192px.svg`, fetched 2026-10-20 | uncut, served as a static file. The hyphen keeps it outside `routes_brand._KEY_RE`, so the connector brand door and `verify_meta` parity never list it | ## S72-T47 terms checked on 2026-10-09 diff --git a/web/public/brand/google-maps.svg b/web/public/brand/google-maps.svg new file mode 100644 index 0000000000000000000000000000000000000000..323088dae3e8a3bed306099954727aa01a6f1ecc --- /dev/null +++ b/web/public/brand/google-maps.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/web/s71_frontend_contracts.py b/web/s71_frontend_contracts.py index 5116acd9a7c5303d96e39f87c1bfbb5b4d5650ef..1e4ca0b90fafff0ab886e1ff3773039819d88533 100644 --- a/web/s71_frontend_contracts.py +++ b/web/s71_frontend_contracts.py @@ -15,7 +15,10 @@ def catalog_host_faults(grid: str, loader: str, map_view: str, catalog: str) -> "only registry Routing owns Routes": 'resolvedApplicationMatches && applicationTemplateSurface(resolvedApplication) === "route"' in grid, "Catalog has no route event listener": bool(re.search(r'useEffect\(\(\) => \{\s*if \(!routeApplicationSurface\) return;\s*const openRoute', grid)), "cold App lookup precedes row admission": grid.find('const addressedDefinition') < grid.find('} = useCustomerData(scope,') - and 'const legacyRowsEnabled = !isCatalogApplication && (!isApplicationSurface || !!addressedDefinition)' in grid + # S78-T17 (C4): a Competitor watch App also reads no source rows. + and bool(re.search(r'const legacyRowsEnabled = !isCatalogApplication && ' + r'(?:!isCompetitorWatchApplication &&\s+)?' + r'\(!isApplicationSurface \|\| !!addressedDefinition\)', grid)) and 'rowsEnabled: legacyRowsEnabled' in grid, "Catalog disables legacy rows and subscriptions": 'options.rowsEnabled === false ? EMPTY_APPLICATION_PAYLOAD' in loader and 'const suppliedPayload = embeddedPayload !== undefined' in loader diff --git a/web/src/apps/competitor-watch/CompetitorWatchView.tsx b/web/src/apps/competitor-watch/CompetitorWatchView.tsx new file mode 100644 index 0000000000000000000000000000000000000000..f0ea9202bee972836a509ee952a8161ae94ce43c --- /dev/null +++ b/web/src/apps/competitor-watch/CompetitorWatchView.tsx @@ -0,0 +1,485 @@ +import { lazy, Suspense, useCallback, useEffect, useMemo, useRef, useState } from "react"; +import type { FormEvent } from "react"; +import type { TopLevelSpec } from "vega-lite"; +import { API_V1, CREDENTIALS } from "../../apiContract"; +import { AppFrame } from "../../customer-grid/AppFrame"; +import "../../styles/sdlc78/competitor-watch.css"; + +// S78-T17 (C4, R20): the Competitor watch App. Reads and writes go through the competitor watch +// routes with the App id, so the server applies may_read and may_edit to this App record. +const VegaLiteChart = lazy(() => import("../../viz/vega/VegaLiteChart")); + +/** R19: the six alert statuses, in this order. */ +export const COMPETITOR_WATCH_STATUSES = [ + "Open", "Repriced", "Ads changed", "Listing improved", "Watching", "Ignore", +] as const; + +/** Series slots read existing tokens; slot 0 is always our ASIN (validated palette, adjacent pairs). */ +const SERIES_TOKENS = ["--lp-blue-solid", "--lp-orange-deep", "--lp-teal-deep", "--lp-purple-deep"]; +const SERIES_SHAPES = ["circle", "square", "triangle-up", "diamond"]; + +export interface CompetitorWatchApplication { + id: string; + name: string; + capabilities?: Record; +} + +interface Week { start: string; end: string } + +interface RankedRow { + rank: number; + asin: string; + title?: string; + ours: boolean; + clickShare: number | null; + conversionShare: number | null; + previousRank: number | null; + previousClickShare: number | null; + previousConversionShare: number | null; + cause?: string; +} + +interface TrendSeries { + asin: string; + ours: boolean; + points: { start: string; clickShare: number | null }[]; +} + +interface TermScreen { + canEdit: boolean; + terms: string[]; + term: string | null; + week: Week | null; + previousWeek: Week | null; + comparisonReady: boolean; + comparisonMessage: string; + top3: RankedRow[]; + trend: TrendSeries[]; + problems: string[]; +} + +interface Alert { + id: string; + term: string; + asin: string; + trigger: string; + owner: string; + status: string; + due: string; + cause?: string; +} + +interface Check { term: string; asin: string; trigger: string; cause: string } + +interface AlertScreen { canEdit: boolean; alerts: Alert[]; checks: Check[] } + +type Load = { phase: "loading" } | { phase: "error"; message: string } | { phase: "ready"; value: T }; + +const text = (value: unknown) => (typeof value === "string" ? value : ""); +const num = (value: unknown) => (typeof value === "number" && Number.isFinite(value) ? value : null); + +async function readJson(response: Response, fallback: string): Promise { + const body: unknown = await response.json().catch(() => null); + if (response.ok) return body; + const error = (body as { detail?: { error?: { message?: unknown } } } | null)?.detail?.error; + throw new Error(text(error?.message) || fallback); +} + +function parseWeek(raw: unknown): Week | null { + const row = raw as { start?: unknown; end?: unknown } | null; + return row && text(row.start) && text(row.end) ? { start: text(row.start), end: text(row.end) } : null; +} + +function parseTermScreen(raw: unknown): TermScreen { + const body = (raw ?? {}) as Record; + const rows = Array.isArray(body.top3) ? body.top3 : []; + const trend = Array.isArray(body.trend) ? body.trend : []; + return { + canEdit: body.canEdit === true, + terms: Array.isArray(body.terms) ? body.terms.map(text).filter(Boolean) : [], + term: text(body.term) || null, + week: parseWeek(body.week), + previousWeek: parseWeek(body.previousWeek), + comparisonReady: body.comparisonReady === true, + comparisonMessage: text(body.comparisonMessage), + top3: rows.map((item) => { + const row = (item ?? {}) as Record; + return { + rank: num(row.rank) ?? 0, + asin: text(row.asin), + title: text(row.title) || undefined, + ours: row.ours === true, + clickShare: num(row.clickShare), + conversionShare: num(row.conversionShare), + previousRank: num(row.previousRank), + previousClickShare: num(row.previousClickShare), + previousConversionShare: num(row.previousConversionShare), + cause: text(row.cause) || undefined, + }; + }).filter((row) => row.asin), + trend: trend.map((item) => { + const series = (item ?? {}) as Record; + const points = Array.isArray(series.points) ? series.points : []; + return { + asin: text(series.asin), + ours: series.ours === true, + points: points.map((point) => { + const row = (point ?? {}) as Record; + return { start: text(row.start), clickShare: num(row.clickShare) }; + }).filter((point) => point.start), + }; + }).filter((series) => series.asin), + problems: Array.isArray(body.problems) ? body.problems.map(text).filter(Boolean) : [], + }; +} + +function parseAlerts(raw: unknown): AlertScreen { + const body = (raw ?? {}) as Record; + const rows = Array.isArray(body.alerts) ? body.alerts : []; + const checks = Array.isArray(body.checks) ? body.checks : []; + return { + canEdit: body.canEdit === true, + alerts: rows.map((item) => parseAlert(item)).filter((row): row is Alert => row !== null), + checks: checks.map((item) => { + const row = (item ?? {}) as Record; + return { term: text(row.term), asin: text(row.asin), trigger: text(row.trigger), cause: text(row.cause) }; + }).filter((row) => row.cause), + }; +} + +function parseAlert(item: unknown): Alert | null { + const row = (item ?? {}) as Record; + const id = text(row.id); + if (!id) return null; + return { + id, term: text(row.term), asin: text(row.asin), trigger: text(row.trigger), + owner: text(row.owner), status: text(row.status), due: text(row.due), + cause: text(row.cause) || undefined, + }; +} + +const shortDate = (iso: string) => { + const date = new Date(iso.length === 10 ? iso + "T00:00:00Z" : iso); + return Number.isNaN(date.getTime()) ? iso + : date.toLocaleDateString(undefined, { month: "short", day: "numeric", timeZone: "UTC" }); +}; +const weekLabel = (week: Week) => `${shortDate(week.start)} to ${shortDate(week.end)}`; +const pct = (value: number | null) => (value === null ? "Not held" : `${value.toFixed(1)}%`); + +/** The change chip copy: "New", "Up 1", "Down 2", "Same" for rank; "+1.2 pts" for a share. */ +export function rankChange(rank: number, previous: number | null): { label: string; tone: "new" | "up" | "down" | "flat" } { + if (previous === null) return { label: "New", tone: "new" }; + if (previous > rank) return { label: `Up ${previous - rank}`, tone: "up" }; + if (previous < rank) return { label: `Down ${rank - previous}`, tone: "down" }; + return { label: "Same", tone: "flat" }; +} + +export function shareChange(value: number | null, previous: number | null): { label: string; tone: "up" | "down" | "flat" } | null { + if (value === null || previous === null) return null; + const delta = Math.round((value - previous) * 10) / 10; + if (delta > 0) return { label: `+${delta.toFixed(1)} pts`, tone: "up" }; + if (delta < 0) return { label: `-${Math.abs(delta).toFixed(1)} pts`, tone: "down" }; + return { label: "0.0 pts", tone: "flat" }; +} + +function ChangeChip({ change }: { change: { label: string; tone: string } | null }) { + if (!change) return null; + return {change.label}; +} + +function chartTokens() { + const style = getComputedStyle(document.documentElement); + const read = (token: string) => style.getPropertyValue(token).trim(); + return { series: SERIES_TOKENS.map(read), line: read("--lp-line"), surface: read("--lp-surface"), + muted: read("--lp-muted") }; +} + +const seriesName = (series: { asin: string; ours: boolean }) => (series.ours ? `${series.asin} (You)` : series.asin); + +function trendSpec(trend: TrendSeries[]): TopLevelSpec { + // Our ASIN takes slot 0; competitors keep the order the server ranked them in. + const ordered = [...trend.filter((row) => row.ours), ...trend.filter((row) => !row.ours)].slice(0, 4); + const domain = ordered.map(seriesName); + const values = ordered.flatMap((series) => series.points + .filter((point) => point.clickShare !== null) + .map((point) => ({ series: seriesName(series), week: point.start, share: point.clickShare }))); + const lastPoints = domain.map((name) => values.filter((row) => row.series === name) + .sort((a, b) => a.week.localeCompare(b.week)).at(-1)).filter(Boolean); + const tokens = chartTokens(); + const color = { field: "series", type: "nominal" as const, title: null, + scale: { domain, range: tokens.series.slice(0, domain.length) }, + legend: { orient: "bottom" as const, symbolStrokeWidth: 2 } }; + const x = { field: "week", type: "ordinal" as const, title: null, sort: "ascending" as const, + axis: { labelAngle: 0, labelExpr: "timeFormat(toDate(datum.value), '%b %-d')" } }; + const y = { field: "share", type: "quantitative" as const, title: "Click share (%)", + axis: { gridColor: tokens.line, tickCount: 4 } }; + return { + $schema: "https://vega.github.io/schema/vega-lite/v6.json", + width: "container", + height: 220, + autosize: { type: "fit", contains: "padding" }, + background: "transparent", + config: { view: { stroke: null }, axis: { domain: false, ticks: false, labelFontSize: 11, titleFontSize: 11, + titleFontWeight: 500, labelColor: tokens.muted, titleColor: tokens.muted }, + legend: { labelFontSize: 11, labelColor: tokens.muted }, font: "Inter" }, + data: { values }, + layer: [ + { mark: { type: "line", strokeWidth: 2, interpolate: "monotone" }, encoding: { x, y, color } }, + { mark: { type: "point", filled: true, size: 90, stroke: tokens.surface, strokeWidth: 1.5 }, + encoding: { x, y, color, + shape: { field: "series", type: "nominal", scale: { domain, range: SERIES_SHAPES.slice(0, domain.length) } }, + tooltip: [{ field: "series", title: "ASIN" }, { field: "week", title: "Week of" }, + { field: "share", title: "Click share (%)", format: ".1f" }] } }, + // Direct label at each line's last week, so identity is never colour alone. + { data: { values: lastPoints }, mark: { type: "text", align: "right", dy: -12, fontSize: 11, + color: tokens.muted }, encoding: { x, y, text: { field: "series" } } }, + ], + } as TopLevelSpec; +} + +function TrendChart({ trend }: { trend: TrendSeries[] }) { + const spec = useMemo(() => trendSpec(trend), [trend]); + const weeks = new Set(trend.flatMap((series) => series.points.map((point) => point.start))); + if (!trend.length) return null; + if (weeks.size < 2) return

The trend shows after two weeks.

; + return ; +} + +function RankedTable({ rows, compare }: { rows: RankedRow[]; compare: boolean }) { + // Before a second week is held there is nothing to compare, so no chip shows. + return
+ + + + + + {rows.map((row) => + + + + + )} + +
RankASINClick shareConversion share
{row.rank} + + {row.asin} + {row.ours && You} + {row.title && {row.title}} + {row.cause && {row.cause}} + {pct(row.clickShare)} + {pct(row.conversionShare)} +
; +} + +function useAppQuery(appId: string) { + return useCallback((path: string, params: Record = {}) => { + const query = new URLSearchParams({ app: appId, ...params }); + return `${API_V1}/competitor-watch${path}?${query.toString()}`; + }, [appId]); +} + +function TermsView({ appId, canEditApp }: { appId: string; canEditApp: boolean }) { + const url = useAppQuery(appId); + const [term, setTerm] = useState(""); + const [state, setState] = useState>({ phase: "loading" }); + const [attempt, setAttempt] = useState(0); + const [draft, setDraft] = useState(""); + const [termError, setTermError] = useState(""); + const [busy, setBusy] = useState(false); + + useEffect(() => { + const controller = new AbortController(); + setState({ phase: "loading" }); + void fetch(url("/screen", term ? { term } : {}), { credentials: CREDENTIALS, signal: controller.signal }) + .then((response) => readJson(response, "The competitor watch could not be loaded.")) + .then((body) => { if (!controller.signal.aborted) setState({ phase: "ready", value: parseTermScreen(body) }); }) + .catch((reason: unknown) => { + if (!controller.signal.aborted) setState({ phase: "error", + message: reason instanceof Error ? reason.message : "The competitor watch could not be loaded." }); + }); + return () => controller.abort(); + }, [url, term, attempt]); + + const screen = state.phase === "ready" ? state.value : null; + const canEdit = canEditApp && screen?.canEdit === true; + const selected = screen?.term ?? ""; + + const editTerms = async (action: "add" | "remove", value: string) => { + setBusy(true); + setTermError(""); + try { + const response = await fetch(url(action === "add" ? "/terms" : "/terms/remove"), { + method: "POST", credentials: CREDENTIALS, headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ terms: [value] }), + }); + await readJson(response, action === "add" ? "The term was not added." : "The term was not removed."); + setDraft(""); + if (action === "add") setTerm(value.trim().toLowerCase()); + else setTerm(""); + setAttempt((count) => count + 1); + } catch (reason) { + setTermError(reason instanceof Error ? reason.message : "The term was not saved."); + } finally { + setBusy(false); + } + }; + const submit = (event: FormEvent) => { + event.preventDefault(); + if (draft.trim()) void editTerms("add", draft.trim()); + }; + + return
+
+ + {screen?.week && + Week of {weekLabel(screen.week)}{screen.previousWeek ? ` against ${weekLabel(screen.previousWeek)}` : ""} + } + {canEdit &&
+ setDraft(event.target.value)} + placeholder="Add a term" aria-label="Add a search term" disabled={busy} /> + + {selected && } +
} +
+ {termError &&

{termError}

} + {state.phase === "loading" &&
+
+
} + {state.phase === "error" &&

{state.message}{" "} +

} + {screen && <> + {screen.problems.map((problem) =>

{problem}

)} + {!screen.comparisonReady &&

+ {screen.comparisonMessage || "Comparison starts after the second weekly pull."}

} + {/* The trend sits beside the ranked list when both fit, and stacks below it when not. */} +
+ {screen.term && screen.top3.length > 0 &&
+

Top 3 this week

+ +
} + {screen.term && screen.top3.length === 0 &&

No top 3 is held for this term yet.

} + {screen.trend.length > 0 &&
+

Trend

+ +
} +
+ } +
; +} + +function AlertsView({ appId, canEditApp }: { appId: string; canEditApp: boolean }) { + const url = useAppQuery(appId); + const [state, setState] = useState>({ phase: "loading" }); + const [attempt, setAttempt] = useState(0); + const [saving, setSaving] = useState(""); + const [rowError, setRowError] = useState<{ id: string; message: string } | null>(null); + const alive = useRef(true); + useEffect(() => () => { alive.current = false; }, []); + + useEffect(() => { + const controller = new AbortController(); + setState({ phase: "loading" }); + void fetch(url("/alerts"), { credentials: CREDENTIALS, signal: controller.signal }) + .then((response) => readJson(response, "Alerts could not be loaded.")) + .then((body) => { if (!controller.signal.aborted) setState({ phase: "ready", value: parseAlerts(body) }); }) + .catch((reason: unknown) => { + if (!controller.signal.aborted) setState({ phase: "error", + message: reason instanceof Error ? reason.message : "Alerts could not be loaded." }); + }); + return () => controller.abort(); + }, [url, attempt]); + + const setStatus = async (alert: Alert, status: string) => { + setSaving(alert.id); + setRowError(null); + try { + const response = await fetch(url(`/alerts/${encodeURIComponent(alert.id)}`), { + method: "PATCH", credentials: CREDENTIALS, headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ status }), + }); + // The route answers with the saved alert itself. + const body = await readJson(response, "The status was not saved.") as { alert?: unknown } | null; + const saved = parseAlert(body?.alert ?? body); + if (!alive.current) return; + setState((current) => current.phase !== "ready" ? current : { phase: "ready", value: { + ...current.value, + alerts: current.value.alerts.map((row) => row.id === alert.id ? (saved ?? { ...row, status }) : row), + } }); + } catch (reason) { + if (alive.current) setRowError({ id: alert.id, + message: reason instanceof Error ? reason.message : "The status was not saved." }); + } finally { + if (alive.current) setSaving(""); + } + }; + + if (state.phase === "loading") return
+
+
; + if (state.phase === "error") return

{state.message}{" "} +

; + const { alerts, checks } = state.value; + const canEdit = canEditApp && state.value.canEdit; + const notChecked = checks.length > 0 &&
+

Not checked

+
    + {checks.map((check, index) =>
  • + {[check.term, check.asin, check.trigger].filter(Boolean).join(", ")} + {check.cause} +
  • )} +
+
; + if (!alerts.length) return

No alerts yet.

{notChecked}
; + return
+ + + + + + {alerts.map((alert) => + + + + + + + )} + +
TermASINTriggerOwnerStatusDue
{alert.term}{alert.asin}{alert.trigger} + {alert.cause && {alert.cause}}{alert.owner} + {canEdit ? : {alert.status}} + {rowError?.id === alert.id && {rowError.message}} + {alert.due ? shortDate(alert.due) : "Not set"}
{notChecked}
; +} + +export default function CompetitorWatchView({ application }: { application: CompetitorWatchApplication }) { + const [tab, setTab] = useState<"terms" | "alerts">("terms"); + const canEditApp = application.capabilities?.edit === true; + const tabs = [["terms", "Terms"], ["alerts", "Alerts"]] as const; + return
+ + {tabs.map(([key, label]) => )} +
}> +
+ {tab === "terms" + ? + : } +
+ +
; +} diff --git a/web/src/customer-grid/AmazonPeriodPicker.tsx b/web/src/customer-grid/AmazonPeriodPicker.tsx new file mode 100644 index 0000000000000000000000000000000000000000..87a3321141fa9b6babe5e6fd72f9ab50d3da7306 --- /dev/null +++ b/web/src/customer-grid/AmazonPeriodPicker.tsx @@ -0,0 +1,82 @@ +import { useCallback, useEffect, useState } from "react"; +import { API_V1, CREDENTIALS, ROWS_STALE_EVENT, signal } from "../apiContract"; +import { clearTopicRowsCache, dropTableRowsCache, refusalMessage } from "./apiBridge"; +import "./amazonPeriodPicker.css"; + +/** The table the period picker belongs to. Every other database renders nothing. */ +export const AMAZON_PROFIT_TABLE = "ut_amazon_sku_profit"; + +type PeriodChoice = { key: string; label: string }; +type ProfitStatus = { + period: { key: string; label: string }; + periods: PeriodChoice[]; + canEdit: boolean; + refusal: string | null; +}; + +/** Period picker for Amazon SKU profit: switching it recomputes every row on the server, keeps + * every owned cell, then re-reads this grid's rows. A viewer sees the period and cannot change it. */ +export default function AmazonPeriodPicker({ database }: { database: string }) { + const [status, setStatus] = useState(null); + const [busy, setBusy] = useState(false); + const [message, setMessage] = useState(null); + const active = database === AMAZON_PROFIT_TABLE; + + const load = useCallback(async () => { + const res = await fetch(`${API_V1}/amazon-profit`, { credentials: CREDENTIALS }); + if (!res.ok) return; + setStatus((await res.json()) as ProfitStatus); + }, []); + + useEffect(() => { + if (active) void load(); + }, [active, load]); + + if (!active || !status) return null; + + const choose = async (key: string) => { + setBusy(true); + setMessage(null); + try { + const res = await fetch(`${API_V1}/amazon-profit/period`, { + method: "POST", credentials: CREDENTIALS, + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ period: key }), + }); + if (!res.ok) { + const body: unknown = await res.json().catch(() => null); + setMessage(refusalMessage(body, res.status, "The period did not change. Try again.")); + } + // A refused tie still rewrote the rows (blank figures, the refusal in Period), so re-read. + clearTopicRowsCache(`tables/${AMAZON_PROFIT_TABLE}/rows`); + dropTableRowsCache(AMAZON_PROFIT_TABLE); + signal(ROWS_STALE_EVENT); + await load(); + } finally { + setBusy(false); + } + }; + + const choices = status.periods.some((p) => p.key === status.period.key) + ? status.periods : [{ key: status.period.key, label: status.period.label }, ...status.periods]; + const note = message ?? status.refusal; + return ( +
+ + {busy && Updating} + {note && {note}} +
+ ); +} diff --git a/web/src/customer-grid/CustomerGrid.tsx b/web/src/customer-grid/CustomerGrid.tsx index 61ec8808f213b32885d6ba2940abb7f13e7d07cd..3bf17e0008d8f84dec9a347aab7a2af39e1952b1 100644 --- a/web/src/customer-grid/CustomerGrid.tsx +++ b/web/src/customer-grid/CustomerGrid.tsx @@ -86,6 +86,7 @@ import { useGetCellContent } from "./useGetCellContent"; import type { GroupRowPlaceholder } from "./useGetCellContent"; import { useGridSelection } from "./useGridSelection"; import Toolbar from "./Toolbar"; +import AmazonPeriodPicker from "./AmazonPeriodPicker"; import { DatePicker } from "./DatePicker"; /** * ⭐⭐ W51-T03 / OWNER RULING R6 + CONTRACT C8 — THE ONE CONFIRM DIALOG, for every destructive @@ -175,7 +176,7 @@ import type { ShowAs, SummaryChoice, SummaryMap } from "./types"; import type { FieldStamps } from "./optimism"; import { evalFormula, orderFormulas, parseFormula } from "./formulaEngine"; import type { FormulaAst } from "./formulaEngine"; -import { applicationTemplateSurface, CalendarSettings, CalendarView, KanbanView, ListView, MapSettings, ModeSwitch, TimelineSettings } from "./viewModes"; +import { applicationTemplateSurface, CalendarSettings, CalendarView, KanbanView, ListView, MapSettings, ModeSwitch, TemplateApplicationView, TimelineSettings } from "./viewModes"; import { SwipeView } from "./SwipeView"; import type { SwipeSpec } from "./SwipeView"; // The chart engine lives in `viz/` since EXIT wave 2 (W2-5/Y3) — the grid is now @@ -2713,8 +2714,11 @@ function CustomerGridSurface({ ? addressedApplication.application : null; const isCatalogApplication = applicationTemplateSurface(addressedDefinition) === "catalog" || (resolvedApplicationMatches && applicationTemplateSurface(resolvedApplication) === "catalog"); + // S78-T17 (C4): a Competitor watch App reads its own routes, never the source database rows. + const isCompetitorWatchApplication = applicationTemplateSurface(addressedDefinition) === "competitor_watch"; // Resolve the registry before allowing an App to use an ordinary row reader. - const legacyRowsEnabled = !isCatalogApplication && (!isApplicationSurface || !!addressedDefinition); + const legacyRowsEnabled = !isCatalogApplication && !isCompetitorWatchApplication && + (!isApplicationSurface || !!addressedDefinition); const catalogReadKey = addressedApplicationKey || applicationRefKey; const [catalogRead, setCatalogRead] = useState<{ key: string; fields: Field[]; rows: Row[] }>( { key: "", fields: [], rows: [] }); @@ -11286,6 +11290,10 @@ function CustomerGridSurface({ /> ) : null; + if (isCompetitorWatchApplication && addressedDefinition) { + return ; + } + return ( // `data-today` is the TENANT'S day, exactly as the payload delivered it. It is here so the // date every relative condition resolves against is OBSERVABLE rather than inferred: a live @@ -11722,6 +11730,7 @@ function CustomerGridSurface({ )} {!routeApplicationSurface && primaryToolbar} + {!routeApplicationSurface && }
{/* ⭐ R11's SECOND ROW — live, on the pivoted database. It renders only once the pivot has actually been SERVED, which is the house posture for a control whose other half may diff --git a/web/src/customer-grid/DeliveryPhotos.tsx b/web/src/customer-grid/DeliveryPhotos.tsx new file mode 100644 index 0000000000000000000000000000000000000000..b88cedad44f33a610d9bc71f5eacf817a55b4bbb --- /dev/null +++ b/web/src/customer-grid/DeliveryPhotos.tsx @@ -0,0 +1,138 @@ +// customer-grid / DeliveryPhotos.tsx +// S78-T14 (contract C5): the proof-of-delivery photos of one Odoo picking, in the record drawer. +// No photos renders nothing. Loading renders nothing. A failed load says so in one sentence. +// The photo bytes come from the server, which checks the delivery read wall on every request. + +import { useEffect, useId, useRef, useState } from "react"; +import { BodyPortal, useOverlayLayer } from "./OverlaySurface"; +import { API_V1, CREDENTIALS, UNAUTHORIZED_EVENT, checkTenant, signal } from "../apiContract"; + +/** The databases whose row id is a `stock.picking` id. Mirrors `odoo_relational.DELIVERY_KEYS`. */ +export const DELIVERY_SCOPES: ReadonlySet = new Set(["ut_odoo_shipments", "ut_odoo_pickings"]); + +export interface DeliveryPhoto { + id: number; + name: string; + mimetype: string; + url: string; +} + +type Load = + | { status: "loading" } + | { status: "ok"; photos: DeliveryPhoto[] } + | { status: "error"; message: string }; + +const LOAD_FAILED = "Delivery photos could not load. Reopen the record to try again."; + +function readPhotos(body: unknown): DeliveryPhoto[] | null { + if (!Array.isArray(body)) return null; + const photos: DeliveryPhoto[] = []; + for (const row of body) { + const item = row as Partial | null; + if (!item || typeof item.id !== "number" || typeof item.url !== "string") return null; + photos.push({ id: item.id, name: String(item.name ?? ""), mimetype: String(item.mimetype ?? ""), + url: item.url }); + } + return photos; +} + +function PhotoView({ photo, index, count, onClose }: { + photo: DeliveryPhoto; index: number; count: number; onClose: () => void; +}) { + const panelRef = useRef(null); + const titleId = useId(); + const [shown, setShown] = useState(false); + useOverlayLayer({ panelRef, onDismiss: onClose, dismissOnOutside: false, + initialFocus: "first", restoreFocus: true, trapFocus: true }); + useEffect(() => { + const frame = window.requestAnimationFrame(() => setShown(true)); + return () => window.cancelAnimationFrame(frame); + }, []); + const label = `Delivery photo ${index + 1} of ${count}`; + return ( + +
+
+
+

{label}

+ +
+ {label} +
+
+
+ ); +} + +export function DeliveryPhotos({ scope, pid }: { scope: string; pid: number }) { + const [load, setLoad] = useState({ status: "loading" }); + const [open, setOpen] = useState(null); + + useEffect(() => { + if (!Number.isInteger(pid) || pid <= 0) { + setLoad({ status: "ok", photos: [] }); + return; + } + const ctrl = new AbortController(); + setLoad({ status: "loading" }); + setOpen(null); + (async () => { + try { + const res = await fetch(`${API_V1}/odoo/deliveries/${pid}/photos`, { + credentials: CREDENTIALS, signal: ctrl.signal, + }); + if (checkTenant(res)) return; + if (res.status === 401) { + signal(UNAUTHORIZED_EVENT); + return; + } + let body: unknown = null; + try { body = await res.json(); } catch { body = null; } + if (ctrl.signal.aborted) return; + if (!res.ok) { + const message = (body as { error?: { message?: unknown } } | null)?.error?.message; + setLoad({ status: "error", message: typeof message === "string" && message ? message : LOAD_FAILED }); + return; + } + const photos = readPhotos(body); + setLoad(photos ? { status: "ok", photos } : { status: "error", message: LOAD_FAILED }); + } catch { + if (!ctrl.signal.aborted) setLoad({ status: "error", message: LOAD_FAILED }); + } + })(); + return () => ctrl.abort(); + }, [scope, pid]); + + if (load.status === "loading") return null; + if (load.status === "ok" && load.photos.length === 0) return null; + const photos = load.status === "ok" ? load.photos : []; + const current = open == null ? null : photos[open] ?? null; + + return ( +
+
Delivery photos
+ {load.status === "error" ? ( +
{load.message}
+ ) : ( +
+ {photos.map((photo, index) => ( + + ))} +
+ )} + {current && open != null && ( + setOpen(null)} /> + )} +
+ ); +} diff --git a/web/src/customer-grid/MapView.tsx b/web/src/customer-grid/MapView.tsx index 161bce515e10caab3ea9a0748826312ed1c23492..e44cb4a9fc5eebc607254f66de0438514fe4e0a4 100644 --- a/web/src/customer-grid/MapView.tsx +++ b/web/src/customer-grid/MapView.tsx @@ -41,8 +41,8 @@ // it always drew rather than an empty rectangle. // // Honesty rules (rule 8b), carried and extended: -// - rows WITHOUT coordinates are never silently hidden — a chip says -// "N matching records have no location"; +// - rows WITHOUT coordinates are never silently hidden: the count line reads +// "N of M mapped", so the unmapped share is the gap between the two numbers; // - a pin per row at its geocoded position, no clustering and no jitter (two // customers at one address overlap rather than being drawn where they are // not); @@ -103,7 +103,7 @@ import { } from "./mapProjection"; import type { GeoStop, Pt, RouteStamp, View } from "./mapProjection"; import { LP_INK, LP_MAP_SELECTED, LP_PURPLE, LP_PURPLE_DEEP, LP_RED, LP_RED_DEEP, LP_ZONE_FILL_OPACITY, LP_ZONE_STROKE_WIDTH } from "./theme"; -import { RouteNav } from "./RouteNav"; +import { GoogleMapsMark, RouteNav } from "./RouteNav"; import { endpointRoutePlan, normalizedRouteMapLinks } from "./routeLinks"; import { fetchRouteDeliveryRecipients, @@ -845,6 +845,8 @@ function MapViewSurfaceContent({ const [zones, setZones] = useState(null); /** C2 named-route read model. */ const [namedRoutes, setNamedRoutes] = useState([]); + // `[]` cannot tell "not read yet" from "none": RouteNav says Loading until the first read answers. + const [routesLoaded, setRoutesLoaded] = useState(false); const [routeFolders, setRouteFolders] = useState([]); const [routeDeliveryRecipients, setRouteDeliveryRecipients] = useState(null); const [deliveryRecipientError, setDeliveryRecipientError] = useState(null); @@ -1254,7 +1256,6 @@ function MapViewSurfaceContent({ } return out; }, [mapRows, field.key, coordinateFields, coordField, sessionCoordinates, colorField, sizeField]); - const noCoords = mapRows.length - points.length; /** * W46-T26 — WHAT EACH STOP IS CALLED. The Route panel used to print a bare ordinal, so the * list said nothing about who the driver is visiting. The name lives on the row, not on the @@ -1344,10 +1345,12 @@ function MapViewSurfaceContent({ const body = res.ok ? await res.json().catch(() => null) : null; if (!routingAlive.current) return; if (routeRequestRef.current.generation !== generation) return; + setRoutesLoaded(true); if (Array.isArray(body?.routes)) setNamedRoutes(body.routes); if (Array.isArray(body?.folders)) setRouteFolders(body.folders); } catch { if (controller.signal.aborted) return; + setRoutesLoaded(true); /* Keep the last honest Route navigation when a refresh is temporarily unavailable. */ } }, [routingFetch]); @@ -3707,8 +3710,6 @@ function MapViewSurfaceContent({ {geocodeSessionOnly ? "No records with a location to map yet." : "No records with a location to map yet. Set latitude and longitude fields in View schema to plot records."} - {noCoords > 0 && - ` ${noCoords.toLocaleString()} matching record${noCoords === 1 ? "" : "s"} have no location.`} {/* The way OUT of the empty state, offered from inside it. Selecting records and looking up their addresses is the one action that turns this screen into a map. */} {geoControl &&
{geoControl}
} @@ -3809,19 +3810,8 @@ function MapViewSurfaceContent({ row the host geocoded, and a lone "1,402" silently redefines the toolbar's 1,550 ([[no-unverifiable-aggregates]]). */} - - {points.length.toLocaleString()} of{" "} - {mapRows.length.toLocaleString()} mapped · one pin per record that has a location - + {points.length.toLocaleString()} of {mapRows.length.toLocaleString()} mapped - {noCoords > 0 && ( - - {noCoords.toLocaleString()} matching record{noCoords === 1 ? " has" : "s have"} no location - - )} {routeSelectionWarning && !routePanelOpen && (
- )} - {!dragging && timerSaving?.costs && ( -

+ {/* S78-T08: a drag hides the apply block but keeps its box, so the stop list under the + pointer does not jump up when a stop is picked up. */} +

+ {timerSaving?.offer && applyActionReason === null &&

+ Reordering {unlocated.length ? "the located stops" : "these stops"} saves {formatSeconds(timerSaving.seconds)}, about{" "} + {Math.abs(timerSaving.percent)}% of the drive. +

} + + {applyActionReason && applyActionReason !== editActionReason && ( + + {applyActionReason} + + )} +
+ {timerSaving?.costs && ( +

Reordering {unlocated.length ? "the located stops" : "these stops"} would add {formatSeconds(timerSaving.seconds)} on the road, so the current located order is the faster one.

@@ -1242,7 +1282,7 @@ function RouteNavContent({ routes, folders, selectedIds, activeRouteId, sources, disabled={editActionReason !== null} title={editActionReason || undefined} onClick={() => finish(onUpdate(route, { mapsUrl: editMapsUrl.trim(), zoneId: editZoneId || null, color: editColor }))}> - Save Route + Save
+ {/* During a stop move the busy reason stays announced but takes no line, so the list holds. */} {editActionReason && ( - + {editActionReason} )} @@ -1278,12 +1320,19 @@ function RouteNavContent({ routes, folders, selectedIds, activeRouteId, sources, draggable={canEdit} onDragStart={(event) => { if (!canEdit) return; + // S78-T08: a name tip opened by the press closes before the row moves. + const main = event.currentTarget.querySelector(".cg-view-main"); + main?.dispatchEvent(new PointerEvent("pointerleave")); + if (main && document.activeElement === main) main.blur(); event.dataTransfer.effectAllowed = "move"; try { event.dataTransfer.setData("text/plain", String(pid)); } catch { /* Some browsers refuse a payload; the drag still works. */ } beginStopDrag(pid); }} - onDragEnd={() => endStopDrag()} + onDragEnd={() => { + endStopDrag(); + setTimerHold((current) => current && !current.committed ? null : current); + }} onDragOver={(event) => { if (dragPid === null) return; event.preventDefault(); @@ -1307,7 +1356,6 @@ function RouteNavContent({ routes, folders, selectedIds, activeRouteId, sources, {/* ⛔ OWNER 2026-08-31, TWICE: *"the < > for the route panel where you can choose @@ -1438,7 +1489,7 @@ function RouteNavContent({ routes, folders, selectedIds, activeRouteId, sources, {route.mapsUrl && !mapsUrl && Maps link is unavailable.}
- +

Current ordered Route stops, split into links of at most 10 stops.

{shownMapLinkRefusal ? ( @@ -1577,7 +1628,7 @@ function RouteNavContent({ routes, folders, selectedIds, activeRouteId, sources, {/* ⭐⭐ D-599 / W46-T22 - THE ROUTE'S ZONE. Until wave 46's hotfix no route record carried a zone at all, so this control had nothing to write and did not exist: Routes and Zones were two separate tables and `__zone__` was a field on a CUSTOMER - row. It saves through the same Save Route button as the Maps URL beside it, + row. It saves through the same Save button as the Maps URL beside it, because both are properties of the Route and two save buttons on one form is how a user loses the edit they did not press. ⚠ THE UNAVAILABLE ARM IS DELIBERATE. A Route can hold a zone this reader cannot @@ -2017,7 +2068,7 @@ function RouteNavContent({ routes, folders, selectedIds, activeRouteId, sources, title={busy ? "Wait for the Route update to finish." : undefined} onClick={() => { setBusy(true); void onShare(createdRoute).then((message) => { setError(message); if (!message) onCreatedDismiss(); - }).finally(() => setBusy(false)); }}>Share route + }).finally(() => setBusy(false)); }}>Share @@ -2052,7 +2103,8 @@ function RouteNavContent({ routes, folders, selectedIds, activeRouteId, sources,
{rows} {!rows.length &&

- {query ? "No Route matches that search." + {!routesLoaded ? "Loading routes." + : query ? "No Route matches that search." : "No routes yet. Create one from selected Zones or records."}

}
diff --git a/web/src/customer-grid/ViewSidebar.tsx b/web/src/customer-grid/ViewSidebar.tsx index 52b7098dd42bce0d903c785ea334d7772d5e1878..22ede3c1070c74479919b6dec0aa857fe307454e 100644 --- a/web/src/customer-grid/ViewSidebar.tsx +++ b/web/src/customer-grid/ViewSidebar.tsx @@ -682,6 +682,13 @@ function dataNavigatorLevelKey(level: DataNavigatorLevel): string { return `database:${level.key}`; } +/** S78-T02 (C1): the Amazon nav facts `/nav` serves. Optional, so a row without them is unchanged. */ +type RailDatabase = DataDatabase & { + group?: string; + /** `at` null is a read "never synced"; an absent `sync` is unknown and shows nothing. */ + sync?: { at: string | null; empty: boolean | null }; +}; + /** Windows-style Data path rendered with the same segmented-path anatomy as the View path. */ function DataDatabaseNavigator({ databases, @@ -697,6 +704,49 @@ function DataDatabaseNavigator({ header, children, }: DataDatabaseNavigatorProps) { + // S78-T02: declared inside the navigator, because verify_folders runs this function alone in a vm. + /** C1's group order under the Amazon level. ⛔ A person reads these: no dash, sentence case. */ + const AMAZON_RAIL_GROUPS = ["Sales", "Fees and payouts", "Inventory", "Listings", "Advertising"]; + + /** A table the mirror reads as 0 rows leaves the rail; Fields and Settings still list it. */ + function railListed(database: DataDatabase, activeKey: string): boolean { + return (database as RailDatabase).sync?.empty !== true || database.key === activeKey; + } + + /** The muted second line of an Amazon row, in plain words, or null when the fact is unknown. */ + function railSyncLine(database: DataDatabase): string | null { + const sync = (database as RailDatabase).sync; + if (!sync) return null; + if (sync.at === null) return "Never synced"; + const at = new Date(sync.at); + if (Number.isNaN(at.getTime())) return null; + return `Synced ${at.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric" })}`; + } + + /** The five groups in order, each with its databases, or null when no database has a group. */ + function railGroupSections( + listed: readonly DataDatabase[] + ): { sections: { label: string; databases: DataDatabase[] }[]; ungrouped: DataDatabase[] } | null { + const grouped = new Map(); + const ungrouped: DataDatabase[] = []; + for (const database of listed) { + const group = (database as RailDatabase).group; + if (group && AMAZON_RAIL_GROUPS.includes(group)) { + grouped.set(group, [...(grouped.get(group) ?? []), database]); + } else { + ungrouped.push(database); + } + } + if (grouped.size === 0) return null; + return { + sections: AMAZON_RAIL_GROUPS.filter((label) => grouped.has(label)).map((label) => ({ + label, + databases: grouped.get(label) ?? [], + })), + ungrouped, + }; + } + const [draggedDatabase, setDraggedDatabase] = useState(null); const [folderMenu, setFolderMenu] = useState<{ folderId: string; anchor: HTMLElement } | null>( null @@ -732,8 +782,9 @@ function DataDatabaseNavigator({ : {}; const sourceGroups = useMemo( - () => groupDataDatabases(databases.filter((database) => !database.folderId)), - [databases] + () => groupDataDatabases(databases.filter((database) => + !database.folderId && railListed(database, activeDatabaseKey))), + [databases, activeDatabaseKey] ); const parentOfDatabase = (database: DataDatabase): DataNavigatorLevel => database.folderId @@ -844,6 +895,7 @@ function DataDatabaseNavigator({ const pathDatabaseRow = (database: DataDatabase) => { const active = database.key === activeDatabaseKey; const draggable = !!onMove && !database.protected && database.managedBy !== "loopable"; + const syncLine = railSyncLine(database); const content = ( <> {/* ⭐⭐ W53-T35 / C4 (owner instruction 26) — THE WRAPPER IS THE FIX, and the mark is @@ -864,7 +916,15 @@ function DataDatabaseNavigator({ - {database.label} + {syncLine ? ( + // S78-T02 (R11): the true last sync as a quiet second line, the view rows' own pair. + + {database.label} + {syncLine} + + ) : ( + {database.label} + )} {database.locked ? ( !dataNeedle || label.toLowerCase().includes(dataNeedle); - const levelDatabases = ( + const levelMatches = ( dataLevel.kind === "origin" ? sourceGroups.find((group) => group.label === dataLevel.label)?.databases ?? [] : dataLevel.kind === "folder" @@ -947,6 +1007,25 @@ function DataDatabaseNavigator({ (dataLevel.kind === "root" || dataLevel.kind === "folder") && (folder.parent ?? null) === (dataLevel.kind === "folder" ? dataLevel.id : null) && matchesNeedle(folder.name)); + // S78-T02 (C1): only the Amazon level groups by job. Every other origin stays one flat list. + const railSections = dataLevel.kind === "origin" + && sourceGroups.find((group) => group.label === dataLevel.label)?.connector === "amazon_sp" + ? railGroupSections(levelMatches) + : null; + // The flat list keeps what no group claims, so a grouped database is drawn exactly once. + const levelDatabases = railSections ? railSections.ungrouped : levelMatches; + // ⛔ A static heading, never a fold: the Data rail is list-replacing (verify_w44 refuses a + // disclosure tree), so a group is the rail's existing quiet section, `.cg-rail-section`. + const railGroupRow = (section: { label: string; databases: DataDatabase[] }) => ( +
+
+ {section.label} +
+ {section.databases.map(pathDatabaseRow)} +
+ ); const pathFolderRow = (folder: DataDatabaseFolder) => { const members = databases.filter((database) => database.folderId === folder.id); const childFolders = folders.filter((child) => child.parent === folder.id); @@ -1081,10 +1160,11 @@ function DataDatabaseNavigator({ {...dropProps(dataLevel.kind === "folder" ? dataLevel.id : null)}> {levelFolders.map(pathFolderRow)} {levelDatabases.map(pathDatabaseRow)} + {railSections?.sections.map(railGroupRow)} {/* The empty line must say WHY it is empty: under a live query "No databases in this folder" is simply false, and a wrong empty state on the control this ticket just un-hid is what QA reopens. */} - {levelDatabases.length === 0 && levelFolders.length === 0 ? ( + {levelMatches.length === 0 && levelFolders.length === 0 ? (
{dataNeedle ? "No matches." : "No databases in this folder."}
diff --git a/web/src/customer-grid/amazonPeriodPicker.css b/web/src/customer-grid/amazonPeriodPicker.css new file mode 100644 index 0000000000000000000000000000000000000000..4fe874315cbecbd8dc775c9282149c723079cf45 --- /dev/null +++ b/web/src/customer-grid/amazonPeriodPicker.css @@ -0,0 +1,3 @@ +.cg-period-picker { display: flex; align-items: center; gap: 8px; padding: 4px 12px; font-size: 13px; } +.cg-period-picker label { display: flex; align-items: center; gap: 6px; } +.cg-period-busy, .cg-period-note { color: var(--muted, #6b7280); } diff --git a/web/src/customer-grid/dataPath.ts b/web/src/customer-grid/dataPath.ts index 555cf4e3234085bf78cab7832ffdc071f6b084f7..15a5bea505a6b03d9274f11bf34551314206dcc0 100644 --- a/web/src/customer-grid/dataPath.ts +++ b/web/src/customer-grid/dataPath.ts @@ -23,6 +23,8 @@ export interface DataDatabase { icon?: FolderIcon; folderId?: string; externalHref?: string; + group?: string; + sync?: { at: string | null; empty: boolean | null }; } export interface DataDatabaseFolder { diff --git a/web/src/customer-grid/map.css b/web/src/customer-grid/map.css index 51d025d6ef07286ad86bb4a82e4935c1d36eb355..bed4a1ede5ee9da9cdc55dc35d7d4d18dd074d89 100644 --- a/web/src/customer-grid/map.css +++ b/web/src/customer-grid/map.css @@ -266,7 +266,11 @@ measured at the narrow breakpoint as rowW 471 against a 420px panel, `overflowsPanel: true` on every row, in the lane's own `stoprow_1000.json`. With the item allowed to shrink, the name's ellipsis engages and the row stays on ONE line, which is what the ticket asked for. */ -.cg-w44-route-stops li { display: flex; align-items: center; flex-wrap: wrap; gap: 4px 8px; min-width: 0; } +/* S78-T08: the row never wraps, so a long name cannot push the X onto a second line. The open + button starts from a zero basis and shrinks, and only an order error claims its own line. */ +.cg-w44-route-stops li { display: flex; align-items: center; flex-wrap: nowrap; gap: 4px 8px; min-width: 0; } +.cg-w44-route-stops li:has(> .cg-w44-route-order-error) { flex-wrap: wrap; } +.cg-w44-route-stops li > .cg-view-main { flex: 1 1 0; min-width: 0; } .cg-w44-route-stop-open { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } /* `margin-left: auto` pins the controls to the trailing edge, so every row's Move button starts on the same x no matter how long the record name is. */ @@ -282,7 +286,7 @@ .cg-w44-route-position-apply { padding: 4px 6px; } .cg-w44-route-step:hover:not(:disabled), .cg-w44-route-position-apply:hover:not(:disabled) { background: var(--lp-blue-tint); } .cg-w44-route-step:disabled, .cg-w44-route-position-apply:disabled { opacity: 0.55; cursor: not-allowed; } -.cg-w44-route-order-error { grid-column: 1 / -1; color: var(--lp-red-deep); font: 500 var(--lp-fs-3xs)/1.4 Inter, system-ui, sans-serif; } +.cg-w44-route-order-error { flex: 1 0 100%; grid-column: 1 / -1; color: var(--lp-red-deep); font: 500 var(--lp-fs-3xs)/1.4 Inter, system-ui, sans-serif; } .cg-w44-route-label, .cg-w44-route-sources { display: grid; gap: 4px; min-width: 0; color: var(--cg-text); font: 500 var(--lp-fs-3xs)/1.4 Inter, system-ui, sans-serif; } .cg-w44-route-sources { margin: 0; padding: 6px; border: 1px solid var(--lp-line); border-radius: var(--lp-r-sm); } .cg-w44-route-sources label { display: flex; gap: 5px; align-items: center; } diff --git a/web/src/customer-grid/viewModes.tsx b/web/src/customer-grid/viewModes.tsx index b0d3630f6dc445ee99c43a8a0462b01c4c77bbbf..e2c0e8550d655a23e3f7d5c0d5d5d522cd66e8ad 100644 --- a/web/src/customer-grid/viewModes.tsx +++ b/web/src/customer-grid/viewModes.tsx @@ -27,8 +27,9 @@ // implementation. ⛔ The SERVER window is a different thing and is untouched. // --------------------------------------------------------------------------- -import { memo, useEffect, useMemo, useRef, useState } from "react"; +import { lazy, memo, Suspense, useEffect, useMemo, useRef, useState } from "react"; import type { CSSProperties, DragEvent } from "react"; +import type { CompetitorWatchApplication } from "../apps/competitor-watch/CompetitorWatchView"; // ⭐⭐ W41-T48 (owner instruction 22) — "touching a record in ANY view, not only Grid, // minimizes the main navigation". All three modes in this file open a record, and each now // raises the SAME public event `CustomerGrid.tsx::onCellClicked` raises, so the shell folds @@ -60,20 +61,32 @@ import { useScrollBox, windowRange } from "./GalleryTimelineViews"; // wave52-owned sheet instead of being added there. import "../styles/wave52/timeline.css"; -/** The two template-backed surfaces which may be launched through the ordinary Map mode. +/** The template-backed surfaces. Routing and Catalog launch through the ordinary Map mode; + * Competitor watch mounts its own view (C4). * The registry object is the discriminator: a name, hash fragment, or client-side template * draft is never allowed to choose the renderer after the Application has been saved. */ -export type ApplicationTemplateSurface = "route" | "catalog" | null; +export type ApplicationTemplateSurface = "route" | "catalog" | "competitor_watch" | null; export function applicationTemplateSurface(application: unknown): ApplicationTemplateSurface { if (!application || typeof application !== "object" || Array.isArray(application)) return null; const row = application as { templateKind?: unknown; fieldMap?: unknown }; if (!row.fieldMap || typeof row.fieldMap !== "object" || Array.isArray(row.fieldMap)) return null; - return row.templateKind === "route" || row.templateKind === "catalog" + return row.templateKind === "route" || row.templateKind === "catalog" || row.templateKind === "competitor_watch" ? row.templateKind : null; } +// C4: the Competitor watch chunk loads only when such an App opens. +const CompetitorWatchView = lazy(() => import("../apps/competitor-watch/CompetitorWatchView")); + +/** C4: the screen of a template App that mounts its own view instead of Map mode, else null. */ +export function TemplateApplicationView({ application }: { application: unknown }) { + if (applicationTemplateSurface(application) !== "competitor_watch") return null; + return + + ; +} + /** Item 4 — plain words for the aggregates. "avg" is jargon in a picker aimed at a day cell. */ const AGG_LABELS: Record = { sum: "Total", diff --git a/web/src/home/TemplatePicker.tsx b/web/src/home/TemplatePicker.tsx index 0e98a6d83d84f206c0de2847a7657b62d688c1e2..ef6d24d54c17d985278e5af2153823fe2621de99 100644 --- a/web/src/home/TemplatePicker.tsx +++ b/web/src/home/TemplatePicker.tsx @@ -71,7 +71,8 @@ export default function TemplatePicker({ initialModule = "sheet", onClose, onCre const create = async (template: PickerTemplate) => { if (applying.current) return; - if (!template.creatable || (module === "app" && !["route", "catalog"].includes(template.key))) return; + if (!template.creatable || (module === "app" && !["route", "catalog"].includes(template.key) + && template.key !== "competitor_watch")) return; if (module === "app" && usesApplicationWizard(template)) { if (onApplication) onApplication(template.key); else { @@ -96,7 +97,8 @@ export default function TemplatePicker({ initialModule = "sheet", onClose, onCre }; const templates = (sections.find((section) => section.module === module)?.templates ?? []) - .filter((template) => module !== "app" || ["route", "catalog"].includes(template.key)); + .filter((template) => module !== "app" || ["route", "catalog"].includes(template.key) + || template.key === "competitor_watch"); const availableModules = moduleOrder.filter((name) => sections.some((section) => section.module === name)); return
diff --git a/web/src/sheet/SheetPage.tsx b/web/src/sheet/SheetPage.tsx index fcd8bb593eb236c7b7fe308328f9994d5fd6fac5..3b49a149a9e28a69374ddcac22dc14435acef82d 100644 --- a/web/src/sheet/SheetPage.tsx +++ b/web/src/sheet/SheetPage.tsx @@ -12,8 +12,8 @@ // the Data module's loading animation, and the rail must be the Data rail. So this page now owns // three states rather than two, and the third is the one that was missing: // -// loading the rail draws `.shell-rail-skeleton*` and the main area draws `.cg-loading` -// with `lp-spin lp-spin--lg`. NO SENTENCE. "Opening your workbook." was the +// loading the rail draws `.shell-rail-skeleton*` and the main area draws `SheetLoading`, +// a labelled row skeleton. NO SENTENCE. "Opening your workbook." was the // defect: a line of prose where the rest of the app shows a skeleton. // refused `loading` is over and nothing opened, so the reason is stated as a plain // sentence naming its subject (R16). diff --git a/web/src/shell/Shell.tsx b/web/src/shell/Shell.tsx index 2b62c499dff59ae55df4d6187d14fc39d05465a6..7b7f3a6feea2dc38bce64317e387e1bf00a2fb3d 100644 --- a/web/src/shell/Shell.tsx +++ b/web/src/shell/Shell.tsx @@ -747,7 +747,14 @@ function ApplicationWizard({ onClick={() => chooseTemplate(candidate)} > - {candidate.kind === "route" ? : } + {candidate.kind === "route" ? + : candidate.kind === "catalog" ? : ( + // Any other kind: stacked sheets, the Templates mark. Inline so verify_app_wizard's extract runs it. + + )} {candidate.label} @@ -764,7 +771,8 @@ function ApplicationWizard({ setFieldMap({}); }}> - {databases.map((database) => )} + {databases.filter((database) => !template?.sourceDatabase || database.key === template.sourceDatabase) + .map((database) => )} {schema.phase === "loading" ? Loading database fields... : null} {schema.phase === "error" ? @@ -2530,6 +2538,8 @@ function ShellFrame() { label: entry.label, ...(entry.source ? { source: entry.source } : {}), ...(entry.sourceGroup ? { source: entry.sourceGroup } : {}), + ...(entry.group ? { group: entry.group } : {}), + ...(entry.sync ? { sync: entry.sync } : {}), ...(entry.managedBy ? { managedBy: entry.managedBy } : {}), ...(entry.protected ? { protected: true } : {}), ...(entry.locked ? { locked: true } : {}), @@ -3327,8 +3337,10 @@ function ShellFrame() { // S68-T54 (R37): Artifacts rides the SAME `analyst` grant, but it is its own rail row, so it // gets its own slot from the one rule (verify_wiring: one `railSlot(` per conditional row). const artifactsSlot: RailSlot = railSlot(!!analyst); - // Workspace follows the rail order. App, Sheet and Data are always rows. - const workspaceVisible = !railLoading && hasVisibleRailSlot([intelligenceSlot, artifactsSlot, automationSlot, "row", "row", "row"]); + // R26: Workspace holds App, Sheet and Table, which are always rows. + const workspaceVisible = !railLoading; + // R26: Workflow shows only when Brain, Artifacts or Automation has a visible slot. + const workflowVisible = !railLoading && hasVisibleRailSlot([intelligenceSlot, artifactsSlot, automationSlot]); const financeAppKey = /^#\/finance\/([^/?#]+)/.exec(hash)?.[1]; const financeCurrent = Boolean(financeAppKey); // WAVE 23 C10 — the flyout's search. A plain label substring, case-folded: this list is at @@ -3530,6 +3542,88 @@ function ShellFrame() {