# UpdateContactPerson

## Permission Scope

contactPerson

## Overview

updateContactPerson modifies the details of an existing contact person. All fields are optional, but validation rules are enforced for any provided values. The contact person's organization association is fixed by the existing record and is not part of the update input. After the update, at least one communication channel (email or phone) must remain present.

## Business Rules

- Contact person must exist in the system
- First name, if provided, must be non-empty and not whitespace-only
- Last name, if provided, must be non-empty and not whitespace-only
- Email, if provided, must be in a valid format
- After update, at least one of email or phone must still be present (whitespace-only values do not count)
- The organization association is not part of the update input
- The primary contact designation (`isPrimary`) is not part of the update input; the existing value is preserved as-is
- Only provided fields are updated; omitted fields retain their current values

## Process Flow

```mermaid
flowchart TD
    A[Receive update request] --> B{Contact person exists?}
    B -->|No| C[Return error: contact person not found]
    B -->|Yes| B2{firstName provided?}
    B2 -->|Yes| B3{firstName non-empty after trim?}
    B3 -->|No| B4[Return error: invalid first name]
    B3 -->|Yes| B5{lastName provided?}
    B2 -->|No| B5
    B5 -->|Yes| B6{lastName non-empty after trim?}
    B6 -->|No| B7[Return error: invalid last name]
    B6 -->|Yes| D{Email provided?}
    B5 -->|No| D
    D -->|Yes| E{Email format valid?}
    E -->|No| F[Return error: invalid email format]
    E -->|Yes| G{At least one communication channel remains?}
    D -->|No| G
    G -->|No| H[Return error: missing communication channel]
    G -->|Yes| I[Update contact person record]
    I --> J[Return updated contact person]
```

## External Dependencies

- None

## Error Scenarios

- **CONTACT_PERSON_NOT_FOUND**: Specified contact person ID does not exist
- **INVALID_FIRST_NAME**: First name is empty, whitespace-only, or not provided
- **INVALID_LAST_NAME**: Last name is empty, whitespace-only, or not provided
- **INVALID_EMAIL_FORMAT**: Provided email is not in a valid format
- **MISSING_COMMUNICATION_CHANNEL**: Neither email nor phone was provided

## Test Cases

- updates contact person details
- returns error when contact not found
- returns error when first name is empty
- returns error when first name is whitespace-only
- returns error when last name is empty
- returns error when last name is whitespace-only
- returns error when email format is invalid
- returns error when update would leave no communication channel
- returns error when whitespace-only phone would leave no communication channel
- does not change organization association
- preserves primary contact designation
