@zeyolabs/shopping 2026.11.54 → 2026.11.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/catalogue/service.server.ts +2 -2
- package/db/schema.ts +7 -2
- package/package.json +2 -2
- package/stock/service.server.ts +38 -9
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
products,
|
|
11
11
|
productSlugAliases,
|
|
12
12
|
} from '../db/schema';
|
|
13
|
-
import {
|
|
13
|
+
import { StockService } from '../stock/service.server';
|
|
14
14
|
import { CoverService } from './covers.server';
|
|
15
15
|
import { Paging, Search } from './library';
|
|
16
16
|
|
|
@@ -89,7 +89,7 @@ export class CatalogueService {
|
|
|
89
89
|
.select()
|
|
90
90
|
.from(products)
|
|
91
91
|
.where(where)
|
|
92
|
-
.orderBy(desc(
|
|
92
|
+
.orderBy(desc(products.sellable), asc(products.sortOrder), asc(products.name))
|
|
93
93
|
.limit(Paging.PER_PAGE)
|
|
94
94
|
.offset((page - 1) * Paging.PER_PAGE),
|
|
95
95
|
]);
|
package/db/schema.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { relations } from 'drizzle-orm';
|
|
1
|
+
import { relations, sql } from 'drizzle-orm';
|
|
2
2
|
import {
|
|
3
3
|
index,
|
|
4
4
|
integer,
|
|
@@ -45,6 +45,8 @@ export const products = sqliteTable(
|
|
|
45
45
|
pricePence: integer('price_pence').notNull(),
|
|
46
46
|
/** When the product last went from not sellable to sellable (set by `StockService.setStock`). */
|
|
47
47
|
restockedAt: integer('restocked_at'),
|
|
48
|
+
/** Whether the product can be bought now; kept by `StockService` so listings can sort on it. */
|
|
49
|
+
sellable: integer('sellable', { mode: 'boolean' }).notNull().default(false),
|
|
48
50
|
shortDescription: text('short_description').notNull().default(''),
|
|
49
51
|
sku: text('sku').notNull().unique(),
|
|
50
52
|
slug: text('slug').notNull().unique(),
|
|
@@ -57,7 +59,10 @@ export const products = sqliteTable(
|
|
|
57
59
|
/** This product's option within its variation group, e.g. "Black". */
|
|
58
60
|
variationLabel: text('variation_label'),
|
|
59
61
|
},
|
|
60
|
-
(t) => [
|
|
62
|
+
(t) => [
|
|
63
|
+
index('products_listing_idx').on(t.active, sql`${t.sellable} desc`, t.sortOrder, t.name),
|
|
64
|
+
index('products_variation_group_idx').on(t.variationGroupId),
|
|
65
|
+
]
|
|
61
66
|
);
|
|
62
67
|
|
|
63
68
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeyolabs/shopping",
|
|
3
|
-
"version": "2026.11.
|
|
3
|
+
"version": "2026.11.56",
|
|
4
4
|
"description": "Shop and order fulfilment for SvelteKit 3 on Cloudflare: catalogue, basket, Stripe Checkout, orders, Royal Mail Click & Drop, order emails, packing, pages and admin.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
"@eslint/compat": "2.1.1",
|
|
68
68
|
"@eslint/js": "10.0.1",
|
|
69
69
|
"@sveltejs/kit": "3.2.0",
|
|
70
|
-
"@zeyolabs/kit": "2026.11.
|
|
70
|
+
"@zeyolabs/kit": "2026.11.48",
|
|
71
71
|
"@zeyolabs/ui": "2026.11.44",
|
|
72
72
|
"drizzle-orm": "0.45.4",
|
|
73
73
|
"eslint": "10.15.0",
|
package/stock/service.server.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Error400, Error404 } from '@zeyolabs/kit/utils/errors';
|
|
2
|
-
import { asc, count, eq, inArray, type SQL, sql } from 'drizzle-orm';
|
|
2
|
+
import { asc, count, eq, inArray, or, type SQL, sql } from 'drizzle-orm';
|
|
3
3
|
|
|
4
4
|
import type { ShopDb } from '../db/index';
|
|
5
5
|
|
|
@@ -115,6 +115,7 @@ export class StockService {
|
|
|
115
115
|
.insert(productComponents)
|
|
116
116
|
.values({ componentId: c.componentId, productId, quantity: c.quantity })
|
|
117
117
|
),
|
|
118
|
+
this.refreshSellable(productId),
|
|
118
119
|
];
|
|
119
120
|
}
|
|
120
121
|
|
|
@@ -152,9 +153,11 @@ export class StockService {
|
|
|
152
153
|
]);
|
|
153
154
|
const packSize = new Map(rows.map((r) => [r.id, r.packSize]));
|
|
154
155
|
const statements = [];
|
|
156
|
+
const takenFrom = new Set<number>();
|
|
155
157
|
for (const line of lines) {
|
|
156
158
|
if (line.singleUnits) {
|
|
157
159
|
statements.push(this.decrement(line.productId, line.quantity));
|
|
160
|
+
takenFrom.add(line.productId);
|
|
158
161
|
continue;
|
|
159
162
|
}
|
|
160
163
|
const parts = components.get(line.productId);
|
|
@@ -163,11 +166,13 @@ export class StockService {
|
|
|
163
166
|
statements.push(
|
|
164
167
|
this.decrement(part.componentId, line.quantity * part.quantity * part.packSize)
|
|
165
168
|
);
|
|
169
|
+
takenFrom.add(part.componentId);
|
|
166
170
|
}
|
|
167
171
|
} else {
|
|
168
172
|
statements.push(
|
|
169
173
|
this.decrement(line.productId, line.quantity * (packSize.get(line.productId) ?? 1))
|
|
170
174
|
);
|
|
175
|
+
takenFrom.add(line.productId);
|
|
171
176
|
}
|
|
172
177
|
statements.push(
|
|
173
178
|
this.db
|
|
@@ -176,9 +181,30 @@ export class StockService {
|
|
|
176
181
|
.where(eq(products.id, line.productId))
|
|
177
182
|
);
|
|
178
183
|
}
|
|
184
|
+
for (const id of takenFrom) statements.push(this.refreshSellable(id));
|
|
179
185
|
return statements;
|
|
180
186
|
}
|
|
181
187
|
|
|
188
|
+
/** Recomputes `sellable` for every product, for a migration or a repair. Reads the whole table. */
|
|
189
|
+
async refreshAllSellable(): Promise<void> {
|
|
190
|
+
await this.db.update(products).set({ sellable: sql`${sellableSql}` });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* The statement that recomputes `sellable` for a product and for the combined products it is a
|
|
195
|
+
* component of. Every write that can change what is sellable ends its batch with it.
|
|
196
|
+
*/
|
|
197
|
+
refreshSellable(productId: number | SQL) {
|
|
198
|
+
const partOf = this.db
|
|
199
|
+
.select({ id: productComponents.productId })
|
|
200
|
+
.from(productComponents)
|
|
201
|
+
.where(eq(productComponents.componentId, productId));
|
|
202
|
+
return this.db
|
|
203
|
+
.update(products)
|
|
204
|
+
.set({ sellable: sql`${sellableSql}` })
|
|
205
|
+
.where(or(eq(products.id, productId), inArray(products.id, partOf)));
|
|
206
|
+
}
|
|
207
|
+
|
|
182
208
|
/** Replaces a combined product's components (checked). An empty list makes it a plain product. */
|
|
183
209
|
async setComponents(
|
|
184
210
|
productId: number,
|
|
@@ -195,14 +221,17 @@ export class StockService {
|
|
|
195
221
|
* goes from not sellable to sellable, so it cannot drift.
|
|
196
222
|
*/
|
|
197
223
|
async setStock(productId: number, units: number, now = Date.now()): Promise<void> {
|
|
198
|
-
const updated = await this.db
|
|
199
|
-
.
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
224
|
+
const [updated] = await this.db.batch([
|
|
225
|
+
this.db
|
|
226
|
+
.update(products)
|
|
227
|
+
.set({
|
|
228
|
+
restockedAt: sql`case when ${products.stock} < ${products.packSize} and ${units} >= ${products.packSize} then ${now} else ${products.restockedAt} end`,
|
|
229
|
+
stock: units,
|
|
230
|
+
})
|
|
231
|
+
.where(eq(products.id, productId))
|
|
232
|
+
.returning({ id: products.id }),
|
|
233
|
+
this.refreshSellable(productId),
|
|
234
|
+
]);
|
|
206
235
|
if (updated.length === 0) throw Error404('Product not found.');
|
|
207
236
|
}
|
|
208
237
|
|