# ReactivateAuditPolicy

## Permission Scope

auditPolicy

## Overview

ReactivateAuditPolicy transitions an audit policy from INACTIVE back to ACTIVE status, resuming audit capture. Reactivation is rejected if a conflicting ACTIVE policy already exists for the same (entityName, companyId, operationType) combination.

This command requires the `manageAuditPolicies` permission at the appropriate scope.

## Business Rules

- Only policies in INACTIVE status can be reactivated
- At most one ACTIVE policy can exist for a given (entityName, companyId, operationType) combination
- If a conflicting ACTIVE policy exists, reactivation is rejected
- Caller must hold `manageAuditPolicies` permission at the appropriate scope
- A caller with company-scoped `manageAuditPolicies` can only reactivate policies for their assigned companies
- A caller with global-scoped `manageAuditPolicies` can only reactivate global policies

## Process Flow

```mermaid
flowchart TD
    A[Receive reactivate request] --> B{Caller has manageAuditPolicies?}
    B -->|No| C[Return error: INSUFFICIENT_PERMISSION]
    B -->|Yes| D{Policy exists?}
    D -->|No| E[Return error: POLICY_NOT_FOUND]
    D -->|Yes| D2{Caller scope covers this policy?}
    D2 -->|No| D3[Return error: UNAUTHORIZED]
    D2 -->|Yes| F{Policy in INACTIVE status?}
    F -->|No| G[Return error: INVALID_STATE]
    F -->|Yes| H{Conflicting ACTIVE policy exists?}
    H -->|Yes| I[Return error: CONFLICTING_ACTIVE_POLICY]
    H -->|No| J[Set status to ACTIVE]
    J --> K[Return reactivated policy]
```

## External Dependencies

- None

## Error Scenarios

- **INSUFFICIENT_PERMISSION**: Caller lacks the required permission for this operation
- **UNAUTHORIZED**: Caller's permission scope does not cover the target resource
- **POLICY_NOT_FOUND**: Specified policy ID does not exist
- **INVALID_STATE**: Target entity is not in a valid status for this operation
- **CONFLICTING_ACTIVE_POLICY**: An ACTIVE policy already exists for the same (entityName, companyId, operationType)

## Test Cases

- reactivates INACTIVE policy to ACTIVE
- resumes audit capture after reactivation
- rejects reactivation of DRAFT policy
- rejects reactivation of ACTIVE policy
- rejects reactivation when conflicting ACTIVE policy exists
- rejects when policy does not exist
- rejects when caller lacks manageAuditPolicies permission
- rejects when company-scoped caller reactivates policy for a different company
- rejects when global-scoped caller reactivates company-scoped policy
