# User

## Description

User represents an identity in the ERP system with a lifecycle managed through status transitions. Each user has an email (unique identifier), name, and status that controls system access. The status-based lifecycle ensures controlled provisioning: users start PENDING until verified, become ACTIVE for full access, and transition to INACTIVE when access should be revoked while preserving data for audit purposes.

This model provides the foundation for all identity-related operations in the ERP system.

## Domain Model Definitions

### Model type

Stateful

#### State Transitions

```mermaid
stateDiagram-v2
    [*] --> Pending: createUser
    Pending --> Active: activateUser
    Active --> Inactive: deactivateUser
    Inactive --> Active: reactivateUser
```

| Operation | From | To | Command |
|-----------|------|----|---------|
| activate | PENDING | ACTIVE | [activateUser](../command/ActivateUser.md) |
| deactivate | ACTIVE | INACTIVE | [deactivateUser](../command/DeactivateUser.md) |
| reactivate | INACTIVE | ACTIVE | [reactivateUser](../command/ReactivateUser.md) |

### Command Definitions

- [createUser](../command/CreateUser.md)
- [updateUser](../command/UpdateUser.md)
- [updateOwnProfile](../command/UpdateOwnProfile.md)
- [activateUser](../command/ActivateUser.md)
- [deactivateUser](../command/DeactivateUser.md)
- [reactivateUser](../command/ReactivateUser.md)

### Query Definitions

### Models

- User

### Invariants

- Email must be unique across all users (active and inactive)
- Email must follow valid email format
- Name is required and cannot be empty
- Status must be one of PENDING, ACTIVE, or INACTIVE
- PENDING and ACTIVE users can receive role assignments; INACTIVE users cannot
- Permissions array is a computed cache of all permission keys from the user's assigned roles while ACTIVE (empty while PENDING or INACTIVE), updated by an executor when role permissions, assignments, or the user's status change

### Relationships

- **Has Many UserRoles**: User is assigned roles through UserRole join records
