# CreatePartnerAddress

## Permission Scope

partnerDetail

## Overview

createPartnerAddress adds a physical or mailing address to a business partner. Addresses are categorized by type and can be designated as default for their type. Partners in DRAFT or ACTIVE status can have addresses added, but INACTIVE partners cannot receive new addresses. When an address is set as default, any previous default of the same type for that partner is automatically unset.

## Business Rules

- Partner must exist in the system
- Partner must be in DRAFT or ACTIVE status (not INACTIVE)
- Line1, city, postal code, and country are required
- Country must be a valid ISO 3166-1 alpha-2 code
- If isDefault is true, the previous default address of the same type for the partner is unset

## Process Flow

```mermaid
flowchart TD
    A[Receive create request] --> B{Partner exists?}
    B -->|No| C[Return error: partner not found]
    B -->|Yes| D{Partner is INACTIVE?}
    D -->|Yes| E[Return error: partner inactive]
    D -->|No| H1{line1, city, postalCode non-empty?}
    H1 -->|No| H2[Return error: missing required address field]
    H1 -->|Yes| H{Country code valid?}
    H -->|No| I[Return error: invalid country code]
    H -->|Yes| J{isDefault true?}
    J -->|Yes| K[Unset previous default of same type]
    K --> L[Create address as default]
    J -->|No| L[Create address record]
    L --> M[Return created address]
```

## External Dependencies

- None

## Error Scenarios

- **PARTNER_NOT_FOUND**: Specified partner ID does not exist
- **PARTNER_INACTIVE**: Partner is in INACTIVE status
- **MISSING_REQUIRED_ADDRESS_FIELD**: One or more required fields (line1, city, postalCode) are empty or not provided
- **INVALID_COUNTRY_CODE**: Country is not a valid ISO 3166-1 alpha-2 code

## Test Cases

- creates address for active partner
- creates address for draft partner
- returns error when partner not found
- returns error when partner is inactive
- returns error when required fields missing
- returns error when country code is invalid
- returns error when country code is lowercase
- creates non-default address
- unsets previous default when creating with isDefault true
