File size: 15,907 Bytes
8133df9
07a68d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f462120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8133df9
 
 
 
f462120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07a68d0
f462120
8873406
f462120
 
07a68d0
 
 
 
 
 
 
 
f462120
 
07a68d0
 
f462120
 
07a68d0
 
 
8133df9
07a68d0
 
 
8133df9
 
07a68d0
f462120
 
07a68d0
 
f462120
07a68d0
 
f462120
07a68d0
 
 
 
 
f462120
 
 
8133df9
 
 
 
f462120
 
8133df9
 
 
07a68d0
f462120
 
07a68d0
 
f462120
 
 
 
 
8133df9
3fac176
8133df9
95fb175
 
 
 
 
 
8133df9
 
 
 
 
95fb175
 
 
 
 
07a68d0
f462120
 
07a68d0
 
 
8873406
 
07a68d0
 
 
8873406
07a68d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
"""C1: the shipped App template catalogue and each template's source or permission contract.

The registry's template contracts and `core.external_tools`' templateKind enum derive from or
mirror `TEMPLATES`; `verify_app_templates.py` holds all three name for name.
"""
from __future__ import annotations

import sys
from importlib import import_module
from types import MappingProxyType

PROCESSES = ("Create value", "Acquire", "Convert", "Deliver", "Retain", "Finance and cash",
             "Risk and compliance", "None")

# A C1 connector key is not a keychain entry type: Amazon SP-API is stored as `amazon_sp` (F25).
CONNECTOR_ENTRY_TYPES = MappingProxyType({"odoo": "odoo", "amazon": "amazon_sp", "amazon_ads": "amazon_ads"})


def source_connector(source):
    """Resolve a declared source without importing its product module."""
    if source.startswith("ut_amzn_ads_"):
        return "amazon_ads"
    if source.startswith("ut_amzn_"):
        return "amazon"
    if source in ("customer_data", "product_data") or source.startswith("ut_odoo_"):
        return "odoo"
    raise ValueError("The template source has no declared connector.")


def source_connectors(connector, sources, optional_sources=(), optional_connectors=()):
    """Order known connectors by primary, declared sources, then optional read features."""
    if (not isinstance(optional_connectors, (tuple, list))
            or any(not isinstance(key, str) or key not in CONNECTOR_ENTRY_TYPES
                   for key in (connector, *optional_connectors))):
        raise ValueError("The template has an unknown connector.")
    return tuple(dict.fromkeys((connector, *(source_connector(source)
                                           for source in (*sources, *optional_sources)),
                               *optional_connectors)))


def validate_connectors(row):
    """Reject unordered, unknown, duplicated or incomplete connector declarations for any row."""
    if row.get("kind") == "alerts":
        return (row.get("connector") is None and row.get("permission") == "automation"
                and all(row.get(key) == () for key in
                        ("connectors", "sources", "optional_sources", "optional_connectors")))
    connectors = row.get("connectors")
    if (not isinstance(connectors, (tuple, list)) or not connectors
            or connectors[0] != row.get("connector")
            or any(not isinstance(key, str) or key not in CONNECTOR_ENTRY_TYPES for key in connectors)
            or len(set(connectors)) != len(connectors)):
        return False
    try:
        required = source_connectors(row["connector"], row["sources"], row.get("optional_sources", ()),
                                     row.get("optional_connectors", ()))
    except (KeyError, TypeError, ValueError, AttributeError):
        return False
    return tuple(connectors) == required


def _amazon(kind, label, purpose, process, sources, *, connector="amazon", proposes=True, tools=(),
            optional_sources=(), optional_connectors=()):
    return {"kind": kind, "label": label, "purpose": purpose, "connector": connector,
            "connectors": source_connectors(connector, sources, optional_sources, optional_connectors),
            "tags": ["Amazon Ads" if connector == "amazon_ads" else "Amazon"],
            "sources": tuple(sources), "optional_sources": tuple(optional_sources),
            "optional_connectors": tuple(optional_connectors), "process": process, "mark": kind,
            "module": "modules." + kind, "router": "routes_" + kind,
            "view": kind.replace("_", "-"), "tools": tuple(tools), "proposes": proposes}


TEMPLATES = (
    # Routing and Catalog render through Map mode; their routes are mounted by main.py.
    # They read FIELD_BINDINGS over their source, so an App of either kind stores no field map.
    {"kind": "route", "label": "Routing", "purpose": "Plan delivery routes for your customers.",
     "connector": "odoo", "connectors": ("odoo",), "tags": ["Odoo"], "sources": ("customer_data",),
     "optional_sources": (), "optional_connectors": (), "process": "None", "mark": "route",
     "module": None, "router": None, "view": None, "tools": (), "proposes": False},
    {"kind": "catalog", "label": "Catalog", "purpose": "Build and print product catalogs.",
     "connector": "odoo", "connectors": ("odoo",), "tags": ["Odoo"], "sources": ("product_data",),
     "optional_sources": (), "optional_connectors": (), "process": "None", "mark": "catalog",
     "module": None, "router": None, "view": None, "tools": ("catalog_build", "catalog_pdf"),
     "proposes": False},
    _amazon("competitor_watch", "Competitor watch", "See who ranks on your search terms each week.",
            "Acquire", ("ut_amzn_brand_analytics", "ut_amzn_listings"), proposes=False),
    _amazon("ranking_diagnostic", "Ranking diagnostic", "Find why a listing lost its rank.",
            "Acquire", ("ut_amzn_pricing", "ut_amzn_catalog_items", "ut_amzn_brand_analytics",
                        "ut_amzn_economics", "ut_amzn_fba_inventory", "ut_amzn_suppressed_listings",
                        "ut_amzn_stranded_inventory", "ut_amzn_inventory_planning"), proposes=False,
            optional_sources=("ut_amzn_search_query_performance", "ut_amzn_listing_items")),
    _amazon("amazon_operator", "Operator inbox", "Approve ad changes in one batch.",
            "Acquire", ("ut_amzn_ads_sp_campaigns", "ut_amzn_ads_sb_campaigns", "ut_amzn_ads_sd_campaigns",
                        "ut_amzn_economics"), connector="amazon_ads", proposes=False),
    _amazon("restock_planner", "Restock planner", "Send stock to Amazon before it runs out.",
            "Deliver", ("ut_amzn_restock_recommendations", "ut_amzn_inventory_planning",
                        "ut_amzn_fba_inventory"), optional_connectors=("odoo",)),
    _amazon("reimbursements", "Reimbursements", "Find FBA money Amazon owes you.",
            "Create value", ("ut_amzn_reimbursements", "ut_amzn_inventory_ledger_detail",
                             "ut_amzn_fba_customer_returns", "ut_amzn_settlements"), optional_connectors=("odoo",)),
    _amazon("aged_stock", "Aged stock", "Move old stock before storage fees grow.",
            "Create value", ("ut_amzn_inventory_planning", "ut_amzn_long_term_storage_fees",
                             "ut_amzn_storage_fees", "ut_amzn_recommended_removals",
                             "ut_amzn_stranded_inventory")),
    _amazon("opportunity_finder", "Opportunity finder", "Find rising searches you do not serve yet.",
            "Create value", ("ut_amzn_brand_analytics", "ut_amzn_search_query_performance", "ut_amzn_listings"),
            optional_connectors=("odoo",)),
    _amazon("returns_refunds", "Returns and refunds", "See why buyers return and track each fix.",
            "Retain", ("ut_amzn_fba_customer_returns", "ut_amzn_fba_shipped_sales", "ut_amzn_mfn_returns",
                       "ut_amzn_settlements", "ut_amzn_transactions", "ut_amzn_order_items"),
            optional_sources=("ut_amzn_fba_inventory", "ut_amzn_removal_orders",
                              "ut_amzn_removal_shipments", "ut_amzn_storage_fees"),
            optional_connectors=("odoo",)),
    _amazon("repeat_buyers", "Repeat buyers", "See who buys again and what they buy together.",
            "Retain", ("ut_amzn_repeat_purchase", "ut_amzn_market_basket"), proposes=False,
            optional_sources=("ut_amzn_replenishment", "ut_amzn_subscribe_save_performance",
                              "ut_amzn_fba_shipped_sales", "ut_amzn_listings")),
    _amazon("payouts", "Payouts", "Tie each payout to its orders and fees.",
            "Finance and cash", ("ut_amzn_settlements", "ut_amzn_economics", "ut_amzn_transactions"),
            optional_connectors=("odoo",)),
    _amazon("account_health", "Account health", "Catch account risks before Amazon acts.",
            "Risk and compliance", ("ut_amzn_account_health",)),
    _amazon("amazon_dashboard", "Amazon dashboard", "See sales, advertising and returns for your account.",
            "None", ("ut_amzn_order_metrics", "ut_amzn_order_items", "ut_amzn_fba_customer_returns",
                     "ut_amzn_fba_shipped_sales", "ut_amzn_mfn_returns", "ut_amzn_settlements",
                     "ut_amzn_transactions", "ut_amzn_ads_sp_campaigns", "ut_amzn_ads_sb_campaigns",
                     "ut_amzn_ads_sd_campaigns"), optional_sources=("product_data",), proposes=False),
    # Project management reads its own module tables (ut_amazon_*), so it declares no
    # source: a cold mirror must not refuse an App whose rows live in the store.
    _amazon("amazon_profit", "Project management", "See profit, ads and stock for every SKU.",
            "Create value", (),
            optional_sources=("ut_amzn_search_query_performance", "ut_amzn_order_items",
                              "ut_amzn_brand_analytics", "ut_amzn_listings", "ut_amzn_catalog_items",
                              "ut_amzn_listing_items", "ut_amzn_competitor_snapshots",
                              "ut_amzn_competitor_images"),
            optional_connectors=("odoo",), proposes=False),
    {"kind": "alerts", "label": "Alerts", "purpose": "Act on changes in your databases.",
     "connector": None, "connectors": (), "permission": "automation", "tags": ["Automation"],
     "sources": (), "optional_sources": (), "optional_connectors": (), "process": "None",
     "mark": "alerts", "module": None, "router": "routes_automation", "view": "alerts",
     "tools": (), "proposes": False},
    {"kind": "reports", "label": "Reports", "purpose": "Download reports shared with you.",
     "connector": "odoo", "connectors": ("odoo",), "tags": ["Odoo"], "sources": (),
     "optional_sources": (), "optional_connectors": (), "process": "None", "mark": "reports",
     "module": "modules.reports", "router": "routes_reports", "view": "reports",
     "tools": (), "proposes": False},
)
if not all(validate_connectors(row) for row in TEMPLATES):
    raise ValueError("An App template omits a source connector.")
BY_KIND = MappingProxyType({row["kind"]: row for row in TEMPLATES})
KINDS = tuple(BY_KIND)

# SDLC 80 ships the rebuilt Apps through their existing connector, grant and source walls.
HELD_KINDS = frozenset()


def held(kind_or_app):
    """True when a template kind, or an App record's `templateKind`, has a release hold."""
    kind = kind_or_app.get("templateKind") if isinstance(kind_or_app, dict) else kind_or_app
    return isinstance(kind, str) and kind in HELD_KINDS

#: R33: the fixed role to field binding each kind reads from its first C1 source.
#: Catalog price is Royal 1 tier 1: the pooled `price_1` is not in the typed product_data schema.
FIELD_BINDINGS = MappingProxyType({
    "route": MappingProxyType({"title": "customer", "address": "street",
                               "latitude": "lat", "longitude": "lon"}),
    "catalog": MappingProxyType({"key": "code", "title": "product", "image": "image",
                                 "sku": "code", "price": "price_royal1_t1", "category": "category"}),
    "competitor_watch": MappingProxyType({"term": "search_term"}),
})
#: Kinds whose App record stores no source or field map: the registry projects C1 on read.
BOUND_KINDS = ("route", "catalog")
_BINDING_KEYS = ("sourceDatabase", "fieldMap")


def source(kind):
    """The C1 source table a kind reads, or None."""
    row = BY_KIND.get(kind)
    return row["sources"][0] if row and row["sources"] else None


def field_binding(kind):
    """A copy of the kind's fixed role to field map; empty when it names none."""
    return dict(FIELD_BINDINGS.get(kind) or {})


def bound(app):
    """Read projection: a copy of a bound kind's App with the C1 source and field map filled in.

    A stored App that still carries its own map keeps it until the S79-T04 migration.
    """
    if not isinstance(app, dict) or app.get("templateKind") not in BOUND_KINDS:
        return app
    row = dict(app)
    projection = {"sourceDatabase": source(row["templateKind"]),
                  "fieldMap": field_binding(row["templateKind"])}
    if row.get("fieldMap"):
        projection = {"sourceDatabase": row.get("sourceDatabase") or projection["sourceDatabase"]}
    row.update(projection)
    return row


def own_binding(app):
    """The binding keys a stored App carries that differ from what `bound` projects."""
    if not isinstance(app, dict) or app.get("templateKind") not in BOUND_KINDS:
        return {key: app[key] for key in _BINDING_KEYS if isinstance(app, dict) and key in app}
    kind = app["templateKind"]
    fixed = field_binding(kind)
    held = app.get("fieldMap") or {}
    if (app.get("sourceDatabase") in (None, source(kind))
            and all(fixed.get(role) == field for role, field in held.items())):
        return {}
    return {key: app[key] for key in _BINDING_KEYS if key in app}

#: Routers `app_routes.mount_app_routers` failed to mount when main.py's record is absent.
ROUTER_MOUNT_FAILURES = {}
#: Routers `app_routes.mount_app_routers` mounted or found already included.
MOUNTED_ROUTERS = set()
# A failed seam import is printed once per process and withholds its kind.
_SEAM_FAILURES = {}


def entry_type(connector):
    """The keychain entry type that proves a C1 connector key is connected."""
    return CONNECTOR_ENTRY_TYPES.get(connector, connector)


def connectors_present(session):
    """C1 connector keys this reader has. Any read failure counts as nothing connected."""
    try:
        from routes_connectors import _entry_types
        types, _scopes, failure = _entry_types(session.runtime, session.uname)
        have = set() if failure else set(types or ())
    except Exception as exc:                                            # noqa: BLE001
        from core.request_timing import raise_if_resource_error
        raise_if_resource_error(exc)
        have = set()
    if "odoo" not in have:
        try:
            from routes_keychain import env_odoo_available
            if env_odoo_available(session.runtime):
                have.add("odoo")
        except Exception:                                               # noqa: BLE001
            pass
    return {key for key in CONNECTOR_ENTRY_TYPES if entry_type(key) in have}


def connector_present(connector, session):
    return connector in connectors_present(session)


def router_failures():
    """Every recorded router mount failure: main.py's record joined with this module's own."""
    merged = {name: list(rows) for name, rows in ROUTER_MOUNT_FAILURES.items()}
    main = sys.modules.get("main")
    record = getattr(main, "ROUTER_MOUNT_FAILURES", None) if main is not None else None
    if isinstance(record, dict) and record is not ROUTER_MOUNT_FAILURES:
        for name, rows in record.items():
            merged.setdefault(name, []).extend(rows)
    return merged


def _importable(name, attribute=None):
    if name in _SEAM_FAILURES:
        return False
    try:
        module = import_module(name)
        if attribute and not hasattr(module, attribute):
            raise ImportError(f"{name} has no {attribute}")
        return True
    except Exception as exc:                                            # noqa: BLE001
        _SEAM_FAILURES[name] = type(exc).__name__
        print(f"[aios-api] APP TEMPLATE WITHHELD: {name}: {type(exc).__name__}")
        return False


def seams_ready(kind):
    """True when the kind's module imports and its router imports and never failed to mount."""
    row = BY_KIND.get(kind)
    if row is None:
        return False
    if row["module"] and not _importable(row["module"]):
        return False
    router = row["router"]
    return not router or (_importable(router, "router") and router not in router_failures())


def reset_seam_cache():
    """Forget recorded seam import failures (test fixtures only)."""
    _SEAM_FAILURES.clear()