@nextblock-cms/ecom 0.8.1 → 0.8.7
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/index.cjs.js +1 -1
- package/index.es.js +136 -3224
- package/lib/CurrencyProvider.cjs.js +1 -0
- package/lib/CurrencyProvider.es.js +99 -0
- package/lib/cart-store.cjs.js +1 -0
- package/lib/cart-store.es.js +116 -0
- package/lib/components/AccountNavigationMenu.cjs.js +1 -0
- package/lib/components/AccountNavigationMenu.es.js +44 -0
- package/lib/components/AddToCartButton.cjs.js +1 -0
- package/lib/components/AddToCartButton.es.js +60 -0
- package/lib/components/Cart.cjs.js +1 -0
- package/lib/components/Cart.es.js +148 -0
- package/lib/components/CartDrawer.cjs.js +1 -0
- package/lib/components/CartDrawer.es.js +119 -0
- package/lib/components/CartIcon.cjs.js +1 -0
- package/lib/components/CartIcon.es.js +34 -0
- package/lib/components/Checkout.cjs.js +1 -0
- package/lib/components/Checkout.es.js +987 -0
- package/lib/components/CouponForm.cjs.js +1 -0
- package/lib/components/CouponForm.es.js +136 -0
- package/lib/components/CurrencySwitcher.cjs.js +1 -0
- package/lib/components/CurrencySwitcher.es.js +18 -0
- package/lib/components/CustomerProfileForm.cjs.js +1 -0
- package/lib/components/CustomerProfileForm.es.js +355 -0
- package/lib/components/FeaturedProduct.cjs.js +1 -0
- package/lib/components/FeaturedProduct.es.js +72 -0
- package/lib/components/InvoiceDocument.cjs.js +1 -0
- package/lib/components/InvoiceDocument.es.js +196 -0
- package/lib/components/InvoiceViewerShell.cjs.js +29 -0
- package/lib/components/InvoiceViewerShell.es.js +100 -0
- package/lib/components/ProductCard.cjs.js +1 -0
- package/lib/components/ProductCard.es.js +119 -0
- package/lib/components/ProductDetailsLayout.cjs.js +1 -0
- package/lib/components/ProductDetailsLayout.es.js +361 -0
- package/lib/components/ProductGallery.cjs.js +1 -0
- package/lib/components/ProductGallery.es.js +41 -0
- package/lib/components/ProductGrid.cjs.js +1 -0
- package/lib/components/ProductGrid.es.js +19 -0
- package/lib/components/ShippingEstimator.cjs.js +1 -0
- package/lib/components/ShippingEstimator.es.js +125 -0
- package/lib/components/SimpleTiptapRenderer.cjs.js +1 -0
- package/lib/components/SimpleTiptapRenderer.es.js +187 -0
- package/lib/components/SubscriptionSelector.cjs.js +1 -0
- package/lib/components/SubscriptionSelector.es.js +140 -0
- package/lib/countries.cjs.js +1 -0
- package/lib/countries.es.js +77 -0
- package/lib/coupon-server.cjs.js +1 -0
- package/lib/coupon-server.es.js +234 -0
- package/lib/coupons.cjs.js +1 -0
- package/lib/coupons.es.js +17 -0
- package/lib/currency-constants.cjs.js +1 -0
- package/lib/currency-constants.es.js +4 -0
- package/lib/currency-store.cjs.js +1 -0
- package/lib/currency-store.es.js +27 -0
- package/lib/currency-sync.cjs.js +1 -0
- package/lib/currency-sync.es.js +180 -0
- package/lib/currency.cjs.js +1 -0
- package/lib/currency.es.js +312 -0
- package/lib/customer-addresses.cjs.js +1 -0
- package/lib/customer-addresses.es.js +116 -0
- package/lib/customer-orders.cjs.js +1 -0
- package/lib/customer-orders.es.js +45 -0
- package/lib/customer.cjs.js +1 -0
- package/lib/customer.es.js +58 -0
- package/lib/export-helpers.cjs.js +1 -0
- package/lib/export-helpers.es.js +56 -0
- package/lib/factory.cjs.js +1 -0
- package/lib/factory.es.js +8 -0
- package/lib/freemius-coupons.cjs.js +1 -0
- package/lib/freemius-coupons.es.js +272 -0
- package/lib/freemius-order-sync.cjs.js +1 -0
- package/lib/freemius-order-sync.es.js +284 -0
- package/lib/inventory-settings.cjs.js +1 -0
- package/lib/inventory-settings.es.js +86 -0
- package/lib/invoice-server.cjs.js +33 -0
- package/lib/invoice-server.es.js +141 -0
- package/lib/invoice-ui.cjs.js +1 -0
- package/lib/invoice-ui.es.js +71 -0
- package/lib/invoice.cjs.js +1 -0
- package/lib/invoice.es.js +102 -0
- package/lib/order-inventory.cjs.js +61 -0
- package/lib/order-inventory.es.js +128 -0
- package/lib/order-tax-details.cjs.js +1 -0
- package/lib/order-tax-details.es.js +164 -0
- package/lib/pages/cms/coupons/CouponEditorForm.cjs.js +1 -0
- package/lib/pages/cms/coupons/CouponEditorForm.es.js +189 -0
- package/lib/pages/cms/coupons/CouponsPage.cjs.js +5 -0
- package/lib/pages/cms/coupons/CouponsPage.es.js +171 -0
- package/lib/pages/cms/coupons/CreateCouponDialog.cjs.js +1 -0
- package/lib/pages/cms/coupons/CreateCouponDialog.es.js +35 -0
- package/lib/pages/cms/coupons/EditCouponPage.cjs.js +1 -0
- package/lib/pages/cms/coupons/EditCouponPage.es.js +48 -0
- package/lib/pages/cms/coupons/ProductScopePicker.cjs.js +1 -0
- package/lib/pages/cms/coupons/ProductScopePicker.es.js +120 -0
- package/lib/pages/cms/coupons/actions.cjs.js +1 -0
- package/lib/pages/cms/coupons/actions.es.js +128 -0
- package/lib/pages/cms/coupons/product-options.cjs.js +1 -0
- package/lib/pages/cms/coupons/product-options.es.js +27 -0
- package/lib/pages/cms/orders/ExportReportsDialog.cjs.js +1 -0
- package/lib/pages/cms/orders/ExportReportsDialog.es.js +130 -0
- package/lib/pages/cms/orders/OrderDetailPage.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderDetailPage.es.js +137 -0
- package/lib/pages/cms/orders/OrderPrintButton.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderPrintButton.es.js +22 -0
- package/lib/pages/cms/orders/OrderStatusForm.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderStatusForm.es.js +71 -0
- package/lib/pages/cms/orders/OrdersPage.cjs.js +1 -0
- package/lib/pages/cms/orders/OrdersPage.es.js +104 -0
- package/lib/pages/cms/orders/actions.cjs.js +7 -0
- package/lib/pages/cms/orders/actions.es.js +71 -0
- package/lib/pages/cms/orders/export-actions.cjs.js +1 -0
- package/lib/pages/cms/orders/export-actions.es.js +27 -0
- package/lib/pages/cms/orders/server-actions.cjs.js +1 -0
- package/lib/pages/cms/orders/server-actions.es.js +55 -0
- package/lib/pages/cms/payments/PaymentsClient.cjs.js +1 -0
- package/lib/pages/cms/payments/PaymentsClient.es.js +188 -0
- package/lib/pages/cms/payments/PaymentsPage.cjs.js +1 -0
- package/lib/pages/cms/payments/PaymentsPage.es.js +29 -0
- package/lib/pages/cms/payments/actions.cjs.js +1 -0
- package/lib/pages/cms/payments/actions.es.js +23 -0
- package/lib/pages/cms/payments/queries.cjs.js +1 -0
- package/lib/pages/cms/payments/queries.es.js +30 -0
- package/lib/pages/cms/products/ProductsPage.cjs.js +1 -0
- package/lib/pages/cms/products/ProductsPage.es.js +46 -0
- package/lib/pages/cms/products/_id_/edit/EditProductPage.cjs.js +1 -0
- package/lib/pages/cms/products/_id_/edit/EditProductPage.es.js +156 -0
- package/lib/pages/cms/products/actions.cjs.js +44 -0
- package/lib/pages/cms/products/actions.es.js +175 -0
- package/lib/pages/cms/products/attributes/AttributeManagementPage.cjs.js +1 -0
- package/lib/pages/cms/products/attributes/AttributeManagementPage.es.js +48 -0
- package/lib/pages/cms/products/attributes/components/AttributeManager.cjs.js +1 -0
- package/lib/pages/cms/products/attributes/components/AttributeManager.es.js +291 -0
- package/lib/pages/cms/products/categories/CategoryManagementPage.cjs.js +1 -0
- package/lib/pages/cms/products/categories/CategoryManagementPage.es.js +36 -0
- package/lib/pages/cms/products/categories/components/CategoryManager.cjs.js +1 -0
- package/lib/pages/cms/products/categories/components/CategoryManager.es.js +263 -0
- package/lib/pages/cms/products/components/CopyProductFromLanguage.cjs.js +1 -0
- package/lib/pages/cms/products/components/CopyProductFromLanguage.es.js +114 -0
- package/lib/pages/cms/products/components/CurrencyPriceFields.cjs.js +1 -0
- package/lib/pages/cms/products/components/CurrencyPriceFields.es.js +107 -0
- package/lib/pages/cms/products/components/DeleteProductButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/DeleteProductButton.es.js +48 -0
- package/lib/pages/cms/products/components/FreemiusPricingDashboard.cjs.js +1 -0
- package/lib/pages/cms/products/components/FreemiusPricingDashboard.es.js +86 -0
- package/lib/pages/cms/products/components/ProductCategorySelector.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductCategorySelector.es.js +160 -0
- package/lib/pages/cms/products/components/ProductForm.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductForm.es.js +702 -0
- package/lib/pages/cms/products/components/ProductMediaManager.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductMediaManager.es.js +104 -0
- package/lib/pages/cms/products/components/ProductsBulkTable.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductsBulkTable.es.js +228 -0
- package/lib/pages/cms/products/components/SaleScheduleFields.cjs.js +1 -0
- package/lib/pages/cms/products/components/SaleScheduleFields.es.js +137 -0
- package/lib/pages/cms/products/components/SyncFreemiusButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/SyncFreemiusButton.es.js +36 -0
- package/lib/pages/cms/products/components/SyncFreemiusPricingButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/SyncFreemiusPricingButton.es.js +40 -0
- package/lib/pages/cms/products/components/VariationsEditor.cjs.js +1 -0
- package/lib/pages/cms/products/components/VariationsEditor.es.js +408 -0
- package/lib/pages/cms/products/inventory/InventoryPage.cjs.js +1 -0
- package/lib/pages/cms/products/inventory/InventoryPage.es.js +16 -0
- package/lib/pages/cms/products/inventory/InventoryTableClient.cjs.js +2 -0
- package/lib/pages/cms/products/inventory/InventoryTableClient.es.js +214 -0
- package/lib/pages/cms/products/inventory/actions.cjs.js +3 -0
- package/lib/pages/cms/products/inventory/actions.es.js +95 -0
- package/lib/pages/cms/products/new/NewProductPage.cjs.js +1 -0
- package/lib/pages/cms/products/new/NewProductPage.es.js +76 -0
- package/lib/pages/cms/products/product-price-sync.cjs.js +1 -0
- package/lib/pages/cms/products/product-price-sync.es.js +98 -0
- package/lib/pages/cms/products/server-actions.cjs.js +1 -0
- package/lib/pages/cms/products/server-actions.es.js +193 -0
- package/lib/pages/cms/shipping/ShippingPage.cjs.js +5 -0
- package/lib/pages/cms/shipping/ShippingPage.es.js +211 -0
- package/lib/pages/cms/shipping/components/RateForm.cjs.js +1 -0
- package/lib/pages/cms/shipping/components/RateForm.es.js +441 -0
- package/lib/pages/cms/shipping/components/ZoneForm.cjs.js +1 -0
- package/lib/pages/cms/shipping/components/ZoneForm.es.js +228 -0
- package/lib/pages/cms/shipping/server-actions.cjs.js +1 -0
- package/lib/pages/cms/shipping/server-actions.es.js +159 -0
- package/lib/pages/cms/taxes/TaxesPage.cjs.js +1 -0
- package/lib/pages/cms/taxes/TaxesPage.es.js +172 -0
- package/lib/pages/cms/taxes/actions.cjs.js +1 -0
- package/lib/pages/cms/taxes/actions.es.js +57 -0
- package/lib/pages/cms/taxes/components/TaxRateForm.cjs.js +1 -0
- package/lib/pages/cms/taxes/components/TaxRateForm.es.js +100 -0
- package/lib/product-actions.cjs.js +237 -0
- package/lib/product-actions.es.js +524 -0
- package/lib/product-context.cjs.js +1 -0
- package/lib/product-context.es.js +16 -0
- package/lib/product-schema.cjs.js +1 -0
- package/lib/product-schema.es.js +134 -0
- package/lib/providers/freemius.cjs.js +5 -0
- package/lib/providers/freemius.es.js +538 -0
- package/lib/providers/stripe.cjs.js +1 -0
- package/lib/providers/stripe.es.js +473 -0
- package/lib/server-actions/coupon-actions.cjs.js +1 -0
- package/lib/server-actions/coupon-actions.es.js +23 -0
- package/lib/server-actions/customer-actions.cjs.js +1 -0
- package/lib/server-actions/customer-actions.es.js +30 -0
- package/lib/server-actions/product-actions.cjs.js +1 -0
- package/lib/server-actions/product-actions.es.js +16 -0
- package/lib/server-actions/shipping-actions.cjs.js +1 -0
- package/lib/server-actions/shipping-actions.es.js +27 -0
- package/lib/server-actions/tax-actions.cjs.js +1 -0
- package/lib/server-actions/tax-actions.es.js +40 -0
- package/lib/shared-inventory.cjs.js +15 -0
- package/lib/shared-inventory.es.js +171 -0
- package/lib/shipping/resolver.cjs.js +7 -0
- package/lib/shipping/resolver.es.js +62 -0
- package/lib/shipping-rate-currency.cjs.js +1 -0
- package/lib/shipping-rate-currency.es.js +72 -0
- package/lib/states.cjs.js +1 -0
- package/lib/states.es.js +127 -0
- package/lib/stripe/checkout.cjs.js +1 -0
- package/lib/stripe/checkout.es.js +182 -0
- package/lib/stripe/client.cjs.js +1 -0
- package/lib/stripe/client.es.js +7 -0
- package/lib/stripe/order-sync.cjs.js +1 -0
- package/lib/stripe/order-sync.es.js +130 -0
- package/lib/stripe/webhooks.cjs.js +1 -0
- package/lib/stripe/webhooks.es.js +30 -0
- package/lib/tax-calculation.cjs.js +1 -0
- package/lib/tax-calculation.es.js +101 -0
- package/lib/trials.cjs.js +1 -0
- package/lib/trials.es.js +29 -0
- package/lib/types.cjs.js +1 -0
- package/lib/types.es.js +33 -0
- package/lib/use-cart.cjs.js +1 -0
- package/lib/use-cart.es.js +44 -0
- package/lib/variation-utils.cjs.js +1 -0
- package/lib/variation-utils.es.js +246 -0
- package/lib/zod-config.cjs.js +1 -0
- package/lib/zod-config.es.js +6 -0
- package/package.json +10 -5
- package/server.cjs.js +1 -110
- package/server.es.js +178 -6037
- package/invoice-ui-BJZfedDd.cjs +0 -332
- package/invoice-ui-GJ1ADAjn.js +0 -5759
- /package/{currency-rest-client-uolJxUkL.cjs → lib/currency-rest-client.cjs.js} +0 -0
- /package/{currency-rest-client-CwoqdgAP.js → lib/currency-rest-client.es.js} +0 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { createClient as A } from "@supabase/supabase-js";
|
|
2
|
+
import { normalizeOrderCustomerDetails as x, normalizeCustomerAddress as v } from "../customer.es.js";
|
|
3
|
+
import { upsertDefaultUserAddresses as w } from "../customer-addresses.es.js";
|
|
4
|
+
import { applyOrderInventoryDeduction as C } from "../order-inventory.es.js";
|
|
5
|
+
import { normalizeOrderTaxDetails as U, buildOrderTaxDetailsFromStripeSession as R } from "../order-tax-details.es.js";
|
|
6
|
+
import { assignInvoiceMetadata as O } from "../invoice-server.es.js";
|
|
7
|
+
import { stripe as b } from "./client.es.js";
|
|
8
|
+
const P = ["total_details.breakdown"], z = ["data.taxes.rate"];
|
|
9
|
+
function k() {
|
|
10
|
+
const r = process.env.NEXT_PUBLIC_SUPABASE_URL, t = process.env.SUPABASE_SERVICE_ROLE_KEY;
|
|
11
|
+
if (!r || !t)
|
|
12
|
+
throw new Error("Missing Supabase Service Role environment variables");
|
|
13
|
+
return A(r, t, {
|
|
14
|
+
auth: {
|
|
15
|
+
autoRefreshToken: !1,
|
|
16
|
+
persistSession: !1
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
function S(r, t) {
|
|
21
|
+
return r ? v({
|
|
22
|
+
recipient_name: t,
|
|
23
|
+
line1: r.line1 ?? null,
|
|
24
|
+
line2: r.line2 ?? null,
|
|
25
|
+
city: r.city ?? null,
|
|
26
|
+
state: r.state ?? null,
|
|
27
|
+
postal_code: r.postal_code ?? null,
|
|
28
|
+
country_code: r.country ?? null
|
|
29
|
+
}) : null;
|
|
30
|
+
}
|
|
31
|
+
function N(r) {
|
|
32
|
+
const t = r ?? {};
|
|
33
|
+
return x({
|
|
34
|
+
email: typeof t.email == "string" ? t.email : null,
|
|
35
|
+
name: typeof t.name == "string" ? t.name : null,
|
|
36
|
+
phone: typeof t.phone == "string" ? t.phone : null,
|
|
37
|
+
billing: t.billing ?? null,
|
|
38
|
+
shipping: t.shipping ?? null
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
async function H(r) {
|
|
42
|
+
const t = k();
|
|
43
|
+
let i = r;
|
|
44
|
+
try {
|
|
45
|
+
i = await b.checkout.sessions.retrieve(r.id, {
|
|
46
|
+
expand: [...P]
|
|
47
|
+
});
|
|
48
|
+
} catch (d) {
|
|
49
|
+
console.error("[Stripe Sync] Failed to rehydrate session tax details:", d);
|
|
50
|
+
}
|
|
51
|
+
const p = i.metadata?.orderId;
|
|
52
|
+
let c = t.from("orders").select(
|
|
53
|
+
"id, user_id, status, total, currency, subtotal, shipping_total, tax_total, tax_details, customer_details"
|
|
54
|
+
).eq("stripe_session_id", r.id).maybeSingle();
|
|
55
|
+
p && (c = t.from("orders").select(
|
|
56
|
+
"id, user_id, status, total, currency, subtotal, shipping_total, tax_total, tax_details, customer_details"
|
|
57
|
+
).eq("id", p).maybeSingle());
|
|
58
|
+
const { data: e, error: u } = await c;
|
|
59
|
+
if (u || !e)
|
|
60
|
+
throw new Error(u?.message || "Order lookup failed");
|
|
61
|
+
const a = N(e.customer_details), o = U(e.tax_details), E = S(
|
|
62
|
+
i.customer_details?.address,
|
|
63
|
+
i.customer_details?.name ?? a.name
|
|
64
|
+
), m = i, D = S(
|
|
65
|
+
m.shipping_details?.address,
|
|
66
|
+
m.shipping_details?.name ?? a.name
|
|
67
|
+
), n = x({
|
|
68
|
+
email: i.customer_details?.email ?? a.email,
|
|
69
|
+
name: i.customer_details?.name ?? a.name,
|
|
70
|
+
phone: i.customer_details?.phone ?? a.phone,
|
|
71
|
+
billing: a.billing ?? E,
|
|
72
|
+
shipping: a.shipping ?? D
|
|
73
|
+
}), I = e.status === "paid", T = await b.checkout.sessions.listLineItems(i.id, {
|
|
74
|
+
limit: 100,
|
|
75
|
+
expand: [...z]
|
|
76
|
+
}), l = R({
|
|
77
|
+
session: i,
|
|
78
|
+
lineItems: T.data,
|
|
79
|
+
subtotal: typeof e.subtotal == "number" ? e.subtotal : o?.subtotal ?? 0,
|
|
80
|
+
shippingTotal: typeof e.shipping_total == "number" ? e.shipping_total : o?.shipping_total ?? 0,
|
|
81
|
+
fallbackMode: o?.mode ?? "automatic",
|
|
82
|
+
currency: (i.currency ?? e.currency ?? o?.currency ?? "USD").toUpperCase()
|
|
83
|
+
}), s = l.tax_total > 0 || l.lines.length > 0 ? l : o ?? l, _ = s?.tax_total ?? (typeof e.tax_total == "number" ? e.tax_total : 0), y = (i.currency ?? e.currency ?? s?.currency ?? "USD").toUpperCase(), f = {
|
|
84
|
+
status: "paid",
|
|
85
|
+
stripe_session_id: i.id,
|
|
86
|
+
payment_intent_id: typeof i.payment_intent == "string" ? i.payment_intent : null,
|
|
87
|
+
provider: "stripe",
|
|
88
|
+
customer_details: n,
|
|
89
|
+
total: typeof i.amount_total == "number" ? i.amount_total : e.total,
|
|
90
|
+
currency: y,
|
|
91
|
+
tax_total: _,
|
|
92
|
+
tax_details: s
|
|
93
|
+
}, { error: g } = await t.from("orders").update(f).eq("id", e.id);
|
|
94
|
+
if (g)
|
|
95
|
+
throw new Error(g.message);
|
|
96
|
+
if (e.user_id)
|
|
97
|
+
try {
|
|
98
|
+
await w({
|
|
99
|
+
userId: e.user_id,
|
|
100
|
+
billingAddress: n.billing,
|
|
101
|
+
shippingAddress: n.shipping,
|
|
102
|
+
client: t
|
|
103
|
+
});
|
|
104
|
+
} catch (d) {
|
|
105
|
+
console.error("[Stripe Sync] Failed to refresh saved customer addresses:", d);
|
|
106
|
+
}
|
|
107
|
+
const h = await O({
|
|
108
|
+
orderId: e.id,
|
|
109
|
+
client: t
|
|
110
|
+
});
|
|
111
|
+
return await C(t, e.id), {
|
|
112
|
+
orderId: e.id,
|
|
113
|
+
alreadyPaid: I,
|
|
114
|
+
customerDetails: n,
|
|
115
|
+
order: {
|
|
116
|
+
id: e.id,
|
|
117
|
+
invoice_number: h.invoiceNumber,
|
|
118
|
+
paid_at: h.paidAt,
|
|
119
|
+
total: f.total,
|
|
120
|
+
currency: y,
|
|
121
|
+
subtotal: typeof e.subtotal == "number" ? e.subtotal : s?.subtotal ?? 0,
|
|
122
|
+
shipping_total: typeof e.shipping_total == "number" ? e.shipping_total : s?.shipping_total ?? 0,
|
|
123
|
+
tax_total: _,
|
|
124
|
+
tax_details: s
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
export {
|
|
129
|
+
H as syncStripeOrderFromSession
|
|
130
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./client.cjs.js"),i=require("./order-sync.cjs.js"),a=async(s,n)=>{const o=process.env.STRIPE_WEBHOOK_SECRET;if(!o)return console.error("Missing STRIPE_WEBHOOK_SECRET"),{received:!1,error:"Server configuration error"};let e;try{e=c.stripe.webhooks.constructEvent(n,s,o)}catch(r){return console.error(`Webhook signature verification failed: ${r.message}`),{received:!1,error:`Webhook Error: ${r.message}`}}switch(e.type){case"checkout.session.completed":{const r=e.data.object;try{await i.syncStripeOrderFromSession(r)}catch(t){return console.error("[Stripe Webhook Error] Failed to sync completed session:",t),{received:!1,error:t.message||"Failed to sync Stripe session"}}break}default:console.log(`Unhandled event type ${e.type}`)}return{received:!0}};exports.handleStripeWebhook=a;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { stripe as c } from "./client.es.js";
|
|
2
|
+
import { syncStripeOrderFromSession as i } from "./order-sync.es.js";
|
|
3
|
+
const d = async (t, n) => {
|
|
4
|
+
const o = process.env.STRIPE_WEBHOOK_SECRET;
|
|
5
|
+
if (!o)
|
|
6
|
+
return console.error("Missing STRIPE_WEBHOOK_SECRET"), { received: !1, error: "Server configuration error" };
|
|
7
|
+
let e;
|
|
8
|
+
try {
|
|
9
|
+
e = c.webhooks.constructEvent(n, t, o);
|
|
10
|
+
} catch (r) {
|
|
11
|
+
return console.error(`Webhook signature verification failed: ${r.message}`), { received: !1, error: `Webhook Error: ${r.message}` };
|
|
12
|
+
}
|
|
13
|
+
switch (e.type) {
|
|
14
|
+
case "checkout.session.completed": {
|
|
15
|
+
const r = e.data.object;
|
|
16
|
+
try {
|
|
17
|
+
await i(r);
|
|
18
|
+
} catch (s) {
|
|
19
|
+
return console.error("[Stripe Webhook Error] Failed to sync completed session:", s), { received: !1, error: s.message || "Failed to sync Stripe session" };
|
|
20
|
+
}
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
default:
|
|
24
|
+
console.log(`Unhandled event type ${e.type}`);
|
|
25
|
+
}
|
|
26
|
+
return { received: !0 };
|
|
27
|
+
};
|
|
28
|
+
export {
|
|
29
|
+
d as handleStripeWebhook
|
|
30
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const C=require("./countries.cjs.js"),m=require("./currency.cjs.js"),T=require("./inventory-settings.cjs.js"),E=require("./states.cjs.js"),b=require("./coupons.cjs.js"),p="txcd_99999999",x="txcd_00000000",P="txcd_92010001";function S(s){return s?p:x}async function y(s,r,i,a,u){if(!r.length)return[];const d=[...new Set(r.map(o=>o.product_id))],{data:c,error:l}=await s.from("products").select("id, is_taxable").in("id",d);if(l)throw new Error(l.message);const _=new Map((c||[]).map(o=>[o.id,o]));return r.reduce((o,e)=>{const t=_.get(e.product_id);if(!t)return o;const n=m.resolveEffectivePriceForCurrency({prices:e.prices,salePrices:e.sale_prices,fallbackPrice:e.price,fallbackSalePrice:e.sale_price,saleStartAt:e.sale_start_at,saleEndAt:e.sale_end_at,scheduledPrice:e.scheduled_price,scheduledPrices:e.scheduled_prices,scheduledPriceAt:e.scheduled_price_at,currencyCode:i,currencies:a});return o.push({product_id:t.id,quantity:e.quantity,unit_amount:n.sale_price??n.price,discount_amount:u?.get(b.getCartLineCouponKey(e))??0,is_taxable:t.is_taxable??!0}),o},[])}async function f(s,r){const i=await T.getEcommerceInventorySettings(s),a=r.items.reduce((t,n)=>n.is_taxable?t+Math.max(0,n.unit_amount*n.quantity-(n.discount_amount??0)):t,0);if(!i.enableTaxes||a<=0)return{enabled:!1,mode:i.taxCalculationMode,amount:0,taxableSubtotal:a,lines:[]};if(i.taxCalculationMode==="automatic")return{enabled:!0,mode:"automatic",amount:0,taxableSubtotal:a,lines:[],isPendingExternalCalculation:!0};const u=C.normalizeCountryCode(r.destination?.country_code),d=E.normalizeSubdivisionCode(u,r.destination?.state)||null;if(!u)return{enabled:!0,mode:"manual",amount:0,taxableSubtotal:a,lines:[]};let c=s.from("tax_rates").select("id, country_code, state_code, tax_name, tax_rate, created_at, updated_at").eq("country_code",u);c=d?c.or(`state_code.is.null,state_code.eq.${d}`):c.is("state_code",null);const{data:l,error:_}=await c;if(_)throw new Error(_.message);const e=(l||[]).map(t=>({id:t.id,name:t.tax_name,rate:Number(t.tax_rate),amount:Math.round(a*Number(t.tax_rate)/100),country_code:t.country_code,state_code:t.state_code??null}));return{enabled:!0,mode:"manual",amount:e.reduce((t,n)=>t+n.amount,0),taxableSubtotal:a,lines:e}}exports.STRIPE_TAX_CODE_NONTAXABLE=x;exports.STRIPE_TAX_CODE_SHIPPING=P;exports.STRIPE_TAX_CODE_TAXABLE_GOODS=p;exports.buildCheckoutTaxableItemsFromCart=y;exports.calculateCheckoutTaxes=f;exports.getStripeTaxCodeForProduct=S;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { normalizeCountryCode as m } from "./countries.es.js";
|
|
2
|
+
import { resolveEffectivePriceForCurrency as p } from "./currency.es.js";
|
|
3
|
+
import { getEcommerceInventorySettings as f } from "./inventory-settings.es.js";
|
|
4
|
+
import { normalizeSubdivisionCode as x } from "./states.es.js";
|
|
5
|
+
import { getCartLineCouponKey as C } from "./coupons.es.js";
|
|
6
|
+
const b = "txcd_99999999", y = "txcd_00000000", g = "txcd_92010001";
|
|
7
|
+
function A(i) {
|
|
8
|
+
return i ? b : y;
|
|
9
|
+
}
|
|
10
|
+
async function w(i, r, s, n, c) {
|
|
11
|
+
if (!r.length)
|
|
12
|
+
return [];
|
|
13
|
+
const u = [...new Set(r.map((o) => o.product_id))], { data: d, error: l } = await i.from("products").select("id, is_taxable").in("id", u);
|
|
14
|
+
if (l)
|
|
15
|
+
throw new Error(l.message);
|
|
16
|
+
const _ = new Map(
|
|
17
|
+
(d || []).map((o) => [o.id, o])
|
|
18
|
+
);
|
|
19
|
+
return r.reduce((o, e) => {
|
|
20
|
+
const t = _.get(e.product_id);
|
|
21
|
+
if (!t)
|
|
22
|
+
return o;
|
|
23
|
+
const a = p({
|
|
24
|
+
prices: e.prices,
|
|
25
|
+
salePrices: e.sale_prices,
|
|
26
|
+
fallbackPrice: e.price,
|
|
27
|
+
fallbackSalePrice: e.sale_price,
|
|
28
|
+
saleStartAt: e.sale_start_at,
|
|
29
|
+
saleEndAt: e.sale_end_at,
|
|
30
|
+
scheduledPrice: e.scheduled_price,
|
|
31
|
+
scheduledPrices: e.scheduled_prices,
|
|
32
|
+
scheduledPriceAt: e.scheduled_price_at,
|
|
33
|
+
currencyCode: s,
|
|
34
|
+
currencies: n
|
|
35
|
+
});
|
|
36
|
+
return o.push({
|
|
37
|
+
product_id: t.id,
|
|
38
|
+
quantity: e.quantity,
|
|
39
|
+
unit_amount: a.sale_price ?? a.price,
|
|
40
|
+
discount_amount: c?.get(C(e)) ?? 0,
|
|
41
|
+
is_taxable: t.is_taxable ?? !0
|
|
42
|
+
}), o;
|
|
43
|
+
}, []);
|
|
44
|
+
}
|
|
45
|
+
async function q(i, r) {
|
|
46
|
+
const s = await f(i), n = r.items.reduce((t, a) => a.is_taxable ? t + Math.max(0, a.unit_amount * a.quantity - (a.discount_amount ?? 0)) : t, 0);
|
|
47
|
+
if (!s.enableTaxes || n <= 0)
|
|
48
|
+
return {
|
|
49
|
+
enabled: !1,
|
|
50
|
+
mode: s.taxCalculationMode,
|
|
51
|
+
amount: 0,
|
|
52
|
+
taxableSubtotal: n,
|
|
53
|
+
lines: []
|
|
54
|
+
};
|
|
55
|
+
if (s.taxCalculationMode === "automatic")
|
|
56
|
+
return {
|
|
57
|
+
enabled: !0,
|
|
58
|
+
mode: "automatic",
|
|
59
|
+
amount: 0,
|
|
60
|
+
taxableSubtotal: n,
|
|
61
|
+
lines: [],
|
|
62
|
+
isPendingExternalCalculation: !0
|
|
63
|
+
};
|
|
64
|
+
const c = m(r.destination?.country_code), u = x(c, r.destination?.state) || null;
|
|
65
|
+
if (!c)
|
|
66
|
+
return {
|
|
67
|
+
enabled: !0,
|
|
68
|
+
mode: "manual",
|
|
69
|
+
amount: 0,
|
|
70
|
+
taxableSubtotal: n,
|
|
71
|
+
lines: []
|
|
72
|
+
};
|
|
73
|
+
let d = i.from("tax_rates").select("id, country_code, state_code, tax_name, tax_rate, created_at, updated_at").eq("country_code", c);
|
|
74
|
+
d = u ? d.or(`state_code.is.null,state_code.eq.${u}`) : d.is("state_code", null);
|
|
75
|
+
const { data: l, error: _ } = await d;
|
|
76
|
+
if (_)
|
|
77
|
+
throw new Error(_.message);
|
|
78
|
+
const e = (l || []).map((t) => ({
|
|
79
|
+
id: t.id,
|
|
80
|
+
name: t.tax_name,
|
|
81
|
+
rate: Number(t.tax_rate),
|
|
82
|
+
amount: Math.round(n * Number(t.tax_rate) / 100),
|
|
83
|
+
country_code: t.country_code,
|
|
84
|
+
state_code: t.state_code ?? null
|
|
85
|
+
}));
|
|
86
|
+
return {
|
|
87
|
+
enabled: !0,
|
|
88
|
+
mode: "manual",
|
|
89
|
+
amount: e.reduce((t, a) => t + a.amount, 0),
|
|
90
|
+
taxableSubtotal: n,
|
|
91
|
+
lines: e
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export {
|
|
95
|
+
y as STRIPE_TAX_CODE_NONTAXABLE,
|
|
96
|
+
g as STRIPE_TAX_CODE_SHIPPING,
|
|
97
|
+
b as STRIPE_TAX_CODE_TAXABLE_GOODS,
|
|
98
|
+
w as buildCheckoutTaxableItemsFromCart,
|
|
99
|
+
q as calculateCheckoutTaxes,
|
|
100
|
+
A as getStripeTaxCodeForProduct
|
|
101
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function t(e){const r=Number(e?.trial_period_days??0);return!Number.isFinite(r)||r<=0?0:Math.round(r)}function i(e){return t(e)===0?null:e?.trial_requires_payment_method?"paid":"free"}function a(e){const r=t(typeof e=="number"?{trial_period_days:e}:e);return r>0?`${r}-day free trial`:null}function n(e){return e?.trial_requires_payment_method?"Payment method required":"No credit card required"}function o(e){const r=a(e);return r?{label:r,paymentRequirementLabel:n(e),checkoutMode:i(e)}:null}exports.getTrialCheckoutMode=i;exports.getTrialLabel=a;exports.getTrialPaymentRequirementLabel=n;exports.getTrialPeriodDays=t;exports.getTrialSummary=o;
|
package/lib/trials.es.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
function t(e) {
|
|
2
|
+
const r = Number(e?.trial_period_days ?? 0);
|
|
3
|
+
return !Number.isFinite(r) || r <= 0 ? 0 : Math.round(r);
|
|
4
|
+
}
|
|
5
|
+
function i(e) {
|
|
6
|
+
return t(e) === 0 ? null : e?.trial_requires_payment_method ? "paid" : "free";
|
|
7
|
+
}
|
|
8
|
+
function n(e) {
|
|
9
|
+
const r = t(typeof e == "number" ? { trial_period_days: e } : e);
|
|
10
|
+
return r > 0 ? `${r}-day free trial` : null;
|
|
11
|
+
}
|
|
12
|
+
function a(e) {
|
|
13
|
+
return e?.trial_requires_payment_method ? "Payment method required" : "No credit card required";
|
|
14
|
+
}
|
|
15
|
+
function o(e) {
|
|
16
|
+
const r = n(e);
|
|
17
|
+
return r ? {
|
|
18
|
+
label: r,
|
|
19
|
+
paymentRequirementLabel: a(e),
|
|
20
|
+
checkoutMode: i(e)
|
|
21
|
+
} : null;
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
i as getTrialCheckoutMode,
|
|
25
|
+
n as getTrialLabel,
|
|
26
|
+
a as getTrialPaymentRequirementLabel,
|
|
27
|
+
t as getTrialPeriodDays,
|
|
28
|
+
o as getTrialSummary
|
|
29
|
+
};
|
package/lib/types.cjs.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t={stripe:!1,freemius:!1};function d(r){if(!r||typeof r!="object"||Array.isArray(r))return{...t};const e=r;return{stripe:e.stripe===!0,freemius:e.freemius===!0}}function i(r){return r==="digital"?"freemius":"stripe"}function n(r){return r?"provider"in r&&r.provider?r.provider:r.payment_provider?r.payment_provider:r.product_type?i(r.product_type):"freemius_product_id"in r&&r.freemius_product_id?"freemius":null:null}function o(r){return n(r)==="freemius"}function u(r){return o(r)}exports.DEFAULT_ENABLED_PAYMENT_PROVIDERS=t;exports.derivePaymentProviderFromProductType=i;exports.getProductPaymentProvider=n;exports.isDigitalItem=u;exports.isDigitalProduct=o;exports.normalizeEnabledPaymentProviders=d;
|
package/lib/types.es.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const i = {
|
|
2
|
+
stripe: !1,
|
|
3
|
+
freemius: !1
|
|
4
|
+
};
|
|
5
|
+
function u(r) {
|
|
6
|
+
if (!r || typeof r != "object" || Array.isArray(r))
|
|
7
|
+
return { ...i };
|
|
8
|
+
const e = r;
|
|
9
|
+
return {
|
|
10
|
+
stripe: e.stripe === !0,
|
|
11
|
+
freemius: e.freemius === !0
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function t(r) {
|
|
15
|
+
return r === "digital" ? "freemius" : "stripe";
|
|
16
|
+
}
|
|
17
|
+
function n(r) {
|
|
18
|
+
return r ? "provider" in r && r.provider ? r.provider : r.payment_provider ? r.payment_provider : r.product_type ? t(r.product_type) : "freemius_product_id" in r && r.freemius_product_id ? "freemius" : null : null;
|
|
19
|
+
}
|
|
20
|
+
function o(r) {
|
|
21
|
+
return n(r) === "freemius";
|
|
22
|
+
}
|
|
23
|
+
function d(r) {
|
|
24
|
+
return o(r);
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
i as DEFAULT_ENABLED_PAYMENT_PROVIDERS,
|
|
28
|
+
t as derivePaymentProviderFromProductType,
|
|
29
|
+
n as getProductPaymentProvider,
|
|
30
|
+
d as isDigitalItem,
|
|
31
|
+
o as isDigitalProduct,
|
|
32
|
+
u as normalizeEnabledPaymentProviders
|
|
33
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("react"),a=require("./cart-store.cjs.js");let s=null;function i(){return a.useCartStore.persist}function o(){return i()?.hasHydrated?.()??!1}function d(){if(typeof window>"u")return Promise.resolve();const e=i();return!e||e.hasHydrated()?Promise.resolve():(s||(s=Promise.resolve().then(()=>e.rehydrate()).finally(()=>{s=null})),s)}const l=e=>{const t=a.useCartStore(e),[u,r]=n.useState(!1);return n.useEffect(()=>{const c=i();if(o()){r(!0);return}const f=c?.onFinishHydration(()=>r(!0));return d(),()=>{f?.()}},[]),u?t:void 0},y=()=>{const[e,t]=n.useState(!1);return n.useEffect(()=>{const r=i()?.onFinishHydration(()=>t(!0));return t(o()),o()||d(),()=>{r?.()}},[]),e};exports.useCart=l;exports.useIsCartHydrated=y;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { useState as u, useEffect as a } from "react";
|
|
3
|
+
import { useCartStore as d } from "./cart-store.es.js";
|
|
4
|
+
let s = null;
|
|
5
|
+
function n() {
|
|
6
|
+
return d.persist;
|
|
7
|
+
}
|
|
8
|
+
function i() {
|
|
9
|
+
return n()?.hasHydrated?.() ?? !1;
|
|
10
|
+
}
|
|
11
|
+
function f() {
|
|
12
|
+
if (typeof window > "u")
|
|
13
|
+
return Promise.resolve();
|
|
14
|
+
const t = n();
|
|
15
|
+
return !t || t.hasHydrated() ? Promise.resolve() : (s || (s = Promise.resolve().then(() => t.rehydrate()).finally(() => {
|
|
16
|
+
s = null;
|
|
17
|
+
})), s);
|
|
18
|
+
}
|
|
19
|
+
const m = (t) => {
|
|
20
|
+
const r = d(t), [o, e] = u(!1);
|
|
21
|
+
return a(() => {
|
|
22
|
+
const c = n();
|
|
23
|
+
if (i()) {
|
|
24
|
+
e(!0);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const l = c?.onFinishHydration(() => e(!0));
|
|
28
|
+
return f(), () => {
|
|
29
|
+
l?.();
|
|
30
|
+
};
|
|
31
|
+
}, []), o ? r : void 0;
|
|
32
|
+
}, H = () => {
|
|
33
|
+
const [t, r] = u(!1);
|
|
34
|
+
return a(() => {
|
|
35
|
+
const e = n()?.onFinishHydration(() => r(!0));
|
|
36
|
+
return r(i()), i() || f(), () => {
|
|
37
|
+
e?.();
|
|
38
|
+
};
|
|
39
|
+
}, []), t;
|
|
40
|
+
};
|
|
41
|
+
export {
|
|
42
|
+
m as useCart,
|
|
43
|
+
H as useIsCartHydrated
|
|
44
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("./currency.cjs.js");function f(e){return!e||typeof e!="object"||Array.isArray(e)?null:Object.entries(e).reduce((r,[n,t])=>(typeof t=="string"&&t.trim()&&(r[n]=t.trim()),r),{})}function p(e,r,n){return!n||!r?e:r[n]?.trim()||e}function k(e,r){return p(e.name,e.name_translations,r)}function E(e,r){return p(e.value,e.value_translations,r)}function V(e){return e?e.startsWith("http")?e:process.env.NEXT_PUBLIC_R2_BASE_URL?`${process.env.NEXT_PUBLIC_R2_BASE_URL}/${e}`:process.env.NEXT_PUBLIC_SUPABASE_URL?`${process.env.NEXT_PUBLIC_SUPABASE_URL}/storage/v1/object/public/media/${e}`:e:null}function I(e){return e.toLowerCase().trim().replace(/\s+/g,"-").replace(/[^a-z0-9-]/g,"").replace(/-+/g,"-").replace(/^-|-$/g,"")}function A(e){return[...e].sort().join("__")}function S(e){return e.map(r=>r.term_value).join(" / ")}function P(e){return e.length===0?[]:e.reduce((r,n)=>r.flatMap(t=>n.map(s=>[...t,s])),[[]])}function B(e){const{baseSku:r,basePrice:n,basePrices:t={},baseSalePrice:s=null,baseSalePrices:i={},selectedAttributes:o,previousVariants:c=[]}=e;if(o.length===0||o.some(l=>l.terms.length===0))return[];const u=new Map(c.map(l=>[l.combination_key,l])),a=o.map(l=>l.terms.map(d=>({attribute_id:l.attribute_id,attribute_name:l.attribute_name,term:d})));return P(a).map(l=>{const d=l.map(({attribute_id:m,attribute_name:M,term:g})=>({attribute_id:m,attribute_name:M,term_id:g.id,term_value:g.value,term_slug:g.slug})),T=d.map(m=>m.term_id),h=A(T),_=u.get(h),y=d.map(m=>(m.term_slug?m.term_slug:I(m.term_value)).toUpperCase()).filter(Boolean).join("-");return{id:_?.id,combination_key:h,sku:_?.sku||[r.trim(),y].filter(Boolean).join("-"),upc:_?.upc??null,price:_?.price??n,prices:_?.prices??b.normalizePriceMap(t),sale_price:_?.sale_price??s,sale_prices:_?.sale_prices??b.normalizeSalePriceMap(i),sale_start_at:_?.sale_start_at??null,sale_end_at:_?.sale_end_at??null,stock_quantity:_?.stock_quantity??0,main_media_id:_?.main_media_id??null,main_image_url:_?.main_image_url??null,attribute_term_ids:T,selected_options:d,label:S(d)}})}function R(e){const r={};for(const n of e)for(const t of n.selected_options??[]){const s=new Set(r[t.attribute_id]??[]);s.add(t.term_id),r[t.attribute_id]=[...s]}return r}function U(e,r){const n=Object.values(r).filter(Boolean);return n.length===0?null:e.find(t=>t.attribute_term_ids.length===n.length&&n.every(s=>t.attribute_term_ids.includes(s)))??null}function v(e,r,n){const t=new Set;for(const s of e){if(!Object.entries(n).every(([c,u])=>!u||c===r?!0:s.attribute_term_ids.includes(u)))continue;const o=s.selected_options.find(c=>c.attribute_id===r);o&&t.add(o.term_id)}return t}function j(e,r){const n=r.find(t=>t.stock_quantity>0)??r[0]??null;return n?n.selected_options.reduce((t,s)=>(t[s.attribute_id]=s.term_id,t),{}):e.reduce((t,s)=>{const i=s.terms[0];return i&&(t[s.id]=i.id),t},{})}function z(e,r,n){const t={...n};for(const s of e){const i=v(r,s.id,t);if(i.size===0){delete t[s.id];continue}const o=t[s.id];if(o&&i.has(o))continue;const c=s.terms.find(u=>i.has(u.id));c&&(t[s.id]=c.id)}return t}function L(e=[],r){const n=new Map,t=e.map(i=>{const o=(i.variant_attribute_mapping||[]).map(u=>{const a=u.product_attribute_terms,l=a?.product_attributes;if(!a||!l)return null;const d=n.get(l.id)||{id:l.id,name:p(l.name,f(l.name_translations),r),slug:l.slug,name_translations:f(l.name_translations),terms:[],_termIds:new Set};return d._termIds.has(a.id)||(d.terms.push({id:a.id,attribute_id:a.attribute_id,value:p(a.value,f(a.value_translations),r),slug:a.slug,sort_order:a.sort_order??null,value_translations:f(a.value_translations)}),d._termIds.add(a.id)),n.set(l.id,d),{attribute_id:l.id,attribute_name:p(l.name,f(l.name_translations),r),term_id:a.id,term_value:p(a.value,f(a.value_translations),r),term_slug:a.slug}}).filter(u=>u!==null).sort((u,a)=>u.attribute_name.localeCompare(a.attribute_name)),c=o.map(u=>u.term_id);return{id:i.id,combination_key:A(c),sku:i.sku,upc:i.upc??null,price:i.price??0,prices:b.normalizePriceMap(i.prices),sale_price:i.sale_price??null,sale_prices:b.normalizeSalePriceMap(i.sale_prices),sale_start_at:i.sale_start_at??null,sale_end_at:i.sale_end_at??null,scheduled_price:i.scheduled_price??null,scheduled_prices:b.normalizePriceMap(i.scheduled_prices),scheduled_price_at:i.scheduled_price_at??null,stock_quantity:i.stock_quantity??0,main_media_id:i.main_media_id??null,image_url:V(i.media?.file_path??i.media?.object_key??null),attribute_term_ids:c,selected_options:o,label:S(o)}});return{attributes:[...n.values()].map(i=>({id:i.id,name:i.name,slug:i.slug,name_translations:i.name_translations,terms:[...i.terms].sort((o,c)=>{const u=o.sort_order??Number.MAX_SAFE_INTEGER,a=c.sort_order??Number.MAX_SAFE_INTEGER;return u!==a?u-a:o.value.localeCompare(c.value)})})).sort((i,o)=>i.name.localeCompare(o.name)),variants:t}}function N(e,r){if(!e.length)return null;const n=e.map(t=>b.isSaleWindowActive({saleStartAt:t.sale_start_at,saleEndAt:t.sale_end_at,now:r})&&typeof t.sale_price=="number"?t.sale_price:t.price);return{min:Math.min(...n),max:Math.max(...n)}}exports.buildCombinationKey=A;exports.buildVariantLabel=S;exports.chooseInitialVariantSelections=j;exports.extractSelectedTermsByAttribute=R;exports.findMatchingVariant=U;exports.generateVariantDrafts=B;exports.getAvailableTermIdsForAttribute=v;exports.getVariantEffectivePriceRange=N;exports.mapRawVariantRelations=L;exports.normalizeSelectionsToAvailableVariants=z;exports.resolveAttributeName=k;exports.resolveTermValue=E;exports.resolveTranslatedText=p;
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { normalizeSalePriceMap as S, normalizePriceMap as g, isSaleWindowActive as E } from "./currency.es.js";
|
|
2
|
+
function p(e) {
|
|
3
|
+
return !e || typeof e != "object" || Array.isArray(e) ? null : Object.entries(e).reduce((r, [i, t]) => (typeof t == "string" && t.trim() && (r[i] = t.trim()), r), {});
|
|
4
|
+
}
|
|
5
|
+
function f(e, r, i) {
|
|
6
|
+
return !i || !r ? e : r[i]?.trim() || e;
|
|
7
|
+
}
|
|
8
|
+
function j(e, r) {
|
|
9
|
+
return f(e.name, e.name_translations, r);
|
|
10
|
+
}
|
|
11
|
+
function L(e, r) {
|
|
12
|
+
return f(e.value, e.value_translations, r);
|
|
13
|
+
}
|
|
14
|
+
function B(e) {
|
|
15
|
+
return e ? e.startsWith("http") ? e : process.env.NEXT_PUBLIC_R2_BASE_URL ? `${process.env.NEXT_PUBLIC_R2_BASE_URL}/${e}` : process.env.NEXT_PUBLIC_SUPABASE_URL ? `${process.env.NEXT_PUBLIC_SUPABASE_URL}/storage/v1/object/public/media/${e}` : e : null;
|
|
16
|
+
}
|
|
17
|
+
function I(e) {
|
|
18
|
+
return e.toLowerCase().trim().replace(/\s+/g, "-").replace(/[^a-z0-9-]/g, "").replace(/-+/g, "-").replace(/^-|-$/g, "");
|
|
19
|
+
}
|
|
20
|
+
function y(e) {
|
|
21
|
+
return [...e].sort().join("__");
|
|
22
|
+
}
|
|
23
|
+
function T(e) {
|
|
24
|
+
return e.map((r) => r.term_value).join(" / ");
|
|
25
|
+
}
|
|
26
|
+
function M(e) {
|
|
27
|
+
return e.length === 0 ? [] : e.reduce(
|
|
28
|
+
(r, i) => r.flatMap((t) => i.map((s) => [...t, s])),
|
|
29
|
+
[[]]
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
function P(e) {
|
|
33
|
+
const {
|
|
34
|
+
baseSku: r,
|
|
35
|
+
basePrice: i,
|
|
36
|
+
basePrices: t = {},
|
|
37
|
+
baseSalePrice: s = null,
|
|
38
|
+
baseSalePrices: n = {},
|
|
39
|
+
selectedAttributes: u,
|
|
40
|
+
previousVariants: c = []
|
|
41
|
+
} = e;
|
|
42
|
+
if (u.length === 0 || u.some((l) => l.terms.length === 0))
|
|
43
|
+
return [];
|
|
44
|
+
const o = new Map(
|
|
45
|
+
c.map((l) => [l.combination_key, l])
|
|
46
|
+
), a = u.map(
|
|
47
|
+
(l) => l.terms.map((d) => ({
|
|
48
|
+
attribute_id: l.attribute_id,
|
|
49
|
+
attribute_name: l.attribute_name,
|
|
50
|
+
term: d
|
|
51
|
+
}))
|
|
52
|
+
);
|
|
53
|
+
return M(a).map((l) => {
|
|
54
|
+
const d = l.map(({ attribute_id: m, attribute_name: k, term: b }) => ({
|
|
55
|
+
attribute_id: m,
|
|
56
|
+
attribute_name: k,
|
|
57
|
+
term_id: b.id,
|
|
58
|
+
term_value: b.value,
|
|
59
|
+
term_slug: b.slug
|
|
60
|
+
})), A = d.map((m) => m.term_id), h = y(A), _ = o.get(h), v = d.map((m) => (m.term_slug ? m.term_slug : I(m.term_value)).toUpperCase()).filter(Boolean).join("-");
|
|
61
|
+
return {
|
|
62
|
+
id: _?.id,
|
|
63
|
+
combination_key: h,
|
|
64
|
+
sku: _?.sku || [r.trim(), v].filter(Boolean).join("-"),
|
|
65
|
+
upc: _?.upc ?? null,
|
|
66
|
+
price: _?.price ?? i,
|
|
67
|
+
prices: _?.prices ?? g(t),
|
|
68
|
+
sale_price: _?.sale_price ?? s,
|
|
69
|
+
sale_prices: _?.sale_prices ?? S(n),
|
|
70
|
+
// IMPORTANT (future agents): every per-variant field a user can edit MUST
|
|
71
|
+
// be carried over from `previous` here. This function re-runs whenever the
|
|
72
|
+
// variations editor mounts or attributes/base prices change, replacing the
|
|
73
|
+
// draft list. If a field is omitted, it silently resets to null on the next
|
|
74
|
+
// regenerate and then gets autosaved away — which is exactly how the sale
|
|
75
|
+
// window (sale_start_at/sale_end_at) used to "revert after publish".
|
|
76
|
+
sale_start_at: _?.sale_start_at ?? null,
|
|
77
|
+
sale_end_at: _?.sale_end_at ?? null,
|
|
78
|
+
stock_quantity: _?.stock_quantity ?? 0,
|
|
79
|
+
main_media_id: _?.main_media_id ?? null,
|
|
80
|
+
main_image_url: _?.main_image_url ?? null,
|
|
81
|
+
attribute_term_ids: A,
|
|
82
|
+
selected_options: d,
|
|
83
|
+
label: T(d)
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
function N(e) {
|
|
88
|
+
const r = {};
|
|
89
|
+
for (const i of e)
|
|
90
|
+
for (const t of i.selected_options ?? []) {
|
|
91
|
+
const s = new Set(r[t.attribute_id] ?? []);
|
|
92
|
+
s.add(t.term_id), r[t.attribute_id] = [...s];
|
|
93
|
+
}
|
|
94
|
+
return r;
|
|
95
|
+
}
|
|
96
|
+
function O(e, r) {
|
|
97
|
+
const i = Object.values(r).filter(Boolean);
|
|
98
|
+
return i.length === 0 ? null : e.find(
|
|
99
|
+
(t) => t.attribute_term_ids.length === i.length && i.every((s) => t.attribute_term_ids.includes(s))
|
|
100
|
+
) ?? null;
|
|
101
|
+
}
|
|
102
|
+
function U(e, r, i) {
|
|
103
|
+
const t = /* @__PURE__ */ new Set();
|
|
104
|
+
for (const s of e) {
|
|
105
|
+
if (!Object.entries(i).every(([c, o]) => !o || c === r ? !0 : s.attribute_term_ids.includes(o)))
|
|
106
|
+
continue;
|
|
107
|
+
const u = s.selected_options.find((c) => c.attribute_id === r);
|
|
108
|
+
u && t.add(u.term_id);
|
|
109
|
+
}
|
|
110
|
+
return t;
|
|
111
|
+
}
|
|
112
|
+
function V(e, r) {
|
|
113
|
+
const i = r.find((t) => t.stock_quantity > 0) ?? r[0] ?? null;
|
|
114
|
+
return i ? i.selected_options.reduce((t, s) => (t[s.attribute_id] = s.term_id, t), {}) : e.reduce((t, s) => {
|
|
115
|
+
const n = s.terms[0];
|
|
116
|
+
return n && (t[s.id] = n.id), t;
|
|
117
|
+
}, {});
|
|
118
|
+
}
|
|
119
|
+
function w(e, r, i) {
|
|
120
|
+
const t = { ...i };
|
|
121
|
+
for (const s of e) {
|
|
122
|
+
const n = U(r, s.id, t);
|
|
123
|
+
if (n.size === 0) {
|
|
124
|
+
delete t[s.id];
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const u = t[s.id];
|
|
128
|
+
if (u && n.has(u))
|
|
129
|
+
continue;
|
|
130
|
+
const c = s.terms.find((o) => n.has(o.id));
|
|
131
|
+
c && (t[s.id] = c.id);
|
|
132
|
+
}
|
|
133
|
+
return t;
|
|
134
|
+
}
|
|
135
|
+
function x(e = [], r) {
|
|
136
|
+
const i = /* @__PURE__ */ new Map(), t = e.map((n) => {
|
|
137
|
+
const u = (n.variant_attribute_mapping || []).map((o) => {
|
|
138
|
+
const a = o.product_attribute_terms, l = a?.product_attributes;
|
|
139
|
+
if (!a || !l)
|
|
140
|
+
return null;
|
|
141
|
+
const d = i.get(l.id) || {
|
|
142
|
+
id: l.id,
|
|
143
|
+
name: f(
|
|
144
|
+
l.name,
|
|
145
|
+
p(l.name_translations),
|
|
146
|
+
r
|
|
147
|
+
),
|
|
148
|
+
slug: l.slug,
|
|
149
|
+
name_translations: p(l.name_translations),
|
|
150
|
+
terms: [],
|
|
151
|
+
_termIds: /* @__PURE__ */ new Set()
|
|
152
|
+
};
|
|
153
|
+
return d._termIds.has(a.id) || (d.terms.push({
|
|
154
|
+
id: a.id,
|
|
155
|
+
attribute_id: a.attribute_id,
|
|
156
|
+
value: f(
|
|
157
|
+
a.value,
|
|
158
|
+
p(a.value_translations),
|
|
159
|
+
r
|
|
160
|
+
),
|
|
161
|
+
slug: a.slug,
|
|
162
|
+
sort_order: a.sort_order ?? null,
|
|
163
|
+
value_translations: p(a.value_translations)
|
|
164
|
+
}), d._termIds.add(a.id)), i.set(l.id, d), {
|
|
165
|
+
attribute_id: l.id,
|
|
166
|
+
attribute_name: f(
|
|
167
|
+
l.name,
|
|
168
|
+
p(l.name_translations),
|
|
169
|
+
r
|
|
170
|
+
),
|
|
171
|
+
term_id: a.id,
|
|
172
|
+
term_value: f(
|
|
173
|
+
a.value,
|
|
174
|
+
p(a.value_translations),
|
|
175
|
+
r
|
|
176
|
+
),
|
|
177
|
+
term_slug: a.slug
|
|
178
|
+
};
|
|
179
|
+
}).filter(
|
|
180
|
+
(o) => o !== null
|
|
181
|
+
).sort(
|
|
182
|
+
(o, a) => o.attribute_name.localeCompare(a.attribute_name)
|
|
183
|
+
), c = u.map((o) => o.term_id);
|
|
184
|
+
return {
|
|
185
|
+
id: n.id,
|
|
186
|
+
combination_key: y(c),
|
|
187
|
+
sku: n.sku,
|
|
188
|
+
upc: n.upc ?? null,
|
|
189
|
+
price: n.price ?? 0,
|
|
190
|
+
prices: g(n.prices),
|
|
191
|
+
sale_price: n.sale_price ?? null,
|
|
192
|
+
sale_prices: S(n.sale_prices),
|
|
193
|
+
sale_start_at: n.sale_start_at ?? null,
|
|
194
|
+
sale_end_at: n.sale_end_at ?? null,
|
|
195
|
+
scheduled_price: n.scheduled_price ?? null,
|
|
196
|
+
scheduled_prices: g(n.scheduled_prices),
|
|
197
|
+
scheduled_price_at: n.scheduled_price_at ?? null,
|
|
198
|
+
stock_quantity: n.stock_quantity ?? 0,
|
|
199
|
+
main_media_id: n.main_media_id ?? null,
|
|
200
|
+
image_url: B(
|
|
201
|
+
n.media?.file_path ?? n.media?.object_key ?? null
|
|
202
|
+
),
|
|
203
|
+
attribute_term_ids: c,
|
|
204
|
+
selected_options: u,
|
|
205
|
+
label: T(u)
|
|
206
|
+
};
|
|
207
|
+
});
|
|
208
|
+
return { attributes: [...i.values()].map((n) => ({
|
|
209
|
+
id: n.id,
|
|
210
|
+
name: n.name,
|
|
211
|
+
slug: n.slug,
|
|
212
|
+
name_translations: n.name_translations,
|
|
213
|
+
terms: [...n.terms].sort((u, c) => {
|
|
214
|
+
const o = u.sort_order ?? Number.MAX_SAFE_INTEGER, a = c.sort_order ?? Number.MAX_SAFE_INTEGER;
|
|
215
|
+
return o !== a ? o - a : u.value.localeCompare(c.value);
|
|
216
|
+
})
|
|
217
|
+
})).sort((n, u) => n.name.localeCompare(u.name)), variants: t };
|
|
218
|
+
}
|
|
219
|
+
function z(e, r) {
|
|
220
|
+
if (!e.length)
|
|
221
|
+
return null;
|
|
222
|
+
const i = e.map((t) => E({
|
|
223
|
+
saleStartAt: t.sale_start_at,
|
|
224
|
+
saleEndAt: t.sale_end_at,
|
|
225
|
+
now: r
|
|
226
|
+
}) && typeof t.sale_price == "number" ? t.sale_price : t.price);
|
|
227
|
+
return {
|
|
228
|
+
min: Math.min(...i),
|
|
229
|
+
max: Math.max(...i)
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
export {
|
|
233
|
+
y as buildCombinationKey,
|
|
234
|
+
T as buildVariantLabel,
|
|
235
|
+
V as chooseInitialVariantSelections,
|
|
236
|
+
N as extractSelectedTermsByAttribute,
|
|
237
|
+
O as findMatchingVariant,
|
|
238
|
+
P as generateVariantDrafts,
|
|
239
|
+
U as getAvailableTermIdsForAttribute,
|
|
240
|
+
z as getVariantEffectivePriceRange,
|
|
241
|
+
x as mapRawVariantRelations,
|
|
242
|
+
w as normalizeSelectionsToAvailableVariants,
|
|
243
|
+
j as resolveAttributeName,
|
|
244
|
+
L as resolveTermValue,
|
|
245
|
+
f as resolveTranslatedText
|
|
246
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("zod");e.z.config({jitless:!0});Object.defineProperty(exports,"z",{enumerable:!0,get:()=>e.z});
|