openapi: 3.1.0
info:
  title: BloomCore API - Customer Webhooks
  version: 1.0.0
  description: |
    Подписанный webhook `customer.changed` для синхронизации клиентов с POS, мобильными
    приложениями и внешними сервисами. Все CRM `id` и `*_id` являются UUID; внутренние
    bigint ID не отправляются. Payload содержит персональные и контактные данные.
    Проверяйте HMAC до JSON parsing, не пишите полное тело в логи, шифруйте данные
    получателя и ограничивайте доступ и срок хранения.
servers:
  - url: https://{tenant_subdomain}.crm.flowers/api/v1
    variables:
      tenant_subdomain:
        default: demo
        description: Поддомен магазина.
paths: {}
webhooks:
  customerChanged:
    post:
      summary: Создан, изменен, удален или восстановлен клиент
      description: |
        CRM отправляет immutable snapshot клиента после изменения. Получатель обязан
        проверить HMAC, дедуплицировать deliveries по `event_id` и применять событие
        только если `sync_version` новее уже сохраненной версии. Клиент принадлежит
        tenant-у целиком; nullable `primary_store_id` обозначает исходный магазин.
      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/CustomerChangedWebhookEnvelope'
            example:
              event: customer.changed
              event_id: 019f58c3-2cd0-77ed-90f7-6e0e79818a55
              created_at: '2026-07-18T07:30: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:
                customer:
                  id: 019f58c3-0180-7b38-a4c0-049096c70ca9
                  tenant_id: 019f58c1-cc38-75aa-afd8-bdf3dc77fa2c
                  primary_store_id: 019f58c1-e3da-72f7-baad-f67b453fe468
                  name: Анна Петрова
                  first_name: Анна
                  last_name: Петрова
                  phone: '+79990000000'
                  email: anna@example.test
                  birth_date: '1992-04-15'
                  gender: female
                  status: active
                  is_blacklisted: false
                  bonus_balance: '1250.00'
                  sync_version: 7
                  sync_updated_at: '2026-07-18T07:29:58+00:00'
                  sync_deleted_at: null
                  created_at: '2026-05-02T10:00:00+00:00'
                  updated_at: '2026-07-18T07:29: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: customer.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:
    CustomerChangedWebhookEnvelope:
      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: customer.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/CustomerChangedWebhookData'}
    CustomerChangedWebhookData:
      type: object
      additionalProperties: false
      required: [customer, change]
      properties:
        customer: {$ref: '#/components/schemas/CustomerWebhookCustomer'}
        change: {$ref: '#/components/schemas/CustomerWebhookChange'}
    CustomerWebhookCustomer:
      type: object
      additionalProperties: false
      required:
        - id
        - tenant_id
        - primary_store_id
        - name
        - first_name
        - last_name
        - phone
        - email
        - birth_date
        - gender
        - status
        - is_blacklisted
        - bonus_balance
        - sync_version
        - sync_updated_at
        - sync_deleted_at
        - created_at
        - updated_at
      properties:
        id:
          type: string
          format: uuid
        tenant_id:
          type: string
          format: uuid
        primary_store_id:
          type: [string, 'null']
          format: uuid
        name: {type: string, minLength: 1}
        first_name: {type: [string, 'null']}
        last_name: {type: [string, 'null']}
        phone: {type: [string, 'null']}
        email: {type: [string, 'null'], format: email}
        birth_date: {type: [string, 'null'], format: date}
        gender: {type: [string, 'null'], enum: [female, male, null]}
        status: {type: string, enum: [active, archived]}
        is_blacklisted: {type: boolean}
        bonus_balance: {type: string, pattern: '^-?[0-9]+\.[0-9]{2}$'}
        sync_version: {type: integer, minimum: 1}
        sync_updated_at: {type: string, 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}
    CustomerWebhookChange:
      type: object
      additionalProperties: false
      required: [action]
      properties:
        action: {type: string, enum: [created, updated, deleted, restored]}
