# Proposed event contract, version 1 (AsyncAPI 3.0): the events this system publishes, their consumers, payloads and delivery
# guarantees, and the events it consumes. Status: proposal for review, not confirmed design.
asyncapi: 3.0.0
info:
  title: Grant Events
  version: 1.0.0
  description: 'The public catalogue changing, farmer documents uploaded and read, and checks changing their verdict
    (req 200 to 218 of the grants requirements). Envelope, delivery guarantees and webhook headers: `common/v1/events.yaml`.
    Status: proposal for review, not confirmed design.'
defaultContentType: application/cloudevents+json
servers:
  prod:
    host: pubsub.googleapis.com
    protocol: googlepubsub
    description: Pub/Sub in the project that hosts the system, prod.
  stage:
    host: pubsub.googleapis.com
    protocol: googlepubsub
    description: Pub/Sub in the project that hosts the system, stage.
channels:
  grant.events.v1:
    address: grant.events.v1
    description: Every event grant publishes. Consumers filter by the `ce-type` attribute.
    messages:
      funding_call_published_v1:
        $ref: '#/components/messages/funding_call_published_v1'
      farmer_document_uploaded_v1:
        $ref: '#/components/messages/farmer_document_uploaded_v1'
      farmer_document_read_v1:
        $ref: '#/components/messages/farmer_document_read_v1'
      grant_check_changed_v1:
        $ref: '#/components/messages/grant_check_changed_v1'
    bindings:
      googlepubsub:
        messageRetentionDuration: 604800s
        schemaSettings:
          encoding: json
          name: projects/moonitoring-<env>-<project>/schemas/grant.events.v1
operations:
  publish_funding_call_published_v1:
    action: send
    channel:
      $ref: '#/channels/grant.events.v1'
    messages:
    - $ref: '#/channels/grant.events.v1/messages/funding_call_published_v1'
    summary: 'A call was published, held, restored or reverted: the public catalogue changed and its version was
      bumped.'
    x-moonitoring-producer: grant
    x-moonitoring-consumers:
    - moochat
    x-moonitoring-webhook: true
    x-moonitoring-requirements:
    - '202.5'
    - '202.6'
    - '202.8'
  publish_farmer_document_uploaded_v1:
    action: send
    channel:
      $ref: '#/channels/grant.events.v1'
    messages:
    - $ref: '#/channels/grant.events.v1/messages/farmer_document_uploaded_v1'
    summary: 'A document was uploaded and finalized; a worker execution is dispatched for it. Internal: one message
      per document.'
    x-moonitoring-producer: grant
    x-moonitoring-consumers: []
    x-moonitoring-webhook: false
    x-moonitoring-requirements:
    - '215.8'
    - '215.13'
  publish_farmer_document_read_v1:
    action: send
    channel:
      $ref: '#/channels/grant.events.v1'
    messages:
    - $ref: '#/channels/grant.events.v1/messages/farmer_document_read_v1'
    summary: 'A worker finished reading a document: ready, partial or failed; its facts are usable at once and the
      profile revision moved.'
    x-moonitoring-producer: grant
    x-moonitoring-consumers:
    - moochat
    x-moonitoring-webhook: true
    x-moonitoring-requirements:
    - '215.9'
    - '216'
    - '217.3'
  publish_grant_check_changed_v1:
    action: send
    channel:
      $ref: '#/channels/grant.events.v1'
    messages:
    - $ref: '#/channels/grant.events.v1/messages/grant_check_changed_v1'
    summary: The check of a call against a farm's facts changed its verdict after a new document, a dispute or a
      rule version.
    x-moonitoring-producer: grant
    x-moonitoring-consumers:
    - moochat
    x-moonitoring-webhook: true
    x-moonitoring-requirements:
    - '218'
    - '218.7'
components:
  messages:
    funding_call_published_v1:
      name: funding_call.published.v1
      title: funding_call.published.v1
      summary: 'A call was published, held, restored or reverted: the public catalogue changed and its version was
        bumped.'
      traits:
      - $ref: ../../../common/v1/events.yaml#/components/messageTraits/Event
      payload:
        allOf:
        - $ref: ../../../common/v1/events.yaml#/components/schemas/EventEnvelope
        - type: object
          properties:
            type:
              type: string
              const: funding_call.published.v1
            subject:
              type: string
              description: The call id.
            data:
              $ref: '#/components/schemas/FundingCallPublishedV1Data'
    farmer_document_uploaded_v1:
      name: farmer_document.uploaded.v1
      title: farmer_document.uploaded.v1
      summary: 'A document was uploaded and finalized; a worker execution is dispatched for it. Internal: one message
        per document.'
      traits:
      - $ref: ../../../common/v1/events.yaml#/components/messageTraits/Event
      payload:
        allOf:
        - $ref: ../../../common/v1/events.yaml#/components/schemas/EventEnvelope
        - type: object
          properties:
            type:
              type: string
              const: farmer_document.uploaded.v1
            subject:
              type: string
              description: The document id.
            data:
              $ref: '#/components/schemas/FarmerDocumentUploadedV1Data'
    farmer_document_read_v1:
      name: farmer_document.read.v1
      title: farmer_document.read.v1
      summary: 'A worker finished reading a document: ready, partial or failed; its facts are usable at once and
        the profile revision moved.'
      traits:
      - $ref: ../../../common/v1/events.yaml#/components/messageTraits/Event
      payload:
        allOf:
        - $ref: ../../../common/v1/events.yaml#/components/schemas/EventEnvelope
        - type: object
          properties:
            type:
              type: string
              const: farmer_document.read.v1
            subject:
              type: string
              description: The document id.
            data:
              $ref: '#/components/schemas/FarmerDocumentReadV1Data'
    grant_check_changed_v1:
      name: grant_check.changed.v1
      title: grant_check.changed.v1
      summary: The check of a call against a farm's facts changed its verdict after a new document, a dispute or
        a rule version.
      traits:
      - $ref: ../../../common/v1/events.yaml#/components/messageTraits/Event
      payload:
        allOf:
        - $ref: ../../../common/v1/events.yaml#/components/schemas/EventEnvelope
        - type: object
          properties:
            type:
              type: string
              const: grant_check.changed.v1
            subject:
              type: string
              description: The check id.
            data:
              $ref: '#/components/schemas/GrantCheckChangedV1Data'
  schemas:
    FundingCallPublishedV1Data:
      type: object
      properties:
        call:
          type: string
          description: The call's public id.
        published:
          type: boolean
        held:
          type: boolean
        catalogue_version:
          type: string
        availability:
          type: string
          enum:
          - open
          - upcoming
          - closed
          - to_confirm
      required:
      - call
      - published
      - catalogue_version
    FarmerDocumentUploadedV1Data:
      type: object
      properties:
        document:
          type: string
          format: uuid
        research:
          type: string
          format: uuid
        kind:
          type: string
          enum:
          - fascicolo
          - visura
          - bdn
          - classyfarm
        expected_hash:
          type: string
      required:
      - document
      - research
      - kind
      - expected_hash
    FarmerDocumentReadV1Data:
      type: object
      properties:
        document:
          type: string
          format: uuid
        research:
          type: string
          format: uuid
        state:
          type: string
          enum:
          - ready
          - partial
          - failed
        facts:
          type: integer
        failure_reason:
          type: string
        profile_revision:
          type: integer
      required:
      - document
      - research
      - state
    GrantCheckChangedV1Data:
      type: object
      properties:
        check:
          type: string
          format: uuid
        research:
          type: string
          format: uuid
        call:
          type: string
        verdict:
          type: string
          enum:
          - excluded
          - candidate
        profile_revision:
          type: integer
        rule_version:
          type: string
      required:
      - check
      - research
      - call
      - verdict
x-moonitoring-consumes:
- event: organization.*.v1, membership.*.v1
  producer: access
  why: Keeps its copy of who may act for an Organization.
- event: entitlement.*.v1, feature_usage.limit_reached.v1
  producer: farm
  why: Gates and meters eligibility checks.
- event: provider.degraded.v1
  producer: ai-gateway
  why: Pauses extraction batches.
- event: erasure_request.executed.v1
  producer: farm
  why: Deletes the farm's documents and checks.
