Repository navigation
Pedidos G5.2: lectores de Pedido.estado → estadoEntrega - #152
Merged
Merged
Conversation
Fase B de ADR-PEDIDO-ESTADO-CANONICO-001. `Pedido.estado` (EstadoPedido) es
mirror legacy de `estadoEntrega` (EstadoEntrega) — mismos 6 valores, siempre
en sync vía PedidoMapper. Este PR migra TODOS los lectores de queries Prisma
de `estado` → `estadoEntrega`. Cero cambio de resultados (enums idénticos).
## Lectores migrados
- src/app/api/cierre/route.ts — 6 where clauses (EstadoPedido → EstadoEntrega)
- src/app/api/forecast-produccion/route.ts — where + select + map
- src/app/(app)/reportes/{page,ventas/page}.tsx, src/app/api/reportes/ventas/route.ts
- src/lib/{route-analysis,ventas}.ts, src/lib/demanda/recompute-cliente.ts
- src/lib/geo/{optimize-ruta,cluster-clientes}.ts
- src/app/api/pedidos/[id]/enviar/route.ts — guard `current.estado` → `current.estadoEntrega`
## No-scope (G5.3)
Los WRITES de `Pedido.estado` (PedidoMapper dual-write, enviar/venta-libre/
recurrentes raw, MaterializarPlan, optimistic UI object). El DROP COLUMN (G5.4).
## Tests
- optimize-ruta.test.ts actualizado (`where.estadoEntrega`)
- 2720 unit + integración cierre/planificador/pedidos-sin-asignar verde
- tsc + eslint limpios
## Rollback
git revert. Sin schema, sin migración.
Co-Authored-By: Claude Sonnet 5 <[email protected]>
1. **Gate de la fase B + backfill.** El ADR pide `count(*) WHERE estado <> estadoEntrega = 0` antes de migrar los lectores. `estadoEntrega` se agregó vía `db push` SIN backfill → filas legacy podían divergir. Migración `20260901_backfill_pedido_estadoentrega`: `UPDATE ... SET estadoEntrega = estado WHERE difieren` (idempotente) + test de integración del invariante. 2. **Índices compuestos.** Las queries migradas perdían `[estado, fecha]` / `[embarqueId, estado]`. Agregados `@@index([estadoEntrega, fecha])` + `@@index([embarqueId, estadoEntrega])` (en la misma migración). `[clienteId, estadoEntrega]` ya lo cubre el prefijo del índice de 3 columnas. 3. **Dead select.** `enviar/route.ts` seleccionaba `estado: true` pero el guard ya lee `estadoEntrega` → removido. Verificación prod antes de mergear: `SELECT count(*) FROM "Pedido" WHERE estado::text <> "estadoEntrega"::text;` = 0 (en dev: 0). tsc + eslint limpios; integración verde. Co-Authored-By: Claude Sonnet 5 <[email protected]>
Owner
Author
Self-review (code-review skill) — 3 hallazgos, resueltos en
|
| # | Hallazgo | Fix |
|---|---|---|
| 1 | Faltaba el gate de la fase B: el ADR pide count(*) WHERE estado <> estadoEntrega = 0 antes de migrar lectores. estadoEntrega se agregó vía db push sin backfill → filas legacy podían divergir (un pedido entregado contado como activo en reportes, o pasando el guard de enviar) |
Migración 20260901_backfill_pedido_estadoentrega (UPDATE idempotente) + src/lib/__tests__/integration/pedido-estado-mirror.test.ts |
| 2 | Las queries migradas perdían los índices compuestos [estado, fecha] / [embarqueId, estado] |
@@index([estadoEntrega, fecha]) + @@index([embarqueId, estadoEntrega]) en la misma migración |
| 3 | enviar/route.ts seleccionaba estado: true ya sin lectores |
Removido |
Verificación en prod antes de mergear (Supabase):
SELECT count(*) FROM "Pedido" WHERE estado::text <> "estadoEntrega"::text;Debe dar 0. Si no, la migración de backfill lo corrige (es lo primero que corre). En dev: 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fase B de
ADR-PEDIDO-ESTADO-CANONICO-001. Migra todos los lectores de queries Prisma de la columna legacyPedido.estadoaestadoEntrega.estadoyestadoEntregason enums idénticos (mismos 6 valores) mantenidos en sync porPedidoMapper. Cero cambio de resultados.Lectores migrados
cierre (6 where), forecast-produccion, reportes (×3), route-analysis, ventas, recompute-cliente, optimize-ruta, cluster-clientes, enviar (guard).
No-scope
Los writes de
Pedido.estado(G5.3), el DROP COLUMN (G5.4, bundle con G6).Verificación
2720 unit + integración (cierre/planificador/pedidos-sin-asignar) verde. tsc + eslint limpios.
Rollback
git revert. Sin schema.🤖 Generated with Claude Code