api.services.catalog_sync ========================= .. py:module:: api.services.catalog_sync .. autoapi-nested-parse:: Airtable writes for the product intake workflow (app.rewake.fr). One product group = one Model, its Variations (+ their Specifications) and its Units. The app holds the workflow; this module turns a group into Airtable records and keeps them in step: - push create/update the group as a draft (``À valider``), §6E1-E2 - validate ``Validé`` + convert an existing default Variation, §6E3 - unvalidate back to ``À valider``, refused once on Shopify, §6E4 - cancel delete what the group created, with guards, §6E6 - accessory read-merge-write of the ``Compatibilité`` row of a host, §6E5 Idempotency: every created record is remembered in a ``RefStore`` under the push's idempotency key *right after* Airtable returns it, so a retry after a timeout or a partial failure reuses it instead of creating a duplicate. Field names come from the raw Airtable schema (Architecture repo, ``schemas/raw/*.json``). Variation ``Short description``/``Long description``, ``Variant_value``, ``ShopifyID`` and ``Status Shopify`` are lookups and are never written. Attributes ---------- .. autoapisummary:: api.services.catalog_sync.logger api.services.catalog_sync.COMPAT_TABLE_NAME api.services.catalog_sync.STATUS_FIELD api.services.catalog_sync.STATUS_DRAFT api.services.catalog_sync.STATUS_VALIDATED api.services.catalog_sync.MODEL_NAME_FIELD api.services.catalog_sync.MODEL_BRAND_FIELD api.services.catalog_sync.MODEL_TYPE_FIELD api.services.catalog_sync.MODEL_SHOPIFY_ID_FIELD api.services.catalog_sync.MODEL_SHOPIFY_LISTING_FIELD api.services.catalog_sync.MODEL_IS_ACCESSORY_FIELD api.services.catalog_sync.MODEL_COMPAT_AS_HOST_FIELD api.services.catalog_sync.VARIATION_MODEL_FIELD api.services.catalog_sync.VARIATION_IS_VARIANT_FIELD api.services.catalog_sync.VARIATION_UNITS_FIELD api.services.catalog_sync.SPEC_KEY_FIELD api.services.catalog_sync.SPEC_VALUE_FIELD api.services.catalog_sync.SPEC_UNIT_FIELD api.services.catalog_sync.SPEC_VARIATION_FIELD api.services.catalog_sync.SPEC_AXIS_VARIATION_FIELD api.services.catalog_sync.UNIT_VARIATION_FIELD api.services.catalog_sync.UNIT_SERIAL_FIELD api.services.catalog_sync.UNIT_AESTHETIC_FIELD api.services.catalog_sync.UNIT_FUNCTIONAL_FIELD api.services.catalog_sync.UNIT_WORKSHOP_NOTE_FIELD api.services.catalog_sync.UNIT_HOST_FIELD api.services.catalog_sync.UNIT_COMMERCIAL_STATUS_FIELD api.services.catalog_sync.UNIT_OPERATION_FIELD api.services.catalog_sync.UNIT_BUYBACK_PRICE_FIELD api.services.catalog_sync.UNIT_CLIENT_PHOTOS_FIELD api.services.catalog_sync.UNIT_STORAGE_PLACE_FIELD api.services.catalog_sync.UNIT_OPERATIONAL_STATUS_FIELD api.services.catalog_sync.UNIT_RECEPTION_STORAGE_PLACE api.services.catalog_sync.UNIT_RECEPTION_OPERATIONAL_STATUS api.services.catalog_sync.UNIT_RECEPTION_LOCATION api.services.catalog_sync.UNIT_PHOTOS_FIELD api.services.catalog_sync.EQUIPMENT_TABLE_NAME api.services.catalog_sync.EQUIPMENT_PRICE_FIELD api.services.catalog_sync.EQUIPMENT_PHOTOS_FIELD api.services.catalog_sync.UNIT_COMMERCIAL_LINK_FIELDS api.services.catalog_sync.UNIT_LOCKED_STATUSES api.services.catalog_sync.COMPAT_HOST_FIELD api.services.catalog_sync.COMPAT_ACCESSORIES_FIELD api.services.catalog_sync.REF_TTL_SECONDS Exceptions ---------- .. autoapisummary:: api.services.catalog_sync.PushIncomplete api.services.catalog_sync.CatalogSyncError Classes ------- .. autoapisummary:: api.services.catalog_sync.AirtableGateway api.services.catalog_sync.AttachmentGateway api.services.catalog_sync.RefStore api.services.catalog_sync.RedisRefStore api.services.catalog_sync.CatalogSync Functions --------- .. autoapisummary:: api.services.catalog_sync.entreprise_id api.services.catalog_sync.model_fields api.services.catalog_sync.variation_fields api.services.catalog_sync.spec_fields api.services.catalog_sync.unit_fields api.services.catalog_sync.is_model_on_shopify api.services.catalog_sync.unit_commercial_lock Module Contents --------------- .. py:data:: logger .. py:data:: COMPAT_TABLE_NAME :value: 'Compatibilité' .. py:data:: STATUS_FIELD :value: 'Statut validation Variation' .. py:data:: STATUS_DRAFT :value: 'À valider' .. py:data:: STATUS_VALIDATED :value: 'Validé' .. py:data:: MODEL_NAME_FIELD :value: 'Modèle' .. py:data:: MODEL_BRAND_FIELD :value: 'Marque' .. py:data:: MODEL_TYPE_FIELD :value: 'Type' .. py:data:: MODEL_SHOPIFY_ID_FIELD :value: 'ShopifyID' .. py:data:: MODEL_SHOPIFY_LISTING_FIELD :value: 'Annonces Shopify' .. py:data:: MODEL_IS_ACCESSORY_FIELD :value: 'is_accessory' .. py:data:: MODEL_COMPAT_AS_HOST_FIELD :value: 'Équipements filles' .. py:data:: VARIATION_MODEL_FIELD :value: 'Model' .. py:data:: VARIATION_IS_VARIANT_FIELD :value: 'is_variant' .. py:data:: VARIATION_UNITS_FIELD :value: 'Toutes les unités' .. py:data:: SPEC_KEY_FIELD :value: 'clé' .. py:data:: SPEC_VALUE_FIELD :value: 'valeur' .. py:data:: SPEC_UNIT_FIELD :value: 'unité' .. py:data:: SPEC_VARIATION_FIELD :value: 'Variation associée' .. py:data:: SPEC_AXIS_VARIATION_FIELD :value: 'Spec_variant' .. py:data:: UNIT_VARIATION_FIELD :value: 'Référence variant' .. py:data:: UNIT_SERIAL_FIELD :value: 'Numéro de série' .. py:data:: UNIT_AESTHETIC_FIELD :value: 'Etat esthétique' .. py:data:: UNIT_FUNCTIONAL_FIELD :value: 'État fonctionnel' .. py:data:: UNIT_WORKSHOP_NOTE_FIELD :value: 'Commentaire atelier (état & accessoires)' .. py:data:: UNIT_HOST_FIELD :value: 'Modules/Accessoires/Consommables' .. py:data:: UNIT_COMMERCIAL_STATUS_FIELD :value: 'Statut commercial' .. py:data:: UNIT_OPERATION_FIELD :value: 'Opération reprise' .. py:data:: UNIT_BUYBACK_PRICE_FIELD :value: 'Prix de rachat' .. py:data:: UNIT_CLIENT_PHOTOS_FIELD :value: 'Photos client' .. py:data:: UNIT_STORAGE_PLACE_FIELD :value: 'Lieu de stockage' .. py:data:: UNIT_OPERATIONAL_STATUS_FIELD :value: 'Statut opérationnel' .. py:data:: UNIT_RECEPTION_STORAGE_PLACE :value: 'Rewake' .. py:data:: UNIT_RECEPTION_OPERATIONAL_STATUS :value: 'À inventorier' .. py:data:: UNIT_RECEPTION_LOCATION :value: 'Réception' .. py:data:: UNIT_PHOTOS_FIELD :value: 'Photos interne' .. py:data:: EQUIPMENT_TABLE_NAME :value: 'Équipements liés aux propositions de reprises' .. py:data:: EQUIPMENT_PRICE_FIELD :value: 'Prix de rachat' .. py:data:: EQUIPMENT_PHOTOS_FIELD :value: 'Photos' .. py:data:: UNIT_COMMERCIAL_LINK_FIELDS :value: ('Vente', 'Devis', 'Ventes aux enchères') .. py:data:: UNIT_LOCKED_STATUSES .. py:data:: COMPAT_HOST_FIELD :value: 'Machine mère' .. py:data:: COMPAT_ACCESSORIES_FIELD :value: 'Machine fille' .. py:data:: REF_TTL_SECONDS :value: 7776000 .. py:exception:: PushIncomplete(ids: dict[str, str], written: dict[str, dict[str, Any]], cause: Exception) Bases: :py:obj:`Exception` A push failed midway. ``ids``/``written`` cover what was created or updated before the failure (also remembered under the idempotency key): retrying the same push resumes from there. .. py:attribute:: ids .. py:attribute:: written .. py:attribute:: cause .. py:exception:: CatalogSyncError(code: str, message: str, details: dict[str, Any] | None = None) Bases: :py:obj:`Exception` A refused operation the app should show to the user (HTTP 409). .. py:attribute:: code .. py:attribute:: message .. py:attribute:: details .. py:function:: entreprise_id(name: str) -> str | None The Entreprise record named exactly ``name`` (cached: it never moves). .. py:class:: AirtableGateway Bases: :py:obj:`Protocol` .. py:method:: get_record(table: str, record_id: str) -> dict[str, Any] .. py:method:: create_record(table: str, fields: dict[str, Any]) -> dict[str, Any] .. py:method:: update_record(table: str, record_id: str, fields: dict[str, Any]) -> dict[str, Any] .. py:method:: delete_record(table: str, record_id: str) -> None .. py:class:: AttachmentGateway Bases: :py:obj:`AirtableGateway`, :py:obj:`Protocol` Plus Airtable's content API: a file appended to an attachment field. .. py:method:: upload_attachment(table_name: str, record_id: str, field: str, content: bytes, filename: str, content_type: str) -> dict[str, Any] .. py:class:: RefStore Bases: :py:obj:`Protocol` .. py:method:: get(key: str, ref: str) -> str | None .. py:method:: set(key: str, ref: str, record_id: str) -> None .. py:class:: RedisRefStore(client: redis.Redis[str]) .. py:attribute:: client .. py:method:: get(key: str, ref: str) -> str | None .. py:method:: set(key: str, ref: str, record_id: str) -> None .. py:function:: model_fields(model: api.domain.intake.ModelIn) -> dict[str, Any] .. py:function:: variation_fields(variation: api.domain.intake.VariationIn, model_id: str) -> dict[str, Any] .. py:function:: spec_fields(spec: api.domain.intake.SpecIn, variation_id: str, *, axis: bool = False) -> dict[str, Any] .. py:function:: unit_fields(unit: api.domain.intake.UnitValues, variation_id: str) -> dict[str, Any] .. py:function:: is_model_on_shopify(model_fields_: dict[str, Any]) -> bool .. py:function:: unit_commercial_lock(unit_fields_: dict[str, Any]) -> str | None Why a Unit must not be deleted, or None. .. py:class:: CatalogSync(gateway: AirtableGateway, refs: RefStore) .. py:attribute:: at .. py:attribute:: refs .. py:method:: push(req: api.domain.intake.PushRequest) -> api.domain.intake.PushResult Create or update the group. ``req.idempotency_key`` must be stable for the product group (not per attempt or revision): it scopes the ref -> record id memo that prevents duplicates on retry. .. py:method:: sync_unit(req: api.domain.intake.UnitSyncRequest) -> api.domain.intake.UnitSyncResult Create or complete the Unit of one received item. On creation it also gets its reprise, buy-back price and client photos. .. py:method:: remove_unit(unit_id: str) -> None Delete a Unit the app created (refused once engaged commercially). .. py:method:: restore_unit(unit_id: str, fields: dict[str, Any]) -> bool Give a pre-created reprise Unit its values back; False if gone. .. py:method:: model_on_shopify(model_id: str) -> bool .. py:method:: validate(req: api.domain.intake.ValidateRequest) -> dict[str, Any] .. py:method:: add_variant(model_id: str, axis: api.domain.intake.SpecIn, specs: list[api.domain.intake.SpecIn], convert_variation_id: str | None = None) -> dict[str, Any] A variant of an existing Model (commercial review): a new Variation (« À valider ») with its axis and specifications, or, with ``convert_variation_id``, the Model's default Variation turned into this variant (axis added). Idempotent per Model and axis value. .. py:method:: unvalidate(req: api.domain.intake.UnvalidateRequest) -> dict[str, Any] .. py:method:: cancel(req: api.domain.intake.CancelRequest) -> api.domain.intake.CancelResult .. py:method:: link_accessory(host_model_id: str, accessory_model_id: str) -> api.domain.intake.AccessoryLinkResult Add the accessory to the host's Compatibilité row (rule 7). Read-merge-write: the row is re-read right before the update and the accessory is appended to the *current* list, never written from a stale copy (Airtable replaces the whole link list on PATCH). .. py:method:: unlink_accessory(host_model_id: str, accessory_model_id: str) -> bool