openapi: 3.1.0
info:
  title: Orders API
  version: 3.1.0
  description: Synchronous order placement and lookup.
  contact:
    name: team-commerce

tags:
  - name: orders
    description: Order placement and lookup.

paths:
  /orders:
    post:
      operationId: placeOrder
      summary: Place an order
      description: >
        Accepts an order for the given customer and lines. On success the
        order is persisted and an OrderPlaced event is guaranteed to be
        published - never one without the other.
      tags: [orders]
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/PlaceOrderRequest'
      responses:
        '201':
          description: Order accepted; an OrderPlaced event will be published.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Order'
        '400':
          description: >
            Invalid order - no lines, a line quantity below 1, a negative
            unit_price_pence, or a line missing its sku.
        '409':
          description: Idempotency key already used with a different body.

  /orders/{order_id}:
    get:
      operationId: getOrder
      summary: Fetch an order
      description: Returns the order's current state by its id.
      tags: [orders]
      parameters:
        - name: order_id
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        '200':
          description: The order.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Order'
        '404':
          description: No such order.

components:
  schemas:
    PlaceOrderRequest:
      type: object
      required: [customer_id, lines]
      additionalProperties: false
      properties:
        customer_id: { type: string, format: uuid }
        lines:
          type: array
          minItems: 1
          items:
            type: object
            required: [sku, quantity, unit_price_pence]
            additionalProperties: false
            properties:
              sku: { type: string }
              quantity: { type: integer, minimum: 1 }
              unit_price_pence: { type: integer, minimum: 0 }
    Order:
      type: object
      required: [order_id, customer_id, status, total_pence]
      additionalProperties: false
      properties:
        order_id: { type: string, format: uuid }
        customer_id: { type: string, format: uuid }
        status:
          type: string
          enum: [placed, paid, cancelled]
        total_pence: { type: integer, minimum: 0 }
