# Block Config API Reference

Detailed field definitions and examples for creating dashboard blocks via API. Each block has a `config` object that controls what data is displayed and how.

## Block Fields

| Field | Type | Description |
|-------|------|-------------|
| `identifier` | string | Unique ID. Auto-generated if omitted. Can be any string (e.g., `revenue`, `top-products`). |
| `title` | string | Display title shown above the block. |
| `description` | string | Optional description text. |
| `pos` | int[] | Grid position as `[row, column]`. The grid has 20 columns. |
| `size` | int[] | Grid size as `[width, height]`. **Minimum: `[3, 6]`**. Blocks smaller than this will render incorrectly. |
| `config` | object | Block configuration — see sections below. |

### Default Sizes by Renderer

**IMPORTANT:** Always use at least these default sizes. Never set height below 6 — blocks will be cut off or invisible.
**Format:** `[width, height]`. 
**Minimum:** `[3, 6]`.

| Renderer | Default size | Description |
|----------|-------------|-------------|
| `single` | `[4, 8]` | KPI block — fits ~5 per row |
| `chart` | `[10, 12]` | Half-width chart |
| `table` | `[10, 12]` | Half-width table |
| `html` | `[10, 6]` | Text/HTML content |

For full-width blocks, use width `20`. For side-by-side layout, use width `10`.

### Positioning Blocks

**Format:** `[row, column]`.

**Column** is always `0`. The grid auto-arranges blocks horizontally.

**Row** formula: `row = max(pos[0] + size[1])` across all existing blocks, or `0` if no blocks yet.

In other words: find the block with the highest `pos[0] + size[1]` (bottom edge) — that value is the new block's row.

Example — creating 3 blocks:
- First block (single, height 8): `pos: [0, 0]` → bottom edge: `0 + 8 = 8`
- Second block (single, height 8): `pos: [8, 0]` → bottom edge: `8 + 8 = 16`
- Third block (chart, height 12): `pos: [16, 0]` → bottom edge: `16 + 12 = 28`

## Config Structure

The `config` object always has:

| Field | Type | Description |
|-------|------|-------------|
| `renderer` | string | **Required.** One of: `single`, `table`, `chart`, `html`. |
| `filters` | array | Optional. Array of filter objects applied to the data query. |
| `date_range` | object | Optional. Override the board's date range for this block. |

Plus renderer-specific fields placed directly on the `config` object (flat structure — NOT nested under the renderer name).

## Renderer: `single`

Displays one metric value with optional comparison and sparkline.

| Field | Required | Description |
|-------|----------|-------------|
| `column` | **Yes** | Metric column (e.g., `sales_order|entity_id__cnt`). |
| `compare` | No | Comparison mode. Use `period` to compare with previous period. |
| `spark_line` | No | `true` to show a trend line below the value. |

```json
{
  "identifier": "order-count",
  "title": "Number of Orders",
  "size": [4, 8],
  "pos": [0, 0],
  "config": {
    "renderer": "single",
    "column": "sales_order|entity_id__cnt",
    "compare": "period",
    "spark_line": true
  }
}
```

## Renderer: `table`

Displays rows grouped by dimensions with metric columns.

| Field | Required | Description |
|-------|----------|-------------|
| `dimensions` | **Yes** | Array of grouping columns (e.g., `["sales_order_item|sku"]`). Must be non-empty. |
| `columns` | No | Array of metric/display columns. |
| `sort_orders` | No | Array of sort order objects: `{"column": "...", "direction": "asc\|desc"}`. |
| `page_size` | No | Max rows per page. Default depends on block size. |
| `base_table` | No | Override the base reporting table. |

```json
{
  "identifier": "top-products",
  "title": "Top Products by Revenue",
  "size": [10, 12],
  "pos": [0, 0],
  "config": {
    "renderer": "table",
    "dimensions": ["sales_order_item|sku"],
    "columns": [
      "catalog_product_entity|name",
      "sales_order|entity_id__cnt",
      "sales_order_item|qty_ordered__sum",
      "sales_order_item|row_total__sum"
    ],
    "sort_orders": [
      {"column": "sales_order_item|row_total__sum", "direction": "desc"}
    ],
    "page_size": 20
  }
}
```

## Renderer: `chart`

Displays data as a chart grouped by a single dimension.

| Field | Required | Description |
|-------|----------|-------------|
| `dimension` | **Yes** | Grouping dimension (e.g., `sales_order|created_at__day`). |
| `columns` | No | Array of metric columns to plot. |
| `compare` | No | Use `period` to overlay the previous period. |

```json
{
  "identifier": "revenue-chart",
  "title": "Daily Revenue",
  "size": [10, 12],
  "pos": [0, 0],
  "config": {
    "renderer": "chart",
    "dimension": "sales_order|created_at__day",
    "columns": [
      "sales_order|grand_total__sum",
      "sales_order|gross_profit__sum"
    ],
    "compare": "period"
  }
}
```

## Renderer: `html`

Displays raw HTML content. No data query is executed.

| Field | Required | Description |
|-------|----------|-------------|
| `html_content` | No | HTML string to render. |

```json
{
  "identifier": "welcome-message",
  "title": "Welcome",
  "size": [10, 6],
  "pos": [0, 0],
  "config": {
    "renderer": "html",
    "html_content": "<h2>Store Dashboard</h2><p>Key metrics for this period.</p>"
  }
}
```

## Filters

Optional array on `config.filters`. Each filter narrows the data query.

| Field | Description |
|-------|-------------|
| `column` | Column to filter (e.g., `cataloginventory_stock_item|qty`). |
| `condition_type` | Operator: `eq`, `neq`, `gt`, `gteq`, `lt`, `lteq`, `like`, `in`, `nin`. |
| `value` | Filter value (string). For `in`/`nin`, comma-separated values. |

```json
{
  "config": {
    "renderer": "table",
    "dimensions": ["catalog_product_entity|sku"],
    "columns": ["catalog_product_entity|name", "cataloginventory_stock_item|qty__sum"],
    "sort_orders": [{"column": "cataloginventory_stock_item|qty__sum", "direction": "asc"}],
    "filters": [
      {"column": "cataloginventory_stock_item|qty", "condition_type": "gt", "value": "1"},
      {"column": "cataloginventory_stock_item|qty", "condition_type": "lteq", "value": "10"}
    ],
    "date_range": {"is_override": true, "range": "lifetime"}
  }
}
```

## Date Range Override

Override the board's date range for a specific block.

| Field | Description |
|-------|-------------|
| `date_range.is_override` | `true` to use the block's own range instead of the board's. |
| `date_range.range` | Range value: `today`, `yesterday`, `last7Days`, `last30Days`, `last90Days`, `last365days`, `week`, `month`, `quarter`, `year`, `prev_week`, `prev_month`, `prev_year`, `lifetime`. |

## Column Name Format

Columns follow the pattern `table|column__aggregation`:

- `sales_order|entity_id__cnt` — count of orders
- `sales_order|grand_total__sum` — sum of grand total
- `sales_order_item|qty_ordered__sum` — sum of qty ordered
- `catalog_product_entity|name` — product name (no aggregation)
- `sales_order|created_at__day` — created at grouped by day

Common aggregations: `__cnt` (count), `__sum` (sum), `__avg` (average), `__min`, `__max`.

## Validation Rules

The API validates block configs on save:

- `renderer` must be one of: `single`, `table`, `chart`, `html`.
- **single**: `column` is required.
- **table**: `dimensions` must be a non-empty array.
- **chart**: `dimension` is required.
- **html**: no required fields.

Invalid configs return a validation error and the board is not saved.

## Block-Level Endpoints

For adding, updating, or deleting individual blocks without affecting other blocks on the board:

| Method | Endpoint | Description |
|--------|----------|-------------|
| `POST` | `/V1/dashboard/board/:boardId/block` | Add a new block (or update if identifier exists) |
| `PUT` | `/V1/dashboard/board/:boardId/block/:blockIdentifier` | Update a single block |
| `DELETE` | `/V1/dashboard/board/:boardId/block/:blockIdentifier` | Delete a single block |

All three return the full board after the operation. Existing blocks are preserved.

**Add a block:**
```json
POST /V1/dashboard/board/7/block
{
  "block": {
    "title": "New Chart",
    "pos": [20, 0],
    "size": [10, 12],
    "config": {
      "renderer": "chart",
      "dimension": "sales_order|created_at__day",
      "columns": ["sales_order|grand_total__sum"]
    }
  }
}
```

**Update a block:**
```json
PUT /V1/dashboard/board/7/block/my_block
{
  "block": {
    "identifier": "my_block",
    "title": "Updated Title",
    "pos": [0, 0],
    "size": [4, 8],
    "config": {
      "renderer": "single",
      "column": "sales_order|entity_id__cnt",
      "compare": "period"
    }
  }
}
```

**Delete a block:**
```
DELETE /V1/dashboard/board/7/block/my_block
```

## Full Board Example with Mixed Blocks

```json
{
  "endpoint": "/V1/dashboard/board",
  "method": "POST",
  "body": {
    "board": {
      "title": "Sales Overview",
      "type": "shared",
      "date_range": "last30Days",
      "blocks": [
        {
          "identifier": "orders",
          "title": "Orders",
          "pos": [0, 0],
          "size": [4, 8],
          "config": {
            "renderer": "single",
            "column": "sales_order|entity_id__cnt",
            "compare": "period",
            "spark_line": true
          }
        },
        {
          "identifier": "revenue",
          "title": "Revenue",
          "pos": [8, 0],
          "size": [4, 8],
          "config": {
            "renderer": "single",
            "column": "sales_order|grand_total__sum",
            "compare": "period",
            "spark_line": true
          }
        },
        {
          "identifier": "revenue-chart",
          "title": "Revenue Trend",
          "pos": [16, 0],
          "size": [12, 16],
          "config": {
            "renderer": "chart",
            "dimension": "sales_order|created_at__day",
            "columns": ["sales_order|grand_total__sum"]
          }
        },
        {
          "identifier": "top-products",
          "title": "Top Products",
          "pos": [32, 0],
          "size": [20, 12],
          "config": {
            "renderer": "table",
            "dimensions": ["sales_order_item|sku"],
            "columns": ["catalog_product_entity|name", "sales_order_item|qty_ordered__sum", "sales_order_item|row_total__sum"],
            "sort_orders": [{"column": "sales_order_item|row_total__sum", "direction": "desc"}],
            "page_size": 10
          }
        }
      ]
    }
  }
}
```
