# CreateEligibilityRule

## Permission Scope

`eligibility`

## Overview

Declares the initial effective-dated generation of an eligibility grant: that a target (worker, position, job-profile, employment type, or work regime) may be assigned a `WorkRule`. The grant is `WORK_RULE`-only (`grantType` is a single-value enum); the former `TIME_ENTRY_CODE` grant and its `timeEntryCodeKey` field were removed in #39.

## Business Rules

- The target is bound by stable id — worker id, position id, job-profile id, or an EmploymentType / WorkRegime catalog id — via `targetType` (discriminator) + `targetId`, never by display name
- The `targetId` must resolve to an existing workforce entity of its `targetType`; a well-formed but dangling id is rejected so a rule can never grant a nonexistent target
- The grant is bound by stable key — a `WorkRule` identity — never by display name; `grantType` is a single-value enum (`WORK_RULE`)
- The created generation has `effectiveEnd IS NULL` (current) and must not overlap an existing generation for the same target + grant combination
- A future-dated `effectiveStart` creates a scheduled grant that does not take effect until that date

## Process Flow

```mermaid
flowchart TD
    A[Receive targetType + targetId, grant WorkRule, effectiveStart] --> B{targetId present?}
    B -- No --> C[Reject: INVALID_TARGET]
    B -- Yes --> T{Target resolves in workforce?}
    T -- No --> TN[Reject: TARGET_NOT_FOUND]
    T -- Yes --> D{Grant WorkRule resolvable?}
    D -- No --> E[Reject: INVALID_GRANT]
    D -- Yes --> F{Overlaps existing generation for target+grant?}
    F -- Yes --> G[Reject: EFFECTIVE_DATE_OVERLAP]
    F -- No --> H[Create EligibilityRule generation, effectiveEnd = null]
    H --> I[Target's assignable WorkRule set now includes this grant asOf effectiveStart]
```

## External Dependencies

- [time-tracking::EligibilityRule](../model/EligibilityRule.md) model — the entity this command creates
- [time-tracking::WorkRule](../model/WorkRule.md) model — resolved by identity as the granted work rule

## Error Scenarios

- **INVALID_TARGET**: no `targetId` was supplied for the target
- **TARGET_NOT_FOUND**: the target does not resolve to an existing workforce entity
- **INVALID_GRANT**: the grant does not resolve to a valid WorkRule (a workRuleId is required)
- **EFFECTIVE_DATE_OVERLAP**: the new generation's effective range overlaps an existing generation

## Test Cases

- a target and grant are stored by id, never by display name
- a future-dated grant is not returned as in force before its `effectiveStart`
- creating a grant with an unresolvable target is rejected
- creating a grant whose target id does not resolve to a workforce entity is rejected
- creating a grant with an unresolvable WorkRule id is rejected
- creating a grant that overlaps an existing generation for the same target and grant is rejected

