@nextblock-cms/ecom 0.21.3 → 0.22.1

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.
Files changed (64) hide show
  1. package/index.cjs +1 -1
  2. package/index.es.js +31 -31
  3. package/lib/components/Checkout.cjs +1 -1
  4. package/lib/components/Checkout.es.js +2 -1
  5. package/lib/coupon-server.cjs +1 -1
  6. package/lib/coupon-server.d.ts +65 -1
  7. package/lib/coupon-server.es.js +151 -91
  8. package/lib/coupons.cjs +1 -1
  9. package/lib/coupons.d.ts +13 -0
  10. package/lib/coupons.es.js +5 -4
  11. package/lib/invoice-server.cjs +2 -2
  12. package/lib/invoice-server.es.js +22 -22
  13. package/lib/order-access-token.cjs +1 -0
  14. package/lib/order-access-token.d.ts +2 -0
  15. package/lib/order-access-token.es.js +18 -0
  16. package/lib/pages/cms/orders/actions.cjs +3 -3
  17. package/lib/pages/cms/orders/actions.es.js +48 -26
  18. package/lib/pages/cms/orders/export-actions.cjs +1 -1
  19. package/lib/pages/cms/orders/export-actions.d.ts +42 -1
  20. package/lib/pages/cms/orders/export-actions.es.js +18 -16
  21. package/lib/pages/cms/orders/server-actions.cjs +1 -1
  22. package/lib/pages/cms/orders/server-actions.es.js +25 -15
  23. package/lib/pages/cms/products/actions.cjs +4 -4
  24. package/lib/pages/cms/products/actions.es.js +44 -41
  25. package/lib/pages/cms/products/inventory/actions.cjs +2 -2
  26. package/lib/pages/cms/products/inventory/actions.es.js +23 -23
  27. package/lib/pages/cms/products/server-actions.cjs +1 -1
  28. package/lib/pages/cms/products/server-actions.es.js +109 -108
  29. package/lib/pages/cms/shipping/server-actions.cjs +1 -1
  30. package/lib/pages/cms/shipping/server-actions.es.js +27 -26
  31. package/lib/payment-config.cjs +1 -1
  32. package/lib/payment-config.d.ts +4 -2
  33. package/lib/payment-config.es.js +73 -72
  34. package/lib/product-actions.cjs +1 -1
  35. package/lib/product-actions.es.js +6 -6
  36. package/lib/providers/checkout-items.cjs +1 -0
  37. package/lib/providers/checkout-items.d.ts +30 -0
  38. package/lib/providers/checkout-items.es.js +43 -0
  39. package/lib/providers/freemius.cjs +1 -1
  40. package/lib/providers/freemius.d.ts +1 -1
  41. package/lib/providers/freemius.es.js +166 -144
  42. package/lib/providers/stripe.cjs +1 -1
  43. package/lib/providers/stripe.d.ts +1 -1
  44. package/lib/providers/stripe.es.js +191 -146
  45. package/lib/server-actions/coupon-actions.cjs +1 -1
  46. package/lib/server-actions/coupon-actions.d.ts +11 -12
  47. package/lib/server-actions/coupon-actions.es.js +19 -11
  48. package/lib/server-actions/require-ecom-role.cjs +1 -0
  49. package/lib/server-actions/require-ecom-role.d.ts +5825 -0
  50. package/lib/server-actions/require-ecom-role.es.js +24 -0
  51. package/lib/server-actions/shipping-actions.cjs +1 -1
  52. package/lib/server-actions/shipping-actions.es.js +4 -4
  53. package/lib/server-actions/tax-actions.cjs +1 -1
  54. package/lib/server-actions/tax-actions.es.js +22 -18
  55. package/lib/shipping/checkout-shipping-method.cjs +7 -0
  56. package/lib/shipping/checkout-shipping-method.d.ts +115 -0
  57. package/lib/shipping/checkout-shipping-method.es.js +167 -0
  58. package/lib/shipping/resolver.cjs +1 -7
  59. package/lib/shipping/resolver.d.ts +7 -12
  60. package/lib/shipping/resolver.es.js +13 -38
  61. package/package.json +4 -4
  62. package/server.cjs +1 -1
  63. package/server.d.ts +1 -0
  64. package/server.es.js +49 -48
@@ -1,58 +1,59 @@
1
1
  import { getDefaultCurrency as e, normalizeCurrencyRecord as t, normalizePriceMap as n, normalizeSalePriceMap as r, resolveEffectivePriceForCurrency as i } from "./currency.es.js";
2
- import { emptyProviderDiscounts as a, getCartLineCouponKey as o, normalizeCouponCode as s } from "./coupons.es.js";
2
+ import { COUPON_INVALID_ERROR_KEY as a, COUPON_INVALID_ERROR_MESSAGE as o, emptyProviderDiscounts as s, getCartLineCouponKey as c, normalizeCouponCode as l } from "./coupons.es.js";
3
+ import "server-only";
3
4
  //#region src/lib/coupon-server.ts
4
- function c(e) {
5
+ function u(e) {
5
6
  if (!e) return null;
6
7
  let t = new Date(e);
7
8
  return Number.isNaN(t.getTime()) ? null : t;
8
9
  }
9
- function l(e, t) {
10
+ function d(e, t) {
10
11
  return e === "all" || e === t;
11
12
  }
12
- function u(e) {
13
+ function f(e) {
13
14
  return e === "stripe" || e === "freemius" ? e : null;
14
15
  }
15
- async function d(e) {
16
+ async function p(e) {
16
17
  let { data: n, error: r } = await e.from("currencies").select("code, symbol, exchange_rate, is_default, is_active, auto_sync_product_prices, auto_update_exchange_rate, exchange_rate_source, exchange_rate_updated_at, rounding_mode, rounding_increment, rounding_charm_amount").eq("is_active", !0).order("code", { ascending: !0 });
17
18
  if (r) throw Error(r.message);
18
19
  return (n || []).map((e) => t(e));
19
20
  }
20
- async function f(t) {
21
- let { client: a, items: s } = t, c = await d(a);
22
- if (c.length === 0) throw Error("No active currencies are configured.");
23
- let l = c.find((e) => e.code === (t.currencyCode || "").toUpperCase()) ?? e(c), f = [...new Set(s.map((e) => e.product_id).filter(Boolean))], p = [...new Set(s.map((e) => e.variant_id).filter((e) => !!e))];
24
- if (f.length === 0) return {
21
+ async function m(t) {
22
+ let { client: a, items: o } = t, s = await p(a);
23
+ if (s.length === 0) throw Error("No active currencies are configured.");
24
+ let l = s.find((e) => e.code === (t.currencyCode || "").toUpperCase()) ?? e(s), u = [...new Set(o.map((e) => e.product_id).filter(Boolean))], d = [...new Set(o.map((e) => e.variant_id).filter((e) => !!e))];
25
+ if (u.length === 0) return {
25
26
  lines: [],
26
- currencies: c,
27
+ currencies: s,
27
28
  selectedCurrency: l
28
29
  };
29
- let [{ data: m, error: h }, g] = await Promise.all([a.from("products").select("id, title, price, prices, sale_price, sale_prices, sale_start_at, sale_end_at, scheduled_price, scheduled_prices, scheduled_price_at, product_type, payment_provider, freemius_product_id, freemius_plan_id").in("id", f), p.length ? a.from("product_variants").select("id, product_id, price, prices, sale_price, sale_prices, sale_start_at, sale_end_at, scheduled_price, scheduled_prices, scheduled_price_at").in("id", p) : Promise.resolve({
30
+ let [{ data: m, error: h }, g] = await Promise.all([a.from("products").select("id, title, price, prices, sale_price, sale_prices, sale_start_at, sale_end_at, scheduled_price, scheduled_prices, scheduled_price_at, product_type, payment_provider, freemius_product_id, freemius_plan_id").in("id", u), d.length ? a.from("product_variants").select("id, product_id, price, prices, sale_price, sale_prices, sale_start_at, sale_end_at, scheduled_price, scheduled_prices, scheduled_price_at").in("id", d) : Promise.resolve({
30
31
  data: [],
31
32
  error: null
32
33
  })]);
33
34
  if (h) throw Error(h.message);
34
35
  if (g.error) throw Error(g.error.message);
35
36
  let _ = m || [], v = g.data || [], y = new Map(_.map((e) => [e.id, e])), b = new Map(v.map((e) => [e.id, e])), x = [];
36
- for (let e of s) {
37
+ for (let e of o) {
37
38
  let t = y.get(e.product_id);
38
39
  if (!t) continue;
39
- let a = u(t.payment_provider) ?? (t.product_type === "digital" ? "freemius" : "stripe"), s = e.variant_id ? b.get(e.variant_id) : null, d = s && s.product_id === t.id ? s : t, f = i({
40
- prices: n(d.prices),
41
- salePrices: r(d.sale_prices),
42
- fallbackPrice: d.price,
43
- fallbackSalePrice: d.sale_price,
44
- saleStartAt: d.sale_start_at,
45
- saleEndAt: d.sale_end_at,
46
- scheduledPrice: d.scheduled_price,
47
- scheduledPrices: n(d.scheduled_prices),
48
- scheduledPriceAt: d.scheduled_price_at,
40
+ let a = f(t.payment_provider) ?? (t.product_type === "digital" ? "freemius" : "stripe"), o = e.variant_id ? b.get(e.variant_id) : null, u = o && o.product_id === t.id ? o : t, d = i({
41
+ prices: n(u.prices),
42
+ salePrices: r(u.sale_prices),
43
+ fallbackPrice: u.price,
44
+ fallbackSalePrice: u.sale_price,
45
+ saleStartAt: u.sale_start_at,
46
+ saleEndAt: u.sale_end_at,
47
+ scheduledPrice: u.scheduled_price,
48
+ scheduledPrices: n(u.scheduled_prices),
49
+ scheduledPriceAt: u.scheduled_price_at,
49
50
  currencyCode: l.code,
50
- currencies: c
51
- }), p = a === "freemius" ? 1 : Math.max(1, Number(e.quantity) || 1), m = Math.max(0, f.sale_price ?? f.price);
51
+ currencies: s
52
+ }), p = a === "freemius" ? 1 : Math.max(1, Number(e.quantity) || 1), m = Math.max(0, d.sale_price ?? d.price);
52
53
  x.push({
53
- key: o(e),
54
+ key: c(e),
54
55
  product_id: t.id,
55
- variant_id: s?.id ?? null,
56
+ variant_id: o?.id ?? null,
56
57
  title: t.title,
57
58
  quantity: p,
58
59
  provider: a,
@@ -63,16 +64,16 @@ async function f(t) {
63
64
  }
64
65
  return {
65
66
  lines: x,
66
- currencies: c,
67
+ currencies: s,
67
68
  selectedCurrency: l
68
69
  };
69
70
  }
70
- async function p(e, t) {
71
+ async function h(e, t) {
71
72
  let { data: n, error: r } = await e.from("coupon_products").select("product_id").eq("coupon_id", t);
72
73
  if (r) throw Error(r.message);
73
74
  return new Set((n || []).map((e) => e.product_id));
74
75
  }
75
- function m(e, t) {
76
+ function g(e, t) {
76
77
  let n = e.reduce((e, t) => e + t.subtotal, 0), r = Math.min(t, n);
77
78
  if (n <= 0 || r <= 0) return /* @__PURE__ */ new Map();
78
79
  let i = /* @__PURE__ */ new Map(), a = 0;
@@ -81,59 +82,70 @@ function m(e, t) {
81
82
  a += s, i.set(t.key, s);
82
83
  }), i;
83
84
  }
84
- async function h(e) {
85
- let t = s(e.code);
86
- if (!t) return {
87
- success: !1,
88
- error: "Enter a coupon code.",
89
- errorKey: "ecommerce.coupon_code_required"
90
- };
91
- if (!e.items.length) return {
92
- success: !1,
93
- error: "Add an item to your cart before applying a coupon.",
94
- errorKey: "ecommerce.coupon_cart_empty"
95
- };
96
- let { data: n, error: r } = await e.client.from("coupons").select("id, code, name, provider_scope, discount_type, discount_amount, is_active, starts_at, ends_at, redemption_limit, redemptions_count").ilike("code", t).maybeSingle();
97
- if (r) throw Error(r.message);
98
- if (!n) return {
99
- success: !1,
85
+ var _ = {
86
+ not_found: {
100
87
  error: "Coupon code not found.",
101
88
  errorKey: "ecommerce.coupon_not_found"
102
- };
103
- let i = n, o = /* @__PURE__ */ new Date(), u = c(i.starts_at), d = c(i.ends_at);
104
- if (!i.is_active) return {
105
- success: !1,
89
+ },
90
+ inactive: {
106
91
  error: "This coupon is not active.",
107
92
  errorKey: "ecommerce.coupon_inactive"
108
- };
109
- if (u && u > o) return {
110
- success: !1,
93
+ },
94
+ not_started: {
111
95
  error: "This coupon is not active yet.",
112
96
  errorKey: "ecommerce.coupon_not_started"
113
- };
114
- if (d && d <= o) return {
115
- success: !1,
97
+ },
98
+ expired: {
116
99
  error: "This coupon has expired.",
117
100
  errorKey: "ecommerce.coupon_expired"
118
- };
119
- if (i.redemption_limit !== null && (i.redemptions_count ?? 0) >= i.redemption_limit) return {
120
- success: !1,
101
+ },
102
+ limit_reached: {
121
103
  error: "This coupon has reached its redemption limit.",
122
104
  errorKey: "ecommerce.coupon_limit_reached"
105
+ },
106
+ not_applicable: {
107
+ error: "This coupon does not apply to the items in your cart.",
108
+ errorKey: "ecommerce.coupon_not_applicable"
109
+ }
110
+ };
111
+ function v(e, t) {
112
+ return {
113
+ success: !1,
114
+ ...t ? _[e] : {
115
+ error: o,
116
+ errorKey: a
117
+ }
123
118
  };
124
- let [{ lines: h }, g] = await Promise.all([f({
119
+ }
120
+ async function y(e) {
121
+ let t = l(e.code), n = e.detailedErrors === !0;
122
+ if (!t) return {
123
+ success: !1,
124
+ error: "Enter a coupon code.",
125
+ errorKey: "ecommerce.coupon_code_required"
126
+ };
127
+ if (!e.items.length) return {
128
+ success: !1,
129
+ error: "Add an item to your cart before applying a coupon.",
130
+ errorKey: "ecommerce.coupon_cart_empty"
131
+ };
132
+ let { data: r, error: i } = await e.client.from("coupons").select("id, code, name, provider_scope, discount_type, discount_amount, is_active, starts_at, ends_at, redemption_limit, redemptions_count").ilike("code", t).maybeSingle();
133
+ if (i) throw Error(i.message);
134
+ if (!r) return v("not_found", n);
135
+ let a = r, o = /* @__PURE__ */ new Date(), c = u(a.starts_at), f = u(a.ends_at);
136
+ if (!a.is_active) return v("inactive", n);
137
+ if (c && c > o) return v("not_started", n);
138
+ if (f && f <= o) return v("expired", n);
139
+ if (a.redemption_limit !== null && (a.redemptions_count ?? 0) >= a.redemption_limit) return v("limit_reached", n);
140
+ let [{ lines: p }, _] = await Promise.all([m({
125
141
  client: e.client,
126
142
  items: e.items,
127
143
  currencyCode: e.currencyCode
128
- }), p(e.client, i.id)]), _ = g.size > 0, v = h.filter((e) => e.subtotal > 0 && l(i.provider_scope, e.provider) && (!_ || g.has(e.product_id)));
129
- if (v.length === 0) return {
130
- success: !1,
131
- error: "This coupon does not apply to the items in your cart.",
132
- errorKey: "ecommerce.coupon_not_applicable"
133
- };
134
- let y = i.discount_type === "fixed" ? m(v, i.discount_amount) : null, b = a(), x = v.map((e) => {
135
- let t = i.discount_type === "percent" ? Math.min(e.subtotal, Math.round(e.subtotal * i.discount_amount / 100)) : y?.get(e.key) ?? 0;
136
- return b[e.provider] += t, {
144
+ }), h(e.client, a.id)]), y = _.size > 0, b = p.filter((e) => e.subtotal > 0 && d(a.provider_scope, e.provider) && (!y || _.has(e.product_id)));
145
+ if (b.length === 0) return v("not_applicable", n);
146
+ let x = a.discount_type === "fixed" ? g(b, a.discount_amount) : null, S = s(), C = b.map((e) => {
147
+ let t = a.discount_type === "percent" ? Math.min(e.subtotal, Math.round(e.subtotal * a.discount_amount / 100)) : x?.get(e.key) ?? 0;
148
+ return S[e.provider] += t, {
137
149
  key: e.key,
138
150
  product_id: e.product_id,
139
151
  variant_id: e.variant_id,
@@ -147,26 +159,40 @@ async function h(e) {
147
159
  return {
148
160
  success: !0,
149
161
  quote: {
150
- couponId: i.id,
151
- code: i.code,
152
- name: i.name,
153
- discountType: i.discount_type,
154
- discountAmount: i.discount_amount,
155
- providerScope: i.provider_scope,
156
- eligibleSubtotal: v.reduce((e, t) => e + t.subtotal, 0),
157
- discountTotal: b.stripe + b.freemius,
158
- providerDiscounts: b,
159
- lineDiscounts: x
162
+ couponId: a.id,
163
+ code: a.code,
164
+ name: a.name,
165
+ discountType: a.discount_type,
166
+ discountAmount: a.discount_amount,
167
+ providerScope: a.provider_scope,
168
+ eligibleSubtotal: b.reduce((e, t) => e + t.subtotal, 0),
169
+ discountTotal: S.stripe + S.freemius,
170
+ providerDiscounts: S,
171
+ lineDiscounts: C
160
172
  }
161
173
  };
162
174
  }
163
- function g(e) {
175
+ function b(e) {
164
176
  let t = /* @__PURE__ */ new Map();
165
177
  for (let n of e?.lineDiscounts ?? []) t.set(n.key, n.discount);
166
178
  return t;
167
179
  }
168
- async function _(e) {
169
- let { error: t } = await e.client.from("coupon_redemptions").insert({
180
+ async function x(e) {
181
+ let t = (e) => ({
182
+ success: !1,
183
+ reason: e,
184
+ error: o,
185
+ errorKey: a
186
+ }), { data: n, error: r } = await e.client.from("coupon_redemptions").select("id").eq("order_id", e.orderId).maybeSingle();
187
+ if (r) return console.error("Failed to look up existing coupon redemption:", r), t("error");
188
+ if (n) return {
189
+ success: !0,
190
+ alreadyRecorded: !0
191
+ };
192
+ let { data: i, error: s } = await e.client.rpc("redeem_coupon_atomically", { p_coupon_id: e.quote.couponId });
193
+ if (s) return console.error("Failed to reserve coupon redemption:", s), t("error");
194
+ if (i !== !0) return t("limit_reached");
195
+ let { error: c } = await e.client.from("coupon_redemptions").insert({
170
196
  coupon_id: e.quote.couponId,
171
197
  order_id: e.orderId,
172
198
  coupon_code: e.quote.code,
@@ -176,15 +202,49 @@ async function _(e) {
176
202
  customer_email: e.customerEmail || null,
177
203
  metadata: e.metadata || {}
178
204
  });
179
- if (t) {
180
- console.error("Failed to record coupon redemption:", t);
181
- return;
205
+ return c && console.error("Failed to record coupon redemption:", c), {
206
+ success: !0,
207
+ alreadyRecorded: !1
208
+ };
209
+ }
210
+ var S = 30, C = 12 / 60, w = 5e3, T = "unknown";
211
+ function E(e) {
212
+ let t = e?.capacity ?? S, n = e?.refillPerSecond ?? C, r = e?.maxBuckets ?? w, i = t / n * 1e3, a = /* @__PURE__ */ new Map();
213
+ function o(e) {
214
+ for (let [t, n] of a) e - n.updatedAt >= i && a.delete(t);
215
+ for (let e of a.keys()) {
216
+ if (a.size < r) break;
217
+ a.delete(e);
218
+ }
182
219
  }
183
- let { data: n } = await e.client.from("coupons").select("redemptions_count").eq("id", e.quote.couponId).single();
184
- await e.client.from("coupons").update({
185
- redemptions_count: (n?.redemptions_count ?? 0) + 1,
186
- updated_at: (/* @__PURE__ */ new Date()).toISOString()
187
- }).eq("id", e.quote.couponId);
220
+ return {
221
+ consume(e, i = Date.now()) {
222
+ let s = a.get(e);
223
+ if (!s) a.size >= r && o(i), s = {
224
+ tokens: t,
225
+ updatedAt: i
226
+ }, a.set(e, s);
227
+ else {
228
+ let e = Math.max(0, i - s.updatedAt) / 1e3;
229
+ s.tokens = Math.min(t, s.tokens + e * n), s.updatedAt = i;
230
+ }
231
+ return s.tokens < 1 ? !1 : (--s.tokens, !0);
232
+ },
233
+ get size() {
234
+ return a.size;
235
+ }
236
+ };
237
+ }
238
+ var D = E();
239
+ function O(e, t) {
240
+ return D.consume(e, t);
241
+ }
242
+ function k(e) {
243
+ let [t] = (e ?? "").split(",");
244
+ return (t?.trim() ?? "") || null;
245
+ }
246
+ function A(e) {
247
+ return k(e.get("x-vercel-forwarded-for")) ?? k(e.get("x-real-ip")) ?? k(e.get("x-forwarded-for")) ?? "unknown";
188
248
  }
189
249
  //#endregion
190
- export { h as getCouponQuote, g as getQuoteLineDiscountMap, _ as recordCouponRedemption };
250
+ export { T as COUPON_VALIDATION_UNKNOWN_CLIENT_KEY, O as consumeCouponValidationToken, E as createCouponValidationThrottle, y as getCouponQuote, b as getQuoteLineDiscountMap, x as recordCouponRedemption, A as resolveCouponValidationThrottleKey };
package/lib/coupons.cjs CHANGED
@@ -1 +1 @@
1
- function e(e){return(e||``).trim().replace(/\s+/g,``).toUpperCase()}function t(e){return`${e.product_id}:${e.variant_id||`base`}`}function n(){return{stripe:0,freemius:0}}exports.emptyProviderDiscounts=n,exports.getCartLineCouponKey=t,exports.normalizeCouponCode=e;
1
+ var e=`ecommerce.coupon_invalid`,t=`This coupon code is invalid or can't be applied to your cart.`,n=`ecommerce.coupon_validation_failed`,r=`Too many coupon attempts. Wait a moment and try again.`;function i(e){return(e||``).trim().replace(/\s+/g,``).toUpperCase()}function a(e){return`${e.product_id}:${e.variant_id||`base`}`}function o(){return{stripe:0,freemius:0}}exports.COUPON_INVALID_ERROR_KEY=e,exports.COUPON_INVALID_ERROR_MESSAGE=t,exports.COUPON_THROTTLED_ERROR_KEY=n,exports.COUPON_THROTTLED_ERROR_MESSAGE=r,exports.emptyProviderDiscounts=o,exports.getCartLineCouponKey=a,exports.normalizeCouponCode=i;
package/lib/coupons.d.ts CHANGED
@@ -36,6 +36,19 @@ export type CouponQuoteResult = {
36
36
  error: string;
37
37
  errorKey?: string;
38
38
  };
39
+ /**
40
+ * The one message a shopper sees for every coupon the store will not honour: unknown code,
41
+ * inactive, not started, expired, exhausted, or not matching the cart. One key for all of them
42
+ * so the public validation action cannot be used to confirm which codes exist.
43
+ *
44
+ * Seed EN + FR for this key in a forward migration (`02015` is the model); until then the
45
+ * client falls back to the English `error` text.
46
+ */
47
+ export declare const COUPON_INVALID_ERROR_KEY = "ecommerce.coupon_invalid";
48
+ export declare const COUPON_INVALID_ERROR_MESSAGE = "This coupon code is invalid or can't be applied to your cart.";
49
+ /** Returned by the public validation action once a client has used up its attempts. */
50
+ export declare const COUPON_THROTTLED_ERROR_KEY = "ecommerce.coupon_validation_failed";
51
+ export declare const COUPON_THROTTLED_ERROR_MESSAGE = "Too many coupon attempts. Wait a moment and try again.";
39
52
  export declare function normalizeCouponCode(code?: string | null): string;
40
53
  export declare function getCartLineCouponKey(item: Pick<CartItem, 'product_id' | 'variant_id'>): string;
41
54
  export declare function emptyProviderDiscounts(): Record<EcommercePaymentProvider, number>;
package/lib/coupons.es.js CHANGED
@@ -1,15 +1,16 @@
1
1
  //#region src/lib/coupons.ts
2
- function e(e) {
2
+ var e = "ecommerce.coupon_invalid", t = "This coupon code is invalid or can't be applied to your cart.", n = "ecommerce.coupon_validation_failed", r = "Too many coupon attempts. Wait a moment and try again.";
3
+ function i(e) {
3
4
  return (e || "").trim().replace(/\s+/g, "").toUpperCase();
4
5
  }
5
- function t(e) {
6
+ function a(e) {
6
7
  return `${e.product_id}:${e.variant_id || "base"}`;
7
8
  }
8
- function n() {
9
+ function o() {
9
10
  return {
10
11
  stripe: 0,
11
12
  freemius: 0
12
13
  };
13
14
  }
14
15
  //#endregion
15
- export { n as emptyProviderDiscounts, t as getCartLineCouponKey, e as normalizeCouponCode };
16
+ export { e as COUPON_INVALID_ERROR_KEY, t as COUPON_INVALID_ERROR_MESSAGE, n as COUPON_THROTTLED_ERROR_KEY, r as COUPON_THROTTLED_ERROR_MESSAGE, o as emptyProviderDiscounts, a as getCartLineCouponKey, i as normalizeCouponCode };
@@ -7,7 +7,7 @@ const e=require("./customer.cjs"),t=require("./invoice.cjs"),n=require("./order-
7
7
  width,
8
8
  height
9
9
  )
10
- `;async function l(e){let{data:t}=await e.from(`site_settings`).select(`value`).eq(`key`,s).maybeSingle(),n=t?.value,r=typeof n==`string`&&n.length>0?n:null;if(r){let{data:t}=await e.from(`logos`).select(c).eq(`id`,r).maybeSingle();if(t)return t}let{data:i}=await e.from(`logos`).select(c).order(`created_at`,{ascending:!1}).limit(1).maybeSingle();return i??null}async function u(e){let n=a(e),[r,i]=await Promise.all([n.from(`site_settings`).select(`value`).eq(`key`,t.INVOICE_SETTINGS_KEY).maybeSingle(),l(n)]);return{settings:t.normalizeInvoiceSettings(r.data?.value??t.DEFAULT_INVOICE_SETTINGS),logo:o(i)}}async function d(e){let{data:t,error:n}=await a(e.client).rpc(`assign_order_invoice_metadata`,{p_order_id:e.orderId,p_paid_at:e.paidAt??null}).single();if(n)throw Error(n.message);return{invoiceNumber:t?.invoice_number??null,paidAt:t?.paid_at??null}}async function f(t,r){let i=a(r),{data:o,error:s}=await i.from(`orders`).select(`
10
+ `;async function l(e){let{data:t}=await e.from(`site_settings`).select(`value`).eq(`key`,s).maybeSingle(),n=t?.value,r=typeof n==`string`&&n.length>0?n:null;if(r){let{data:t}=await e.from(`logos`).select(c).eq(`id`,r).maybeSingle();if(t)return t}let{data:i}=await e.from(`logos`).select(c).order(`created_at`,{ascending:!1}).limit(1).maybeSingle();return i??null}async function u(e){let n=a(e),[r,i]=await Promise.all([n.from(`site_settings`).select(`value`).eq(`key`,t.INVOICE_SETTINGS_KEY).maybeSingle(),l(n)]);return{settings:t.normalizeInvoiceSettings(r.data?.value??t.DEFAULT_INVOICE_SETTINGS),logo:o(i)}}async function d(e){let{data:t,error:n}=await a(e.client).rpc(`assign_order_invoice_metadata`,{p_order_id:e.orderId,p_paid_at:e.paidAt??null}).single();if(n)throw Error(n.message);return{invoiceNumber:t?.invoice_number??null,paidAt:t?.paid_at??null}}async function f(t,i){let{data:o,error:s}=await a(i).from(`orders`).select(`
11
11
  id,
12
12
  invoice_number,
13
13
  paid_at,
@@ -30,4 +30,4 @@ const e=require("./customer.cjs"),t=require("./invoice.cjs"),n=require("./order-
30
30
  quantity,
31
31
  price_at_purchase
32
32
  )
33
- `).eq(`id`,t).single();if(s||!o)throw Error(s?.message||`Order not found`);let c=(o.order_items||[]).map(e=>e.product_id).filter(Boolean),l=(o.order_items||[]).map(e=>e.variant_id).filter(Boolean),[u,d]=await Promise.all([c.length?i.from(`products`).select(`id, title, sku`).in(`id`,c):Promise.resolve({data:[],error:null}),l.length?i.from(`product_variants`).select(`id, sku`).in(`id`,l):Promise.resolve({data:[],error:null})]),f=new Map((u.data||[]).map(e=>[e.id,e])),p=new Map((d.data||[]).map(e=>[e.id,e])),m=(o.order_items||[]).map(e=>{let t=e.product_id?f.get(e.product_id):null,n=e.variant_id?p.get(e.variant_id):null,r=t?.title||`Product`,i=n?.sku?`SKU: ${n.sku}`:t?.sku?`SKU: ${t.sku}`:null;return{id:e.id,product_id:e.product_id??null,variant_id:e.variant_id??null,title:r,description:i,quantity:e.quantity,unit_amount:e.price_at_purchase,total_amount:e.price_at_purchase*e.quantity,sku:n?.sku??t?.sku??null}});return{id:o.id,invoice_number:o.invoice_number??null,paid_at:o.paid_at??null,created_at:o.created_at??null,currency:o.currency||`usd`,status:o.status,provider:o.provider??null,subtotal:typeof o.subtotal==`number`?o.subtotal:m.reduce((e,t)=>e+t.total_amount,0),shipping_total:typeof o.shipping_total==`number`?o.shipping_total:0,discount_total:typeof o.discount_total==`number`?o.discount_total:0,coupon_code:o.coupon_code??null,tax_total:typeof o.tax_total==`number`?o.tax_total:0,total:o.total,customer_details:e.normalizeOrderCustomerDetails(o.customer_details??{}),tax_details:n.normalizeOrderTaxDetails(o.tax_details),items:m}}async function p(e,t){let n=a(t),[r,i]=await Promise.all([f(e,n),u(n)]);return{order:r,settings:i.settings,logo:i.logo}}exports.assignInvoiceMetadata=d,exports.getInvoiceBrandingData=u,exports.getInvoiceOrder=f,exports.getInvoicePresentationData=p;
33
+ `).eq(`id`,t).single();if(s||!o)throw Error(s?.message||`Order not found`);let c=(o.order_items||[]).map(e=>e.product_id).filter(Boolean),l=(o.order_items||[]).map(e=>e.variant_id).filter(Boolean),u=(0,r.getServiceRoleSupabaseClient)(),[d,f]=await Promise.all([c.length?u.from(`products`).select(`id, title, sku`).in(`id`,c):Promise.resolve({data:[],error:null}),l.length?u.from(`product_variants`).select(`id, sku`).in(`id`,l):Promise.resolve({data:[],error:null})]),p=new Map((d.data||[]).map(e=>[e.id,e])),m=new Map((f.data||[]).map(e=>[e.id,e])),h=(o.order_items||[]).map(e=>{let t=e.product_id?p.get(e.product_id):null,n=e.variant_id?m.get(e.variant_id):null,r=t?.title||`Product`,i=n?.sku?`SKU: ${n.sku}`:t?.sku?`SKU: ${t.sku}`:null;return{id:e.id,product_id:e.product_id??null,variant_id:e.variant_id??null,title:r,description:i,quantity:e.quantity,unit_amount:e.price_at_purchase,total_amount:e.price_at_purchase*e.quantity,sku:n?.sku??t?.sku??null}});return{id:o.id,invoice_number:o.invoice_number??null,paid_at:o.paid_at??null,created_at:o.created_at??null,currency:o.currency||`usd`,status:o.status,provider:o.provider??null,subtotal:typeof o.subtotal==`number`?o.subtotal:h.reduce((e,t)=>e+t.total_amount,0),shipping_total:typeof o.shipping_total==`number`?o.shipping_total:0,discount_total:typeof o.discount_total==`number`?o.discount_total:0,coupon_code:o.coupon_code??null,tax_total:typeof o.tax_total==`number`?o.tax_total:0,total:o.total,customer_details:e.normalizeOrderCustomerDetails(o.customer_details??{}),tax_details:n.normalizeOrderTaxDetails(o.tax_details),items:h}}async function p(e,t){let n=a(t),[r,i]=await Promise.all([f(e,n),u(n)]);return{order:r,settings:i.settings,logo:i.logo}}exports.assignInvoiceMetadata=d,exports.getInvoiceBrandingData=u,exports.getInvoiceOrder=f,exports.getInvoicePresentationData=p;
@@ -49,16 +49,16 @@ async function p(e) {
49
49
  };
50
50
  }
51
51
  async function m(t, n) {
52
- let r = s(n), { data: a, error: o } = await r.from("orders").select("\n id,\n invoice_number,\n paid_at,\n created_at,\n currency,\n status,\n provider,\n subtotal,\n shipping_total,\n discount_total,\n coupon_code,\n tax_total,\n total,\n customer_details,\n tax_details,\n order_items (\n id,\n product_id,\n variant_id,\n quantity,\n price_at_purchase\n )\n ").eq("id", t).single();
53
- if (o || !a) throw Error(o?.message || "Order not found");
54
- let c = (a.order_items || []).map((e) => e.product_id).filter(Boolean), l = (a.order_items || []).map((e) => e.variant_id).filter(Boolean), [u, d] = await Promise.all([c.length ? r.from("products").select("id, title, sku").in("id", c) : Promise.resolve({
52
+ let { data: r, error: o } = await s(n).from("orders").select("\n id,\n invoice_number,\n paid_at,\n created_at,\n currency,\n status,\n provider,\n subtotal,\n shipping_total,\n discount_total,\n coupon_code,\n tax_total,\n total,\n customer_details,\n tax_details,\n order_items (\n id,\n product_id,\n variant_id,\n quantity,\n price_at_purchase\n )\n ").eq("id", t).single();
53
+ if (o || !r) throw Error(o?.message || "Order not found");
54
+ let c = (r.order_items || []).map((e) => e.product_id).filter(Boolean), l = (r.order_items || []).map((e) => e.variant_id).filter(Boolean), u = a(), [d, f] = await Promise.all([c.length ? u.from("products").select("id, title, sku").in("id", c) : Promise.resolve({
55
55
  data: [],
56
56
  error: null
57
- }), l.length ? r.from("product_variants").select("id, sku").in("id", l) : Promise.resolve({
57
+ }), l.length ? u.from("product_variants").select("id, sku").in("id", l) : Promise.resolve({
58
58
  data: [],
59
59
  error: null
60
- })]), f = new Map((u.data || []).map((e) => [e.id, e])), p = new Map((d.data || []).map((e) => [e.id, e])), m = (a.order_items || []).map((e) => {
61
- let t = e.product_id ? f.get(e.product_id) : null, n = e.variant_id ? p.get(e.variant_id) : null, r = t?.title || "Product", i = n?.sku ? `SKU: ${n.sku}` : t?.sku ? `SKU: ${t.sku}` : null;
60
+ })]), p = new Map((d.data || []).map((e) => [e.id, e])), m = new Map((f.data || []).map((e) => [e.id, e])), h = (r.order_items || []).map((e) => {
61
+ let t = e.product_id ? p.get(e.product_id) : null, n = e.variant_id ? m.get(e.variant_id) : null, r = t?.title || "Product", i = n?.sku ? `SKU: ${n.sku}` : t?.sku ? `SKU: ${t.sku}` : null;
62
62
  return {
63
63
  id: e.id,
64
64
  product_id: e.product_id ?? null,
@@ -72,22 +72,22 @@ async function m(t, n) {
72
72
  };
73
73
  });
74
74
  return {
75
- id: a.id,
76
- invoice_number: a.invoice_number ?? null,
77
- paid_at: a.paid_at ?? null,
78
- created_at: a.created_at ?? null,
79
- currency: a.currency || "usd",
80
- status: a.status,
81
- provider: a.provider ?? null,
82
- subtotal: typeof a.subtotal == "number" ? a.subtotal : m.reduce((e, t) => e + t.total_amount, 0),
83
- shipping_total: typeof a.shipping_total == "number" ? a.shipping_total : 0,
84
- discount_total: typeof a.discount_total == "number" ? a.discount_total : 0,
85
- coupon_code: a.coupon_code ?? null,
86
- tax_total: typeof a.tax_total == "number" ? a.tax_total : 0,
87
- total: a.total,
88
- customer_details: e(a.customer_details ?? {}),
89
- tax_details: i(a.tax_details),
90
- items: m
75
+ id: r.id,
76
+ invoice_number: r.invoice_number ?? null,
77
+ paid_at: r.paid_at ?? null,
78
+ created_at: r.created_at ?? null,
79
+ currency: r.currency || "usd",
80
+ status: r.status,
81
+ provider: r.provider ?? null,
82
+ subtotal: typeof r.subtotal == "number" ? r.subtotal : h.reduce((e, t) => e + t.total_amount, 0),
83
+ shipping_total: typeof r.shipping_total == "number" ? r.shipping_total : 0,
84
+ discount_total: typeof r.discount_total == "number" ? r.discount_total : 0,
85
+ coupon_code: r.coupon_code ?? null,
86
+ tax_total: typeof r.tax_total == "number" ? r.tax_total : 0,
87
+ total: r.total,
88
+ customer_details: e(r.customer_details ?? {}),
89
+ tax_details: i(r.tax_details),
90
+ items: h
91
91
  };
92
92
  }
93
93
  async function h(e, t) {
@@ -0,0 +1 @@
1
+ const e=require("../_virtual/_rolldown/runtime.cjs");require("server-only");let t=require("node:crypto");t=e.__toESM(t,1);function n(){let e=process.env.SUPABASE_SERVICE_ROLE_KEY||process.env.SUPABASE_SECRET_KEY||process.env.NB_2FA_SECRET;if(!e)throw Error(`Missing SUPABASE_SERVICE_ROLE_KEY / SUPABASE_SECRET_KEY for order access tokens.`);return e}function r(e){return t.default.createHmac(`sha256`,n()).update(`order:${e}`).digest(`base64url`)}function i(e,n){if(!n||typeof n!=`string`)return!1;let i=r(e),a=Buffer.from(i),o=Buffer.from(n);return a.length===o.length&&t.default.timingSafeEqual(a,o)}exports.mintOrderAccessToken=r,exports.verifyOrderAccessToken=i;
@@ -0,0 +1,2 @@
1
+ export declare function mintOrderAccessToken(orderId: string): string;
2
+ export declare function verifyOrderAccessToken(orderId: string, token: string | null | undefined): boolean;
@@ -0,0 +1,18 @@
1
+ import "server-only";
2
+ import e from "node:crypto";
3
+ //#region src/lib/order-access-token.ts
4
+ function t() {
5
+ let e = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_SECRET_KEY || process.env.NB_2FA_SECRET;
6
+ if (!e) throw Error("Missing SUPABASE_SERVICE_ROLE_KEY / SUPABASE_SECRET_KEY for order access tokens.");
7
+ return e;
8
+ }
9
+ function n(n) {
10
+ return e.createHmac("sha256", t()).update(`order:${n}`).digest("base64url");
11
+ }
12
+ function r(t, r) {
13
+ if (!r || typeof r != "string") return !1;
14
+ let i = n(t), a = Buffer.from(i), o = Buffer.from(r);
15
+ return a.length === o.length && e.timingSafeEqual(a, o);
16
+ }
17
+ //#endregion
18
+ export { n as mintOrderAccessToken, r as verifyOrderAccessToken };
@@ -1,7 +1,7 @@
1
- "use server";let e=require("@nextblock-cms/db/server");var t=20;async function n(n=1,r,i){let a=await(0,e.createClient)(),o=(n-1)*t,s=o+t-1,c=a.from(`orders`).select(`
1
+ "use server";const e=require("../../../server-actions/require-ecom-role.cjs");let t=require("@nextblock-cms/db/server");var n=20,r=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function i(e){let t=typeof e==`string`?e.trim():``;return!t||!r.test(t)?null:t.toLowerCase()}async function a(){return await e.getEcomActorRole()===`ADMIN`?(0,t.getServiceRoleSupabaseClient)():null}async function o(e=1,t,r){let o=i(r);if(r&&!o)return{data:[],total:0,totalPages:0};let s=await a();if(!s)return{data:[],total:0,totalPages:0};let c=((Number.isInteger(e)&&e>0?e:1)-1)*n,l=c+n-1,u=s.from(`orders`).select(`
2
2
  *,
3
3
  order_items (*)
4
- `,{count:`exact`}).order(`created_at`,{ascending:!1}).range(o,s);r&&r!==`all`&&(c=c.eq(`status`,r)),i&&(c=c.or(`id.eq.${i},user_id.eq.${i}`));let{data:l,error:u,count:d}=await c;if(u)throw console.error(`Error fetching orders:`,u),Error(`Failed to fetch orders`);let f=l||[],p=Array.from(new Set(f.map(e=>e.user_id).filter(Boolean))),m=new Map;if(p.length>0){let{data:e,error:t}=await a.from(`profiles`).select(`*`).in(`id`,p);!t&&e&&e.forEach(e=>m.set(e.id,e))}return{data:f.map(e=>({...e,customer:e.user_id?m.get(e.user_id):null})),total:d||0,totalPages:d?Math.ceil(d/t):0}}async function r(t){let n=await(0,e.createClient)(),{data:r,error:i}=await n.from(`orders`).select(`
4
+ `,{count:`exact`}).order(`created_at`,{ascending:!1}).range(c,l);t&&t!==`all`&&(u=u.eq(`status`,t)),o&&(u=u.or(`id.eq.${o},user_id.eq.${o}`));let{data:d,error:f,count:p}=await u;if(f)throw console.error(`Error fetching orders:`,f),Error(`Failed to fetch orders`);let m=d||[],h=Array.from(new Set(m.map(e=>e.user_id).filter(Boolean))),g=new Map;if(h.length>0){let{data:e,error:t}=await s.from(`profiles`).select(`*`).in(`id`,h);!t&&e&&e.forEach(e=>g.set(e.id,e))}return{data:m.map(e=>({...e,customer:e.user_id?g.get(e.user_id):null})),total:p||0,totalPages:p?Math.ceil(p/n):0}}async function s(e){let t=await a();if(!t)return null;let{data:n,error:r}=await t.from(`orders`).select(`
5
5
  *,
6
6
  order_items (*)
7
- `).eq(`id`,t).single();if(i||!r)return console.error(`Error fetching order ${t}:`,i),null;let a=r,o=null;if(a.user_id){let{data:e}=await n.from(`profiles`).select(`*`).eq(`id`,a.user_id).single();o=e}let s=a.order_items.map(e=>e.product_id).filter(Boolean),c=new Map;if(s.length>0){let{data:e}=await n.from(`products`).select(`id, title, slug, product_media(media(file_path))`).in(`id`,s);e&&e.forEach(e=>{let t=null;e.product_media&&e.product_media.length>0&&e.product_media[0].media&&(t=e.product_media[0].media.file_path),c.set(e.id,{title:e.title,slug:e.slug,image_url:t})})}let l=a.order_items.map(e=>({...e,product:e.product_id?c.get(e.product_id):null}));return{...a,order_items:l,customer:o}}exports.getOrderDetails=r,exports.getOrders=n;
7
+ `).eq(`id`,e).single();if(r||!n)return console.error(`Error fetching order ${e}:`,r),null;let i=n,o=null;if(i.user_id){let{data:e}=await t.from(`profiles`).select(`*`).eq(`id`,i.user_id).single();o=e}let s=i.order_items.map(e=>e.product_id).filter(Boolean),c=new Map;if(s.length>0){let{data:e}=await t.from(`products`).select(`id, title, slug, product_media(media(file_path))`).in(`id`,s);e&&e.forEach(e=>{let t=null;e.product_media&&e.product_media.length>0&&e.product_media[0].media&&(t=e.product_media[0].media.file_path),c.set(e.id,{title:e.title,slug:e.slug,image_url:t})})}let l=i.order_items.map(e=>({...e,product:e.product_id?c.get(e.product_id):null}));return{...i,order_items:l,customer:o}}exports.getOrderDetails=s,exports.getOrders=o;
@@ -1,37 +1,59 @@
1
1
  "use server";
2
- import { createClient as e } from "@nextblock-cms/db/server";
2
+ import { getEcomActorRole as e } from "../../../server-actions/require-ecom-role.es.js";
3
+ import { getServiceRoleSupabaseClient as t } from "@nextblock-cms/db/server";
3
4
  //#region src/lib/pages/cms/orders/actions.ts
4
- var t = 20;
5
- async function n(n = 1, r, i) {
6
- let a = await e(), o = (n - 1) * t, s = o + t - 1, c = a.from("orders").select("\n *,\n order_items (*)\n ", { count: "exact" }).order("created_at", { ascending: !1 }).range(o, s);
7
- r && r !== "all" && (c = c.eq("status", r)), i && (c = c.or(`id.eq.${i},user_id.eq.${i}`));
8
- let { data: l, error: u, count: d } = await c;
9
- if (u) throw console.error("Error fetching orders:", u), Error("Failed to fetch orders");
10
- let f = l || [], p = Array.from(new Set(f.map((e) => e.user_id).filter(Boolean))), m = /* @__PURE__ */ new Map();
11
- if (p.length > 0) {
12
- let { data: e, error: t } = await a.from("profiles").select("*").in("id", p);
13
- !t && e && e.forEach((e) => m.set(e.id, e));
5
+ var n = 20, r = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
6
+ function i(e) {
7
+ let t = typeof e == "string" ? e.trim() : "";
8
+ return !t || !r.test(t) ? null : t.toLowerCase();
9
+ }
10
+ async function a() {
11
+ return await e() === "ADMIN" ? t() : null;
12
+ }
13
+ async function o(e = 1, t, r) {
14
+ let o = i(r);
15
+ if (r && !o) return {
16
+ data: [],
17
+ total: 0,
18
+ totalPages: 0
19
+ };
20
+ let s = await a();
21
+ if (!s) return {
22
+ data: [],
23
+ total: 0,
24
+ totalPages: 0
25
+ };
26
+ let c = ((Number.isInteger(e) && e > 0 ? e : 1) - 1) * n, l = c + n - 1, u = s.from("orders").select("\n *,\n order_items (*)\n ", { count: "exact" }).order("created_at", { ascending: !1 }).range(c, l);
27
+ t && t !== "all" && (u = u.eq("status", t)), o && (u = u.or(`id.eq.${o},user_id.eq.${o}`));
28
+ let { data: d, error: f, count: p } = await u;
29
+ if (f) throw console.error("Error fetching orders:", f), Error("Failed to fetch orders");
30
+ let m = d || [], h = Array.from(new Set(m.map((e) => e.user_id).filter(Boolean))), g = /* @__PURE__ */ new Map();
31
+ if (h.length > 0) {
32
+ let { data: e, error: t } = await s.from("profiles").select("*").in("id", h);
33
+ !t && e && e.forEach((e) => g.set(e.id, e));
14
34
  }
15
35
  return {
16
- data: f.map((e) => ({
36
+ data: m.map((e) => ({
17
37
  ...e,
18
- customer: e.user_id ? m.get(e.user_id) : null
38
+ customer: e.user_id ? g.get(e.user_id) : null
19
39
  })),
20
- total: d || 0,
21
- totalPages: d ? Math.ceil(d / t) : 0
40
+ total: p || 0,
41
+ totalPages: p ? Math.ceil(p / n) : 0
22
42
  };
23
43
  }
24
- async function r(t) {
25
- let n = await e(), { data: r, error: i } = await n.from("orders").select("\n *,\n order_items (*)\n ").eq("id", t).single();
26
- if (i || !r) return console.error(`Error fetching order ${t}:`, i), null;
27
- let a = r, o = null;
28
- if (a.user_id) {
29
- let { data: e } = await n.from("profiles").select("*").eq("id", a.user_id).single();
44
+ async function s(e) {
45
+ let t = await a();
46
+ if (!t) return null;
47
+ let { data: n, error: r } = await t.from("orders").select("\n *,\n order_items (*)\n ").eq("id", e).single();
48
+ if (r || !n) return console.error(`Error fetching order ${e}:`, r), null;
49
+ let i = n, o = null;
50
+ if (i.user_id) {
51
+ let { data: e } = await t.from("profiles").select("*").eq("id", i.user_id).single();
30
52
  o = e;
31
53
  }
32
- let s = a.order_items.map((e) => e.product_id).filter(Boolean), c = /* @__PURE__ */ new Map();
54
+ let s = i.order_items.map((e) => e.product_id).filter(Boolean), c = /* @__PURE__ */ new Map();
33
55
  if (s.length > 0) {
34
- let { data: e } = await n.from("products").select("id, title, slug, product_media(media(file_path))").in("id", s);
56
+ let { data: e } = await t.from("products").select("id, title, slug, product_media(media(file_path))").in("id", s);
35
57
  e && e.forEach((e) => {
36
58
  let t = null;
37
59
  e.product_media && e.product_media.length > 0 && e.product_media[0].media && (t = e.product_media[0].media.file_path), c.set(e.id, {
@@ -41,15 +63,15 @@ async function r(t) {
41
63
  });
42
64
  });
43
65
  }
44
- let l = a.order_items.map((e) => ({
66
+ let l = i.order_items.map((e) => ({
45
67
  ...e,
46
68
  product: e.product_id ? c.get(e.product_id) : null
47
69
  }));
48
70
  return {
49
- ...a,
71
+ ...i,
50
72
  order_items: l,
51
73
  customer: o
52
74
  };
53
75
  }
54
76
  //#endregion
55
- export { r as getOrderDetails, n as getOrders };
77
+ export { s as getOrderDetails, o as getOrders };
@@ -1 +1 @@
1
- "use server";let e=require("@nextblock-cms/db/server");async function t(t,n){let r=(0,e.createClient)(),i=r.from(`orders`).select(`*, order_items(*)`);if(t&&(i=i.gte(`created_at`,t)),n){let e=new Date(n);e.setHours(23,59,59,999),i=i.lte(`created_at`,e.toISOString())}let{data:a,error:o}=await i.order(`created_at`,{ascending:!0});if(o)throw console.error(`Error fetching report data:`,o),Error(`Failed to fetch report data: ${o.message}`);if(!a||a.length===0)return[];let s=Array.from(new Set(a.map(e=>e.user_id).filter(Boolean))),c=new Map;if(s.length>0){let{data:e}=await r.from(`profiles`).select(`id, full_name, email`).in(`id`,s);e&&e.forEach(e=>c.set(e.id,e))}return a.map(e=>({...e,customer:e.user_id?c.get(e.user_id):null}))}exports.fetchOrderReportData=t;
1
+ "use server";const e=require("../../../server-actions/require-ecom-role.cjs");let t=require("@nextblock-cms/db/server");async function n(n,r){if(await e.getEcomActorRole()!==`ADMIN`)return[];let i=(0,t.getServiceRoleSupabaseClient)(),a=i.from(`orders`).select(`*, order_items(*)`);if(n&&(a=a.gte(`created_at`,n)),r){let e=new Date(r);e.setHours(23,59,59,999),a=a.lte(`created_at`,e.toISOString())}let{data:o,error:s}=await a.order(`created_at`,{ascending:!0});if(s)throw console.error(`Error fetching report data:`,s),Error(`Failed to fetch report data: ${s.message}`);if(!o||o.length===0)return[];let c=Array.from(new Set(o.map(e=>e.user_id).filter(e=>!!e))),l=new Map;if(c.length>0){let{data:e}=await i.from(`profiles`).select(`id, full_name`).in(`id`,c);e&&e.forEach(e=>l.set(e.id,e))}return o.map(e=>({...e,customer:e.user_id?l.get(e.user_id):null}))}exports.fetchOrderReportData=n;