# pipelines.stage_rules_set

Definir as regras de uma etapa. Troca as regras da etapa aberta (campos de `pipelines.stage_rules_get`; o que não for enviado fica como está). Os campos obrigatórios padrão (`kind: standard`) são: value, expectedClosingDate, priority, probability, origin, title, productIds, primaryCompany, primaryAttendant; `kind: custom` leva o id de um campo de `custom_fields.list` com `entityType: opportunities` (campo arquivado ou apagado deixa de exigir). Exige a migration de configurações.

- Endpoint: `POST https://app.atomiks.com.br/api/v1/pipelines.stage_rules_set` (corpo JSON)
- Quem pode chamar: administrador da conta
- Escopo da chave: `crm:write`
- Grava dados: sim
- Pela chave de API: sim
- Ferramenta MCP: `pipelines_stage_rules_set`
- Ficha em JSON: `https://app.atomiks.com.br/api/v1/openapi.json` (operationId `pipelines.stage_rules_set`)

## Entrada

| Campo | Tipo | Obrigatório | Nota |
|---|---|---|---|
| `stageId` | string (uuid) | sim |  |
| `requiredOn` | boolean | não |  |
| `requiredFields` | object[] | não |  |
| `wipOn` | boolean | não |  |
| `wipLimit` | integer | não |  |
| `wipMode` | "warn" \| "block" | não |  |
| `directionOn` | boolean | não |  |
| `direction` | "any" \| "forward" \| "one_step" \| "terminal" | não |  |

<details><summary>JSON Schema</summary>

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "stageId": {
      "type": "string",
      "format": "uuid",
      "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$"
    },
    "requiredOn": {
      "type": "boolean"
    },
    "requiredFields": {
      "maxItems": 30,
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "kind": {
            "type": "string",
            "enum": [
              "standard",
              "custom"
            ]
          },
          "id": {
            "type": "string",
            "minLength": 1,
            "maxLength": 100
          }
        },
        "required": [
          "kind",
          "id"
        ]
      }
    },
    "wipOn": {
      "type": "boolean"
    },
    "wipLimit": {
      "type": "integer",
      "minimum": 1,
      "maximum": 100000
    },
    "wipMode": {
      "type": "string",
      "enum": [
        "warn",
        "block"
      ]
    },
    "directionOn": {
      "type": "boolean"
    },
    "direction": {
      "type": "string",
      "enum": [
        "any",
        "forward",
        "one_step",
        "terminal"
      ]
    }
  },
  "required": [
    "stageId"
  ]
}
```

</details>

## Exemplo

```bash
curl -X POST https://app.atomiks.com.br/api/v1/pipelines.stage_rules_set \
  -H "Authorization: Bearer $ATOMIKS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"stageId":"00000000-0000-0000-0000-000000000000","wipOn":true,"wipLimit":20,"wipMode":"warn"}'
```

CLI:

```bash
node cli/atomiks.mjs pipelines.stage_rules_set --json '{"stageId":"00000000-0000-0000-0000-000000000000","wipOn":true,"wipLimit":20,"wipMode":"warn"}'
```

MCP: ferramenta `pipelines_stage_rules_set` com a mesma entrada.

## Saída

Sucesso: `{"data": ...}` com o formato abaixo.

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "stageId": {
      "type": "string"
    },
    "requiredOn": {
      "type": "boolean"
    },
    "requiredFields": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "kind": {
            "type": "string",
            "enum": [
              "standard",
              "custom"
            ]
          },
          "id": {
            "type": "string",
            "minLength": 1,
            "maxLength": 100
          }
        },
        "required": [
          "kind",
          "id"
        ],
        "additionalProperties": false
      }
    },
    "wipOn": {
      "type": "boolean"
    },
    "wipLimit": {
      "type": "number"
    },
    "wipMode": {
      "type": "string",
      "enum": [
        "warn",
        "block"
      ]
    },
    "directionOn": {
      "type": "boolean"
    },
    "direction": {
      "type": "string",
      "enum": [
        "any",
        "forward",
        "one_step",
        "terminal"
      ]
    },
    "currentCount": {
      "type": "number"
    }
  },
  "required": [
    "stageId",
    "requiredOn",
    "requiredFields",
    "wipOn",
    "wipLimit",
    "wipMode",
    "directionOn",
    "direction",
    "currentCount"
  ],
  "additionalProperties": false
}
```

## Erros

`{"error": {"code", "message", "requestId"}}`. Códigos: `invalid_input` (400), `unauthorized` (401), `forbidden` / `account_pending` / `scope_missing` (403), `not_found` (404), `limit_reached` (409), `internal` (500).
