# UpdateOwnProfile

## Overview

UpdateOwnProfile allows an ACTIVE user to update their own profile fields (name, email). The command implicitly targets the calling user — no userId parameter is needed. This provides a self-service update path that is separate from the admin-only updateUser command.

Only ACTIVE users can update their own profile. PENDING and INACTIVE users are rejected.

## Business Rules

- Caller must be an existing user (resolved from context)
- Caller must be in ACTIVE status
- At least one field (name or email) must be provided
- Name, if provided, must be non-empty
- Email, if provided, must follow valid email format
- Email must be unique across all users (active and inactive); case-insensitive comparison
- Generates USER_UPDATED audit event with actor ID, timestamp, and changed fields

## Process Flow

```mermaid
flowchart TD
    A[Receive update request] --> B{Caller user exists?}
    B -->|No| C[Return error: USER_NOT_FOUND]
    B -->|Yes| D{Caller status is ACTIVE?}
    D -->|No| E[Return error: INVALID_STATUS_TRANSITION]
    D -->|Yes| F{At least one field provided?}
    F -->|No| G[Return error: MISSING_REQUIRED_FIELD]
    F -->|Yes| H{Name provided and empty?}
    H -->|Yes| I[Return error: INVALID_NAME]
    H -->|No| J{Email provided?}
    J -->|Yes| K{Valid email format?}
    K -->|No| L[Return error: INVALID_EMAIL]
    K -->|Yes| M{Email unique?}
    M -->|No| N[Return error: USER_ALREADY_EXISTS]
    M -->|Yes| O[Update user record]
    J -->|No| O
    O --> P[Log USER_UPDATED audit event]
    P --> Q[Return updated user]
```

## External Dependencies

- None

## Error Scenarios

- **USER_NOT_FOUND**: Caller's user ID does not exist in the system
- **INVALID_STATUS_TRANSITION**: Caller is not in ACTIVE status (PENDING or INACTIVE)
- **MISSING_REQUIRED_FIELD**: Neither name nor email was provided in the update input
- **INVALID_NAME**: Name is empty or whitespace only
- **INVALID_EMAIL**: Email does not follow valid email format
- **USER_ALREADY_EXISTS**: Email address is already registered by another user

## Test Cases

- updates own name
- updates own email
- updates both own name and email
- returns error when caller user does not exist
- returns error when caller is in PENDING status
- returns error when caller is in INACTIVE status
- returns error when no fields are provided
- returns error when name is empty
- returns error when name is whitespace only
- returns error when email format is invalid
- returns error when email is already used by another user
- succeeds when email is unchanged (same as current)
