# CalculateCategoryDepth

## Overview

CalculateCategoryDepth determines the depth of a product category in the tree hierarchy by walking up the ancestor chain from a given category to the root. Depth is defined as the number of nodes in the path from root to the target category (root = depth 1). This is essential for enforcing maximum depth constraints when creating or moving categories.

## Business Rules

- Depth is calculated by traversing parentId references from the target category up to the root
- Root categories (parentId = null) have depth 1
- Each additional ancestor increments the depth by 1
- The traversal stops when a category with parentId = null is reached or a parent is not found
- Used together with maxDepth parameter to enforce tree depth limits

## Process Flow

```mermaid
flowchart TD
    A[Receive categoryId] --> B[SELECT category where id = categoryId]
    B --> C{Category found?}
    C -->|No| D[Return error: CATEGORY_NOT_FOUND]
    C -->|Yes| E[Initialize depth = 1, currentId = parentId]
    E --> F{currentId is null?}
    F -->|Yes| G[Return current depth]
    F -->|No| H[SELECT ancestor where id = currentId]
    H --> I{Ancestor found?}
    I -->|No| G
    I -->|Yes| J[Increment depth]
    J --> K[Set currentId = ancestor.parentId]
    K --> F
```

## External Dependencies

- None

## Error Scenarios

- **CATEGORY_NOT_FOUND**: Specified category ID does not exist

## Test Cases

- returns depth 1 for root category
- returns depth 2 for child of root
- returns depth 3 for grandchild of root
- returns CategoryNotFoundError when category does not exist
- returns partial depth when ancestor in chain is not found
