@zeyolabs/shopping 2026.11.11 → 2026.11.13
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/README.md +24 -2
- package/basket/client.remote.ts +62 -0
- package/basket/cookies.server.ts +38 -0
- package/basket/library.ts +44 -0
- package/{pages → basket/pages}/Basket.svelte +31 -19
- package/basket/schema.ts +14 -0
- package/basket/service.server.ts +123 -0
- package/bundles/admin/BundleEditPage.svelte +24 -0
- package/bundles/admin/BundleForm.svelte +107 -0
- package/bundles/admin/BundlesPage.svelte +40 -0
- package/bundles/admin.remote.ts +30 -0
- package/bundles/client.remote.ts +29 -0
- package/bundles/loads.server.ts +6 -0
- package/bundles/pages/Bundle.svelte +145 -0
- package/bundles/schema.ts +82 -0
- package/bundles/service.server.ts +204 -0
- package/catalogue/admin/CategoriesPage.svelte +53 -0
- package/catalogue/admin/CategoryForm.svelte +50 -0
- package/catalogue/admin/ProductEditPage.svelte +25 -0
- package/catalogue/admin/ProductForm.svelte +249 -0
- package/catalogue/admin/ProductsPage.svelte +88 -0
- package/catalogue/admin.remote.ts +50 -0
- package/catalogue/admin.service.server.ts +239 -0
- package/catalogue/client.remote.ts +52 -0
- package/{components → catalogue/components}/ProductCard.svelte +8 -8
- package/catalogue/covers.server.ts +33 -0
- package/catalogue/library.ts +38 -0
- package/{routes/catalogue-loads.ts → catalogue/loads.server.ts} +11 -3
- package/{pages → catalogue/pages}/Category.svelte +5 -5
- package/{pages → catalogue/pages}/Product.svelte +37 -25
- package/catalogue/pages/Search.svelte +52 -0
- package/{pages → catalogue/pages}/Shop.svelte +2 -2
- package/catalogue/schema.ts +120 -0
- package/catalogue/service.server.ts +141 -0
- package/{routes/checkout-complete.ts → checkout/complete.server.ts} +6 -5
- package/checkout/manager.server.ts +127 -0
- package/{pages → checkout/pages}/CheckoutSuccess.svelte +2 -2
- package/{routes/stripe-webhook.ts → checkout/webhook.server.ts} +7 -7
- package/{royalmail.ts → clients/royalmail.ts} +30 -11
- package/clients/shop.server.ts +20 -0
- package/clients/stripe.ts +11 -0
- package/config.ts +23 -4
- package/context.ts +24 -0
- package/customers/client.remote.ts +19 -0
- package/customers/pages/Account.svelte +70 -0
- package/customers/service.server.ts +57 -0
- package/db/schema.ts +96 -0
- package/{admin → fulfilment/admin}/OrderPage.svelte +23 -16
- package/{admin → fulfilment/admin}/OrdersPage.svelte +7 -7
- package/{remote/admin-orders.remote.ts → fulfilment/admin.remote.ts} +11 -28
- package/fulfilment/dispatch-email.ts +66 -0
- package/fulfilment/invoice.server.ts +14 -0
- package/{routes/order-label.ts → fulfilment/label.server.ts} +3 -3
- package/fulfilment/library.ts +52 -0
- package/fulfilment/manager.server.ts +188 -0
- package/fulfilment/service.server.ts +155 -0
- package/{server/mail.ts → mail/default.ts} +9 -9
- package/{remote/order.remote.ts → orders/client.remote.ts} +10 -5
- package/orders/email.ts +111 -0
- package/orders/invoice.server.ts +19 -0
- package/orders/library.ts +26 -0
- package/orders/manager.server.ts +132 -0
- package/{pages → orders/pages}/Order.svelte +58 -16
- package/orders/service.server.ts +99 -0
- package/package.json +20 -32
- package/reviews/admin/ReviewsPage.svelte +95 -0
- package/reviews/admin.remote.ts +22 -0
- package/reviews/client.remote.ts +43 -0
- package/reviews/components/ReviewForm.svelte +32 -0
- package/reviews/components/Reviews.svelte +48 -0
- package/reviews/schema.ts +21 -0
- package/reviews/service.server.ts +122 -0
- package/shipping/library.ts +18 -0
- package/shipping/service.server.ts +88 -0
- package/{admin → stock/admin}/StockPage.svelte +1 -1
- package/{admin → stock/admin}/StockRow.svelte +2 -2
- package/stock/admin.remote.ts +19 -0
- package/{admin/stock.schema.ts → stock/schema.ts} +2 -1
- package/{server/stock-service.ts → stock/service.server.ts} +105 -58
- package/stock-alerts/email.ts +42 -0
- package/stock-alerts/job.server.ts +75 -0
- package/stock-alerts/service.server.ts +74 -0
- package/{components → ui}/PageTitle.svelte +5 -5
- package/ui/Stars.svelte +18 -0
- package/utils/batches.ts +21 -0
- package/utils/errors.ts +15 -0
- package/utils/form-input.ts +28 -0
- package/utils/images.ts +7 -0
- package/utils/money.ts +9 -0
- package/utils/schemas.ts +22 -0
- package/client.ts +0 -24
- package/emails/back-in-stock.ts +0 -36
- package/emails/dispatch.ts +0 -60
- package/emails/order.ts +0 -103
- package/images.ts +0 -2
- package/money.ts +0 -4
- package/order-links.ts +0 -11
- package/packing.ts +0 -49
- package/remote/admin-stock.remote.ts +0 -13
- package/remote/basket.remote.ts +0 -63
- package/remote/catalogue.remote.ts +0 -45
- package/server/admin-orders.ts +0 -304
- package/server/admin-stock.ts +0 -28
- package/server/basket.ts +0 -187
- package/server/catalogue.ts +0 -114
- package/server/checkout.ts +0 -41
- package/server/orders.ts +0 -182
- package/server/shipping.ts +0 -82
- package/server/stock-alert-service.ts +0 -30
- package/server/stock-alerts.ts +0 -105
- package/server/stripe.ts +0 -70
- package/shipping-rules.ts +0 -15
- /package/{admin → fulfilment}/filters.ts +0 -0
- /package/{admin/orders.schema.ts → fulfilment/schema.ts} +0 -0
- /package/{stock.ts → stock/library.ts} +0 -0
package/server/stock-alerts.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { logger } from '@zeyolabs/kit/server/logger';
|
|
2
|
-
import { and, asc, eq, isNull } from 'drizzle-orm';
|
|
3
|
-
|
|
4
|
-
import type { BackInStockEmailData } from '../emails/back-in-stock';
|
|
5
|
-
|
|
6
|
-
import { shopping } from '../config';
|
|
7
|
-
import { products, stockAlerts } from '../db/schema';
|
|
8
|
-
import { sellableSql } from './stock-service';
|
|
9
|
-
|
|
10
|
-
/** How many back-in-stock emails go out per run. The worker runs once a minute. */
|
|
11
|
-
export const BACK_IN_STOCK_BATCH = 50;
|
|
12
|
-
|
|
13
|
-
/** An SMTP error can carry the recipient's address, so logs get the failure's kind, not its text. */
|
|
14
|
-
const safeError = (what: string, error: unknown) =>
|
|
15
|
-
new Error(`${what}: ${error instanceof Error ? error.name : 'unknown error'}`);
|
|
16
|
-
|
|
17
|
-
export type PendingAlert = BackInStockEmailData & {
|
|
18
|
-
createdAt: number;
|
|
19
|
-
id: number;
|
|
20
|
-
productId: number;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/** Signups waiting on a product that is sellable again (a whole pack, or every component of a set), oldest first. Rows already marked notified are skipped. */
|
|
24
|
-
export async function pendingAlerts(limit = BACK_IN_STOCK_BATCH): Promise<PendingAlert[]> {
|
|
25
|
-
return shopping()
|
|
26
|
-
.db()
|
|
27
|
-
.select({
|
|
28
|
-
createdAt: stockAlerts.createdAt,
|
|
29
|
-
email: stockAlerts.email,
|
|
30
|
-
id: stockAlerts.id,
|
|
31
|
-
pricePence: products.pricePence,
|
|
32
|
-
productId: stockAlerts.productId,
|
|
33
|
-
productName: products.name,
|
|
34
|
-
slug: products.slug,
|
|
35
|
-
})
|
|
36
|
-
.from(stockAlerts)
|
|
37
|
-
.innerJoin(products, eq(products.id, stockAlerts.productId))
|
|
38
|
-
.where(and(sellableSql, eq(products.active, true), isNull(stockAlerts.notifiedAt)))
|
|
39
|
-
.orderBy(asc(stockAlerts.id))
|
|
40
|
-
.limit(limit);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Emails each alert once. A signup is claimed (deleted) before the send, so two overlapping runs
|
|
45
|
-
* can't both email the same person: only the run whose delete took the row sends. If the send
|
|
46
|
-
* fails the signup is put back and tried again on the next run; one failure never stops the rest.
|
|
47
|
-
* The cost of claiming first: a Worker killed between the delete and the send loses that signup.
|
|
48
|
-
*/
|
|
49
|
-
export async function deliverAlerts(
|
|
50
|
-
alerts: PendingAlert[],
|
|
51
|
-
send: (alert: BackInStockEmailData) => Promise<void>,
|
|
52
|
-
claim: (id: number) => Promise<boolean>,
|
|
53
|
-
restore: (alert: PendingAlert) => Promise<void>
|
|
54
|
-
): Promise<{ failed: number; sent: number }> {
|
|
55
|
-
let sent = 0;
|
|
56
|
-
let failed = 0;
|
|
57
|
-
for (const alert of alerts) {
|
|
58
|
-
try {
|
|
59
|
-
if (!(await claim(alert.id))) continue; // another run already took it
|
|
60
|
-
} catch (error) {
|
|
61
|
-
failed++;
|
|
62
|
-
logger.error(error, { alertId: alert.id, op: 'backInStockClaim' });
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
try {
|
|
66
|
-
await send(alert);
|
|
67
|
-
sent++;
|
|
68
|
-
} catch (error) {
|
|
69
|
-
failed++;
|
|
70
|
-
logger.error(safeError('Back-in-stock send failed', error), {
|
|
71
|
-
alertId: alert.id,
|
|
72
|
-
op: 'backInStockEmail',
|
|
73
|
-
});
|
|
74
|
-
try {
|
|
75
|
-
await restore(alert);
|
|
76
|
-
} catch (restoreError) {
|
|
77
|
-
logger.error(restoreError, { alertId: alert.id, op: 'backInStockRestore' });
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return { failed, sent };
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** One run of the cron: email up to a batch of waiting customers and delete their signups. */
|
|
85
|
-
export async function sendBackInStockBatch(limit = BACK_IN_STOCK_BATCH) {
|
|
86
|
-
const db = shopping().db();
|
|
87
|
-
const { backInStock } = shopping().mail;
|
|
88
|
-
// A shop without a back-in-stock email sends nothing and leaves the signups alone.
|
|
89
|
-
if (!backInStock) return { failed: 0, sent: 0 };
|
|
90
|
-
return deliverAlerts(
|
|
91
|
-
await pendingAlerts(limit),
|
|
92
|
-
({ email, pricePence, productName, slug }) =>
|
|
93
|
-
backInStock({ email, pricePence, productName, slug }),
|
|
94
|
-
async (id) => {
|
|
95
|
-
const taken = await db
|
|
96
|
-
.delete(stockAlerts)
|
|
97
|
-
.where(eq(stockAlerts.id, id))
|
|
98
|
-
.returning({ id: stockAlerts.id });
|
|
99
|
-
return taken.length > 0;
|
|
100
|
-
},
|
|
101
|
-
async ({ createdAt, email, productId }) => {
|
|
102
|
-
await db.insert(stockAlerts).values({ createdAt, email, productId }).onConflictDoNothing();
|
|
103
|
-
}
|
|
104
|
-
);
|
|
105
|
-
}
|
package/server/stripe.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import Stripe from 'stripe';
|
|
2
|
-
|
|
3
|
-
import type { BasketSummary } from './basket';
|
|
4
|
-
|
|
5
|
-
import { secret, shopping } from '../config';
|
|
6
|
-
import { productImageUrl } from '../images';
|
|
7
|
-
|
|
8
|
-
type ShippableSummary = BasketSummary & { shipping: NonNullable<BasketSummary['shipping']> };
|
|
9
|
-
|
|
10
|
-
export async function getStripe(): Promise<Stripe> {
|
|
11
|
-
return new Stripe(secret('stripeSecretKey'), {
|
|
12
|
-
httpClient: Stripe.createFetchHttpClient(),
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** Subtle-crypto verifier, required for webhooks on Workers (no Node crypto). */
|
|
17
|
-
export const webhookCrypto = Stripe.createSubtleCryptoProvider();
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Starts a hosted Checkout for the priced basket. The shipping fee is a Stripe shipping rate so the
|
|
21
|
-
* customer sees it before paying; the order itself is only created from the webhook once paid.
|
|
22
|
-
*/
|
|
23
|
-
export async function createCheckoutSession(
|
|
24
|
-
stripe: Stripe,
|
|
25
|
-
summary: ShippableSummary,
|
|
26
|
-
origin: string
|
|
27
|
-
): Promise<string> {
|
|
28
|
-
const session = await stripe.checkout.sessions.create({
|
|
29
|
-
cancel_url: `${origin}/basket`,
|
|
30
|
-
line_items: summary.lines.map((line) => ({
|
|
31
|
-
price_data: {
|
|
32
|
-
currency: 'gbp',
|
|
33
|
-
product_data: {
|
|
34
|
-
images: line.cover
|
|
35
|
-
? [new URL(productImageUrl(shopping().imageBaseUrl, line.cover.src), origin).href]
|
|
36
|
-
: undefined,
|
|
37
|
-
metadata: { productId: String(line.id), sku: line.sku },
|
|
38
|
-
name: line.name,
|
|
39
|
-
},
|
|
40
|
-
unit_amount: line.pricePence,
|
|
41
|
-
},
|
|
42
|
-
quantity: line.available,
|
|
43
|
-
})),
|
|
44
|
-
metadata: {
|
|
45
|
-
country: summary.country,
|
|
46
|
-
shippingService: summary.shipping.service,
|
|
47
|
-
weightGrams: String(summary.weightGrams),
|
|
48
|
-
},
|
|
49
|
-
mode: 'payment',
|
|
50
|
-
phone_number_collection: { enabled: true },
|
|
51
|
-
shipping_address_collection: {
|
|
52
|
-
allowed_countries: [
|
|
53
|
-
summary.country as Stripe.Checkout.SessionCreateParams.ShippingAddressCollection.AllowedCountry,
|
|
54
|
-
],
|
|
55
|
-
},
|
|
56
|
-
shipping_options: [
|
|
57
|
-
{
|
|
58
|
-
shipping_rate_data: {
|
|
59
|
-
display_name:
|
|
60
|
-
summary.shippingPence === 0 ? `Free ${summary.shipping.name}` : summary.shipping.name,
|
|
61
|
-
fixed_amount: { amount: summary.shippingPence, currency: 'gbp' },
|
|
62
|
-
type: 'fixed_amount',
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
],
|
|
66
|
-
success_url: `${origin}/checkout/complete?session_id={CHECKOUT_SESSION_ID}`,
|
|
67
|
-
});
|
|
68
|
-
if (!session.url) throw new Error('Stripe returned a Checkout Session without a url');
|
|
69
|
-
return session.url;
|
|
70
|
-
}
|
package/shipping-rules.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ShippingRules } from './config';
|
|
2
|
-
|
|
3
|
-
/** The customer's postage charge, or null when the order can't be shipped to that zone. */
|
|
4
|
-
export function applyShippingRules(
|
|
5
|
-
rules: ShippingRules,
|
|
6
|
-
input: { basePence: number; subtotalPence: number; zone: string }
|
|
7
|
-
): null | number {
|
|
8
|
-
if (input.zone === 'uk') {
|
|
9
|
-
return input.subtotalPence >= rules.freeUkFromPence ? 0 : input.basePence;
|
|
10
|
-
}
|
|
11
|
-
if (input.subtotalPence >= rules.internationalMaxOrderPence) return null;
|
|
12
|
-
return input.subtotalPence >= rules.insuranceFromPence
|
|
13
|
-
? input.basePence + rules.insuranceUpliftPence
|
|
14
|
-
: input.basePence;
|
|
15
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|