Stock: items.trackStock, so an empty stock really means unlimited
PocketBase 0.23 can't store an empty number (it reads back as 0), so a product saved with "Unlimited when empty" stock was sold out — on main too. trackStock says whether stock counts; existing products get it on, so nothing that sells today changes. The shop maps untracked stock to null (unlimited), as the UI always assumed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW
This commit is contained in:
parent
e30e1d3beb
commit
32984ac01d
4 changed files with 35 additions and 8 deletions
|
|
@ -1,12 +1,19 @@
|
||||||
import type PocketBase from 'pocketbase';
|
import type PocketBase from 'pocketbase';
|
||||||
import { getFileUrl } from '../pb';
|
import { getFileUrl } from '../pb';
|
||||||
import { parseDurations } from '../items';
|
import { parseDurations } from '../items';
|
||||||
import type { BookableTime, ItemRecord } from '../types';
|
import type { BookableTime, BundlePart, ItemRecord, ItemType } from '../types';
|
||||||
|
|
||||||
|
/** A bundle's `bundleParts` field (JSON) → valid parts. */
|
||||||
|
export function parseBundleParts(raw: unknown): BundlePart[] {
|
||||||
|
return (Array.isArray(raw) ? raw : [])
|
||||||
|
.map((p) => ({ item: String(p?.item || ''), minutes: p?.minutes ? Math.floor(Number(p.minutes)) : null, count: Math.floor(Number(p?.count)) || 0 }))
|
||||||
|
.filter((p) => p.item && p.count > 0);
|
||||||
|
}
|
||||||
|
|
||||||
/** An `items` record (with `expand.image`) → `ItemRecord`. */
|
/** An `items` record (with `expand.image`) → `ItemRecord`. */
|
||||||
export function mapItemRecord(record: Record<string, any>): ItemRecord {
|
export function mapItemRecord(record: Record<string, any>): ItemRecord {
|
||||||
const media = record.expand?.image;
|
const media = record.expand?.image;
|
||||||
const type = record.type === 'item_with_time' ? 'item_with_time' : 'item';
|
const type: ItemType = record.type === 'item_with_time' || record.type === 'bundle' ? record.type : 'item';
|
||||||
return {
|
return {
|
||||||
id: record.id,
|
id: record.id,
|
||||||
name: String(record.name || ''),
|
name: String(record.name || ''),
|
||||||
|
|
@ -15,8 +22,11 @@ export function mapItemRecord(record: Record<string, any>): ItemRecord {
|
||||||
image: String(record.image || ''),
|
image: String(record.image || ''),
|
||||||
imageUrl: media ? getFileUrl('media', media.id, media.file) : '',
|
imageUrl: media ? getFileUrl('media', media.id, media.file) : '',
|
||||||
type,
|
type,
|
||||||
stock: record.stock ?? null,
|
// null = unlimited: PocketBase reads an empty number back as 0, so `trackStock` says whether it counts.
|
||||||
|
stock: type === 'item' && record.trackStock ? Number(record.stock) || 0 : null,
|
||||||
durations: type === 'item_with_time' ? parseDurations(record.durations) : [],
|
durations: type === 'item_with_time' ? parseDurations(record.durations) : [],
|
||||||
|
bundleParts: type === 'bundle' ? parseBundleParts(record.bundleParts) : [],
|
||||||
|
validMonths: type === 'bundle' ? Number(record.validMonths) || null : null,
|
||||||
bufferMinutes: record.bufferMinutes ?? null,
|
bufferMinutes: record.bufferMinutes ?? null,
|
||||||
capacity: record.capacity ?? null,
|
capacity: record.capacity ?? null,
|
||||||
active: record.active !== false
|
active: record.active !== false
|
||||||
|
|
|
||||||
|
|
@ -330,6 +330,7 @@
|
||||||
{ "id": "rel_itemimage01", "name": "image", "type": "relation", "system": false, "required": false, "presentable": false, "unique": false, "collectionId": "pbc_media00001", "cascadeDelete": false, "minSelect": 0, "maxSelect": 1, "displayFields": null },
|
{ "id": "rel_itemimage01", "name": "image", "type": "relation", "system": false, "required": false, "presentable": false, "unique": false, "collectionId": "pbc_media00001", "cascadeDelete": false, "minSelect": 0, "maxSelect": 1, "displayFields": null },
|
||||||
{ "id": "sel_itemtype001", "name": "type", "type": "select", "system": false, "required": true, "presentable": false, "unique": false, "maxSelect": 1, "values": ["item", "item_with_time", "bundle"] },
|
{ "id": "sel_itemtype001", "name": "type", "type": "select", "system": false, "required": true, "presentable": false, "unique": false, "maxSelect": 1, "values": ["item", "item_with_time", "bundle"] },
|
||||||
{ "id": "num_itemstock01", "name": "stock", "type": "number", "system": false, "required": false, "presentable": false, "unique": false, "options": { "min": 0, "max": null } },
|
{ "id": "num_itemstock01", "name": "stock", "type": "number", "system": false, "required": false, "presentable": false, "unique": false, "options": { "min": 0, "max": null } },
|
||||||
|
{ "id": "bool_itemtrackst", "name": "trackStock", "type": "bool", "system": false, "required": false, "presentable": false, "hidden": false },
|
||||||
{ "id": "json_itemdurations", "name": "durations", "type": "json", "system": false, "required": false, "presentable": false, "hidden": false, "maxSize": 20000 },
|
{ "id": "json_itemdurations", "name": "durations", "type": "json", "system": false, "required": false, "presentable": false, "hidden": false, "maxSize": 20000 },
|
||||||
{ "id": "json_itembundle1", "name": "bundleParts", "type": "json", "system": false, "required": false, "presentable": false, "hidden": false, "maxSize": 20000 },
|
{ "id": "json_itembundle1", "name": "bundleParts", "type": "json", "system": false, "required": false, "presentable": false, "hidden": false, "maxSize": 20000 },
|
||||||
{ "id": "num_itemvalidmo1", "name": "validMonths", "type": "number", "system": false, "required": false, "presentable": false, "hidden": false, "min": 1, "max": 120, "onlyInt": true },
|
{ "id": "num_itemvalidmo1", "name": "validMonths", "type": "number", "system": false, "required": false, "presentable": false, "hidden": false, "min": 1, "max": 120, "onlyInt": true },
|
||||||
|
|
|
||||||
|
|
@ -490,7 +490,7 @@ function normaliseCode(value) {
|
||||||
.replace(/[^A-Z0-9]/g, '');
|
.replace(/[^A-Z0-9]/g, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** `ABCD2345` → `ABCD-2345`, as shown to people. */
|
/** `ABCD2345` → `ABCD-2345`, as shown to people (formatCode in packages/shared/src/items.ts does the same). */
|
||||||
function formatCode(code) {
|
function formatCode(code) {
|
||||||
return code.length === CODE_LENGTH ? code.slice(0, 4) + '-' + code.slice(4) : code;
|
return code.length === CODE_LENGTH ? code.slice(0, 4) + '-' + code.slice(4) : code;
|
||||||
}
|
}
|
||||||
|
|
@ -628,8 +628,7 @@ function stockProblem(app, needs) {
|
||||||
for (const id of Object.keys(needs)) {
|
for (const id of Object.keys(needs)) {
|
||||||
const item = findOrNull(app, 'items', id);
|
const item = findOrNull(app, 'items', id);
|
||||||
if (!item) continue;
|
if (!item) continue;
|
||||||
const stock = item.get('stock');
|
if (item.getBool('trackStock') && needs[id] > item.getInt('stock')) {
|
||||||
if (stock != null && stock !== '' && needs[id] > item.getInt('stock')) {
|
|
||||||
return conflict(item.getInt('stock') > 0 ? `Only ${item.getInt('stock')} of "${item.getString('name')}" left in stock` : `"${item.getString('name')}" is sold out`);
|
return conflict(item.getInt('stock') > 0 ? `Only ${item.getInt('stock')} of "${item.getString('name')}" left in stock` : `"${item.getString('name')}" is sold out`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1176,7 +1175,7 @@ function createOrderFromIntent(app, intent) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function takeStock(app, item, quantity) {
|
function takeStock(app, item, quantity) {
|
||||||
if (!item || item.get('stock') == null || item.get('stock') === '') return;
|
if (!item || !item.getBool('trackStock')) return;
|
||||||
item.set('stock', Math.max(0, item.getInt('stock') - quantity));
|
item.set('stock', Math.max(0, item.getInt('stock') - quantity));
|
||||||
app.save(item);
|
app.save(item);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,11 @@
|
||||||
// - baskets.pass: the code applied at checkout. basket_lines.pass /
|
// - baskets.pass: the code applied at checkout. basket_lines.pass /
|
||||||
// order_lines.pass: the pass that pays for that booking.
|
// order_lines.pass: the pass that pays for that booking.
|
||||||
// - basket_lines.giftTo / giftMessage: who a bundle in the basket is for.
|
// - basket_lines.giftTo / giftMessage: who a bundle in the basket is for.
|
||||||
|
// - items.trackStock: whether `stock` is enforced. PocketBase can't store an
|
||||||
|
// empty number (it reads back as 0), so "unlimited when empty" never
|
||||||
|
// worked — every product without stock was sold out. Existing products
|
||||||
|
// get trackStock = true: nothing that sells today changes; clearing the
|
||||||
|
// stock field in the admin makes a product unlimited.
|
||||||
// - Not required any more, as PocketBase counts 0 / '' as blank:
|
// - Not required any more, as PocketBase counts 0 / '' as blank:
|
||||||
// order_lines.price (a booking paid with a code, or a product inside a
|
// order_lines.price (a booking paid with a code, or a product inside a
|
||||||
// bundle, costs 0), orders.total and orders.stripePaymentIntentId (an
|
// bundle, costs 0), orders.total and orders.stripePaymentIntentId (an
|
||||||
|
|
@ -42,6 +47,18 @@ migrate(
|
||||||
|
|
||||||
const type = items.fields.getByName('type');
|
const type = items.fields.getByName('type');
|
||||||
if (type && type.values.indexOf('bundle') < 0) type.values = [...type.values, 'bundle'];
|
if (type && type.values.indexOf('bundle') < 0) type.values = [...type.values, 'bundle'];
|
||||||
|
if (!items.fields.getByName('trackStock')) {
|
||||||
|
items.fields.add(new BoolField({ id: 'bool_itemtrackst', name: 'trackStock' }));
|
||||||
|
app.save(items);
|
||||||
|
for (const item of app.findRecordsByFilter('items', "type = 'item'", '', 0, 0)) {
|
||||||
|
item.set('trackStock', true);
|
||||||
|
try {
|
||||||
|
app.saveNoValidate(item);
|
||||||
|
} catch (e) {
|
||||||
|
console.log(`[migration] could not set trackStock on item ${item.id}: ${e}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
if (!items.fields.getByName('bundleParts')) {
|
if (!items.fields.getByName('bundleParts')) {
|
||||||
items.fields.add(new JSONField({ id: 'json_itembundle1', name: 'bundleParts', maxSize: 20000 }));
|
items.fields.add(new JSONField({ id: 'json_itembundle1', name: 'bundleParts', maxSize: 20000 }));
|
||||||
items.fields.add(new NumberField({ id: 'num_itemvalidmo1', name: 'validMonths', min: 1, max: 120, onlyInt: true }));
|
items.fields.add(new NumberField({ id: 'num_itemvalidmo1', name: 'validMonths', min: 1, max: 120, onlyInt: true }));
|
||||||
|
|
@ -133,7 +150,7 @@ migrate(
|
||||||
}
|
}
|
||||||
const type = items.fields.getByName('type');
|
const type = items.fields.getByName('type');
|
||||||
if (type) type.values = type.values.filter((value) => value !== 'bundle');
|
if (type) type.values = type.values.filter((value) => value !== 'bundle');
|
||||||
for (const name of ['bundleParts', 'validMonths']) {
|
for (const name of ['bundleParts', 'validMonths', 'trackStock']) {
|
||||||
if (items.fields.getByName(name)) items.fields.removeByName(name);
|
if (items.fields.getByName(name)) items.fields.removeByName(name);
|
||||||
}
|
}
|
||||||
app.save(items);
|
app.save(items);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue