feat(wake): order analytics — totals, PII-free projection, dashboard, rate gate

Merged
TH
4d ago
+1094-46

Why

Analytics apps reading a Wake store's orders through this MCP (first one: insights-mcp's sales radar) hit four problems:

  1. No total. WAKE_LIST_ORDERS dropped Wake's x-total-count header. A caller couldn't size its paging or tell how many orders a day had.
  2. A bare day returned an empty list. Wake reads 2026-11-25 as midnight. A date-only dataFinal ended the range where it started, with no error.
  3. Every order carried personal data. Buyer name, CPF, e-mail, phones, street address, card holder and masked number, Pix QR code and free-text notes all went through the gateway, even when the caller only needed totals.
  4. No protection of the token. Wake allows 120 req/min per Admin token. A token that keeps calling after a 429 is blocked for an hour, and that block hits every integration sharing it.

What

WAKE_LIST_ORDERS (backwards compatible: items and count are unchanged, fields defaults to full)

  • Returns total (from x-total-count), page, pageSize and hasMore.
  • A bare day means the whole day: 00:00:00 to 23:59:59. T is accepted as the separator.
  • New params: direcaoOrdenacao, listaPedidosIds (up to 50 ids in one call, instead of WAKE_GET_ORDER in a loop), DataStatus in enumTipoFiltroData, and camposAdicionais. Array params are sent as repeated keys.
  • fields: "analytics" is a whitelist projection (lib/order-analytics.ts).
    • Keeps: status, dates, totals, coupon, channel, split siblings, fulfilling distribution centers, pickup store, city/state, items with their promotion id and coupon, payment group/brand/acquirer, and UTMs and other non-personal metadata.
    • Drops everything that identifies a person. A field Wake adds later stays out until someone adds it to the projection.

New read tools

ToolEndpoint
WAKE_DASHBOARD/dashboard/{receita,transacoes,ticketMedio,faturamento,graficofaturamento,graficoformapagamento,produtos,novoscompradores,carrinhoabandonado,produtoestoque}
WAKE_LIST_PAYMENT_METHODS/formasPagamento
WAKE_LIST_ORDER_ORIGINS/origensPedido
WAKE_LIST_DISTRIBUTION_CENTERS/centrosdistribuicao
WAKE_LIST_PHYSICAL_STORES/lojasFisicas (paged, with total)

Rate gate (lib/rate-gate.ts, applied to every Admin call)

  • Paces each token to WAKE_ADMIN_MAX_PER_MINUTE calls per minute (default 100) per process. A call waits up to 15 s for a free slot.
  • After a 429, refuses that token locally until Retry-After has passed, without reaching Wake.
  • Never retries. A refusal fails with [wake-rate-limited] … retry-after=<s>, so callers can back off.
  • State is keyed by a SHA-256 of the token.

Also: the README documents dates (store local time, no offset), the page-199 ceiling and the rate limit. app.json, registry.json and the version (1.1.0) are updated.

Verified

  • bun test (wake): 22 pass. They cover the gate (pacing, refusal, 429 block, Retry-After parsing), the client (x-total-count, the 429 stop, array query strings), the date range and hasMore, and the analytics projection. The projection tests use a synthetic order that carries every personal field, and assert that none of them survive.
  • scripts/check.ts wake, oxfmt --check, oxlint and check:registry are clean. The one wake lint warning (body on GET in fetch) predates this change.
  • Against a live store's Admin API before this PR, through the current tools:
    • order dates match GA4's purchase timestamps to the minute, which confirms they are local time with no offset;
    • a date-only range returns [];
    • the list carries complete orders, with UTMs in metadados and promotions with coupon in itens[].ajustes.
  • Not yet run live: the new endpoints (/dashboard/*, /formasPagamento, /origensPedido, /centrosdistribuicao, /lojasFisicas) and the x-total-count header. Their paths and params follow Wake's API reference. A wrong path would surface as a clear HTTP 404 from the tool. I'll call each one on a real connection right after deploy.

šŸ¤– Generated with Claude Code


Summary by cubic

Adds order analytics to the Wake MCP: WAKE_LIST_ORDERS now reports totals, handles date-only ranges, and can project orders without personal data, plus new dashboard and reference tools and a per-token rate gate.

Order listing improvements

  • Returns total, page, pageSize, and hasMore from Wake's x-total-count header.
  • A bare day (2026-11-25) now means the whole day; previously a date-only dataFinal ended at midnight and returned nothing.
  • New params: direcaoOrdenacao, listaPedidosIds (up to 50 ids in one call), DataStatus filter, and camposAdicionais.
  • fields: "analytics" returns a whitelist projection that drops buyer name, CPF, e-mail, phones, street address, card data, Pix QR code, and free-text notes; a field Wake adds later stays out until added to the projection.
  • items and count are unchanged; fields defaults to full.

New tools and rate gate

  • Adds WAKE_DASHBOARD (revenue, transactions, average ticket, payment methods, best sellers, abandoned carts), WAKE_LIST_PAYMENT_METHODS, WAKE_LIST_ORDER_ORIGINS, WAKE_LIST_DISTRIBUTION_CENTERS, and WAKE_LIST_PHYSICAL_STORES.
  • Every Admin call now goes through a per-token gate: paced to 100 calls/minute (configurable) with a 15 s wait, and after a 429 the token is refused locally until Retry-After passes, without reaching Wake. The refusal carries a [wake-rate-limited] error so callers can back off.
  • Wake blocks a token for an hour when calls keep coming after a 429, and that block hits every integration sharing the token.
  • Bumps version to 1.1.0 and documents dates, paging limits, and the rate limit in the README.