{
  "info": {
    "_postman_id": "centralcrm-api-collection",
    "name": "CentralCRM API",
    "description": "Collection completa da API CentralCRM.\n\n**Base URL:** `{{baseUrl}}` (default `http://localhost:3000/api`)\n\n**Autenticação:** execute `Auth — Login` — o `accessToken` é guardado automaticamente (Bearer nas rotas protegidas).\n\n**Tenant:** muitas rotas exigem `tenantId` no JWT ou cabeçalho `x-tenant-id: {{tenantId}}`.\n\n**Swagger:** `GET {{baseUrl}}/docs` | OpenAPI JSON: `GET {{baseUrl}}/docs-json`\n\n**Atualizado:** 2026-06-06 — inclui diretório (sync roles/contacts), operacional, presença, portal e cadastro completo.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "auth": {
    "type": "bearer",
    "bearer": [{ "key": "token", "value": "{{accessToken}}", "type": "string" }]
  },
  "variable": [
    { "key": "baseUrl", "value": "http://localhost:3000/api" },
    { "key": "accessToken", "value": "" },
    { "key": "tenantId", "value": "f1e2d3c4-b5a6-4789-8012-345678901234" },
    { "key": "companyId", "value": "64c6dd93-5fe7-4765-8fe1-87ecc2c194e4" },
    { "key": "parentCompanyId", "value": "64c6dd93-5fe7-4765-8fe1-87ecc2c194e4" },
    { "key": "subTenantId", "value": "f1e2d3c4-b5a6-4789-8012-345678901234" },
    { "key": "userId", "value": "413c0c2c-501d-4bd9-bd8c-bf3f488c7bf7" },
    { "key": "addressId", "value": "" },
    { "key": "documentId", "value": "" },
    { "key": "estabelecimentoId", "value": "" },
    { "key": "portalTenantId", "value": "" },
    { "key": "cnpj", "value": "00000000000191" },
    { "key": "cep", "value": "89010025" }
  ],
  "item": [
    {
      "name": "Meta & Saúde",
      "item": [
        {
          "name": "Root — info da API",
          "request": {
            "auth": { "type": "noauth" },
            "method": "GET",
            "header": [],
            "url": "{{baseUrl}}",
            "description": "GET /api — links para docs e swagger."
          }
        },
        {
          "name": "Health check",
          "request": {
            "auth": { "type": "noauth" },
            "method": "GET",
            "header": [],
            "url": "{{baseUrl}}/health",
            "description": "GET /api/health — estado da BD e dependências."
          }
        }
      ]
    },
    {
      "name": "Auth",
      "item": [
        {
          "name": "Login",
          "event": [
            {
              "listen": "test",
              "script": {
                "exec": [
                  "const json = pm.response.json();",
                  "if (json.accessToken) {",
                  "  pm.collectionVariables.set('accessToken', json.accessToken);",
                  "  pm.environment.set('accessToken', json.accessToken);",
                  "}",
                  "if (json.user?.id) {",
                  "  pm.collectionVariables.set('userId', json.user.id);",
                  "  pm.environment.set('userId', json.user.id);",
                  "}"
                ],
                "type": "text/javascript"
              }
            }
          ],
          "request": {
            "auth": { "type": "noauth" },
            "method": "POST",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"email\": \"usuario@exemplo.com\",\n  \"password\": \"sua-senha\",\n  \"tenantId\": \"{{tenantId}}\"\n}"
            },
            "url": "{{baseUrl}}/v1/auth/login",
            "description": "POST /api/v1/auth/login — BD G_DB_POSTGRESS. Guarda accessToken automaticamente."
          }
        },
        {
          "name": "Register",
          "request": {
            "auth": { "type": "noauth" },
            "method": "POST",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"nome\": \"Novo Utilizador\",\n  \"email\": \"novo@exemplo.com\",\n  \"password\": \"SenhaForte123\"\n}"
            },
            "url": "{{baseUrl}}/v1/auth/register",
            "description": "POST /api/v1/auth/register — cria `public.\"user\"` + tabelas relacionais (G_DB_POSTGRESS). Resposta: UserFullResponseDto."
          }
        },
        {
          "name": "Logout",
          "request": {
            "method": "POST",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "url": "{{baseUrl}}/v1/auth/logout",
            "description": "POST /api/v1/auth/logout — decrementa metadata.sessoesAtivas."
          }
        },
        {
          "name": "Get user (full)",
          "request": {
            "method": "GET",
            "header": [],
            "url": "{{baseUrl}}/v1/auth/users/{{userId}}",
            "description": "GET /api/v1/auth/users/:userId — perfil completo (próprio utilizador ou admin)."
          }
        },
        {
          "name": "Patch user",
          "request": {
            "method": "PATCH",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"displayName\": \"Nome Atualizado\",\n  \"status\": \"ativo\"\n}"
            },
            "url": "{{baseUrl}}/v1/auth/users/{{userId}}",
            "description": "PATCH /api/v1/auth/users/:userId — atualização parcial."
          }
        },
        {
          "name": "Patch user permissions",
          "request": {
            "method": "PATCH",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"grant\": [\"perm-user-edit\"],\n  \"revoke\": []\n}"
            },
            "url": "{{baseUrl}}/v1/auth/users/{{userId}}/permissions"
          }
        }
      ]
    },
    {
      "name": "CRM Admin — Utilizadores",
      "item": [
        {
          "name": "Listar utilizadores (admin)",
          "request": {
            "method": "GET",
            "header": [],
            "url": "{{baseUrl}}/v1/crm-admin/users",
            "description": "GET /api/v1/crm-admin/users — lista public.\"user\" (apenas admin). BD G_DB_POSTGRESS."
          }
        }
      ]
    },
    {
      "name": "Empresas (global)",
      "item": [
        {
          "name": "Criar empresa",
          "request": {
            "method": "POST",
            "header": [
              { "key": "Content-Type", "value": "application/json" },
              { "key": "x-tenant-id", "value": "{{tenantId}}" }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"razaoSocial\": \"ACME Serviços LTDA\",\n  \"tenantId\": \"{{tenantId}}\"\n}"
            },
            "url": "{{baseUrl}}/v1/companies"
          }
        },
        {
          "name": "Listar empresas (paginado)",
          "request": {
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{baseUrl}}/v1/companies?pageNumber=1&pageSize=10",
              "host": ["{{baseUrl}}"],
              "path": ["v1", "companies"],
              "query": [
                { "key": "pageNumber", "value": "1" },
                { "key": "pageSize", "value": "10" }
              ]
            }
          }
        }
      ]
    },
    {
      "name": "CentralCRM — Cadastro completo",
      "item": [
        {
          "name": "POST cadastrocompany",
          "request": {
            "method": "POST",
            "header": [{ "key": "Content-Type", "value": "application/json" }],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"isFilial\": false,\n  \"razaoSocial\": \"ACME Serviços LTDA\",\n  \"nomeFantasia\": \"ACME\",\n  \"cnpj\": \"12.345.678/0001-90\",\n  \"subdominio\": \"minha-empresa\",\n  \"contacts\": [\n    {\n      \"nome\": \"João Silva\",\n      \"email\": \"joao@acme.com.br\",\n      \"criarUsuarioNoSistema\": true\n    }\n  ]\n}"
            },
            "url": "{{baseUrl}}/centralcrm/cadastrocompany",
            "description": "POST /api/centralcrm/cadastrocompany — sem prefixo v1. Requer perm-user-edit."
          }
        }
      ]
    },
    {
      "name": "Diretório de empresas",
      "item": [
        {
          "name": "Criar empresa no tenant",
          "request": {
            "method": "POST",
            "header": [
              { "key": "Content-Type", "value": "application/json" },
              { "key": "x-tenant-id", "value": "{{tenantId}}" }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"razaoSocial\": \"Nova Empresa SA\"\n}"
            },
            "url": "{{baseUrl}}/v1/company/directory"
          }
        },
        {
          "name": "Listar empresas (tenant)",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": {
              "raw": "{{baseUrl}}/v1/company/directory?pageNumber=1&pageSize=10",
              "host": ["{{baseUrl}}"],
              "path": ["v1", "company", "directory"],
              "query": [
                { "key": "pageNumber", "value": "1" },
                { "key": "pageSize", "value": "10" }
              ]
            }
          }
        },
        {
          "name": "Detalhe do diretório (sync roles)",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}",
            "description": "Sincroniza metadata.contacts.role_number ↔ user.role_number, atualiza user.role e devolve contacts com campo `role` (não role_number)."
          }
        },
        {
          "name": "PATCH empresa (metadata)",
          "request": {
            "method": "PATCH",
            "header": [
              { "key": "Content-Type", "value": "application/json" },
              { "key": "x-tenant-id", "value": "{{tenantId}}" }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"nomeFantasia\": \"Nome Atualizado\"\n}"
            },
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}"
          }
        },
        {
          "name": "PUT branding",
          "request": {
            "method": "PUT",
            "header": [
              { "key": "Content-Type", "value": "application/json" },
              { "key": "x-tenant-id", "value": "{{tenantId}}" }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"corPrincipal\": \"#191B1F\",\n  \"logotipo\": \"https://cdn.example.com/logo.png\"\n}"
            },
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/branding"
          }
        },
        {
          "name": "POST endereço",
          "request": {
            "method": "POST",
            "header": [
              { "key": "Content-Type", "value": "application/json" },
              { "key": "x-tenant-id", "value": "{{tenantId}}" }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"cep\": \"01310-100\",\n  \"endereco\": \"Av. Paulista\",\n  \"numero\": \"1000\",\n  \"cidade\": \"São Paulo\",\n  \"estado\": \"SP\",\n  \"principal\": true\n}"
            },
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/addresses"
          }
        },
        {
          "name": "PATCH endereço",
          "request": {
            "method": "PATCH",
            "header": [
              { "key": "Content-Type", "value": "application/json" },
              { "key": "x-tenant-id", "value": "{{tenantId}}" }
            ],
            "body": { "mode": "raw", "raw": "{\n  \"complemento\": \"Sala 10\"\n}" },
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/addresses/{{addressId}}"
          }
        },
        {
          "name": "DELETE endereço",
          "request": {
            "method": "DELETE",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/addresses/{{addressId}}"
          }
        },
        {
          "name": "POST documento",
          "request": {
            "method": "POST",
            "header": [
              { "key": "Content-Type", "value": "application/json" },
              { "key": "x-tenant-id", "value": "{{tenantId}}" }
            ],
            "body": {
              "mode": "raw",
              "raw": "{\n  \"tipo\": \"contrato\",\n  \"caminhoArquivo\": \"https://cdn.example.com/doc.pdf\"\n}"
            },
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/documents"
          }
        },
        {
          "name": "DELETE documento",
          "request": {
            "method": "DELETE",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/documents/{{documentId}}"
          }
        }
      ]
    },
    {
      "name": "Utilizadores por empresa",
      "item": [
        {
          "name": "Contagem users da empresa",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/{{companyId}}/user",
            "description": "GET /api/v1/company/{companyId}/user — devolve `{ total }`."
          }
        },
        {
          "name": "Contagem users (mãe + sub-empresas)",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/{{companyId}}/user/with-subcompanies"
          }
        },
        {
          "name": "Listar sub-empresas",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/{{companyId}}/tenentid/{{subTenantId}}"
          }
        },
        {
          "name": "Listar users das sub-empresas",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/{{companyId}}/tenentid/{{subTenantId}}/user"
          }
        }
      ]
    },
    {
      "name": "Operacional (por empresa)",
      "item": [
        {
          "name": "Listar estabelecimentos",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/operational/estabelecimentos"
          }
        },
        {
          "name": "Detalhe estabelecimento",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/operational/estabelecimentos/{{estabelecimentoId}}"
          }
        },
        {
          "name": "Dashboard TPV",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": {
              "raw": "{{baseUrl}}/v1/company/directory/{{companyId}}/operational/dashboard?dataInicio=2026-01-01&dataFim=2026-12-31",
              "host": ["{{baseUrl}}"],
              "path": ["v1", "company", "directory", "{{companyId}}", "operational", "dashboard"],
              "query": [
                { "key": "dataInicio", "value": "2026-01-01" },
                { "key": "dataFim", "value": "2026-12-31" }
              ]
            }
          }
        },
        {
          "name": "Top estabelecimentos",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": {
              "raw": "{{baseUrl}}/v1/company/directory/{{companyId}}/operational/top-estabelecimentos?limite=10",
              "host": ["{{baseUrl}}"],
              "path": ["v1", "company", "directory", "{{companyId}}", "operational", "top-estabelecimentos"],
              "query": [{ "key": "limite", "value": "10" }]
            }
          }
        },
        {
          "name": "Comissões resumo",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/operational/comissoes/resumo"
          }
        },
        {
          "name": "Alugueis resumo",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/operational/alugueis/resumo"
          }
        },
        {
          "name": "Listar propostas",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/company/directory/{{companyId}}/operational/propostas"
          }
        }
      ]
    },
    {
      "name": "Presença",
      "item": [
        {
          "name": "Active users (tenant)",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/presence/active-users"
          }
        },
        {
          "name": "Active users global (admin)",
          "request": {
            "method": "GET",
            "header": [],
            "url": "{{baseUrl}}/v1/presence/active-users-global"
          }
        },
        {
          "name": "Online count — empresas mãe",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": {
              "raw": "{{baseUrl}}/v1/presence/online-count/main-companies?parentCompanyId={{parentCompanyId}}",
              "host": ["{{baseUrl}}"],
              "path": ["v1", "presence", "online-count", "main-companies"],
              "query": [{ "key": "parentCompanyId", "value": "{{parentCompanyId}}" }]
            }
          }
        },
        {
          "name": "Online count — sub-empresas",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": "{{baseUrl}}/v1/presence/online-count/sub-companies"
          }
        },
        {
          "name": "Online count — agregado por mãe",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": {
              "raw": "{{baseUrl}}/v1/presence/online-count/aggregated-by-parent?parentCompanyId={{parentCompanyId}}",
              "host": ["{{baseUrl}}"],
              "path": ["v1", "presence", "online-count", "aggregated-by-parent"],
              "query": [{ "key": "parentCompanyId", "value": "{{parentCompanyId}}" }]
            }
          }
        }
      ]
    },
    {
      "name": "White label",
      "item": [
        {
          "name": "Listar por tenant",
          "request": {
            "method": "GET",
            "header": [{ "key": "x-tenant-id", "value": "{{tenantId}}" }],
            "url": {
              "raw": "{{baseUrl}}/v1/white-labels/{{tenantId}}?pageNumber=1&pageSize=10",
              "host": ["{{baseUrl}}"],
              "path": ["v1", "white-labels", "{{tenantId}}"],
              "query": [
                { "key": "pageNumber", "value": "1" },
                { "key": "pageSize", "value": "10" }
              ]
            }
          }
        }
      ]
    },
    {
      "name": "Portal",
      "item": [
        {
          "name": "URL do subdomínio (público)",
          "request": {
            "auth": { "type": "noauth" },
            "method": "GET",
            "header": [],
            "url": {
              "raw": "{{baseUrl}}/v1/portal/subdomain-url?portalTenantId={{portalTenantId}}",
              "host": ["{{baseUrl}}"],
              "path": ["v1", "portal", "subdomain-url"],
              "query": [{ "key": "portalTenantId", "value": "{{portalTenantId}}" }]
            }
          }
        }
      ]
    },
    {
      "name": "APIs externas",
      "description": "Proxies autenticados para OpenCNPJ e BrasilAPI.",
      "item": [
        {
          "name": "OpenCNPJ — consultar CNPJ",
          "request": {
            "method": "GET",
            "header": [],
            "url": "{{baseUrl}}/v1/external-apis/opencnpj/{{cnpj}}"
          }
        },
        {
          "name": "BrasilAPI — CEP v1",
          "request": {
            "method": "GET",
            "header": [],
            "url": "{{baseUrl}}/v1/external-apis/cep/v1/{{cep}}"
          }
        },
        {
          "name": "BrasilAPI — CEP v2",
          "request": {
            "method": "GET",
            "header": [],
            "url": "{{baseUrl}}/v1/external-apis/cep/v2/{{cep}}"
          }
        },
        {
          "name": "BrasilAPI — bancos v1",
          "request": {
            "method": "GET",
            "header": [],
            "url": "{{baseUrl}}/v1/external-apis/banks/v1"
          }
        }
      ]
    }
  ]
}
