> ## Documentation Index
> Fetch the complete documentation index at: https://compass.docs.sunnyscoach.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Get Department Hierarchy

> Admin-only endpoint that retrieves the full hierarchical structure of all departments.   Each department may include nested child departments inside the `children` array.


Admin-only endpoint that retrieves the full hierarchical structure of all departments. Each department may include nested child departments inside the `children` array.

## Request

### Headers

| Name          | Type   | Required | Description  |
| ------------- | ------ | -------- | ------------ |
| Authorization | string | Yes      | Bearer token |

## Response

### 200 OK - Successfully retrieved department hierarchy

```json theme={null}
{
  "message": "Department hierarchy retrieved successfully",
  "data": {
    "departments": [
      {
        "_id": "64b7f2b3e4b0a5d3f9c54321",
        "name": "Customer Support",
        "description": "Handles customer inquiries and support tickets",
        "parentId": null,
        "createdAt": "2024-01-15T10:30:00.000Z",
        "updatedAt": "2024-01-15T10:30:00.000Z",
        "children": [
          {
            "_id": "64b7f3c4e4b0a5d3f9c98765",
            "name": "Tier 1 Support",
            "description": "First level customer support",
            "parentId": "64b7f2b3e4b0a5d3f9c54321",
            "createdAt": "2024-01-15T10:30:00.000Z",
            "updatedAt": "2024-01-15T10:30:00.000Z",
            "children": [
              {
                "_id": "64b7f4d5e4b0a5d3f9c11111",
                "name": "Email Support",
                "description": "Email-based customer support",
                "parentId": "64b7f3c4e4b0a5d3f9c98765",
                "createdAt": "2024-01-15T10:30:00.000Z",
                "updatedAt": "2024-01-15T10:30:00.000Z",
                "children": []
              }
            ]
          },
          {
            "_id": "64b7f5e6e4b0a5d3f9c22222",
            "name": "Tier 2 Support",
            "description": "Second level escalated support",
            "parentId": "64b7f2b3e4b0a5d3f9c54321",
            "createdAt": "2024-01-15T10:30:00.000Z",
            "updatedAt": "2024-01-15T10:30:00.000Z",
            "children": []
          }
        ]
      },
      {
        "_id": "64b7f6f7e4b0a5d3f9c33333",
        "name": "Engineering",
        "description": "Software development and technical operations",
        "parentId": null,
        "createdAt": "2024-01-15T10:30:00.000Z",
        "updatedAt": "2024-01-15T10:30:00.000Z",
        "children": []
      }
    ]
  }
}
```

### 401 Unauthorized

```json theme={null}
{
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Missing or invalid token or insufficient permissions"
  }
}
```

### 500 Internal Server Error

```json theme={null}
{
  "error": {
    "code": "SERVER_ERROR",
    "message": "Internal server error"
  }
}
```

## Example

```bash theme={null}
curl -X GET 'http://localhost:2000/department/hierarchy' \
  -H 'Authorization: Bearer your-jwt-token'
```

## Notes

* This is an admin-only endpoint - requires administrative privileges
* Returns departments in a hierarchical tree structure
* Root departments have `parentId: null`
* Child departments are nested in the `children` array
* The hierarchy can have multiple levels of nesting
* Use this for organizational charts or department trees
* Departments are sorted alphabetically within each level
* Empty `children` arrays indicate leaf departments (no sub-departments)

## Use Cases

* **Organizational Charts**: Display company structure
* **Department Selection**: Create hierarchical dropdown menus
* **Reporting Structure**: Show management hierarchy
* **Access Control**: Implement department-based permissions
* **Analytics**: Analyze departmental data with hierarchy context

## Performance Considerations

* Large organizational hierarchies may take longer to load
* Consider caching the hierarchy for frequently accessed data
* The endpoint returns the complete hierarchy in a single request
* For very large organizations, consider using the list endpoint with pagination


## OpenAPI

````yaml GET /department/hierarchy
openapi: 3.0.3
info:
  title: COMPASS API
  version: 1.0.0
  description: OpenAPI specification for the Compass backend routes.
servers:
  - url: http://localhost:2000
    description: Local development server
security: []
tags:
  - name: Auth
    description: >-
      Authentication and session management endpoints (OAuth and session
      validation)
  - name: Users
    description: Operations for managing user records
  - name: Departments
    description: Operations for managing departments
  - name: Email Meter
    description: APIs to get Email Meter Stats
  - name: HubSpot Tickets
    description: Feedback APIs
  - name: Permissions
    description: Permissions APIs
  - name: Transcription
    description: Transcription APIs
paths:
  /department/hierarchy:
    get:
      tags:
        - Departments
      summary: Get department hierarchy
      description: >
        Admin-only endpoint that retrieves the full hierarchical structure of
        all departments.   Each department may include nested child departments
        inside the `children` array.
      responses:
        '200':
          description: Successfully retrieved department hierarchy
          content:
            application/json:
              schema:
                type: object
                properties:
                  message:
                    type: string
                  data:
                    type: object
                    properties:
                      departments:
                        type: array
                        items:
                          $ref: '#/components/schemas/DepartmentWithChildren'
        '401':
          description: Unauthorized (missing/invalid token or insufficient permissions)
        '500':
          description: Server error
      security:
        - bearerAuth: []
components:
  schemas:
    DepartmentWithChildren:
      allOf:
        - $ref: '#/components/schemas/Department'
        - type: object
          properties:
            employeesCount:
              type: integer
              description: Number of employees in this department
              example: 25
            children:
              type: array
              description: Nested child departments
              items:
                $ref: '#/components/schemas/DepartmentWithChildren'
    Department:
      type: object
      properties:
        _id:
          type: string
          example: 64ef3c29f9a1c27e1b2c3a4d
        name:
          type: string
          example: Engineering
        parentDepartment:
          type: object
          description: Parent department info (if applicable)
          example:
            _id: 64ef3c29f9a1c27e1b2c3aaa
            name: Technology
            description: Some description for this department
            code: TECH
        manager:
          $ref: '#/components/schemas/MatchSuggestionUser'
          type: object
          description: Manager info attached to department
        description:
          type: string
          example: Handles all software engineering operations
    MatchSuggestionUser:
      type: object
      properties:
        _id:
          type: string
          example: 64ef3c29f9a1c27e1b2c3a4d
        emails:
          type: array
          items:
            type: string
          example:
            - user@example.com
        personalInformation:
          type: object
          properties:
            firstName:
              type: string
              example: John
            lastName:
              type: string
              example: Doe
            name:
              type: string
              example: John Doe
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

````