---
title: "Organization Admin"
description: "This page outlines the functionalities and architecture related to \"Organization Admin\" within the API, focusing on managing organization-level settings, organizational charts, and contextual data...."
last_updated: "2026-05-06T07:29:41.620964+00:00"
canonical_url: "https://www.doc0.dev/docs/49c89830-117b-4def-8edc-b5bcc50766e0/section-3/organization-admin"
---


This page outlines the functionalities and architecture related to "Organization Admin" within the API, focusing on managing organization-level settings, organizational charts, and contextual data. It covers the API endpoints, service logic, and data structures involved in these administrative tasks, providing a comprehensive overview for developers and administrators.

The core components for organization administration are handled by the `OrganizationController` and `OrganizationService`, which manage details like organization name, logo, and ownership. Additionally, the `OrgChartController` and `OrgChartService` facilitate the creation, update, and deletion of organizational charts, including image uploads. The `ContextController` and `ContextService` provide mechanisms for managing organization-specific contextual data.

<Callout title="Authentication & Authorization" variant="info">
All administrative endpoints generally require authentication, typically handled by the `HybridAuthGuard`. This guard supports both API key authentication (via `X-API-Key` header) and session-based JWT authentication. For session-based authentication, the `X-Organization-Id` header is often required to specify the target organization.
</Callout>

## Organization Management

The `OrganizationController` exposes API endpoints for managing an organization's core properties, including retrieving details, updating information, transferring ownership, and deleting the organization. The `OrganizationService` encapsulates the business logic and database interactions for these operations.

### API Endpoints

The following table summarizes the API endpoints available for organization management:

| Method | Path                       | Description                                   | Authentication |
| :----- | :------------------------- | :-------------------------------------------- | :------------- |
| `GET`  | `/organization`            | Retrieve details of the authenticated organization. | Required       |
| `PATCH`| `/organization`            | Update specific properties of the organization. | Required       |
| `POST` | `/organization/transfer-ownership` | Transfer ownership of the organization to another member. | Required       |
| `DELETE`| `/organization`            | Delete the authenticated organization.        | Required       |
| `GET`  | `/organization/primary-color` | Retrieve the organization's primary color. Can use an access token for public access. | Optional (token) |


### Service Logic

The `OrganizationService` handles the core business logic for organization operations:

*   **`findById(id: string)`**: Retrieves an organization by its ID, selecting specific fields such as `id`, `name`, `slug`, `logo`, `metadata`, `website`, `primaryColor`, and more. Throws `NotFoundException` if the organization does not exist.
*   **`updateById(id: string, updateData: UpdateOrganizationDto)`**: Updates an existing organization's details. It first verifies the organization's existence and then applies the provided `updateData`.
*   **`deleteById(id: string)`**: Deletes an organization after verifying its existence.
*   **`transferOwnership(organizationId: string, currentUserId: string, newOwnerId: string)`**: This critical operation transfers the `owner` role from the `currentUserId` to `newOwnerId` within the specified `organizationId`. It performs several validations:
    *   Ensures `newOwnerId` is provided.
    *   Verifies the `currentUserId` is a member of the organization.
    *   Confirms the `currentUserId` holds the `owner` role.
    *   Checks that the `newOwnerId` corresponds to an active member of the organization.
    *   Prevents transferring ownership to the current owner.
    *   Updates roles for both members in a database transaction: the current owner loses `owner` and gains `admin` (if not already present), and the new owner gains `owner`.
*   **`getPrimaryColor(organizationId: string, token?: string)`**: Retrieves the `primaryColor` of an organization. It supports an optional `token` parameter for public access, which resolves the organization ID via an access grant. If a token is provided and valid, it bypasses standard authentication.


### Ownership Transfer Flow

The ownership transfer process involves several steps, including validation and role updates, ensuring that only authorized users can perform this critical action.



### Data Transfer Objects

The `TransferOwnershipDto` defines the request body for transferring ownership, while `TransferOwnershipResponseDto` describes the response structure.



## Org Chart Management

The `OrgChartController` provides endpoints for managing an organization's chart, allowing for both interactive chart data and image uploads. The `OrgChartService` handles the storage and retrieval, including integration with AWS S3 for image assets.

### API Endpoints

| Method | Path                       | Description                                   | Authentication |
| :----- | :------------------------- | :-------------------------------------------- | :------------- |
| `GET`  | `/org-chart`               | Retrieve the organization chart.              | Required       |
| `PUT`  | `/org-chart`               | Create or update an interactive organization chart (nodes/edges). | Required       |
| `POST` | `/org-chart/upload`        | Upload an image to be used as the organization chart. | Required       |
| `DELETE`| `/org-chart`               | Delete the organization chart.                | Required       |


### Service Logic and S3 Integration

The `OrgChartService` manages the lifecycle of organization charts, distinguishing between `interactive` charts (stored as nodes and edges) and `uploaded` charts (stored as images in S3).

*   **`findByOrganization(organizationId: string)`**: Retrieves the organization chart for a given organization. If the chart is an `uploaded` type with an `uploadedImageUrl`, it generates a presigned S3 URL for temporary access to the image.
*   **`upsertInteractive(organizationId: string, data: UpsertOrgChartDto)`**: Creates or updates an interactive chart. If a previous chart was an uploaded image, it triggers the deletion of the old S3 object *after* the database update succeeds to prevent orphaned S3 files.
*   **`uploadImage(organizationId: string, data: UploadOrgChartDto)`**: Handles the upload of an image file to S3.
    *   Validates the file type against `ALLOWED_UPLOAD_MIME_TYPES` (PNG, JPEG, GIF, WebP, SVG, BMP, TIFF, PDF).
    *   Enforces a `MAX_FILE_SIZE_BYTES` limit (100MB).
    *   Deletes any existing uploaded image from S3 before uploading the new one.
    *   Uploads the base64 encoded image data to S3, generating a unique key.
    *   Updates the database record to reflect the `uploaded` chart type and the S3 key.
    *   Returns a presigned URL for the newly uploaded image.
*   **`delete(organizationId: string)`**: Deletes the organization chart record from the database. If an S3 image was associated, it also deletes the corresponding object from S3.
*   **`getSignedUrl(s3Key: string)`**: Private helper method to generate a presigned URL for an S3 object, expiring in 15 minutes (`SIGNED_URL_EXPIRY`).
*   **`deleteS3Object(s3Key: string)`**: Private helper method to delete an object from S3.


### Org Chart Image Upload Flow

The process for uploading an organizational chart image involves client-side preparation, API interaction, and S3 storage.



## Context Management

The `ContextController` and `ContextService` provide a way to manage organization-specific contextual data. While not strictly "admin" in the sense of managing the organization itself, it allows administrators to define and manage data relevant to their organization's operations.

### API Endpoints

| Method | Path                       | Description                                   | Authentication |
| :----- | :------------------------- | :-------------------------------------------- | :------------- |
| `GET`  | `/context`                 | Retrieve all context entries for the organization. | Required       |
| `GET`  | `/context/:id`             | Retrieve a specific context entry by ID.      | Required       |
| `POST` | `/context`                 | Create a new context entry.                   | Required       |
| `PATCH`| `/context/:id`             | Update an existing context entry.             | Required       |
| `DELETE`| `/context/:id`             | Delete a context entry.                       | Required       |


### Service Logic

The `ContextService` provides standard CRUD operations for context entries:

*   **`findAllByOrganization(organizationId: string)`**: Fetches all context entries associated with a given `organizationId`, ordered by creation date.
*   **`findById(id: string, organizationId: string)`**: Retrieves a single context entry by its ID, ensuring it belongs to the specified `organizationId`. Throws `NotFoundException` if not found.
*   **`create(organizationId: string, createContextDto: CreateContextDto)`**: Creates a new context entry, associating it with the `organizationId`.
*   **`updateById(id: string, organizationId: string, updateContextDto: UpdateContextDto)`**: Updates an existing context entry. It first validates the entry's existence and ownership before applying updates.
*   **`deleteById(id: string, organizationId: string)`**: Deletes a context entry after verifying its existence and ownership.


## Sitemap

See the full [sitemap](https://www.doc0.dev/docs/49c89830-117b-4def-8edc-b5bcc50766e0/llms.txt) for all pages in this wiki.
