# UpdateSite

## Permission Scope

organizationUnit

## Overview

UpdateSite modifies fields on an existing site. Updatable fields include name, type, address fields, country, and timezone. Company ownership is fixed by the existing record and is not part of the update input. Updating is permitted on both ACTIVE and INACTIVE sites.

This command supports address corrections, type reclassification, and timezone updates.

## Business Rules

- Site must exist
- Name, if provided, must be non-empty
- Name, if changed, must remain unique within the same company
- Type, if provided, must be one of the allowed classifications
- companyId is not part of the update input
- Timezone, if provided, must be a valid IANA timezone identifier
- Country, if provided, must reference a valid country code
- Address fields can be individually updated
- Required address fields (street, city, postalCode, country), if provided, must be non-empty
- State/province is optional and may be set or cleared
- Updating is allowed on both ACTIVE and INACTIVE sites

## Process Flow

```mermaid
flowchart TD
    A[Receive update request] --> B{Site exists?}
    B -->|No| C[Return error: SITE_NOT_FOUND]
    B -->|Yes| D{Name provided and empty?}
    D -->|Yes| E[Return error: INVALID_NAME]
    D -->|No| F{Name changed?}
    F -->|Yes| G{Name unique in company?}
    G -->|No| H[Return error: DUPLICATE_NAME]
    G -->|Yes| I{Type valid?}
    F -->|No| I
    I -->|Invalid| J[Return error: INVALID_TYPE]
    I -->|Valid or unchanged| I2{Required address field set to empty?}
    I2 -->|Yes| I3[Return error: MISSING_REQUIRED_FIELDS]
    I2 -->|No| K{Country valid?}
    K -->|Invalid| K2[Return error: INVALID_COUNTRY]
    K -->|Valid or unchanged| L{Timezone valid?}
    L -->|Invalid| L2[Return error: INVALID_TIMEZONE]
    L -->|Valid or unchanged| M[Update site record]
    M --> N[Return updated site]
```

## External Dependencies

- None

## Error Scenarios

- **SITE_NOT_FOUND**: Specified site ID does not exist
- **INVALID_NAME**: Name is empty, whitespace-only, or not provided
- **DUPLICATE_NAME**: Site with same name already exists in the same company
- **INVALID_TYPE**: Site type is not one of the allowed classifications
- **MISSING_REQUIRED_FIELDS**: One or more required fields are missing or empty
- **INVALID_COUNTRY**: Country code is not a valid country code
- **INVALID_TIMEZONE**: Timezone is not a valid IANA timezone identifier

## Test Cases

- updates site name
- updates site address fields
- updates site type
- updates site timezone
- returns error when site does not exist
- returns error when name is empty
- returns error when name already exists in same company
- returns error when type is invalid
- returns error when country code is invalid
- returns error when timezone is invalid
- updates site country
- returns error when street is set to empty
- returns error when city is set to empty
- returns error when postalCode is set to empty
- returns error when country is set to empty
- updates state/province field
- clears state/province field
- updates address fields on INACTIVE site
