domaino/pocketbase/pb_migrations/1793500000_item_priority.js
agent e896fd3c99 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
2026-11-04 22:36:50 +00:00

29 lines
959 B
JavaScript

/// <reference path="../pb_data/types.d.ts" />
// items.priority: where an item comes in Shop blocks under "Recommended"
// (the default order). Higher comes first; equal priorities go by name.
// Empty reads back as 0, so every existing item starts at 0 and the shop
// keeps its old alphabetical order until priorities are set.
function find(app, name) {
try {
return app.findCollectionByNameOrId(name);
} catch (e) {
return null; // fresh install: pb_schema.json (already with the field) is imported later
}
}
migrate(
(app) => {
const items = find(app, 'items');
if (!items || items.fields.getByName('priority')) return;
items.fields.add(new NumberField({ id: 'num_itempriority', name: 'priority', min: -1000, max: 1000, onlyInt: true }));
app.save(items);
},
(app) => {
const items = find(app, 'items');
if (!items || !items.fields.getByName('priority')) return;
items.fields.removeByName('priority');
app.save(items);
}
);