# DeactivateDepartment

## Permission Scope

organizationUnit

## Overview

DeactivateDepartment transitions a department from ACTIVE to INACTIVE status. A department cannot be deactivated while it has active sub-departments. Historical references to the department remain intact.

## Business Rules

- Department must exist
- Department must be in ACTIVE status
- Department must not have any active sub-departments
- Deactivation does not affect historical records referencing this department

## Process Flow

```mermaid
flowchart TD
    A[Receive deactivate request] --> B{Department exists?}
    B -->|No| C[Return error: DEPARTMENT_NOT_FOUND]
    B -->|Yes| D{Department in ACTIVE?}
    D -->|No| E[Return error: INVALID_STATE]
    D -->|Yes| F{Has active sub-departments?}
    F -->|Yes| G[Return error: HAS_ACTIVE_CHILDREN]
    F -->|No| H[Set status: INACTIVE]
    H --> I[Return deactivated department]
```

## External Dependencies

- None

## Error Scenarios

- **DEPARTMENT_NOT_FOUND**: Specified department ID does not exist
- **INVALID_STATE**: Target entity is not in a valid status for this operation
- **HAS_ACTIVE_CHILDREN**: Department has one or more active sub-departments

## Test Cases

- deactivates ACTIVE department with no sub-departments
- deactivates ACTIVE department with only INACTIVE sub-departments
- throws when department does not exist
- throws when department is already INACTIVE
- throws when department has active sub-departments
