openapi: 3.1.0
info:
  title: BloomCore API - Document Webhooks
  version: 1.1.0
  description: |
    Подписанное событие `document.changed` сообщает exact магазину об изменениях
    общего хранилища документов. Все идентификаторы сущностей являются
    каноническими UUID; внутренние bigint ID не передаются.
    Payload никогда не содержит содержимое файла, S3 locator, исходное имя,
    content hash, scanner diagnostics, ответственного сотрудника или numeric ID.
    Проверяйте HMAC до JSON parsing и дедуплицируйте deliveries по `event_id`.
servers:
  - url: https://{tenant_subdomain}.crm.flowers/api/v1
    variables:
      tenant_subdomain:
        default: demo
        description: Поддомен магазина.
paths: {}
webhooks:
  documentChanged:
    post:
      summary: Документ создан, изменен, назначен, снят, архивирован или получил новую версию
      description: |
        Событие store-scoped. Для `unassigned` поле `document` равно `null`, а
        `change` содержит минимальный tombstone, который получатель обязан удалить
        из своей проекции. Для `version_ready` current file совпадает с
        `change.version_id` и готов к чтению через отдельные разрешенные CRM/API
        механизмы; сам webhook файл или download URL не выдает.
      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/DocumentChangedWebhookEnvelope'
            example:
              event: document.changed
              event_id: 019f6d20-1538-7be0-8a35-0c17922493cc
              created_at: '2026-07-30T15:00:00+00:00'
              payload_schema_version: crm.webhook.v1
              tenant_id: 019f6d1f-c931-74c4-91b1-546da4a59a06
              store_id: 019f6d1f-dabb-7dd5-88b2-ef8cfdd4bdde
              retry_count: 0
              data:
                document:
                  id: 019f6d20-008f-7508-a16a-aa9947c7541d
                  tenant_id: 019f6d1f-c931-74c4-91b1-546da4a59a06
                  store_id: 019f6d1f-dabb-7dd5-88b2-ef8cfdd4bdde
                  folder_id: 019f6d1f-f58d-7937-90d1-90096ee92f01
                  type: contract
                  title: Договор аренды
                  status: active
                  issued_on: '2026-07-01'
                  expires_on: '2027-07-01'
                  current_file:
                    id: 019f6d20-0b29-7a29-b49c-c75a57fb75a0
                    version_number: 2
                    availability: ready
                    mime_type: application/pdf
                    size_bytes: 182400
                    ready_at: '2026-07-30T14:59:58+00:00'
                  created_at: '2026-07-01T08:00:00+00:00'
                  updated_at: '2026-07-30T14:59:58+00:00'
                change:
                  action: version_ready
                  document_id: 019f6d20-008f-7508-a16a-aa9947c7541d
                  tenant_id: 019f6d1f-c931-74c4-91b1-546da4a59a06
                  store_id: 019f6d1f-dabb-7dd5-88b2-ef8cfdd4bdde
                  version_id: 019f6d20-0b29-7a29-b49c-c75a57fb75a0
      responses:
        '200':
          description: Событие принято интеграцией.
  orderDocumentCorrectedVersionIssued:
    post:
      summary: Выпущена исправленная версия документа заказа
      description: |
        Событие exact магазина создается атомарно с новой версией документа
        после примененной корректировки заказа. Исходная версия сохраняется.
        Payload не содержит PII, причины, HTML/PDF, download URL, S3 locator,
        metadata, snapshot или внутренние bigint ID.
      parameters:
        - $ref: '#/components/parameters/webhookOrderDocumentCorrectedEvent'
        - $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/OrderDocumentCorrectedWebhookEnvelope'
            example:
              event: order_document.corrected_version_issued
              event_id: 019f6d20-1538-7be0-8a35-0c17922493cd
              created_at: '2026-08-01T03:30:00+00:00'
              payload_schema_version: crm.webhook.v1
              tenant_id: 019f6d1f-c931-74c4-91b1-546da4a59a06
              store_id: 019f6d1f-dabb-7dd5-88b2-ef8cfdd4bdde
              retry_count: 0
              data:
                document:
                  id: 019f6d20-008f-7508-a16a-aa9947c7541e
                  tenant_id: 019f6d1f-c931-74c4-91b1-546da4a59a06
                  store_id: 019f6d1f-dabb-7dd5-88b2-ef8cfdd4bdde
                  order_id: 019f6d20-008f-7508-a16a-aa9947c75410
                  previous_document_id: 019f6d20-008f-7508-a16a-aa9947c75411
                  correction_id: 019f6d20-008f-7508-a16a-aa9947c75412
                  order_revision_id: 019f6d20-008f-7508-a16a-aa9947c75413
                  version: 2
                  number: INV-20260801-0002
                  type: invoice
                  status: generated
                  pdf_status: pending
                  pdf_available: false
                  issued_at: '2026-08-01T03:30:00+00:00'
                change:
                  action: corrected_version_issued
                  source_document_id: 019f6d20-008f-7508-a16a-aa9947c75411
                  source_document_version: 1
                  document_id: 019f6d20-008f-7508-a16a-aa9947c7541e
                  document_version: 2
      responses:
        '200':
          description: Событие принято интеграцией.
components:
  parameters:
    webhookEvent:
      name: X-CRM-Webhook-Event
      in: header
      required: true
      schema: {type: string, const: document.changed}
    webhookEventId:
      name: X-CRM-Webhook-Id
      in: header
      required: true
      schema: {type: string, format: uuid}
    webhookOrderDocumentCorrectedEvent:
      name: X-CRM-Webhook-Event
      in: header
      required: true
      schema: {type: string, const: order_document.corrected_version_issued}
    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:
    DocumentChangedWebhookEnvelope:
      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: document.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
          format: uuid
        retry_count: {type: integer, minimum: 0}
        data: {$ref: '#/components/schemas/DocumentChangedWebhookData'}
    DocumentChangedWebhookData:
      type: object
      additionalProperties: false
      required: [document, change]
      properties:
        document:
          oneOf:
            - {$ref: '#/components/schemas/DocumentWebhookDocument'}
            - {type: 'null'}
        change: {$ref: '#/components/schemas/DocumentWebhookChange'}
    DocumentWebhookDocument:
      type: object
      additionalProperties: false
      required: [id, tenant_id, store_id, folder_id, type, title, status, issued_on, expires_on, current_file, created_at, updated_at]
      properties:
        id:
          type: string
          format: uuid
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        folder_id:
          type: [string, 'null']
          format: uuid
        type: {type: string, enum: [contract, invoice, act, waybill, instruction, legal, other]}
        title: {type: string, minLength: 1, maxLength: 255}
        status: {type: string, enum: [active, archived]}
        issued_on: {type: [string, 'null'], format: date}
        expires_on: {type: [string, 'null'], format: date}
        current_file:
          oneOf:
            - {$ref: '#/components/schemas/DocumentWebhookCurrentFile'}
            - {type: 'null'}
        created_at: {type: string, format: date-time}
        updated_at: {type: string, format: date-time}
    DocumentWebhookCurrentFile:
      type: object
      additionalProperties: false
      required: [id, version_number, availability, mime_type, size_bytes, ready_at]
      properties:
        id:
          type: string
          format: uuid
        version_number: {type: integer, minimum: 1}
        availability: {type: string, enum: [ready, processing, unavailable]}
        mime_type: {type: [string, 'null']}
        size_bytes: {type: [integer, 'null'], minimum: 1}
        ready_at: {type: [string, 'null'], format: date-time}
    DocumentWebhookChange:
      type: object
      additionalProperties: false
      required: [action, document_id, tenant_id, store_id, version_id]
      properties:
        action:
          type: string
          enum: [created, updated, assigned, unassigned, archived, version_uploaded, version_ready]
        document_id:
          type: string
          format: uuid
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        version_id:
          type: [string, 'null']
          format: uuid
    OrderDocumentCorrectedWebhookEnvelope:
      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: order_document.corrected_version_issued}
        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
          format: uuid
        retry_count: {type: integer, minimum: 0}
        data: {$ref: '#/components/schemas/OrderDocumentCorrectedWebhookData'}
    OrderDocumentCorrectedWebhookData:
      type: object
      additionalProperties: false
      required: [document, change]
      properties:
        document: {$ref: '#/components/schemas/OrderDocumentCorrectedWebhookDocument'}
        change: {$ref: '#/components/schemas/OrderDocumentCorrectedWebhookChange'}
    OrderDocumentCorrectedWebhookDocument:
      type: object
      additionalProperties: false
      required: [id, tenant_id, store_id, order_id, previous_document_id, correction_id, order_revision_id, version, number, type, status, pdf_status, pdf_available, issued_at]
      properties:
        id:
          type: string
          format: uuid
        tenant_id:
          type: string
          format: uuid
        store_id:
          type: string
          format: uuid
        order_id:
          type: string
          format: uuid
        previous_document_id:
          type: string
          format: uuid
        correction_id:
          type: string
          format: uuid
        order_revision_id:
          type: string
          format: uuid
        version: {type: integer, minimum: 2}
        number: {type: string, minLength: 1, maxLength: 255}
        type: {type: string, minLength: 1, maxLength: 64}
        status: {type: string, const: generated}
        pdf_status: {type: string, enum: [pending, processing, ready, failed]}
        pdf_available: {type: boolean}
        issued_at: {type: string, format: date-time}
    OrderDocumentCorrectedWebhookChange:
      type: object
      additionalProperties: false
      required: [action, source_document_id, source_document_version, document_id, document_version]
      properties:
        action: {type: string, const: corrected_version_issued}
        source_document_id:
          type: string
          format: uuid
        source_document_version: {type: integer, minimum: 1}
        document_id:
          type: string
          format: uuid
        document_version: {type: integer, minimum: 2}
