openapi: 3.1.0
info:
  title: BloomCore API - Product Webhooks
  version: 1.0.0
  description: |
    Подписанный webhook `product.changed` для синхронизации каталога, POS и внешних витрин.
    Все CRM `id` и `*_id` являются UUID; внутренние bigint ID не отправляются.
    Payload содержит коммерческие цены, себестоимость и складские настройки. Ограничьте доступ,
    проверяйте HMAC до JSON parsing и храните webhook secret только в защищенном secret storage.
servers:
  - url: https://{tenant_subdomain}.crm.flowers/api/v1
    variables:
      tenant_subdomain:
        default: demo
        description: Поддомен магазина.
paths: {}
webhooks:
  productChanged:
    post:
      summary: Создан, изменен, удален или восстановлен товар
      description: |
        CRM отправляет immutable snapshot после действия над товаром. Получатель обязан
        дедуплицировать deliveries по `event_id`, проверять HMAC и применять изменения
        только внутри UUID tenant/store из envelope и product snapshot.
      parameters:
        - $ref: '#/components/parameters/webhookEvent'
        - $ref: '#/components/parameters/webhookEventId'
        - $ref: '#/components/parameters/webhookDeliveryId'
        - $ref: '#/components/parameters/webhookTimestamp'
        - $ref: '#/components/parameters/webhookSignature'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ProductChangedWebhookEnvelope'
            example:
              event: product.changed
              event_id: 019f58c2-209e-74d5-a294-ab60d9243d8d
              created_at: '2026-07-18T06:45:00+00:00'
              payload_schema_version: crm.webhook.v1
              tenant_id: 019f58c1-cc38-75aa-afd8-bdf3dc77fa2c
              store_id: 019f58c1-e3da-72f7-baad-f67b453fe468
              retry_count: 0
              data:
                product:
                  id: 019f58c2-038f-75ec-a49a-694503ec63e4
                  tenant_id: 019f58c1-cc38-75aa-afd8-bdf3dc77fa2c
                  store_id: 019f58c1-e3da-72f7-baad-f67b453fe468
                  folder_id: 019f58c1-f9cf-7264-ae3b-e00bdfeb1ad8
                  type: flower
                  name: Роза Explorer
                  sku: ROSE-EX
                  unit_id: null
                  is_stock_tracked: true
                  is_popular: true
                  min_stock_qty: '20.000'
                  allow_negative_stock: false
                  allow_fractional_quantity: true
                  default_price: '350.00'
                  default_cost: '140.00'
                  freshness_days: 7
                  is_visible_to_franchisor: true
                  status: active
                  sync_version: 4
                  sync_updated_at: '2026-07-18T06:44:58+00:00'
                  sync_deleted_at: null
                  created_at: '2026-07-01T08:00:00+00:00'
                  updated_at: '2026-07-18T06:44:58+00:00'
                change:
                  action: updated
      responses:
        '200':
          description: Событие принято интеграцией.
components:
  parameters:
    webhookEvent:
      name: X-CRM-Webhook-Event
      in: header
      required: true
      schema: {type: string, const: product.changed}
    webhookEventId:
      name: X-CRM-Webhook-Id
      in: header
      required: true
      schema: {type: string, format: uuid}
    webhookDeliveryId:
      name: X-CRM-Webhook-Delivery
      in: header
      required: true
      schema: {type: string, format: uuid}
    webhookTimestamp:
      name: X-CRM-Webhook-Timestamp
      in: header
      required: true
      schema: {type: string, pattern: '^[0-9]{10}$'}
    webhookSignature:
      name: X-CRM-Webhook-Signature
      in: header
      required: true
      schema: {type: string, pattern: '^sha256=[a-f0-9]{64}$'}
  schemas:
    ProductChangedWebhookEnvelope:
      type: object
      additionalProperties: false
      required: [event, event_id, created_at, payload_schema_version, tenant_id, store_id, retry_count, data]
      properties:
        event: {type: string, const: product.changed}
        event_id:
          type: string
          format: uuid
        created_at: {type: string, format: date-time}
        payload_schema_version: {type: string, const: crm.webhook.v1}
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: [string, 'null']
          format: uuid
        retry_count: {type: integer, minimum: 0}
        data: {$ref: '#/components/schemas/ProductChangedWebhookData'}
    ProductChangedWebhookData:
      type: object
      additionalProperties: false
      required: [product, change]
      properties:
        product: {$ref: '#/components/schemas/ProductWebhookProduct'}
        change: {$ref: '#/components/schemas/ProductWebhookChange'}
    ProductWebhookProduct:
      type: object
      additionalProperties: false
      required:
        - id
        - tenant_id
        - store_id
        - folder_id
        - type
        - name
        - sku
        - unit_id
        - is_stock_tracked
        - is_popular
        - min_stock_qty
        - allow_negative_stock
        - allow_fractional_quantity
        - default_price
        - default_cost
        - freshness_days
        - is_visible_to_franchisor
        - status
        - sync_version
        - sync_updated_at
        - sync_deleted_at
        - created_at
        - updated_at
      properties:
        id:
          type: string
          format: uuid
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: [string, 'null']
          format: uuid
        folder_id:
          type: [string, 'null']
          format: uuid
        type: {type: string, enum: [flower, product, consumable, gift, certificate, service]}
        name: {type: string, minLength: 1}
        sku: {type: [string, 'null']}
        unit_id:
          type: [string, 'null']
          format: uuid
        is_stock_tracked: {type: boolean}
        is_popular: {type: boolean}
        min_stock_qty: {type: string, pattern: '^[0-9]+\.[0-9]{3}$'}
        allow_negative_stock: {type: boolean}
        allow_fractional_quantity: {type: boolean}
        default_price: {type: string, pattern: '^[0-9]+\.[0-9]{2}$'}
        default_cost:
          type: [string, 'null']
          pattern: '^[0-9]+\.[0-9]{2}$'
        freshness_days: {type: [integer, 'null'], minimum: 0}
        is_visible_to_franchisor: {type: boolean}
        status: {type: string, enum: [active, archived]}
        sync_version: {type: integer, minimum: 0}
        sync_updated_at: {type: [string, 'null'], format: date-time}
        sync_deleted_at: {type: [string, 'null'], format: date-time}
        created_at: {type: string, format: date-time}
        updated_at: {type: string, format: date-time}
    ProductWebhookChange:
      type: object
      additionalProperties: false
      required: [action]
      properties:
        action: {type: string, enum: [created, updated, deleted, restored]}
