# GetSerialNumberHistory

## Overview

GetSerialNumberHistory retrieves the full transaction history for a serial number — all state transitions, stock movements, and location changes. This provides a complete audit trail ordered chronologically.

## Business Rules

- Requires `serialNumberId`
- Returns an ordered list of history entries, each containing:
  - `timestamp` — when the event occurred
  - `fromState` — previous serial number status
  - `toState` — new serial number status
  - `stockMovementId` — reference to the associated stock movement
  - `fromLocationId` — previous storage location
  - `toLocationId` — new storage location
- Queries all StockMoveLine records referencing this serial number
- Results are ordered by movement date ascending (oldest first)
- Returns error if the serial number does not exist
- Returns an empty history for a newly created serial with no movements

## Process Flow

```mermaid
flowchart TD
    A[Receive serialNumberId] --> B{Serial number exists?}
    B -->|No| C[Return SERIAL_NUMBER_NOT_FOUND error]
    B -->|Yes| D[SELECT from StockMoveLine where serialNumberId matches]
    D --> E[JOIN with StockMovement for dates and references]
    E --> F[Order by movement date ascending]
    F --> G{Records found?}
    G -->|Yes| H[Return ordered list of history entries]
    G -->|No| I[Return empty history]
```

## External Dependencies

- None

## Error Scenarios

- **SERIAL_NUMBER_NOT_FOUND**: The specified serial number does not exist

## Test Cases

- returns full history ordered by date
- includes state transitions
- includes location changes
- returns error when serial not found
- returns empty history for newly created serial
