Shop: item priority, block sort order and an optional "Sort by" menu

- items.priority (migration + pb_schema.json): Recommended order in Shop
  blocks, higher first, then by name. Set under Services & products, which
  now lists items in that order.
- Shop block: choose its order (Recommended, Most popular, price up/down,
  name) and optionally let visitors re-sort with a "Sort by" menu.
  sortItems() in shared/items.ts is the one place items are ordered.
- Most popular: GET /api/shop/popular returns item ids by units sold in
  paid orders over the last 90 days (ids only, no sales figures).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gWt7pNdAsocjxujY3m
This commit is contained in:
agent 2026-11-04 22:36:50 +00:00
parent 97b1da2433
commit e896fd3c99
14 changed files with 211 additions and 19 deletions

View file

@ -16,6 +16,12 @@ routerAdd('GET', '/api/shop/slots', (e) => {
return e.json(200, shop.bookableTimes($app, { item: query.get('item'), minutes: query.get('minutes'), from: query.get('from'), to: query.get('to') }));
});
// Best sellers first (ids only), for the Shop block's "Most popular" sort.
routerAdd('GET', '/api/shop/popular', (e) => {
const shop = require(`${__hooks}/shop/lib.js`);
return e.json(200, { items: shop.popularItems($app) });
});
routerAdd('GET', '/api/shop/basket/{id}', (e) => {
const shop = require(`${__hooks}/shop/lib.js`);
shop.sweepExpiredHolds($app);

View file

@ -1305,6 +1305,33 @@ function createOrder(app, basketId, payment) {
return orderId;
}
// ── Popularity ──────────────────────────────────────────────────────────
/** "Most popular" in the Shop block counts what sold in this many days. */
const POPULAR_DAYS = 90;
/**
* Item ids, best-selling first, by units in paid orders over the last
* POPULAR_DAYS (refunded orders don't count). A booking paid with a code
* counts for its service; products handed over inside a bundle (price 0, no
* code) don't — the bundle's own line counts for it. Only the order is
* returned, never the numbers: this is public, and sales figures aren't.
*/
function popularItems(app) {
const rows = arrayOf(new DynamicModel({ item: '', sold: 0 }));
app
.db()
.newQuery(
`SELECT ol.item AS item, SUM(ol.quantity) AS sold
FROM order_lines ol JOIN orders o ON o.id = ol.[[order]]
WHERE o.status = 'paid' AND o.created >= {:since} AND (ol.price > 0 OR ol.pass != '')
GROUP BY ol.item HAVING sold > 0 ORDER BY sold DESC, ol.item`
)
.bind({ since: pbDate(Date.now() - POPULAR_DAYS * DAY_MS) })
.all(rows);
return rows.map((row) => row.item);
}
// ── Orders ──────────────────────────────────────────────────────────────
function globalData(app, key) {
@ -1409,6 +1436,7 @@ module.exports = {
issuePass,
bookableTimes,
slotDetails,
popularItems,
sweepExpiredHolds,
loadActiveBasket,
basketPayload,