---
asyncapi: 3.1.0
info:
  title: Catalog API integration events
  version: 1.0.0
  description: |
    AsyncAPI contract for the Catalog service's Kafka integration events.
    Catalog consumes rating changes from the Rating service and publishes a
    failure event when a feedback-created message references an unknown book.
defaultContentType: application/cloudevents+json
channels:
  feedbackCreated:
    address: bookworm.{env}.rating.feedback-create
    description: Rating service publishes this event when feedback is created.
    parameters:
      env:
        $ref: "#/components/parameters/env"
    messages:
      feedbackCreated:
        $ref: "#/components/messages/FeedbackCreatedIntegrationEvent"
  feedbackDeleted:
    address: bookworm.{env}.rating.feedback-delete
    description: Rating service publishes this event when feedback is deleted.
    parameters:
      env:
        $ref: "#/components/parameters/env"
    messages:
      feedbackDeleted:
        $ref: "#/components/messages/FeedbackDeletedIntegrationEvent"
  bookUpdatedRatingFailed:
    address: bookworm.{env}.catalog.book-update-rating-failed
    description: Catalog publishes this event when a feedback-created event cannot update a book.
    parameters:
      env:
        $ref: "#/components/parameters/env"
    messages:
      bookUpdatedRatingFailed:
        $ref: "#/components/messages/BookUpdatedRatingFailedIntegrationEvent"
operations:
  receiveFeedbackCreated:
    action: receive
    channel:
      $ref: "#/channels/feedbackCreated"
    summary: Consume feedback-created events
    messages:
      - $ref: "#/channels/feedbackCreated/messages/feedbackCreated"
  receiveFeedbackDeleted:
    action: receive
    channel:
      $ref: "#/channels/feedbackDeleted"
    summary: Consume feedback-deleted events
    messages:
      - $ref: "#/channels/feedbackDeleted/messages/feedbackDeleted"
  sendBookUpdatedRatingFailed:
    action: send
    channel:
      $ref: "#/channels/bookUpdatedRatingFailed"
    summary: Publish rating update failures
    messages:
      - $ref: "#/channels/bookUpdatedRatingFailed/messages/bookUpdatedRatingFailed"
components:
  parameters:
    env:
      description: BookWorm deployment environment.
      enum:
        - development
        - staging
        - production
      default: development
  messages:
    FeedbackCreatedIntegrationEvent:
      name: FeedbackCreatedIntegrationEvent
      title: Feedback created
      summary: A new rating was created for a book.
      contentType: application/json
      payload:
        $ref: "#/components/schemas/FeedbackRatingPayload"
    FeedbackDeletedIntegrationEvent:
      name: FeedbackDeletedIntegrationEvent
      title: Feedback deleted
      summary: An existing rating was deleted from a book.
      contentType: application/json
      payload:
        $ref: "#/components/schemas/FeedbackRatingPayload"
    BookUpdatedRatingFailedIntegrationEvent:
      name: BookUpdatedRatingFailedIntegrationEvent
      title: Book rating update failed
      summary: Catalog could not update a book rating for the feedback event.
      contentType: application/json
      payload:
        $ref: "#/components/schemas/BookUpdatedRatingFailedPayload"
  schemas:
    FeedbackRatingPayload:
      type: object
      additionalProperties: false
      required:
        - bookId
        - rating
        - feedbackId
      properties:
        bookId:
          type: string
          format: uuid
          description: Identifier of the book whose rating summary changes.
        rating:
          type: integer
          description: Rating value added to or removed from the book.
        feedbackId:
          type: string
          format: uuid
          description: Identifier of the feedback record.
    BookUpdatedRatingFailedPayload:
      type: object
      additionalProperties: false
      required:
        - feedbackId
      properties:
        feedbackId:
          type: string
          format: uuid
          description: Identifier of the feedback that could not be applied.
