# Order API notes

Base path: `/api/v1`. JSON in and out. Controllers never return JPA entities.

## Resources

### `POST /api/v1/orders`

Creates an order in `CREATED`. Requires header `Idempotency-Key` (1–128 chars).

Request:

```json
{
  "customerId": "uuid",
  "items": [
    { "productId": "uuid", "quantity": 1, "unitPrice": 10.00 }
  ]
}
```

Validation: `customerId` required; `items` not empty; each `quantity >= 1`;
`unitPrice >= 0.01`.

Response `201` with `Location: /api/v1/orders/{id}` and body:

```json
{
  "id": "uuid",
  "customerId": "uuid",
  "status": "CREATED",
  "items": [{ "productId": "uuid", "quantity": 1, "unitPrice": 10.00 }],
  "totalAmount": 10.00,
  "createdAt": "2026-09-11T21:00:00Z",
  "updatedAt": "2026-09-11T21:00:00Z",
  "version": 0
}
```

Idempotency: same key + same body replays the original `201` and body. Same key +
different body → `409` `IDEMPOTENCY_KEY_CONFLICT`. Missing key → `400`
`MISSING_IDEMPOTENCY_KEY`.

### `GET /api/v1/orders/{id}`

`200` or `404` `ORDER_NOT_FOUND`.

### `GET /api/v1/orders`

Query: `page` (default 0), `size` (default 20, max 100). Sorted by `createdAt` desc.

```json
{
  "content": [],
  "page": 0,
  "size": 20,
  "totalElements": 0,
  "totalPages": 0,
  "first": true,
  "last": true
}
```

### `PATCH /api/v1/orders/{id}/status`

```json
{ "status": "CONFIRMED", "version": 0 }
```

`version` must match `@Version` on the row. Mismatch → `409` `OPTIMISTIC_LOCK_CONFLICT`.
Illegal transition → `409` `ORDER_STATE_CONFLICT`.

Allowed transitions:

- `CREATED` → `CONFIRMED`, `CANCELLED`
- `CONFIRMED` → `PROCESSING`, `CANCELLED`
- `PROCESSING` → `SHIPPED`, `CANCELLED`
- `SHIPPED`, `CANCELLED` are terminal

## Errors

```json
{
  "timestamp": "2026-09-11T21:00:00Z",
  "status": 404,
  "code": "ORDER_NOT_FOUND",
  "message": "Order not found: …",
  "correlationId": "…",
  "errors": []
}
```

Validation failures use `400` `VALIDATION_ERROR` and populate `errors` with
`{ "field", "message" }`.

Send `X-Correlation-Id` to pin a request. The filter echoes it on the response
and puts it in MDC / error JSON. If omitted, the filter generates a UUID.
