# CreateRouting

## Permission Scope

manufacturingMaster

## Overview

CreateRouting creates a draft process definition for one manufactured item or scope. The command stores ordered operation payload, standard times, instructions, and work-center assignments that later activation validates.

## Business Rules

- Target manufactured item and company scope are required.
- At least one operation is required.
- Operation sequence numbers must be unique within the routing.
- Standard setup and run times must be zero or greater.
- Every operation must reference a valid work center in the same company scope.
- New routings are created in `DRAFT`.

## Process Flow

```mermaid
flowchart TD
    A[Receive create request] --> B[Validate parent item and scope]
    B --> C[Validate operation payload]
    C --> D{Sequence unique?}
    D -->|No| E[Return DUPLICATE_OPERATION_SEQUENCE]
    D -->|Yes| F[Create draft routing and operations]
    F --> G[Return draft routing]
```

## External Dependencies

- [Item](../../../item-management/docs/models/Item.md) - The routing targets one manufactured parent item.
- [WorkCenter](../models/WorkCenter.md) - Each operation must reference a valid work center.

## Error Scenarios

- **ROUTING_ITEM_NOT_FOUND**: Referenced parent item does not exist.
- **OPERATION_REQUIRED**: Routing has no operations
- **DUPLICATE_OPERATION_SEQUENCE**: Two operations use the same sequence number.
- **INVALID_STANDARD_TIME**: Setup or run time is negative.
- **WORK_CENTER_NOT_FOUND**: Referenced work center does not exist
- **CROSS_COMPANY_WORK_CENTER**: A work center belongs to a different company scope.

## Test Cases

- creates a draft routing with ordered operations
- returns error when no operations are provided
- returns error when two operations share the same sequence
- returns error when a standard time is negative
- returns error when a referenced work center does not exist
- returns error when a work center is outside the routing company
- returns error when parent item does not exist
