feat(manager): products and categories on v2, a product in several categories, allergens (platform-v2 step 6.3)
- Management/ProductsTab + ProductFormModal (3.1k lines) → src/pages/products/: category tree, list/grid, toolbar + filters (search folds accents and Latin letters), bulk actions, category dialogs; the editor in editor/ (details, sold at, cost, fiscal; the waiter's choices). Arithmetic in productsLogic.js / editor/formLogic.js (tests/products.test.mjs). - Several categories per product with a main one (D-23): edited in the editor, "also in" on the rows, each category's list ordered on its own (drag or up/down → PUT /reorder with category_id); bulk "move to" / "also list in". The waiter's picker now shows a product in every category. - Allergens: products.allergens (Alembic 0032, core/allergens.py: 14 EU, contains | may_contain, catalog.allergen), in the API, POS menu, menu tablet, catalogue bundles and the cloud snapshot (C-2); @xenia/access allergens.js + <Allergens> shown on the phone, the counter, the tablet. - Bell: only the newest load sets the list (an older answer could hide a live notification). - e2e: manager (two categories, order per category, allergens), terminal / menu tablet / waiter (allergens shown); media saves by test id. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
// The products page: categories, per-category order, filters and the editor's form (plan step 6.3).
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import {
|
||||
SCOPES, applyFilters, bulkCategories, categoryTree, costOf, countIn, groupProducts, moveId, reorderBody, scopeProducts, stepBody, treeLabel,
|
||||
} from '../src/pages/products/productsLogic.js'
|
||||
import { addCategory, buildBody, buildForm, canSave, cleanTag, favourites, moveChoice, moveFavourite, removeCategory, removeChoice, removeGroup, setSub, toggleFavourite } from '../src/pages/products/editor/formLogic.js'
|
||||
|
||||
const cats = [
|
||||
{ id: 1, name: 'Coffees', sort_order: 0 },
|
||||
{ id: 2, name: 'Breakfast', sort_order: 1 },
|
||||
{ id: 3, name: 'Hot', parent_id: 1, sort_order: 0 },
|
||||
]
|
||||
const prod = (id, name, ids, positions = {}, o = {}) => ({
|
||||
id, name, base_price: 3, is_available: true, lifecycle_status: 'active', category_ids: ids, primary_category_id: ids[0] ?? null,
|
||||
categories: ids.map(c => ({ category_id: c, sort_order: positions[c] ?? 0 })), ...o,
|
||||
})
|
||||
const freddo = prod(10, 'Freddo', [1, 2], { 1: 0, 2: 5 })
|
||||
const toast = prod(11, 'Τοστ', [2], { 2: 1 })
|
||||
const latte = prod(12, 'Latte', [3], { 3: 0 }, { base_price: 4.5, image_url: '/x' })
|
||||
const old = prod(13, 'Old', [1], {}, { lifecycle_status: 'archived' })
|
||||
const fork = prod(14, 'Fork', [], {}, { is_service_item: true })
|
||||
const loose = prod(15, 'Loose', [])
|
||||
const all = [freddo, toast, latte, old, fork, loose]
|
||||
|
||||
test('the tree, counts and scopes follow every listing', () => {
|
||||
assert.deepEqual(categoryTree(cats).map(x => [x.cat.id, x.depth]), [[1, 0], [3, 1], [2, 0]])
|
||||
assert.equal(treeLabel(cats[2], 1).endsWith('↳ Hot'), true)
|
||||
assert.equal(countIn(all, cats, 1), 2) // freddo + latte (sub), not the archived one
|
||||
assert.equal(countIn(all, cats, 2), 2) // freddo also counts here
|
||||
assert.deepEqual(scopeProducts(all, cats, 2).map(p => p.id), [10, 11])
|
||||
assert.deepEqual(scopeProducts(all, cats, 1).map(p => p.id), [10, 12])
|
||||
assert.deepEqual(scopeProducts(all, cats, 1, { showArchived: true }).map(p => p.id), [10, 12, 13])
|
||||
assert.deepEqual(scopeProducts(all, cats, SCOPES.service).map(p => p.id), [14])
|
||||
assert.deepEqual(scopeProducts(all, cats, SCOPES.none).map(p => p.id), [15])
|
||||
assert.deepEqual(scopeProducts(all, cats, SCOPES.archived).map(p => p.id), [13])
|
||||
assert.deepEqual(scopeProducts(all, cats, SCOPES.all).map(p => p.id), [10, 11, 12, 15])
|
||||
})
|
||||
|
||||
test('filters: search ignores accents and finds Greek names in Latin letters', () => {
|
||||
const list = scopeProducts(all, cats, SCOPES.all)
|
||||
assert.deepEqual(applyFilters(list, { search: 'tost' }).map(p => p.id), [11])
|
||||
assert.deepEqual(applyFilters(list, { search: 'ΤΟΣΤ' }).map(p => p.id), [11])
|
||||
assert.deepEqual(applyFilters(list, { priceMin: 4 }).map(p => p.id), [12])
|
||||
assert.deepEqual(applyFilters(list, { quality: 'no-photo' }).map(p => p.id), [10, 11, 15])
|
||||
assert.deepEqual(applyFilters(list, { quality: 'no-cat' }).map(p => p.id), [15])
|
||||
})
|
||||
|
||||
test('groups: each category its own list in its own order; a product in two shows in both', () => {
|
||||
const inBreakfast = groupProducts(scopeProducts(all, cats, 2), cats, 2)
|
||||
assert.deepEqual(inBreakfast.map(g => [g.key, g.items.map(p => p.id)]), [[2, [11, 10]]]) // toast 1, freddo 5
|
||||
const coffees = groupProducts(scopeProducts(all, cats, 1), cats, 1)
|
||||
assert.deepEqual(coffees.map(g => [g.key, g.items.map(p => p.id)]), [[1, [10]], [3, [12]]])
|
||||
assert.deepEqual(groupProducts(scopeProducts(all, cats, 1), cats, 1, 3).map(g => g.key), [3])
|
||||
const everything = groupProducts(scopeProducts(all, cats, SCOPES.all), cats, SCOPES.all)
|
||||
assert.deepEqual(everything.map(g => [g.key, g.items.map(p => p.id)]), [[1, [10]], [3, [12]], [2, [11]], ['__none__', [15]]]) // by main category
|
||||
})
|
||||
|
||||
test('reordering sends positions for that category only', () => {
|
||||
const g = groupProducts(scopeProducts(all, cats, 2), cats, 2)[0] // [toast, freddo]
|
||||
assert.deepEqual(reorderBody(g, 10, 11), [{ id: 10, sort_order: 0, category_id: 2 }, { id: 11, sort_order: 1, category_id: 2 }])
|
||||
assert.deepEqual(stepBody(g, 11, 1), [{ id: 10, sort_order: 0, category_id: 2 }, { id: 11, sort_order: 1, category_id: 2 }])
|
||||
assert.equal(stepBody(g, 11, -1), null)
|
||||
assert.deepEqual(reorderBody({ key: '__none__', items: [loose, fork] }, 14, 15), [{ id: 14, sort_order: 0 }, { id: 15, sort_order: 1 }])
|
||||
assert.deepEqual(moveId([1, 2, 3], 3, 1), [3, 1, 2])
|
||||
assert.equal(moveId([1, 2], 1, 1), null)
|
||||
})
|
||||
|
||||
test('cost, margin and bulk categories', () => {
|
||||
assert.deepEqual(costOf({ base_price: 4, cost_simple: 1 }), { cost: 1, over: false, margin: 0.75 })
|
||||
assert.equal(costOf({ base_price: 4, cost_breakdown: [{ amount: 3 }, { amount: 2 }] }).over, true)
|
||||
assert.equal(costOf({ base_price: 4 }), null)
|
||||
assert.deepEqual(bulkCategories(freddo, 3, 'move'), { category_ids: [3], primary_category_id: 3 })
|
||||
assert.deepEqual(bulkCategories(freddo, 3, 'add'), { category_ids: [1, 2, 3], primary_category_id: 1 })
|
||||
assert.deepEqual(bulkCategories(loose, 2, 'add'), { category_ids: [2], primary_category_id: 2 })
|
||||
})
|
||||
|
||||
test('the editor keeps categories, the main one and allergens', () => {
|
||||
const f = buildForm({ ...freddo, allergens: { milk: 'contains' }, primary_category_id: 2 })
|
||||
assert.deepEqual([f.category_ids, f.primary_category_id], [[1, 2], 2])
|
||||
let g = removeCategory(f, 2)
|
||||
assert.deepEqual([g.category_ids, g.primary_category_id], [[1], 1]) // the main one went: the next takes over
|
||||
g = addCategory(removeCategory(g, 1), 3)
|
||||
assert.deepEqual([g.category_ids, g.primary_category_id], [[3], 3]) // the first one becomes main
|
||||
const body = buildBody({ ...f, base_price: '3,5' })
|
||||
assert.deepEqual([body.category_ids, body.primary_category_id, body.base_price, body.allergens], [[1, 2], 2, 3.5, { milk: 'contains' }])
|
||||
assert.equal(canSave({ ...f, name: ' ' }), false)
|
||||
assert.equal(canSave({ ...f, base_price: '' }), false)
|
||||
assert.equal(canSave({ ...f, base_price: '', is_service_item: true }), true)
|
||||
assert.equal(cleanTag(['vegan'], ' Spicy, '), 'spicy')
|
||||
assert.equal(cleanTag(['vegan'], 'VEGAN'), null)
|
||||
})
|
||||
|
||||
test('the editor: favourites across lists, choices, groups', () => {
|
||||
let f = buildForm({ name: 'X', base_price: 1, quick_options: [{ name: 'Bacon' }], ingredients: [{ name: 'Onion', group_id: 7 }],
|
||||
options: [], preference_sets: [{ name: 'Sugar', default_choice_id: 2, choices: [{ id: 1, name: 'None' }, { id: 2, name: 'Medium' }, { id: 3, name: 'Sweet' }] }],
|
||||
modifier_groups: [{ id: 7, modifier_type: 'ingredient', name: 'Veg' }] })
|
||||
f = toggleFavourite(toggleFavourite(f, 'ingredient', 0), 'quick', 0)
|
||||
assert.deepEqual(favourites(f).map(x => x.item.name), ['Onion', 'Bacon'])
|
||||
f = moveFavourite(f, 1, -1)
|
||||
assert.deepEqual(favourites(f).map(x => x.item.name), ['Bacon', 'Onion'])
|
||||
const ps = f.preference_sets[0]
|
||||
assert.equal(ps.default_choice_index, 1)
|
||||
assert.equal(moveChoice(ps, 1, 1).default_choice_index, 2) // the default follows its choice
|
||||
assert.equal(removeChoice(ps, 0).default_choice_index, 0)
|
||||
assert.equal(removeChoice(ps, 1).default_choice_index, -1)
|
||||
assert.deepEqual(setSub([{ is_default: true }, { is_default: false }], 1, 'is_default', true).map(s => s.is_default), [false, true])
|
||||
const body = buildBody(f)
|
||||
assert.equal(body.ingredients[0].group_id, 0) // the group's index in modifier_groups
|
||||
assert.equal(buildBody(removeGroup(f, 7)).ingredients[0].group_id, null)
|
||||
})
|
||||
Reference in New Issue
Block a user