Files
botino/src/modules/shared/sellUnitPolicy.js
Lucas Tettamanti 423a99ab48 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>
2026-05-18 00:40:12 -03:00

24 lines
935 B
JavaScript

export function normalizeUnitToken(unit) {
const s = String(unit || "").trim().toLowerCase();
if (s === "g" || s === "kg" || s === "gramos" || s === "kilo" || s === "kilos") return "kg";
if (s === "unit" || s === "unidad" || s === "unidades" || s === "u") return "unit";
return null;
}
export function computeAllowedOrderUnits({ sell_unit, raw_override } = {}) {
if (normalizeUnitToken(sell_unit) === "unit") return ["unit"];
if (Array.isArray(raw_override) && raw_override.length > 0) {
const filtered = [...new Set(raw_override.map(normalizeUnitToken).filter((u) => u !== null))];
if (filtered.length > 0) return filtered;
}
return ["kg", "unit"];
}
export function isUnitAllowed(unit, allowed) {
const normalized = normalizeUnitToken(unit);
if (normalized === null) return false;
const list = Array.isArray(allowed) ? allowed : [];
return list.some((u) => normalizeUnitToken(u) === normalized);
}