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:
agent 2026-10-25 21:39:51 +00:00
parent e30e1d3beb
commit 32984ac01d
4 changed files with 35 additions and 8 deletions

View file

@ -1,12 +1,19 @@
import type PocketBase from 'pocketbase';
import { getFileUrl } from '../pb';
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`. */
export function mapItemRecord(record: Record<string, any>): ItemRecord {
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 {
id: record.id,
name: String(record.name || ''),
@ -15,8 +22,11 @@ export function mapItemRecord(record: Record<string, any>): ItemRecord {
image: String(record.image || ''),
imageUrl: media ? getFileUrl('media', media.id, media.file) : '',
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) : [],
bundleParts: type === 'bundle' ? parseBundleParts(record.bundleParts) : [],
validMonths: type === 'bundle' ? Number(record.validMonths) || null : null,
bufferMinutes: record.bufferMinutes ?? null,
capacity: record.capacity ?? null,
active: record.active !== false