api.scripts.shopify_catalog_reconciliation ========================================== .. py:module:: api.scripts.shopify_catalog_reconciliation .. autoapi-nested-parse:: Verification that Shopify reflects the sellable Airtable catalog. The comparison itself is read-only; once it's done, every discrepancy create/update_product can fix is queued for them automatically (see auto_fix) -- the dashboard's manual buttons still work the same. This logic was developed and validated against real production data, including two real matching bugs found and fixed along the way (see "Airtable's ``Variation.ShopifyID`` is *not* a variant id" below for one of them). Three checks, from broad to specific: 1. Every relevant Model exists as a Shopify product. 2. Every relevant Variation of a Model found in Shopify exists as a Shopify variant. 3. Every unit marked ``Statut commercial = "A vendre"`` *of a Variation flagged* ``to_sell_on_shopify`` maps to a Shopify variant whose inventory quantity matches the count of such units. A unit can be "A vendre" (for sale) through a completely different channel -- eBay, an auction, direct sale -- without ever being meant for Shopify; only Shopify-bound stock is checked against Shopify. Units in an excluded condition (``EXCLUDED_CONDITIONS``: "Abimé", "Très abimé") are never put on Shopify by create/update_product, so they aren't expected there either. "Relevant" means ``to_sell_on_shopify`` *and* currently has at least one "A vendre" unit -- a Model/Variation that's the kind of thing sold online but has zero current stock is correctly absent from Shopify, not "missing". The reverse is checked too: a Model still on Shopify that isn't ``to_sell_on_shopify`` (``shopify_products_not_a_vendre``), and a variant with Shopify stock but no "A vendre" unit behind it (``shopify_variants_not_a_vendre``). Both raise an alert email and queue update_product, which deletes the first and zeroes the second's stock. A ``to_sell_on_shopify`` Model with nothing "A vendre" stays listed at 0. Airtable's ``Variation.ShopifyID`` is *not* a variant id -- confirmed against production data, it always duplicates the parent Model's product id, even across Variations that are genuinely different Shopify variants. There is no Airtable field holding a real Shopify variant id at all. Instead, a Shopify variant is identified by its ``selectedOptions`` *values* (option names vary per product type, so matching is name-agnostic), and those literal value strings do match Airtable exactly: a Variation's own ``Variant_value`` (e.g. a size/range) plus a Unit's ``Etat esthétique`` (condition) together pin down one specific Shopify variant. One Airtable Variation therefore corresponds to *one Shopify variant per condition it has stock in*, not a single variant. Plus a documentation check on every relevant Model found in Shopify that has a "Documentation" PDF in Airtable (the source of truth): is its product's "PDF Descriptif" metafield missing, or different from Airtable's current attachment (per the companion source-id metafield -- see api/scripts/_product_documentation.py)? And the to-do list upstream of that: every relevant Model -- on Shopify yet or not -- with no PDF in its Airtable "Documentation" field at all. Plus a historical diff of Model/Variation ids against the previous run, so catalog growth over time is an explicit metric. Attributes ---------- .. autoapisummary:: api.scripts.shopify_catalog_reconciliation.logger api.scripts.shopify_catalog_reconciliation.DOCUMENTATION_DETAIL_KEYS api.scripts.shopify_catalog_reconciliation.NOT_A_VENDRE_DETAIL_KEYS api.scripts.shopify_catalog_reconciliation.UPDATE_VARIATION_DETAIL_KEYS api.scripts.shopify_catalog_reconciliation.UPDATE_MODEL_DETAIL_KEYS Classes ------- .. autoapisummary:: api.scripts.shopify_catalog_reconciliation.ShopifyCatalogReconciliation Functions --------- .. autoapisummary:: api.scripts.shopify_catalog_reconciliation.find_price_issues api.scripts.shopify_catalog_reconciliation.find_listing_issues api.scripts.shopify_catalog_reconciliation.build_result api.scripts.shopify_catalog_reconciliation.attach_airtable_urls api.scripts.shopify_catalog_reconciliation.attach_creation_attempts api.scripts.shopify_catalog_reconciliation.attach_update_attempts api.scripts.shopify_catalog_reconciliation.attach_documentation_attempts api.scripts.shopify_catalog_reconciliation.alert_not_a_vendre api.scripts.shopify_catalog_reconciliation.auto_fix Module Contents --------------- .. py:data:: logger .. py:function:: find_price_issues(shopify_units: list[api.domain.catalog.Unit], variations_by_id: dict[str, api.domain.catalog.Variation], products: list[api.integrations.shopify.catalog.ShopifyProduct], stocked_variant_ids: set[str]) -> dict[str, list[dict[str, Any]]] Prices at 0 (or empty/invalid) on stock meant to be sold: A vendre units that create/update_product would refuse to list, and Shopify variants that Airtable has A vendre stock for (``stocked_variant_ids``). A 0 € variant Airtable has no stock for is correct -- the sales team zeroes the price when stock runs out -- and archived products aren't orderable at all. .. py:function:: find_listing_issues(models: list[api.domain.catalog.Model], variations_by_id: dict[str, api.domain.catalog.Variation], units: list[api.domain.catalog.Unit], listings: list[api.domain.catalog.ShopifyListing], products: list[api.integrations.shopify.catalog.ShopifyProduct], dev_store_matches: dict[str, api.integrations.shopify.catalog.ShopifyProduct]) -> dict[str, list[dict[str, Any]]] Everything that breaks "one Model = one Annonces Shopify = one Shopify product": duplicates (e.g. a create that ran twice), Shopify products Airtable doesn't know about, and units reaching several listings. .. py:function:: build_result(models: list[api.domain.catalog.Model], variations: list[api.domain.catalog.Variation], units: list[api.domain.catalog.Unit], listings: list[api.domain.catalog.ShopifyListing], products: list[api.integrations.shopify.catalog.ShopifyProduct], previous_snapshot: dict[str, Any] | None) -> tuple[api.scripts.base.ScriptResult, dict[str, Any]] .. py:data:: DOCUMENTATION_DETAIL_KEYS :value: ('documentation_missing_in_airtable', 'documentation_missing_in_shopify',... .. py:function:: attach_airtable_urls(details: dict[str, Any]) -> None Best-effort: add a ``url`` key to every ``{id, name}`` entry in ``details``. Never raises -- ``record_url`` already degrades to ``None`` on failure (e.g. a token that loses schema-read access), and any unexpected shape issue here is caught too: a cosmetic feature must never fail the run. .. py:function:: attach_creation_attempts(details: dict[str, Any], store: api.worker.run_store.RunStore) -> None Best-effort: for each entry in ``models_missing_in_shopify``, note the most recent ``create_product_shopify`` run for that same Model, if any. A Model showing up here as "missing" might not be untried at all -- it could already have been attempted and be failing for a knowable reason (bad data, a stale Shopify desync, an image upload conflict), which is very different from "nobody has tried yet". Never raises -- a cosmetic enrichment must never fail the reconciliation run itself. .. py:function:: attach_update_attempts(details: dict[str, Any], store: api.worker.run_store.RunStore) -> None Best-effort: for each entry in ``variations_missing_in_shopify``, note the most recent ``update_product`` run for that same Variation, if any (matched via that run's own ``record_id`` -- its default, per-Variation entry mode; a bulk model_id-mode run isn't attributed back to its individual Variations here). Same rationale as attach_creation_attempts: "missing" doesn't mean "untried". Never raises -- a cosmetic enrichment must never fail the reconciliation run. .. py:function:: attach_documentation_attempts(details: dict[str, Any], store: api.worker.run_store.RunStore) -> None Best-effort: for each Model in the missing/outdated documentation lists, note its most recent ``update_product`` run that actually tried to sync documentation -- matched by that run's ``model_id``, which both entry modes record. A run that found no PDF in Airtable at the time (status "absent"/"unchanged") isn't a documentation attempt and is ignored, so a stale pre-PDF run can't pose as "synced but still missing". Never raises -- same rationale as attach_creation_attempts. .. py:data:: NOT_A_VENDRE_DETAIL_KEYS :value: ('shopify_products_not_a_vendre', 'shopify_variants_not_a_vendre') .. py:function:: alert_not_a_vendre(details: dict[str, Any]) -> None One email per run listing what's on Shopify but shouldn't be. .. py:data:: UPDATE_VARIATION_DETAIL_KEYS :value: ('variations_missing_in_shopify', 'a_vendre_units_missing_variant', 'a_vendre_quantity_mismatches') .. py:data:: UPDATE_MODEL_DETAIL_KEYS :value: ('documentation_missing_in_shopify', 'documentation_outdated_in_shopify',... .. py:function:: auto_fix(details: dict[str, Any], variations: list[api.domain.catalog.Variation]) -> dict[str, list[str]] Queues the same runs the dashboard's manual buttons do: create_product_shopify for each Model missing on Shopify, and one update_product per Model (by model_id, so a Model with several broken Variations is synced once) for stock, variant and documentation discrepancies. Returns what was queued; never raises -- a failed enqueue must not fail the reconciliation run itself. .. py:class:: ShopifyCatalogReconciliation Compares the Airtable sellable catalog against Shopify, then queues create/update_product for what they can fix. .. py:attribute:: name :value: 'shopify_catalog_reconciliation' .. py:attribute:: schedule .. py:method:: is_configured() -> bool .. py:method:: run(previous_snapshot: dict[str, Any] | None = None, report_stage: api.scripts.base.StageReporter = lambda stage: None, **kwargs: Any) -> tuple[api.scripts.base.ScriptResult, dict[str, Any] | None]