Criar os endpoints REST `/api/crm/*` dentro do bot Express para que o frontend CRM (Next.js/Vercel) consiga ler conversas, clientes, pipeline e enviar mensagens. Tambem implementar o silenciamento do bot quando humano assume.
ACCEPTANCE CRITERIA (18/18)
☑ 1. Middleware de auth CRM com token estatico via header `X-CRM-Token`
☑ 2. Token configuravel via `CRM_API_TOKEN` no `.env`
☑ 3. CORS configurado para aceitar requests do Vercel
☑ 4. `GET /api/crm/conversations` — lista conversas com filtros (status, stage, busca)
☑ 5. `GET /api/crm/conversations/:id` — conversa completa com customer + messages
☑ 6. `GET /api/crm/conversations/:id/messages` — mensagens com paginacao
☑ 7. `POST /api/crm/conversations/:id/messages` — atendente envia mensagem (grava + Z-API)
☑ 8. Migration 022: campo `sender_type` e `agent_id` na tabela messages
☑ 9. `POST /api/crm/conversations/:id/take-over` — humano assume
☑ 10. `POST /api/crm/conversations/:id/release` — devolve ao bot
☑ 11. Bot SILENCIA quando conversation.status = 'waiting_human' (mudanca no whatsapp.handler.ts)
☑ 12. `GET /api/crm/customers` — busca por nome, telefone, CNPJ
☑ 13. `GET /api/crm/customers/:id` — ficha 360 (conversas, pedidos, NPS, shipments)
☑ 14. `GET /api/crm/pipeline` — dados do pipeline por etapa
☑ 15. `PATCH /api/crm/pipeline/:leadId/stage` — mover lead de etapa
☑ 16. `GET /api/crm/queue` — fila de atendimento pendente
☑ 17. TypeScript compila sem erros (`npx tsc --noEmit`)
☑ 18. Rotas registradas no index.ts
TIMELINE
01/04/2026
chore: session cleanup — 10 stories Done, security+QA gates, MCC setup, V4 audit [Stories 6.3, 7.2, 9.9, 9.10, 9.11, 9.12, 19.4, 20.1, 20.2, 21.3]
550daa2
ARQUIVOS (10)
propek-whatsapp-bot/src/api/crm/index.ts Created
propek-whatsapp-bot/src/api/crm/auth.middleware.ts Created
propek-whatsapp-bot/src/api/crm/conversations.ts Created
propek-whatsapp-bot/src/api/crm/customers.ts Created
propek-whatsapp-bot/src/api/crm/pipeline.ts Created
propek-whatsapp-bot/src/api/crm/queue.ts Created
propek-whatsapp-bot/src/migrations/022_add_message_sender_fields.sql Created
propek-whatsapp-bot/src/index.ts Modified
propek-whatsapp-bot/src/handlers/whatsapp.handler.ts Modified
propek-whatsapp-bot/.env.example Modified