# DeleteWorkRule

## Permission Scope

`workRule`

## Overview

Removes a `WorkRule` generation, but only when no current assignment (`assignWorkRule`) references it, so an assigned worker can never lose their rule unexpectedly.

## Business Rules

- Deletion is rejected when a current `WorkRuleAssignment` binds this `WorkRule` generation, or when an in-force `EligibilityRule` grants it (grantType `WORK_RULE`) — either reference means removing the rule would strand live configuration
- Deleting a generation does not alter sibling generations sharing the same `versionOf`
- Deletion is for a generation entered in error; a policy change is expressed via update, which closes-and-inserts rather than deletes

## Process Flow

```mermaid
flowchart TD
    A[Receive WorkRule id] --> B{WorkRule exists?}
    B -- No --> C[Reject: WORK_RULE_NOT_FOUND]
    B -- Yes --> D{Referenced by any current assignment?}
    D -- Yes --> E[Reject: WORK_RULE_IN_USE]
    D -- No --> F[Delete WorkRule generation]
```

## External Dependencies

- [time-tracking::WorkRule](../model/WorkRule.md) model — the entity this command deletes

## Error Scenarios

- **WORK_RULE_NOT_FOUND**: no WorkRule exists for the given id, or no WorkRule is effective for the relevant date
- **WORK_RULE_IN_USE**: a current assignment references this `WorkRule` and it cannot be deleted

## Test Cases

- deleting a WorkRule referenced by a current assignment is rejected
- deleting a WorkRule referenced by an in-force eligibility grant is rejected
- deleting a WorkRule with no referencing assignment succeeds
- deleting a non-existent WorkRule id is rejected

