qarpeo 0.2.1 → 0.3.0
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/dist/chunks/tools.mjs +993 -31
- package/dist/cli/index.mjs +1071 -31
- package/dist/fate/index.d.ts +9 -1
- package/dist/fate/index.mjs +20 -3
- package/dist/module.mjs +292 -748
- package/dist/runtime/browser.mjs +2 -2
- package/dist/runtime/comments-server.d.ts +3 -0
- package/dist/runtime/comments-server.mjs +2 -0
- package/dist/runtime/comments.d.ts +0 -2
- package/dist/runtime/comments.mjs +0 -2
- package/dist/runtime/db.d.ts +10 -9
- package/dist/runtime/db.mjs +79 -8
- package/dist/runtime/drizzle.mjs +21 -6
- package/dist/runtime/index.d.ts +4 -0
- package/dist/runtime/realtime-server.d.ts +4 -0
- package/dist/runtime/realtime-server.mjs +13 -0
- package/dist/runtime/realtime.d.ts +1 -4
- package/dist/runtime/realtime.mjs +0 -13
- package/dist/runtime/replica.d.ts +27 -0
- package/dist/runtime/replica.mjs +229 -0
- package/dist/runtime/server/durable-objects/agent.mjs +14 -2
- package/dist/runtime/server/durable-objects/room.d.ts +10 -0
- package/dist/runtime/server/durable-objects/room.mjs +119 -28
- package/dist/runtime/server/middleware/devtools.mjs +1 -1
- package/dist/runtime/server/middleware/tenancy.mjs +7 -4
- package/dist/runtime/server/plugins/store.mjs +1 -1
- package/dist/runtime/server/routes/__qarpeo/mail/track/click/[id].get.mjs +57 -17
- package/dist/runtime/server/routes/agents/[agent]/chat.post.mjs +19 -12
- package/dist/runtime/server/routes/agents/[agent]/clear.post.mjs +8 -9
- package/dist/runtime/server/routes/agents/[agent]/history.get.mjs +8 -9
- package/dist/runtime/server/routes/analytics/events.get.mjs +12 -2
- package/dist/runtime/server/routes/analytics/pageviews.get.mjs +10 -1
- package/dist/runtime/server/routes/analytics/summary.get.mjs +10 -1
- package/dist/runtime/server/routes/auth/2fa/disable.post.mjs +11 -2
- package/dist/runtime/server/routes/auth/2fa/setup.post.mjs +7 -0
- package/dist/runtime/server/routes/auth/2fa/verify.post.mjs +11 -2
- package/dist/runtime/server/routes/auth/account.delete.mjs +7 -0
- package/dist/runtime/server/routes/auth/magic-link/verify.get.mjs +5 -3
- package/dist/runtime/server/routes/auth/organization/sso/index.post.mjs +10 -0
- package/dist/runtime/server/routes/auth/otp/verify.post.mjs +5 -3
- package/dist/runtime/server/routes/auth/sso/saml/acs.post.mjs +18 -0
- package/dist/runtime/server/routes/auth/webauthn/login/options.post.d.ts +1 -1
- package/dist/runtime/server/routes/auth/webauthn/login/options.post.mjs +11 -2
- package/dist/runtime/server/routes/auth/webauthn/login/verify.post.mjs +10 -1
- package/dist/runtime/server/routes/auth/webauthn/register/options.post.d.ts +1 -1
- package/dist/runtime/server/routes/auth/webauthn/register/options.post.mjs +10 -1
- package/dist/runtime/server/routes/auth/webauthn/register/verify.post.mjs +10 -1
- package/dist/runtime/server/routes/browser/crawl.post.mjs +13 -0
- package/dist/runtime/server/routes/browser/invoice.post.mjs +15 -1
- package/dist/runtime/server/routes/browser/og-image.post.mjs +13 -0
- package/dist/runtime/server/routes/browser/pdf.post.mjs +15 -1
- package/dist/runtime/server/routes/browser/screenshot.post.mjs +13 -0
- package/dist/runtime/server/routes/cms/[collection]/[id]/preview-token.post.mjs +23 -8
- package/dist/runtime/server/routes/cms/[collection]/[id]/publish.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id]/review.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id]/revisions/[revId]/restore.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/[id].put.mjs +2 -1
- package/dist/runtime/server/routes/cms/[collection]/index.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/categories/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/categories/[id].put.mjs +2 -1
- package/dist/runtime/server/routes/cms/categories.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/collections/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/collections/[id].put.mjs +2 -1
- package/dist/runtime/server/routes/cms/collections/index.post.mjs +2 -1
- package/dist/runtime/server/routes/cms/export.post.mjs +1 -1
- package/dist/runtime/server/routes/cms/subscribers/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/subscribers/[id].patch.mjs +2 -1
- package/dist/runtime/server/routes/cms/tags/[id].delete.mjs +2 -1
- package/dist/runtime/server/routes/cms/tags.post.mjs +2 -1
- package/dist/runtime/server/routes/comments/[id]/react.post.mjs +7 -3
- package/dist/runtime/server/routes/comments/[id]/report.post.mjs +3 -0
- package/dist/runtime/server/routes/comments/[id].delete.mjs +1 -1
- package/dist/runtime/server/routes/comments/[id].get.mjs +13 -0
- package/dist/runtime/server/routes/comments/[id].patch.mjs +1 -1
- package/dist/runtime/server/routes/fate.d.ts +6 -0
- package/dist/runtime/server/routes/fate.mjs +32 -3
- package/dist/runtime/server/routes/mail/bounce.post.mjs +14 -2
- package/dist/runtime/server/routes/mail/unsubscribe.mjs +2 -1
- package/dist/runtime/server/routes/mail/webhooks.post.mjs +18 -4
- package/dist/runtime/server/routes/notifications/[id].get.mjs +3 -4
- package/dist/runtime/server/routes/notifications/index.get.mjs +4 -2
- package/dist/runtime/server/routes/notifications/push/subscribe.post.mjs +3 -1
- package/dist/runtime/server/routes/notifications/push/unsubscribe.post.mjs +5 -3
- package/dist/runtime/server/routes/notifications/read-all.post.mjs +6 -2
- package/dist/runtime/server/routes/notifications/send.post.mjs +13 -2
- package/dist/runtime/server/routes/payments/cancel.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/change-plan.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/checkout.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/pause.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/portal.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/subscription.get.mjs +1 -1
- package/dist/runtime/server/routes/payments/usage.get.mjs +1 -1
- package/dist/runtime/server/routes/payments/usage.post.mjs +1 -1
- package/dist/runtime/server/routes/payments/webhook.post.mjs +8 -1
- package/dist/runtime/server/routes/realtime/broadcast.post.mjs +3 -2
- package/dist/runtime/server/routes/realtime/history.delete.mjs +2 -2
- package/dist/runtime/server/routes/realtime/history.get.mjs +2 -2
- package/dist/runtime/server/routes/realtime/presence.get.mjs +3 -3
- package/dist/runtime/server/routes/realtime/state.get.mjs +3 -3
- package/dist/runtime/server/routes/realtime/state.post.mjs +13 -4
- package/dist/runtime/server/routes/realtime/ws.get.mjs +2 -2
- package/dist/runtime/server/routes/storage/file/[...key].delete.mjs +1 -1
- package/dist/runtime/server/routes/storage/file/[...key].get.mjs +7 -0
- package/dist/runtime/server/routes/storage/files.get.d.ts +1 -1
- package/dist/runtime/server/routes/storage/files.get.mjs +24 -4
- package/dist/runtime/server/routes/storage/multipart/abort.post.mjs +16 -1
- package/dist/runtime/server/routes/storage/multipart/complete.post.d.ts +1 -1
- package/dist/runtime/server/routes/storage/multipart/complete.post.mjs +15 -1
- package/dist/runtime/server/routes/storage/multipart/create.post.mjs +7 -0
- package/dist/runtime/server/routes/storage/multipart/part.put.mjs +15 -1
- package/dist/runtime/server/routes/storage/presigned-r2.post.mjs +8 -1
- package/dist/runtime/server/routes/storage/presigned.post.d.ts +1 -1
- package/dist/runtime/server/routes/storage/presigned.post.mjs +8 -1
- package/dist/runtime/server/routes/storage/sign.post.mjs +7 -0
- package/dist/runtime/server/routes/storage/upload.post.d.ts +2 -2
- package/dist/runtime/server/routes/store/cart/index.delete.mjs +3 -4
- package/dist/runtime/server/routes/store/cart/index.get.mjs +3 -10
- package/dist/runtime/server/routes/store/cart/items/[id].delete.mjs +6 -4
- package/dist/runtime/server/routes/store/cart/items/[id].patch.mjs +12 -4
- package/dist/runtime/server/routes/store/cart/items.post.mjs +9 -10
- package/dist/runtime/server/routes/store/categories/index.get.mjs +4 -2
- package/dist/runtime/server/routes/store/checkout.post.mjs +13 -3
- package/dist/runtime/server/routes/store/coupons/apply.post.mjs +6 -5
- package/dist/runtime/server/routes/store/orders/[id].get.mjs +2 -2
- package/dist/runtime/server/routes/store/orders/download/[token].get.mjs +6 -3
- package/dist/runtime/server/routes/store/orders/index.get.mjs +1 -1
- package/dist/runtime/server/routes/store/products/[slug].get.mjs +2 -2
- package/dist/runtime/server/routes/store/products/index.get.mjs +1 -1
- package/dist/runtime/server/routes/store/tax/calculate.post.mjs +10 -0
- package/dist/runtime/server/routes/tenants/[tenant]/domains/[domain]/index.delete.mjs +26 -25
- package/dist/runtime/server/routes/tenants/[tenant]/domains/[domain]/verify.post.mjs +26 -25
- package/dist/runtime/server/utils/ai.d.ts +6 -0
- package/dist/runtime/server/utils/ai.mjs +12 -2
- package/dist/runtime/server/utils/analytics-query.mjs +8 -4
- package/dist/runtime/server/utils/browser-utils.d.ts +9 -0
- package/dist/runtime/server/utils/browser-utils.mjs +135 -0
- package/dist/runtime/server/utils/cms/cache.d.ts +13 -0
- package/dist/runtime/server/utils/cms/cache.mjs +95 -0
- package/dist/runtime/server/utils/cms/common.mjs +1 -1
- package/dist/runtime/server/utils/cms/markdown.mjs +38 -8
- package/dist/runtime/server/utils/cms/mutations.d.ts +26 -0
- package/dist/runtime/server/utils/cms/mutations.mjs +569 -0
- package/dist/runtime/server/utils/cms/path.d.ts +8 -0
- package/dist/runtime/server/utils/cms/path.mjs +23 -0
- package/dist/runtime/server/utils/cms/queries.d.ts +27 -0
- package/dist/runtime/server/utils/cms/queries.mjs +366 -0
- package/dist/runtime/server/utils/cms/references.d.ts +48 -0
- package/dist/runtime/server/utils/cms/references.mjs +309 -0
- package/dist/runtime/server/utils/cms/revisions.d.ts +7 -0
- package/dist/runtime/server/utils/cms/revisions.mjs +60 -0
- package/dist/runtime/server/utils/cms/sync.mjs +3 -3
- package/dist/runtime/server/utils/cms.d.ts +5 -82
- package/dist/runtime/server/utils/cms.mjs +28 -1367
- package/dist/runtime/server/utils/comments.d.ts +11 -0
- package/dist/runtime/server/utils/comments.mjs +65 -17
- package/dist/runtime/server/utils/crypto.mjs +1 -1
- package/dist/runtime/server/utils/domains.mjs +9 -0
- package/dist/runtime/server/utils/drizzle.d.ts +26 -1
- package/dist/runtime/server/utils/drizzle.mjs +78 -19
- package/dist/runtime/server/utils/email-inbound.mjs +9 -1
- package/dist/runtime/server/utils/impersonation.mjs +58 -1
- package/dist/runtime/server/utils/lock.d.ts +11 -0
- package/dist/runtime/server/utils/lock.mjs +4 -0
- package/dist/runtime/server/utils/mail-suppression.mjs +48 -13
- package/dist/runtime/server/utils/mail.d.ts +2 -1
- package/dist/runtime/server/utils/mail.mjs +17 -11
- package/dist/runtime/server/utils/notifications.d.ts +13 -4
- package/dist/runtime/server/utils/notifications.mjs +20 -4
- package/dist/runtime/server/utils/organization.mjs +36 -1
- package/dist/runtime/server/utils/passwordless.mjs +8 -0
- package/dist/runtime/server/utils/payments/checkout.d.ts +34 -0
- package/dist/runtime/server/utils/payments/checkout.mjs +158 -0
- package/dist/runtime/server/utils/payments/common.d.ts +45 -0
- package/dist/runtime/server/utils/payments/common.mjs +234 -0
- package/dist/runtime/server/utils/payments/index.d.ts +9 -0
- package/dist/runtime/server/utils/payments/index.mjs +21 -0
- package/dist/runtime/server/utils/payments/lemonsqueezy.d.ts +7 -0
- package/dist/runtime/server/utils/payments/lemonsqueezy.mjs +55 -0
- package/dist/runtime/server/utils/payments/paddle.d.ts +7 -0
- package/dist/runtime/server/utils/payments/paddle.mjs +92 -0
- package/dist/runtime/server/utils/payments/polar.d.ts +11 -0
- package/dist/runtime/server/utils/payments/polar.mjs +66 -0
- package/dist/runtime/server/utils/payments/stripe.d.ts +7 -0
- package/dist/runtime/server/utils/payments/stripe.mjs +159 -0
- package/dist/runtime/server/utils/payments/subscriptions.d.ts +89 -0
- package/dist/runtime/server/utils/payments/subscriptions.mjs +236 -0
- package/dist/runtime/server/utils/payments/usage.d.ts +12 -0
- package/dist/runtime/server/utils/payments/usage.mjs +143 -0
- package/dist/runtime/server/utils/payments/webhooks.d.ts +10 -0
- package/dist/runtime/server/utils/payments/webhooks.mjs +200 -0
- package/dist/runtime/server/utils/payments.d.ts +1 -158
- package/dist/runtime/server/utils/payments.mjs +1 -1289
- package/dist/runtime/server/utils/queue.d.ts +2 -0
- package/dist/runtime/server/utils/queue.mjs +34 -21
- package/dist/runtime/server/utils/rag.mjs +30 -17
- package/dist/runtime/server/utils/realtime.d.ts +5 -1
- package/dist/runtime/server/utils/realtime.mjs +43 -7
- package/dist/runtime/server/utils/session.mjs +16 -3
- package/dist/runtime/server/utils/sso.d.ts +3 -0
- package/dist/runtime/server/utils/sso.mjs +43 -0
- package/dist/runtime/server/utils/storage.mjs +12 -1
- package/dist/runtime/server/utils/store/cart.d.ts +63 -0
- package/dist/runtime/server/utils/store/cart.mjs +542 -0
- package/dist/runtime/server/utils/store/checkout.d.ts +14 -0
- package/dist/runtime/server/utils/store/checkout.mjs +118 -0
- package/dist/runtime/server/utils/store/common.d.ts +29 -0
- package/dist/runtime/server/utils/store/common.mjs +479 -0
- package/dist/runtime/server/utils/store/index.d.ts +7 -0
- package/dist/runtime/server/utils/store/index.mjs +30 -0
- package/dist/runtime/server/utils/store/inventory.d.ts +25 -0
- package/dist/runtime/server/utils/store/inventory.mjs +190 -0
- package/dist/runtime/server/utils/store/orders.d.ts +60 -0
- package/dist/runtime/server/utils/store/orders.mjs +590 -0
- package/dist/runtime/server/utils/store/products.d.ts +35 -0
- package/dist/runtime/server/utils/store/products.mjs +461 -0
- package/dist/runtime/server/utils/store/vendor.d.ts +18 -0
- package/dist/runtime/server/utils/store/vendor.mjs +68 -0
- package/dist/runtime/server/utils/store.d.ts +1 -207
- package/dist/runtime/server/utils/store.mjs +1 -2227
- package/dist/runtime/server/utils/string.d.ts +1 -1
- package/dist/runtime/server/utils/string.mjs +12 -4
- package/dist/runtime/server/utils/tenant.mjs +19 -5
- package/dist/runtime/server/utils/tracker.mjs +2 -0
- package/dist/runtime/server.d.ts +3 -3
- package/dist/runtime/server.mjs +4 -4
- package/dist/runtime/storage-server.d.ts +2 -0
- package/dist/runtime/storage-server.mjs +1 -0
- package/dist/runtime/storage.d.ts +1 -1
- package/dist/runtime/storage.mjs +0 -1
- package/dist/runtime/store.d.ts +0 -1
- package/dist/runtime/store.mjs +0 -1
- package/dist/runtime/test-utils.d.ts +4 -0
- package/dist/runtime/test-utils.mjs +54 -0
- package/dist/runtime/types/d1.d.ts +43 -0
- package/dist/runtime/types/drizzle.d.ts +6 -0
- package/dist/runtime/types/store.d.ts +1 -1
- package/dist/types/analytics.d.ts +80 -0
- package/dist/types/auth.d.ts +282 -0
- package/dist/types/cache.d.ts +55 -0
- package/dist/types/comments.d.ts +130 -0
- package/dist/types/db.d.ts +29 -0
- package/dist/types/email.d.ts +182 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/jobs.d.ts +58 -0
- package/dist/types/notifications.d.ts +77 -0
- package/dist/types/options.d.ts +363 -0
- package/dist/types/payments.d.ts +145 -0
- package/dist/types/ratelimit.d.ts +71 -0
- package/dist/types/realtime.d.ts +136 -0
- package/dist/types/site.d.ts +121 -0
- package/dist/types/storage.d.ts +164 -0
- package/dist/types/store-options.d.ts +199 -0
- package/dist/types/tenant.d.ts +100 -0
- package/dist/types.d.ts +1 -2174
- package/dist/utils/migration-lock.d.ts +139 -0
- package/dist/utils/pkg.d.ts +14 -1
- package/dist/utils/schema-diff.d.ts +39 -0
- package/package.json +55 -17
- package/dist/chunks/auth-schema.mjs +0 -364
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
import { nanoid } from "../crypto.mjs";
|
|
2
|
+
import {
|
|
3
|
+
getD1,
|
|
4
|
+
slugify,
|
|
5
|
+
getStoreConfig,
|
|
6
|
+
storeProductTbl,
|
|
7
|
+
storeVariantTbl,
|
|
8
|
+
storeInventoryTbl,
|
|
9
|
+
storeCategoryTbl,
|
|
10
|
+
rowToProduct,
|
|
11
|
+
rowToVariant,
|
|
12
|
+
rowToInventory,
|
|
13
|
+
rowToCategory
|
|
14
|
+
} from "./common.mjs";
|
|
15
|
+
import { getStoreInventory } from "./inventory.mjs";
|
|
16
|
+
export async function queryStoreProducts(opts = {}, event) {
|
|
17
|
+
const db = getD1(event);
|
|
18
|
+
const p = storeProductTbl(event);
|
|
19
|
+
const v = storeVariantTbl(event);
|
|
20
|
+
const inv = storeInventoryTbl(event);
|
|
21
|
+
const whereClauses = [];
|
|
22
|
+
const params = [];
|
|
23
|
+
if (opts.tenantId !== void 0) {
|
|
24
|
+
whereClauses.push("tenant_id = ?");
|
|
25
|
+
params.push(opts.tenantId);
|
|
26
|
+
}
|
|
27
|
+
if (opts.categoryId) {
|
|
28
|
+
whereClauses.push("category_id = ?");
|
|
29
|
+
params.push(opts.categoryId);
|
|
30
|
+
}
|
|
31
|
+
if (opts.status) {
|
|
32
|
+
whereClauses.push("status = ?");
|
|
33
|
+
params.push(opts.status);
|
|
34
|
+
} else {
|
|
35
|
+
whereClauses.push("status = 'active'");
|
|
36
|
+
}
|
|
37
|
+
if (opts.type) {
|
|
38
|
+
whereClauses.push("type = ?");
|
|
39
|
+
params.push(opts.type);
|
|
40
|
+
}
|
|
41
|
+
if (opts.search) {
|
|
42
|
+
whereClauses.push("(title LIKE ? OR description LIKE ?)");
|
|
43
|
+
const searchPattern = `%${opts.search}%`;
|
|
44
|
+
params.push(searchPattern, searchPattern);
|
|
45
|
+
}
|
|
46
|
+
if (opts.minPrice !== void 0) {
|
|
47
|
+
whereClauses.push("base_price >= ?");
|
|
48
|
+
params.push(opts.minPrice);
|
|
49
|
+
}
|
|
50
|
+
if (opts.maxPrice !== void 0) {
|
|
51
|
+
whereClauses.push("base_price <= ?");
|
|
52
|
+
params.push(opts.maxPrice);
|
|
53
|
+
}
|
|
54
|
+
if (opts.inStockOnly) {
|
|
55
|
+
whereClauses.push(
|
|
56
|
+
`(type = 'digital' OR id IN (SELECT product_id FROM ${inv} WHERE (stock_quantity - reserved_quantity) > 0 OR allow_backorder = 1))`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
if (opts.vendorId !== void 0) {
|
|
60
|
+
whereClauses.push("vendor_id = ?");
|
|
61
|
+
params.push(opts.vendorId);
|
|
62
|
+
}
|
|
63
|
+
const whereSql = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
|
|
64
|
+
let orderSql = "ORDER BY created_at DESC";
|
|
65
|
+
if (opts.sortBy === "price_asc") orderSql = "ORDER BY base_price ASC";
|
|
66
|
+
else if (opts.sortBy === "price_desc") orderSql = "ORDER BY base_price DESC";
|
|
67
|
+
else if (opts.sortBy === "title") orderSql = "ORDER BY title ASC";
|
|
68
|
+
else if (opts.sortBy === "newest") orderSql = "ORDER BY created_at DESC";
|
|
69
|
+
const limit = Math.min(opts.limit ?? 20, 100);
|
|
70
|
+
const offset = opts.offset ?? 0;
|
|
71
|
+
const countResult = await db.prepare(`SELECT COUNT(*) as cnt FROM ${p} ${whereSql}`).bind(...params).first();
|
|
72
|
+
const total = countResult?.cnt ?? 0;
|
|
73
|
+
if (total === 0) {
|
|
74
|
+
return { products: [], total: 0 };
|
|
75
|
+
}
|
|
76
|
+
const rows = await db.prepare(`SELECT * FROM ${p} ${whereSql} ${orderSql} LIMIT ? OFFSET ?`).bind(...params, limit, offset).all();
|
|
77
|
+
const products = (rows.results ?? []).map(rowToProduct);
|
|
78
|
+
if (products.length > 0) {
|
|
79
|
+
const productIds = products.map((prod) => prod.id);
|
|
80
|
+
const placeholders = productIds.map(() => "?").join(", ");
|
|
81
|
+
const variantRows = await db.prepare(`SELECT * FROM ${v} WHERE product_id IN (${placeholders}) ORDER BY created_at ASC`).bind(...productIds).all().catch(() => ({ results: [] }));
|
|
82
|
+
const variants = (variantRows.results ?? []).map(rowToVariant);
|
|
83
|
+
if (variants.length > 0) {
|
|
84
|
+
const variantIds = variants.map((vItem) => vItem.id);
|
|
85
|
+
const vPlaceholders = variantIds.map(() => "?").join(", ");
|
|
86
|
+
const invRows = await db.prepare(`SELECT * FROM ${inv} WHERE variant_id IN (${vPlaceholders})`).bind(...variantIds).all().catch(() => ({ results: [] }));
|
|
87
|
+
const invMap = /* @__PURE__ */ new Map();
|
|
88
|
+
for (const row of invRows.results ?? []) {
|
|
89
|
+
invMap.set(row.variant_id, rowToInventory(row));
|
|
90
|
+
}
|
|
91
|
+
for (const variant of variants) {
|
|
92
|
+
variant.inventory = invMap.get(variant.id);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const variantsByProduct = /* @__PURE__ */ new Map();
|
|
96
|
+
for (const variant of variants) {
|
|
97
|
+
if (!variantsByProduct.has(variant.productId)) {
|
|
98
|
+
variantsByProduct.set(variant.productId, []);
|
|
99
|
+
}
|
|
100
|
+
variantsByProduct.get(variant.productId).push(variant);
|
|
101
|
+
}
|
|
102
|
+
for (const prod of products) {
|
|
103
|
+
prod.variants = variantsByProduct.get(prod.id) ?? [];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return { products, total };
|
|
107
|
+
}
|
|
108
|
+
export async function getStoreProduct(idOrSlug, event, opts = {}) {
|
|
109
|
+
const db = getD1(event);
|
|
110
|
+
const p = storeProductTbl(event);
|
|
111
|
+
const v = storeVariantTbl(event);
|
|
112
|
+
const inv = storeInventoryTbl(event);
|
|
113
|
+
let sql = `SELECT * FROM ${p} WHERE (id = ? OR slug = ?)`;
|
|
114
|
+
const params = [idOrSlug, idOrSlug];
|
|
115
|
+
if (opts.tenantId !== void 0) {
|
|
116
|
+
sql += " AND tenant_id = ?";
|
|
117
|
+
params.push(opts.tenantId);
|
|
118
|
+
}
|
|
119
|
+
sql += " LIMIT 1";
|
|
120
|
+
const row = await db.prepare(sql).bind(...params).first();
|
|
121
|
+
if (!row) return null;
|
|
122
|
+
const product = rowToProduct(row);
|
|
123
|
+
const variantRows = await db.prepare(`SELECT * FROM ${v} WHERE product_id = ? ORDER BY created_at ASC`).bind(product.id).all();
|
|
124
|
+
const variants = (variantRows.results ?? []).map(rowToVariant);
|
|
125
|
+
if (variants.length > 0) {
|
|
126
|
+
const variantIds = variants.map((vItem) => vItem.id);
|
|
127
|
+
const vPlaceholders = variantIds.map(() => "?").join(", ");
|
|
128
|
+
const invRows = await db.prepare(`SELECT * FROM ${inv} WHERE variant_id IN (${vPlaceholders})`).bind(...variantIds).all();
|
|
129
|
+
const invMap = /* @__PURE__ */ new Map();
|
|
130
|
+
for (const iRow of invRows.results ?? []) {
|
|
131
|
+
invMap.set(iRow.variant_id, rowToInventory(iRow));
|
|
132
|
+
}
|
|
133
|
+
for (const vItem of variants) {
|
|
134
|
+
vItem.inventory = invMap.get(vItem.id);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
product.variants = variants;
|
|
138
|
+
return product;
|
|
139
|
+
}
|
|
140
|
+
export async function createStoreProduct(data, event) {
|
|
141
|
+
const db = getD1(event);
|
|
142
|
+
const p = storeProductTbl(event);
|
|
143
|
+
const v = storeVariantTbl(event);
|
|
144
|
+
const inv = storeInventoryTbl(event);
|
|
145
|
+
const id = nanoid();
|
|
146
|
+
const slug = data.slug ? slugify(data.slug) : slugify(data.title) || nanoid(8);
|
|
147
|
+
const now = Date.now();
|
|
148
|
+
const currency = data.currency || getStoreConfig(event).currency || "USD";
|
|
149
|
+
const status = data.status || "draft";
|
|
150
|
+
const type = data.type || "physical";
|
|
151
|
+
const images = data.images || [];
|
|
152
|
+
const metadata = data.metadata || {};
|
|
153
|
+
await db.prepare(
|
|
154
|
+
`INSERT INTO ${p} (
|
|
155
|
+
id, tenant_id, vendor_id, slug, title, description, status, type,
|
|
156
|
+
base_price, currency, category_id, images_json, metadata_json,
|
|
157
|
+
created_at, updated_at
|
|
158
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
159
|
+
).bind(
|
|
160
|
+
id,
|
|
161
|
+
data.tenantId ?? null,
|
|
162
|
+
data.vendorId ?? null,
|
|
163
|
+
slug,
|
|
164
|
+
data.title,
|
|
165
|
+
data.description ?? null,
|
|
166
|
+
status,
|
|
167
|
+
type,
|
|
168
|
+
data.basePrice,
|
|
169
|
+
currency,
|
|
170
|
+
data.categoryId ?? null,
|
|
171
|
+
JSON.stringify(images),
|
|
172
|
+
JSON.stringify(metadata),
|
|
173
|
+
now,
|
|
174
|
+
now
|
|
175
|
+
).run();
|
|
176
|
+
const variantId = nanoid();
|
|
177
|
+
const sku = `${slug.toUpperCase().slice(0, 8)}-DEF`;
|
|
178
|
+
await db.prepare(
|
|
179
|
+
`INSERT INTO ${v} (
|
|
180
|
+
id, product_id, sku, title, options_json, price_override,
|
|
181
|
+
weight, barcode, image, created_at, updated_at
|
|
182
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
183
|
+
).bind(variantId, id, sku, "Default", "{}", null, null, null, null, now, now).run();
|
|
184
|
+
const inventoryId = nanoid();
|
|
185
|
+
await db.prepare(
|
|
186
|
+
`INSERT INTO ${inv} (
|
|
187
|
+
id, variant_id, product_id, stock_quantity, reserved_quantity,
|
|
188
|
+
allow_backorder, low_stock_threshold, created_at, updated_at
|
|
189
|
+
) VALUES (?, ?, ?, 0, 0, 0, 5, ?, ?)`
|
|
190
|
+
).bind(inventoryId, variantId, id, now, now).run();
|
|
191
|
+
return await getStoreProduct(id, event);
|
|
192
|
+
}
|
|
193
|
+
export async function updateStoreProduct(id, data, event) {
|
|
194
|
+
const db = getD1(event);
|
|
195
|
+
const p = storeProductTbl(event);
|
|
196
|
+
const updates = [];
|
|
197
|
+
const params = [];
|
|
198
|
+
if (data.title !== void 0) {
|
|
199
|
+
updates.push("title = ?");
|
|
200
|
+
params.push(data.title);
|
|
201
|
+
}
|
|
202
|
+
if (data.slug !== void 0) {
|
|
203
|
+
updates.push("slug = ?");
|
|
204
|
+
params.push(slugify(data.slug));
|
|
205
|
+
}
|
|
206
|
+
if (data.description !== void 0) {
|
|
207
|
+
updates.push("description = ?");
|
|
208
|
+
params.push(data.description);
|
|
209
|
+
}
|
|
210
|
+
if (data.status !== void 0) {
|
|
211
|
+
updates.push("status = ?");
|
|
212
|
+
params.push(data.status);
|
|
213
|
+
}
|
|
214
|
+
if (data.type !== void 0) {
|
|
215
|
+
updates.push("type = ?");
|
|
216
|
+
params.push(data.type);
|
|
217
|
+
}
|
|
218
|
+
if (data.basePrice !== void 0) {
|
|
219
|
+
updates.push("base_price = ?");
|
|
220
|
+
params.push(data.basePrice);
|
|
221
|
+
}
|
|
222
|
+
if (data.currency !== void 0) {
|
|
223
|
+
updates.push("currency = ?");
|
|
224
|
+
params.push(data.currency);
|
|
225
|
+
}
|
|
226
|
+
if (data.categoryId !== void 0) {
|
|
227
|
+
updates.push("category_id = ?");
|
|
228
|
+
params.push(data.categoryId);
|
|
229
|
+
}
|
|
230
|
+
if (data.images !== void 0) {
|
|
231
|
+
updates.push("images_json = ?");
|
|
232
|
+
params.push(JSON.stringify(data.images));
|
|
233
|
+
}
|
|
234
|
+
if (data.metadata !== void 0) {
|
|
235
|
+
updates.push("metadata_json = ?");
|
|
236
|
+
params.push(JSON.stringify(data.metadata));
|
|
237
|
+
}
|
|
238
|
+
if (data.vendorId !== void 0) {
|
|
239
|
+
updates.push("vendor_id = ?");
|
|
240
|
+
params.push(data.vendorId);
|
|
241
|
+
}
|
|
242
|
+
if (updates.length === 0) return getStoreProduct(id, event);
|
|
243
|
+
const now = Date.now();
|
|
244
|
+
updates.push("updated_at = ?");
|
|
245
|
+
params.push(now);
|
|
246
|
+
params.push(id);
|
|
247
|
+
await db.prepare(`UPDATE ${p} SET ${updates.join(", ")} WHERE id = ?`).bind(...params).run();
|
|
248
|
+
return getStoreProduct(id, event);
|
|
249
|
+
}
|
|
250
|
+
export async function deleteStoreProduct(id, event) {
|
|
251
|
+
const db = getD1(event);
|
|
252
|
+
const p = storeProductTbl(event);
|
|
253
|
+
const v = storeVariantTbl(event);
|
|
254
|
+
const inv = storeInventoryTbl(event);
|
|
255
|
+
await db.batch([
|
|
256
|
+
db.prepare(`DELETE FROM ${inv} WHERE product_id = ?`).bind(id),
|
|
257
|
+
db.prepare(`DELETE FROM ${v} WHERE product_id = ?`).bind(id),
|
|
258
|
+
db.prepare(`DELETE FROM ${p} WHERE id = ?`).bind(id)
|
|
259
|
+
]);
|
|
260
|
+
return true;
|
|
261
|
+
}
|
|
262
|
+
export async function createStoreVariant(productId, data, event) {
|
|
263
|
+
const db = getD1(event);
|
|
264
|
+
const v = storeVariantTbl(event);
|
|
265
|
+
const inv = storeInventoryTbl(event);
|
|
266
|
+
const id = nanoid();
|
|
267
|
+
const now = Date.now();
|
|
268
|
+
await db.prepare(
|
|
269
|
+
`INSERT INTO ${v} (
|
|
270
|
+
id, product_id, sku, title, options_json, price_override,
|
|
271
|
+
weight, barcode, image, created_at, updated_at
|
|
272
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
273
|
+
).bind(
|
|
274
|
+
id,
|
|
275
|
+
productId,
|
|
276
|
+
data.sku,
|
|
277
|
+
data.title,
|
|
278
|
+
JSON.stringify(data.options || {}),
|
|
279
|
+
data.priceOverride ?? null,
|
|
280
|
+
data.weight ?? null,
|
|
281
|
+
data.barcode ?? null,
|
|
282
|
+
data.image ?? null,
|
|
283
|
+
now,
|
|
284
|
+
now
|
|
285
|
+
).run();
|
|
286
|
+
const inventoryId = nanoid();
|
|
287
|
+
await db.prepare(
|
|
288
|
+
`INSERT INTO ${inv} (
|
|
289
|
+
id, variant_id, product_id, stock_quantity, reserved_quantity,
|
|
290
|
+
allow_backorder, low_stock_threshold, created_at, updated_at
|
|
291
|
+
) VALUES (?, ?, ?, 0, 0, 0, 5, ?, ?)`
|
|
292
|
+
).bind(inventoryId, id, productId, now, now).run();
|
|
293
|
+
const row = await db.prepare(`SELECT * FROM ${v} WHERE id = ?`).bind(id).first();
|
|
294
|
+
const variant = rowToVariant(row);
|
|
295
|
+
variant.inventory = await getStoreInventory(id, event) ?? void 0;
|
|
296
|
+
return variant;
|
|
297
|
+
}
|
|
298
|
+
export async function updateStoreVariant(id, data, event) {
|
|
299
|
+
const db = getD1(event);
|
|
300
|
+
const v = storeVariantTbl(event);
|
|
301
|
+
const updates = [];
|
|
302
|
+
const params = [];
|
|
303
|
+
if (data.sku !== void 0) {
|
|
304
|
+
updates.push("sku = ?");
|
|
305
|
+
params.push(data.sku);
|
|
306
|
+
}
|
|
307
|
+
if (data.title !== void 0) {
|
|
308
|
+
updates.push("title = ?");
|
|
309
|
+
params.push(data.title);
|
|
310
|
+
}
|
|
311
|
+
if (data.options !== void 0) {
|
|
312
|
+
updates.push("options_json = ?");
|
|
313
|
+
params.push(JSON.stringify(data.options));
|
|
314
|
+
}
|
|
315
|
+
if (data.priceOverride !== void 0) {
|
|
316
|
+
updates.push("price_override = ?");
|
|
317
|
+
params.push(data.priceOverride);
|
|
318
|
+
}
|
|
319
|
+
if (data.weight !== void 0) {
|
|
320
|
+
updates.push("weight = ?");
|
|
321
|
+
params.push(data.weight);
|
|
322
|
+
}
|
|
323
|
+
if (data.barcode !== void 0) {
|
|
324
|
+
updates.push("barcode = ?");
|
|
325
|
+
params.push(data.barcode);
|
|
326
|
+
}
|
|
327
|
+
if (data.image !== void 0) {
|
|
328
|
+
updates.push("image = ?");
|
|
329
|
+
params.push(data.image);
|
|
330
|
+
}
|
|
331
|
+
if (updates.length > 0) {
|
|
332
|
+
const now = Date.now();
|
|
333
|
+
updates.push("updated_at = ?");
|
|
334
|
+
params.push(now);
|
|
335
|
+
params.push(id);
|
|
336
|
+
await db.prepare(`UPDATE ${v} SET ${updates.join(", ")} WHERE id = ?`).bind(...params).run();
|
|
337
|
+
}
|
|
338
|
+
const row = await db.prepare(`SELECT * FROM ${v} WHERE id = ?`).bind(id).first();
|
|
339
|
+
if (!row) return null;
|
|
340
|
+
const variant = rowToVariant(row);
|
|
341
|
+
variant.inventory = await getStoreInventory(id, event) ?? void 0;
|
|
342
|
+
return variant;
|
|
343
|
+
}
|
|
344
|
+
export async function deleteStoreVariant(id, event) {
|
|
345
|
+
const db = getD1(event);
|
|
346
|
+
const v = storeVariantTbl(event);
|
|
347
|
+
const inv = storeInventoryTbl(event);
|
|
348
|
+
await db.batch([
|
|
349
|
+
db.prepare(`DELETE FROM ${inv} WHERE variant_id = ?`).bind(id),
|
|
350
|
+
db.prepare(`DELETE FROM ${v} WHERE id = ?`).bind(id)
|
|
351
|
+
]);
|
|
352
|
+
return true;
|
|
353
|
+
}
|
|
354
|
+
export async function queryStoreCategories(opts = {}, event) {
|
|
355
|
+
const db = getD1(event);
|
|
356
|
+
const c = storeCategoryTbl(event);
|
|
357
|
+
const whereClauses = [];
|
|
358
|
+
const params = [];
|
|
359
|
+
if (opts.tenantId !== void 0) {
|
|
360
|
+
whereClauses.push("tenant_id = ?");
|
|
361
|
+
params.push(opts.tenantId);
|
|
362
|
+
}
|
|
363
|
+
if (opts.parentId !== void 0) {
|
|
364
|
+
if (opts.parentId === null) {
|
|
365
|
+
whereClauses.push("parent_id IS NULL");
|
|
366
|
+
} else {
|
|
367
|
+
whereClauses.push("parent_id = ?");
|
|
368
|
+
params.push(opts.parentId);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const whereSql = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
|
|
372
|
+
const rows = await db.prepare(`SELECT * FROM ${c} ${whereSql} ORDER BY order_num ASC, name ASC`).bind(...params).all();
|
|
373
|
+
return (rows.results ?? []).map(rowToCategory);
|
|
374
|
+
}
|
|
375
|
+
export async function getStoreCategory(idOrSlug, event, opts = {}) {
|
|
376
|
+
const db = getD1(event);
|
|
377
|
+
const c = storeCategoryTbl(event);
|
|
378
|
+
let sql = `SELECT * FROM ${c} WHERE (id = ? OR slug = ?)`;
|
|
379
|
+
const params = [idOrSlug, idOrSlug];
|
|
380
|
+
if (opts.tenantId !== void 0) {
|
|
381
|
+
sql += " AND tenant_id = ?";
|
|
382
|
+
params.push(opts.tenantId);
|
|
383
|
+
}
|
|
384
|
+
sql += " LIMIT 1";
|
|
385
|
+
const row = await db.prepare(sql).bind(...params).first();
|
|
386
|
+
return row ? rowToCategory(row) : null;
|
|
387
|
+
}
|
|
388
|
+
export async function createStoreCategory(data, event) {
|
|
389
|
+
const db = getD1(event);
|
|
390
|
+
const c = storeCategoryTbl(event);
|
|
391
|
+
const id = nanoid();
|
|
392
|
+
const slug = data.slug ? slugify(data.slug) : slugify(data.name) || nanoid(8);
|
|
393
|
+
const now = Date.now();
|
|
394
|
+
await db.prepare(
|
|
395
|
+
`INSERT INTO ${c} (
|
|
396
|
+
id, tenant_id, slug, name, description, image, parent_id,
|
|
397
|
+
order_num, metadata_json, created_at, updated_at
|
|
398
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
|
399
|
+
).bind(
|
|
400
|
+
id,
|
|
401
|
+
data.tenantId ?? null,
|
|
402
|
+
slug,
|
|
403
|
+
data.name,
|
|
404
|
+
data.description ?? null,
|
|
405
|
+
data.image ?? null,
|
|
406
|
+
data.parentId ?? null,
|
|
407
|
+
data.order ?? 0,
|
|
408
|
+
JSON.stringify(data.metadata || {}),
|
|
409
|
+
now,
|
|
410
|
+
now
|
|
411
|
+
).run();
|
|
412
|
+
return await getStoreCategory(id, event);
|
|
413
|
+
}
|
|
414
|
+
export async function updateStoreCategory(id, data, event) {
|
|
415
|
+
const db = getD1(event);
|
|
416
|
+
const c = storeCategoryTbl(event);
|
|
417
|
+
const updates = [];
|
|
418
|
+
const params = [];
|
|
419
|
+
if (data.name !== void 0) {
|
|
420
|
+
updates.push("name = ?");
|
|
421
|
+
params.push(data.name);
|
|
422
|
+
}
|
|
423
|
+
if (data.slug !== void 0) {
|
|
424
|
+
updates.push("slug = ?");
|
|
425
|
+
params.push(slugify(data.slug));
|
|
426
|
+
}
|
|
427
|
+
if (data.description !== void 0) {
|
|
428
|
+
updates.push("description = ?");
|
|
429
|
+
params.push(data.description);
|
|
430
|
+
}
|
|
431
|
+
if (data.image !== void 0) {
|
|
432
|
+
updates.push("image = ?");
|
|
433
|
+
params.push(data.image);
|
|
434
|
+
}
|
|
435
|
+
if (data.parentId !== void 0) {
|
|
436
|
+
updates.push("parent_id = ?");
|
|
437
|
+
params.push(data.parentId);
|
|
438
|
+
}
|
|
439
|
+
if (data.order !== void 0) {
|
|
440
|
+
updates.push("order_num = ?");
|
|
441
|
+
params.push(data.order);
|
|
442
|
+
}
|
|
443
|
+
if (data.metadata !== void 0) {
|
|
444
|
+
updates.push("metadata_json = ?");
|
|
445
|
+
params.push(JSON.stringify(data.metadata));
|
|
446
|
+
}
|
|
447
|
+
if (updates.length > 0) {
|
|
448
|
+
const now = Date.now();
|
|
449
|
+
updates.push("updated_at = ?");
|
|
450
|
+
params.push(now);
|
|
451
|
+
params.push(id);
|
|
452
|
+
await db.prepare(`UPDATE ${c} SET ${updates.join(", ")} WHERE id = ?`).bind(...params).run();
|
|
453
|
+
}
|
|
454
|
+
return getStoreCategory(id, event);
|
|
455
|
+
}
|
|
456
|
+
export async function deleteStoreCategory(id, event) {
|
|
457
|
+
const db = getD1(event);
|
|
458
|
+
const c = storeCategoryTbl(event);
|
|
459
|
+
await db.prepare(`DELETE FROM ${c} WHERE id = ?`).bind(id).run();
|
|
460
|
+
return true;
|
|
461
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { RequestEvent } from "nuxt/server";
|
|
2
|
+
import type { StoreProduct, StoreOrderItem, StoreQueryProductsOptions, StoreVendorMetrics } from "../../../types/store";
|
|
3
|
+
export declare function isMultiVendorStore(event?: RequestEvent): boolean;
|
|
4
|
+
export declare function getStoreVendorMetrics(vendorId: string, opts?: {
|
|
5
|
+
tenantId?: string;
|
|
6
|
+
}, event?: RequestEvent): Promise<StoreVendorMetrics>;
|
|
7
|
+
export declare function queryVendorProducts(vendorId: string, opts?: Omit<StoreQueryProductsOptions, "vendorId">, event?: RequestEvent): Promise<{
|
|
8
|
+
products: StoreProduct[];
|
|
9
|
+
total: number;
|
|
10
|
+
}>;
|
|
11
|
+
export declare function queryVendorOrderItems(vendorId: string, opts?: {
|
|
12
|
+
tenantId?: string;
|
|
13
|
+
limit?: number;
|
|
14
|
+
offset?: number;
|
|
15
|
+
}, event?: RequestEvent): Promise<{
|
|
16
|
+
items: StoreOrderItem[];
|
|
17
|
+
total: number;
|
|
18
|
+
}>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getD1,
|
|
3
|
+
getStoreConfig,
|
|
4
|
+
storeProductTbl,
|
|
5
|
+
storeOrderTbl,
|
|
6
|
+
storeOrderItemTbl,
|
|
7
|
+
rowToOrderItem
|
|
8
|
+
} from "./common.mjs";
|
|
9
|
+
import { queryStoreProducts } from "./products.mjs";
|
|
10
|
+
export function isMultiVendorStore(event) {
|
|
11
|
+
return getStoreConfig(event)?.mode === "multi-vendor";
|
|
12
|
+
}
|
|
13
|
+
export async function getStoreVendorMetrics(vendorId, opts = {}, event) {
|
|
14
|
+
const db = getD1(event);
|
|
15
|
+
const ord = storeOrderTbl(event);
|
|
16
|
+
const ordItem = storeOrderItemTbl(event);
|
|
17
|
+
const p = storeProductTbl(event);
|
|
18
|
+
const cfg = getStoreConfig(event);
|
|
19
|
+
const commissionRate = cfg.multiVendor?.commissionRate ?? 0;
|
|
20
|
+
const sales = await db.prepare(
|
|
21
|
+
`SELECT
|
|
22
|
+
COALESCE(SUM(oi.total_price), 0) as gross_revenue,
|
|
23
|
+
COALESCE(SUM(oi.quantity), 0) as items_sold,
|
|
24
|
+
COUNT(DISTINCT oi.order_id) as order_count
|
|
25
|
+
FROM ${ordItem} oi
|
|
26
|
+
JOIN ${ord} o ON o.id = oi.order_id
|
|
27
|
+
WHERE oi.vendor_id = ? AND o.status = 'paid'`
|
|
28
|
+
).bind(vendorId).first();
|
|
29
|
+
const prodCount = await db.prepare(`SELECT COUNT(*) as cnt FROM ${p} WHERE vendor_id = ?`).bind(vendorId).first();
|
|
30
|
+
const grossRevenue = sales?.gross_revenue ?? 0;
|
|
31
|
+
const commissionTotal = Math.round(grossRevenue * commissionRate);
|
|
32
|
+
const netPayout = grossRevenue - commissionTotal;
|
|
33
|
+
return {
|
|
34
|
+
vendorId,
|
|
35
|
+
totalRevenue: grossRevenue,
|
|
36
|
+
netPayout,
|
|
37
|
+
commissionTotal,
|
|
38
|
+
orderCount: sales?.order_count ?? 0,
|
|
39
|
+
itemsSoldCount: sales?.items_sold ?? 0,
|
|
40
|
+
totalProducts: prodCount?.cnt ?? 0
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export async function queryVendorProducts(vendorId, opts = {}, event) {
|
|
44
|
+
return queryStoreProducts({ ...opts, vendorId }, event);
|
|
45
|
+
}
|
|
46
|
+
export async function queryVendorOrderItems(vendorId, opts = {}, event) {
|
|
47
|
+
const db = getD1(event);
|
|
48
|
+
const ordItem = storeOrderItemTbl(event);
|
|
49
|
+
const ord = storeOrderTbl(event);
|
|
50
|
+
const whereClauses = ["oi.vendor_id = ?"];
|
|
51
|
+
const params = [vendorId];
|
|
52
|
+
if (opts.tenantId !== void 0) {
|
|
53
|
+
whereClauses.push("o.tenant_id = ?");
|
|
54
|
+
params.push(opts.tenantId);
|
|
55
|
+
}
|
|
56
|
+
const whereSql = `WHERE ${whereClauses.join(" AND ")}`;
|
|
57
|
+
const limit = Math.min(opts.limit ?? 20, 100);
|
|
58
|
+
const offset = opts.offset ?? 0;
|
|
59
|
+
const countResult = await db.prepare(
|
|
60
|
+
`SELECT COUNT(*) as cnt FROM ${ordItem} oi JOIN ${ord} o ON o.id = oi.order_id ${whereSql}`
|
|
61
|
+
).bind(...params).first();
|
|
62
|
+
const total = countResult?.cnt ?? 0;
|
|
63
|
+
if (total === 0) return { items: [], total: 0 };
|
|
64
|
+
const rows = await db.prepare(
|
|
65
|
+
`SELECT oi.* FROM ${ordItem} oi JOIN ${ord} o ON o.id = oi.order_id ${whereSql} ORDER BY oi.created_at DESC LIMIT ? OFFSET ?`
|
|
66
|
+
).bind(...params, limit, offset).all();
|
|
67
|
+
return { items: (rows.results ?? []).map(rowToOrderItem), total };
|
|
68
|
+
}
|