Productos: allowed_order_units + fix sync que pisaba overrides

Introduce un campo nuevo allowed_order_units por producto (array kg/unit)
que separa "cómo se vende" de "cómo se puede pedir". Productos sell_unit=unit
solo aceptan unit (regla dura aplicada por código y handler). Productos
sell_unit=kg defaultean a ambos, editable desde la UI con checkboxes.

Fixea bug crítico de wooSnapshot.upsertSnapshotItems que pisaba raw entero
en cada sync, perdiendo overrides locales (_sell_unit_override,
_categories_override y el nuevo _allowed_order_units). El UPDATE ahora
mergea preservando los overrides.

Las tools del agente (addToCart, setQuantity, selectCandidate) validan
unit contra allowed_order_units y devuelven unit_not_allowed para que el
LLM re-pregunte. workingMemory expone allowed_order_units y needs_unit_choice.
buildLineItems agrega meta_data unit/sell_unit_product/unit_mode_mismatch
y omite subtotal/total cuando hay mismatch para que Woo recalcule.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lucas Tettamanti
2026-05-18 00:40:12 -03:00
parent c786e47160
commit 423a99ab48
18 changed files with 431 additions and 72 deletions

View File

@@ -0,0 +1,24 @@
-- migrate:up
-- Backfill _allowed_order_units en raw para productos existentes.
-- Regla:
-- sell_unit_override = 'unit' -> ["unit"] (regla dura)
-- sell_unit_override = 'kg' o null -> ["kg","unit"] (default permisivo)
-- Idempotente: solo aplica si el campo no existe todavía.
update woo_products_snapshot
set raw = jsonb_set(
coalesce(raw, '{}'::jsonb),
'{_allowed_order_units}',
case
when coalesce(raw->>'_sell_unit_override', 'kg') = 'unit' then '["unit"]'::jsonb
else '["kg","unit"]'::jsonb
end,
true
)
where raw is null
or not (raw ? '_allowed_order_units');
-- migrate:down
update woo_products_snapshot
set raw = raw - '_allowed_order_units'
where raw ? '_allowed_order_units';