@nextblock-cms/ecom 0.8.1 → 0.8.6

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 (71) hide show
  1. package/AddToCartButton-DsSEH9yp.cjs +1 -0
  2. package/AddToCartButton-ffE0DhHu.js +59 -0
  3. package/CouponForm-BI_5SBHQ.js +152 -0
  4. package/CouponForm-D_uu2bLP.cjs +1 -0
  5. package/CurrencyProvider-Cg-Q6ucj.cjs +1 -0
  6. package/CurrencyProvider-OzlRN96V.js +120 -0
  7. package/CurrencyProvider.cjs.js +1 -0
  8. package/CurrencyProvider.es.js +10 -0
  9. package/ProductDetailsLayout-BxJH_BxR.cjs +1 -0
  10. package/ProductDetailsLayout-Dg8dRTYp.js +530 -0
  11. package/ProductGrid-BdL7iglO.cjs +1 -0
  12. package/ProductGrid-D3AWM8sr.js +130 -0
  13. package/actions-AHmBrS5e.cjs +48 -0
  14. package/actions-j5fuSZX-.js +820 -0
  15. package/cart-store.cjs.js +1 -0
  16. package/cart-store.es.js +116 -0
  17. package/components/Cart.cjs.js +1 -0
  18. package/components/Cart.es.js +259 -0
  19. package/components/CartDrawer.cjs.js +1 -0
  20. package/components/CartDrawer.es.js +118 -0
  21. package/components/CartIcon.cjs.js +1 -0
  22. package/components/CartIcon.es.js +33 -0
  23. package/components/Checkout.cjs.js +1 -0
  24. package/components/Checkout.es.js +1020 -0
  25. package/components/CurrencySwitcher.cjs.js +1 -0
  26. package/components/CurrencySwitcher.es.js +17 -0
  27. package/components/FeaturedProduct.cjs.js +1 -0
  28. package/components/FeaturedProduct.es.js +71 -0
  29. package/components/ProductDetailsLayout.cjs.js +1 -0
  30. package/components/ProductDetailsLayout.es.js +19 -0
  31. package/components/ProductGrid.cjs.js +1 -0
  32. package/components/ProductGrid.es.js +6 -0
  33. package/components/SimpleTiptapRenderer.cjs.js +1 -0
  34. package/components/SimpleTiptapRenderer.es.js +187 -0
  35. package/coupon-server-C9ySm3I5.cjs +1 -0
  36. package/coupon-server-DUKVtyYs.js +248 -0
  37. package/currency-constants.cjs.js +1 -0
  38. package/currency-constants.es.js +4 -0
  39. package/currency.cjs.js +1 -0
  40. package/currency.es.js +312 -0
  41. package/customer-C-UPvrYN.cjs +1 -0
  42. package/customer-C3xccjUg.js +57 -0
  43. package/index.cjs.js +1 -1
  44. package/index.es.js +414 -3105
  45. package/invoice-ui-CAnJPu6n.cjs +29 -0
  46. package/invoice-ui-DtpbTJE1.js +2886 -0
  47. package/package.json +10 -5
  48. package/product-actions-CONBF8i6.cjs +251 -0
  49. package/product-actions-DjlIWCMH.js +691 -0
  50. package/resolver-Fz_FKsfX.cjs +7 -0
  51. package/resolver-cfrCaHpE.js +128 -0
  52. package/server-actions/product-actions.cjs.js +1 -0
  53. package/server-actions/product-actions.es.js +15 -0
  54. package/server.cjs.js +27 -27
  55. package/server.es.js +478 -463
  56. package/shipping-actions-BnHjlo2q.js +25 -0
  57. package/shipping-actions-D6zt20gg.cjs +1 -0
  58. package/states-CWYRxV4B.cjs +1 -0
  59. package/states-DlHJezQ1.js +200 -0
  60. package/tax-calculation-Dab89aHd.js +181 -0
  61. package/tax-calculation-MkDKOEkL.cjs +1 -0
  62. package/trials-DvZaOtNy.cjs +1 -0
  63. package/trials-RDcRAZKx.js +29 -0
  64. package/types.cjs.js +1 -0
  65. package/types.es.js +33 -0
  66. package/use-cart.cjs.js +1 -0
  67. package/use-cart.es.js +43 -0
  68. package/variation-utils.cjs.js +1 -0
  69. package/variation-utils.es.js +246 -0
  70. package/invoice-ui-BJZfedDd.cjs +0 -332
  71. package/invoice-ui-GJ1ADAjn.js +0 -5759
@@ -0,0 +1,691 @@
1
+ import { deleteMediaFiles as E } from "@nextblock-cms/utils/server";
2
+ import "server-only";
3
+ import { getServiceRoleSupabaseClient as F } from "@nextblock-cms/db/server";
4
+ import { normalizeCurrencyCode as A } from "@nextblock-cms/utils";
5
+ function q(e) {
6
+ return e ?? F();
7
+ }
8
+ function R(e) {
9
+ return e ? (Array.isArray(e) ? e : [e]).map((r) => r?.code?.toUpperCase().trim()).filter((r) => !!r) : [];
10
+ }
11
+ function g(e) {
12
+ return [...new Set(e)].sort((t, r) => t.localeCompare(r));
13
+ }
14
+ function U(e, t) {
15
+ return !e || e === t ? t : "mixed";
16
+ }
17
+ async function w(e, t) {
18
+ const r = Array.from(
19
+ t.reduce((c, s) => {
20
+ const i = s.sku.trim();
21
+ return i && c.set(i, {
22
+ sku: i,
23
+ quantity: Math.max(0, Math.trunc(s.quantity))
24
+ }), c;
25
+ }, /* @__PURE__ */ new Map()).values()
26
+ );
27
+ if (r.length === 0)
28
+ return;
29
+ const { error: a } = await e.from("inventory_items").upsert(r, { onConflict: "sku" });
30
+ if (a)
31
+ throw new Error(a.message);
32
+ }
33
+ async function x(e, t) {
34
+ const r = g(
35
+ t.map((s) => s.trim()).filter(Boolean)
36
+ );
37
+ if (r.length === 0)
38
+ return /* @__PURE__ */ new Map();
39
+ const { data: a, error: c } = await e.from("inventory_items").select("sku, quantity").in("sku", r);
40
+ if (c)
41
+ throw new Error(c.message);
42
+ return new Map(
43
+ (a || []).map((s) => [
44
+ s.sku,
45
+ Math.max(0, s.quantity ?? 0)
46
+ ])
47
+ );
48
+ }
49
+ async function b(e) {
50
+ const { data: t, error: r } = await e.from("products").select(
51
+ `
52
+ id,
53
+ title,
54
+ sku,
55
+ stock,
56
+ status,
57
+ languages (
58
+ code
59
+ ),
60
+ product_variants (
61
+ id,
62
+ sku,
63
+ stock_quantity
64
+ )
65
+ `
66
+ ).order("title", { ascending: !0 });
67
+ if (r)
68
+ throw new Error(r.message);
69
+ const a = /* @__PURE__ */ new Map(), c = (s, i, o, d, u) => {
70
+ const _ = s.trim();
71
+ if (!_)
72
+ return;
73
+ const l = R(d.languages), n = a.get(_);
74
+ if (n) {
75
+ n.usageType = U(n.usageType, i), n.stockFallback = Math.min(n.stockFallback, o), n.productTitles.add(d.title), n.parentProductSkus.add(d.sku), n.languages = /* @__PURE__ */ new Set([...n.languages, ...l]), n.statuses.add(d.status), n.productIds.add(d.id), u && n.variantIds.add(u);
76
+ return;
77
+ }
78
+ a.set(_, {
79
+ sku: _,
80
+ stockFallback: o,
81
+ usageType: i,
82
+ productTitles: /* @__PURE__ */ new Set([d.title]),
83
+ parentProductSkus: /* @__PURE__ */ new Set([d.sku]),
84
+ languages: new Set(l),
85
+ statuses: /* @__PURE__ */ new Set([d.status]),
86
+ productIds: /* @__PURE__ */ new Set([d.id]),
87
+ variantIds: u ? /* @__PURE__ */ new Set([u]) : /* @__PURE__ */ new Set()
88
+ });
89
+ };
90
+ for (const s of t || []) {
91
+ const i = s.product_variants || [];
92
+ if (i.length > 0) {
93
+ for (const o of i)
94
+ c(
95
+ o.sku,
96
+ "variant",
97
+ Math.max(0, o.stock_quantity ?? 0),
98
+ s,
99
+ o.id
100
+ );
101
+ continue;
102
+ }
103
+ c(
104
+ s.sku,
105
+ "product",
106
+ Math.max(0, s.stock ?? 0),
107
+ s
108
+ );
109
+ }
110
+ return a;
111
+ }
112
+ async function O(e) {
113
+ const t = q(e), r = await b(t), a = [...r.keys()];
114
+ if (a.length === 0)
115
+ return [];
116
+ let c = await x(t, a);
117
+ const s = a.filter((i) => !c.has(i)).flatMap((i) => {
118
+ const o = r.get(i);
119
+ return o ? [
120
+ {
121
+ sku: i,
122
+ quantity: o.stockFallback
123
+ }
124
+ ] : [];
125
+ });
126
+ return s.length > 0 && (await w(t, s), c = await x(t, a)), a.flatMap((i) => {
127
+ const o = r.get(i);
128
+ return o ? {
129
+ key: i,
130
+ sku: i,
131
+ stock: c.get(i) ?? o.stockFallback,
132
+ usageType: o.usageType,
133
+ productTitles: g(o.productTitles),
134
+ parentProductSkus: g(o.parentProductSkus),
135
+ languages: g(o.languages),
136
+ statuses: g(o.statuses),
137
+ productIds: g(o.productIds),
138
+ variantIds: g(o.variantIds)
139
+ } : [];
140
+ }).sort((i, o) => i.sku.localeCompare(o.sku) || i.usageType.localeCompare(o.usageType) || i.productTitles.join(", ").localeCompare(o.productTitles.join(", ")));
141
+ }
142
+ async function V(e, t) {
143
+ const r = q(t), a = e.sku.trim(), c = Math.max(0, Math.trunc(e.stock));
144
+ if (!a)
145
+ throw new Error("SKU is required to update inventory.");
146
+ await w(r, [{ sku: a, quantity: c }]);
147
+ const i = (await b(r)).get(a), o = (i?.productIds.size ?? 0) + (i?.variantIds.size ?? 0);
148
+ return { stock: c, affectedCount: o };
149
+ }
150
+ async function j(e, t, r) {
151
+ const a = q(r);
152
+ if (t.variants && t.variants.length > 0) {
153
+ const c = Array.from(
154
+ t.variants.reduce((s, i) => (s.set(
155
+ i.sku.trim(),
156
+ Math.max(0, Math.trunc(i.stock_quantity))
157
+ ), s), /* @__PURE__ */ new Map()).entries()
158
+ ).map(([s, i]) => ({ sku: s, quantity: i }));
159
+ await w(a, c);
160
+ return;
161
+ }
162
+ await w(a, [
163
+ {
164
+ sku: t.sku,
165
+ quantity: Math.max(0, Math.trunc(t.stock))
166
+ }
167
+ ]);
168
+ }
169
+ const y = (e) => Math.round(e * 100);
170
+ function h(e) {
171
+ return Object.entries(e || {}).reduce(
172
+ (t, [r, a]) => (typeof a == "number" && Number.isFinite(a) && a >= 0 && (t[A(r)] = y(a)), t),
173
+ {}
174
+ );
175
+ }
176
+ function z(e) {
177
+ return (e || []).map((t) => ({
178
+ id: t.id,
179
+ sku: t.sku,
180
+ upc: t.upc ?? null,
181
+ price: y(t.price),
182
+ sale_price: typeof t.sale_price == "number" && !isNaN(t.sale_price) ? y(t.sale_price) : null,
183
+ prices: h(t.prices),
184
+ sale_prices: h(t.sale_prices),
185
+ sale_start_at: t.sale_start_at ?? null,
186
+ sale_end_at: t.sale_end_at ?? null,
187
+ stock_quantity: t.stock_quantity,
188
+ main_media_id: t.main_media_id ?? null,
189
+ attribute_term_ids: t.attribute_term_ids
190
+ }));
191
+ }
192
+ function P(e, t) {
193
+ const a = e.product_type === "digital" && e.payment_provider === "freemius" ? Math.max(0, Number(e.trial_period_days ?? 0)) : 0;
194
+ return {
195
+ id: t,
196
+ product_type: e.product_type,
197
+ payment_provider: e.payment_provider,
198
+ title: e.title,
199
+ slug: e.slug,
200
+ sku: e.sku,
201
+ upc: e.upc ?? null,
202
+ stock: e.stock,
203
+ status: e.status,
204
+ short_description: e.short_description ?? null,
205
+ description_json: e.description_json ?? null,
206
+ metadata: {},
207
+ price: y(e.price),
208
+ sale_price: typeof e.sale_price == "number" && !isNaN(e.sale_price) ? y(e.sale_price) : null,
209
+ prices: h(e.prices),
210
+ sale_prices: h(e.sale_prices),
211
+ sale_start_at: e.sale_start_at ?? null,
212
+ sale_end_at: e.sale_end_at ?? null,
213
+ freemius_plan_id: e.freemius_plan_id ?? null,
214
+ freemius_product_id: e.freemius_product_id ?? null,
215
+ trial_period_days: a,
216
+ trial_requires_payment_method: a > 0 ? e.trial_requires_payment_method ?? !1 : !1,
217
+ is_taxable: e.is_taxable,
218
+ language_id: e.language_id,
219
+ translation_group_id: e.translation_group_id || void 0,
220
+ variants: z(e.variants)
221
+ };
222
+ }
223
+ async function C(e, t, r) {
224
+ const { error: a } = await e.from("products").update({
225
+ is_taxable: r,
226
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
227
+ }).eq("id", t);
228
+ if (a)
229
+ throw a;
230
+ }
231
+ async function T(e, t, r) {
232
+ const a = (/* @__PURE__ */ new Date()).toISOString(), { data: c } = await e.from("products").select("sku, sale_price, sale_prices").eq("id", t).maybeSingle(), s = c?.sku ?? r.sku, { data: i } = await e.from("products").select("id").eq("sku", s), o = (i ?? []).map((n) => n.id);
233
+ o.length === 0 && o.push(t);
234
+ const { error: d } = await e.from("products").update({
235
+ sale_price: c?.sale_price ?? null,
236
+ sale_prices: c?.sale_prices ?? null,
237
+ sale_start_at: r.sale_start_at ?? null,
238
+ sale_end_at: r.sale_end_at ?? null,
239
+ updated_at: a
240
+ }).in("id", o);
241
+ if (d)
242
+ throw d;
243
+ const u = r.variants ?? [];
244
+ if (u.length === 0)
245
+ return;
246
+ const { data: _ } = await e.from("product_variants").select("sku, sale_price, sale_prices").eq("product_id", t), l = new Map(
247
+ (_ ?? []).map((n) => [n.sku, n])
248
+ );
249
+ for (const n of u) {
250
+ const p = l.get(n.sku);
251
+ if (!p)
252
+ continue;
253
+ const { error: m } = await e.from("product_variants").update({
254
+ sale_price: p.sale_price ?? null,
255
+ sale_prices: p.sale_prices ?? null,
256
+ sale_start_at: n.sale_start_at ?? null,
257
+ sale_end_at: n.sale_end_at ?? null,
258
+ updated_at: a
259
+ }).eq("sku", n.sku).in("product_id", o);
260
+ if (m)
261
+ throw m;
262
+ }
263
+ }
264
+ async function K(e, {
265
+ page: t = 1,
266
+ limit: r = 10,
267
+ search: a = "",
268
+ languageId: c,
269
+ categoryId: s
270
+ } = {}) {
271
+ const i = (t - 1) * r, o = i + r - 1;
272
+ let d = e.from("products").select(
273
+ "id, title, sku, upc, price, prices, sale_price, sale_prices, sale_start_at, sale_end_at, scheduled_price, scheduled_prices, scheduled_price_at, is_taxable, product_type, payment_provider, short_description, stock, status, slug, language_id, translation_group_id, freemius_product_id, freemius_plan_id, trial_period_days, trial_requires_payment_method, product_media(media(file_path, object_key)), product_variants(id, price, prices, sale_price, sale_prices, sale_start_at, sale_end_at, scheduled_price, scheduled_prices, scheduled_price_at), freemius_plans(id, name, title, freemius_pricing(id, license_quota, api_monthly_price, api_annual_price, api_lifetime_price, override_monthly_price, override_annual_price, override_lifetime_price, is_active)), product_categories(category:categories(id, name, slug, description, name_translations, description_translations))",
274
+ { count: "exact" }
275
+ ).range(i, o).order("created_at", { ascending: !1 });
276
+ if (c && (d = d.eq("language_id", c)), s) {
277
+ const { data: u, error: _ } = await e.from("product_categories").select("product_id").eq("category_id", s);
278
+ if (_)
279
+ return { data: [], error: _, count: 0 };
280
+ const l = (u || []).map((n) => n.product_id).filter(Boolean);
281
+ if (l.length === 0)
282
+ return { data: [], error: null, count: 0 };
283
+ d = d.in("id", l);
284
+ }
285
+ return a && (d = d.or(`title.ilike.%${a}%,sku.ilike.%${a}%`)), d;
286
+ }
287
+ async function L(e, t) {
288
+ return e.from("products").select(
289
+ `
290
+ *,
291
+ languages (
292
+ code
293
+ ),
294
+ product_media (
295
+ media_id,
296
+ sort_order,
297
+ media (
298
+ id,
299
+ file_path,
300
+ object_key,
301
+ file_name,
302
+ blur_data_url,
303
+ width,
304
+ height
305
+ )
306
+ ),
307
+ product_variants (
308
+ id,
309
+ sku,
310
+ upc,
311
+ main_media_id,
312
+ price,
313
+ prices,
314
+ sale_price,
315
+ sale_prices,
316
+ sale_start_at,
317
+ sale_end_at,
318
+ scheduled_price,
319
+ scheduled_prices,
320
+ scheduled_price_at,
321
+ stock_quantity,
322
+ media:main_media_id (
323
+ id,
324
+ file_path,
325
+ object_key,
326
+ description
327
+ ),
328
+ variant_attribute_mapping (
329
+ attribute_term_id,
330
+ product_attribute_terms (
331
+ id,
332
+ attribute_id,
333
+ value,
334
+ slug,
335
+ sort_order,
336
+ value_translations,
337
+ product_attributes (
338
+ id,
339
+ name,
340
+ slug,
341
+ name_translations
342
+ )
343
+ )
344
+ )
345
+ ),
346
+ freemius_plans (
347
+ id,
348
+ name,
349
+ title,
350
+ freemius_pricing (
351
+ id,
352
+ license_quota,
353
+ api_monthly_price,
354
+ api_annual_price,
355
+ api_lifetime_price,
356
+ override_monthly_price,
357
+ override_annual_price,
358
+ override_lifetime_price,
359
+ is_active
360
+ )
361
+ ),
362
+ product_categories (
363
+ category:categories (
364
+ id,
365
+ name,
366
+ slug,
367
+ description,
368
+ name_translations,
369
+ description_translations
370
+ )
371
+ )
372
+ `
373
+ ).eq("id", t).single();
374
+ }
375
+ async function Q(e, t, r) {
376
+ const { data: a, error: c } = await e.from("products").select(
377
+ `
378
+ *,
379
+ languages (
380
+ id,
381
+ code,
382
+ is_default
383
+ ),
384
+ product_media (
385
+ media_id,
386
+ sort_order,
387
+ media (
388
+ id,
389
+ file_path,
390
+ object_key,
391
+ file_name,
392
+ blur_data_url,
393
+ width,
394
+ height
395
+ )
396
+ ),
397
+ product_variants (
398
+ id,
399
+ sku,
400
+ upc,
401
+ main_media_id,
402
+ price,
403
+ prices,
404
+ sale_price,
405
+ sale_prices,
406
+ sale_start_at,
407
+ sale_end_at,
408
+ scheduled_price,
409
+ scheduled_prices,
410
+ scheduled_price_at,
411
+ stock_quantity,
412
+ media:main_media_id (
413
+ id,
414
+ file_path,
415
+ object_key,
416
+ description
417
+ ),
418
+ variant_attribute_mapping (
419
+ attribute_term_id,
420
+ product_attribute_terms (
421
+ id,
422
+ attribute_id,
423
+ value,
424
+ slug,
425
+ sort_order,
426
+ value_translations,
427
+ product_attributes (
428
+ id,
429
+ name,
430
+ slug,
431
+ name_translations
432
+ )
433
+ )
434
+ )
435
+ ),
436
+ freemius_plans (
437
+ id,
438
+ name,
439
+ title,
440
+ freemius_pricing (
441
+ id,
442
+ license_quota,
443
+ api_monthly_price,
444
+ api_annual_price,
445
+ api_lifetime_price,
446
+ override_monthly_price,
447
+ override_annual_price,
448
+ override_lifetime_price,
449
+ is_active
450
+ )
451
+ ),
452
+ product_categories (
453
+ category:categories (
454
+ id,
455
+ name,
456
+ slug,
457
+ description,
458
+ name_translations,
459
+ description_translations
460
+ )
461
+ )
462
+ `
463
+ ).eq("slug", t);
464
+ if (c)
465
+ return { data: null, error: c };
466
+ if (!a || a.length === 0)
467
+ return { data: null, error: { message: "Product not found", code: "PGRST116" } };
468
+ let s = null;
469
+ return r && (s = a.find((i) => (Array.isArray(i.languages) ? i.languages[0] : i.languages)?.code === r)), s || (s = a.find((i) => (Array.isArray(i.languages) ? i.languages[0] : i.languages)?.is_default)), s || (s = a.find((i) => (Array.isArray(i.languages) ? i.languages[0] : i.languages)?.code === "en")), s || (s = a[0]), { data: s, error: null };
470
+ }
471
+ async function G(e, t) {
472
+ const { data: r, error: a } = await e.rpc("upsert_product_with_variants", {
473
+ product_payload: P(t)
474
+ });
475
+ if (a || !r) throw a || new Error("Failed to create product");
476
+ if (t.product_media && t.product_media.length > 0) {
477
+ const s = t.product_media.map((i, o) => ({
478
+ product_id: r,
479
+ media_id: i.media_id,
480
+ sort_order: o
481
+ }));
482
+ await e.from("product_media").insert(s);
483
+ } else t.media_id && await e.from("product_media").insert({
484
+ product_id: r,
485
+ media_id: t.media_id,
486
+ sort_order: 0
487
+ });
488
+ await C(e, r, t.is_taxable), await T(e, r, t), await j(r, t), t.category_ids !== void 0 && await I(e, r, t.category_ids);
489
+ const { data: c } = await e.from("products").select("*").eq("id", r).single();
490
+ return c;
491
+ }
492
+ async function H(e, t, r) {
493
+ const { data: a } = await e.from("product_media").select("media_id").eq("product_id", t), c = a?.map((n) => n.media_id) || [], { data: s, error: i } = await e.rpc("upsert_product_with_variants", {
494
+ product_payload: P(r, t)
495
+ });
496
+ if (i || !s) throw i || new Error("Failed to update product");
497
+ if (r.product_media) {
498
+ if (await e.from("product_media").delete().eq("product_id", t), r.product_media.length > 0) {
499
+ const n = r.product_media.map((p, m) => ({
500
+ product_id: t,
501
+ media_id: p.media_id,
502
+ sort_order: m
503
+ }));
504
+ await e.from("product_media").insert(n);
505
+ }
506
+ } else r.media_id && (await e.from("product_media").delete().eq("product_id", t), await e.from("product_media").insert({
507
+ product_id: t,
508
+ media_id: r.media_id,
509
+ sort_order: 0
510
+ }));
511
+ await C(e, s, r.is_taxable), await T(e, s, r);
512
+ const o = r.product_media ? r.product_media.map((n) => n.media_id) : r.media_id ? [r.media_id] : [], d = r.explicitly_removed_media_ids || [], u = c.filter((n) => !o.includes(n)), _ = Array.from(/* @__PURE__ */ new Set([...u, ...d]));
513
+ if (_.length > 0)
514
+ for (const n of _) {
515
+ const { count: p } = await e.from("product_media").select("*", { count: "exact", head: !0 }).eq("media_id", n);
516
+ if (p && p > 0) continue;
517
+ const { count: m } = await e.from("posts").select("*", { count: "exact", head: !0 }).eq("feature_image_id", n);
518
+ if (m && m > 0) continue;
519
+ const { count: k } = await e.from("logos").select("*", { count: "exact", head: !0 }).eq("media_id", n);
520
+ if (k && k > 0) continue;
521
+ const { count: v } = await e.from("product_variants").select("*", { count: "exact", head: !0 }).eq("main_media_id", n);
522
+ if (v && v > 0) continue;
523
+ const { data: f } = await e.from("media").select("object_key, variants").eq("id", n).single();
524
+ if (f) {
525
+ const S = [f.object_key];
526
+ f.variants && Array.isArray(f.variants) && f.variants.forEach((M) => {
527
+ M.objectKey && S.push(M.objectKey);
528
+ }), await E(S), await e.from("media").delete().eq("id", n);
529
+ }
530
+ }
531
+ await j(s, r), r.category_ids !== void 0 && await I(e, t, r.category_ids);
532
+ const { data: l } = await e.from("products").select("*").eq("id", s).single();
533
+ return l;
534
+ }
535
+ async function J(e, t) {
536
+ const { error: r } = await e.from("products").delete().eq("id", t);
537
+ if (r) throw r;
538
+ return !0;
539
+ }
540
+ async function W(e, t, r) {
541
+ const { data: a, error: c } = await e.from("products").select("*").eq("id", r).single();
542
+ if (c || !a)
543
+ throw new Error(c?.message || "Source product not found");
544
+ const { data: s, error: i } = await e.from("products").select("language_id").eq("id", t).single();
545
+ if (i || !s)
546
+ throw new Error(i?.message || "Target product not found");
547
+ const { error: o } = await e.from("products").update({
548
+ title: a.title,
549
+ short_description: a.short_description,
550
+ description_json: a.description_json
551
+ }).eq("id", t);
552
+ if (o) throw o;
553
+ const { data: d, error: u } = await e.from("blocks").select("*").eq("product_id", r).order("order", { ascending: !0 });
554
+ if (u) throw u;
555
+ if (await e.from("blocks").delete().eq("product_id", t), d && d.length > 0) {
556
+ const l = d.map((p) => {
557
+ const { id: m, created_at: k, updated_at: v, ...f } = p;
558
+ return {
559
+ ...f,
560
+ product_id: t,
561
+ language_id: s.language_id
562
+ };
563
+ }), { error: n } = await e.from("blocks").insert(l);
564
+ if (n) throw n;
565
+ }
566
+ await e.from("product_drafts").delete().eq("product_id", t), await e.from("product_media").delete().eq("product_id", t);
567
+ const { data: _ } = await e.from("product_media").select("media_id, sort_order").eq("product_id", r);
568
+ if (_ && _.length > 0) {
569
+ const l = _.map((n) => ({
570
+ product_id: t,
571
+ media_id: n.media_id,
572
+ sort_order: n.sort_order
573
+ }));
574
+ await e.from("product_media").insert(l);
575
+ }
576
+ return { success: !0 };
577
+ }
578
+ async function X(e, t, r, a = [], c = []) {
579
+ const { data: s } = await e.from("languages").select("id").eq("code", r).single();
580
+ if (!s)
581
+ return { data: [], error: "Language not found" };
582
+ const i = [];
583
+ t.length > 0 && i.push(`translation_group_id.in.(${t.join(",")})`), a.length > 0 && i.push(`sku.in.(${a.map((u) => `"${u}"`).join(",")})`), c.length > 0 && i.push(`id.in.(${c.join(",")})`);
584
+ let o = e.from("products").select(`
585
+ id,
586
+ title,
587
+ sku,
588
+ price,
589
+ prices,
590
+ sale_price,
591
+ sale_prices,
592
+ sale_start_at,
593
+ sale_end_at,
594
+ scheduled_price,
595
+ scheduled_prices,
596
+ scheduled_price_at,
597
+ stock,
598
+ slug,
599
+ language_id,
600
+ product_type,
601
+ payment_provider,
602
+ freemius_product_id,
603
+ freemius_plan_id,
604
+ trial_period_days,
605
+ trial_requires_payment_method,
606
+ is_taxable,
607
+ product_media (
608
+ media (
609
+ file_path,
610
+ object_key
611
+ )
612
+ ),
613
+ product_variants (
614
+ id,
615
+ sku,
616
+ upc,
617
+ main_media_id,
618
+ price,
619
+ prices,
620
+ sale_price,
621
+ sale_prices,
622
+ sale_start_at,
623
+ sale_end_at,
624
+ scheduled_price,
625
+ scheduled_prices,
626
+ scheduled_price_at,
627
+ stock_quantity,
628
+ media:main_media_id (
629
+ file_path,
630
+ object_key,
631
+ description
632
+ ),
633
+ variant_attribute_mapping (
634
+ attribute_term_id,
635
+ product_attribute_terms (
636
+ id,
637
+ attribute_id,
638
+ value,
639
+ slug,
640
+ sort_order,
641
+ value_translations,
642
+ product_attributes (
643
+ id,
644
+ name,
645
+ slug,
646
+ name_translations
647
+ )
648
+ )
649
+ )
650
+ ),
651
+ translation_group_id
652
+ `).eq("language_id", s.id).eq("status", "active");
653
+ return i.length > 0 && (o = o.or(i.join(","))), await o.order("id");
654
+ }
655
+ async function I(e, t, r) {
656
+ const { data: a } = await e.from("products").select("translation_group_id").eq("id", t).single();
657
+ if (!a || !a.translation_group_id) {
658
+ if (await e.from("product_categories").delete().eq("product_id", t), r.length > 0) {
659
+ const i = r.map((o) => ({
660
+ product_id: t,
661
+ category_id: o
662
+ }));
663
+ await e.from("product_categories").insert(i);
664
+ }
665
+ return;
666
+ }
667
+ const { data: c } = await e.from("products").select("id").eq("translation_group_id", a.translation_group_id), s = c && c.length > 0 ? c.map((i) => i.id) : [t];
668
+ if (await e.from("product_categories").delete().in("product_id", s), r.length > 0) {
669
+ const i = [];
670
+ for (const o of s)
671
+ for (const d of r)
672
+ i.push({
673
+ product_id: o,
674
+ category_id: d
675
+ });
676
+ await e.from("product_categories").insert(i);
677
+ }
678
+ }
679
+ export {
680
+ G as a,
681
+ L as b,
682
+ W as c,
683
+ J as d,
684
+ Q as e,
685
+ X as f,
686
+ O as g,
687
+ K as h,
688
+ I as i,
689
+ V as s,
690
+ H as u
691
+ };
@@ -0,0 +1,7 @@
1
+ "use strict";const v=require("@nextblock-cms/db/server"),m=require("./currency.cjs.js"),y=require("@nextblock-cms/utils"),b=require("./variation-utils.cjs.js"),S=["auto","manual"];function p(e){if(typeof e=="string"){const o=e.trim().toLowerCase();if(S.includes(o))return o}return"auto"}function C(e,o=0){return typeof e=="number"&&Number.isFinite(e)&&e>=0?Math.round(e):o}function h(e){const o=y.normalizeCurrencyCode(e.sourceCurrencyCode),t=m.normalizePriceMap(e.amountMap);if(t[o]!==void 0)return o;const c=Object.keys(t)[0];return c?y.normalizeCurrencyCode(c):m.getDefaultCurrency(e.currencies).code}function O(e){const o=p(e.mode),t=m.normalizePriceMap(e.costAmounts),c=m.normalizePriceMap(e.minOrderAmounts),r=h({amountMap:Object.keys(t).length>0?t:c,sourceCurrencyCode:e.sourceCurrencyCode,currencies:e.currencies}),u=t[r]??C(e.fallbackCostAmount),s=c[r]??C(e.fallbackMinOrderAmount);if(o==="auto")return{mode:o,sourceCurrencyCode:r,costAmounts:{[r]:u},minOrderAmounts:{[r]:s}};const a=Object.keys(t).length>0?{...t}:{[r]:u},d=Object.keys(c).length>0?{...c}:{[r]:s};return a[r]===void 0&&(a[r]=u),d[r]===void 0&&(d[r]=s),{mode:o,sourceCurrencyCode:r,costAmounts:a,minOrderAmounts:d}}function M(e){const o=p(e.mode),t=m.normalizePriceMap(e.amountMap),c=y.normalizeCurrencyCode(e.currencyCode);if(o==="manual"&&t[c]!==void 0)return t[c];const r=h({amountMap:t,sourceCurrencyCode:e.sourceCurrencyCode,currencies:e.currencies}),u=t[r]??C(e.fallbackAmount);return c===r?u:m.convertMinorUnitAmount({amount:u,fromCurrencyCode:r,toCurrencyCode:c,currencies:e.currencies})}async function R(e,o,t,c){const r=v.getSsgSupabaseClient(),{data:u}=await r.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}),s=u??[],a=m.getDefaultCurrency(s),d=c||a.code,{data:l,error:z}=await r.from("shipping_zone_locations").select(`
2
+ zone_id,
3
+ country_code,
4
+ state_code,
5
+ postal_code,
6
+ shipping_zones!inner(priority_order)
7
+ `).eq("country_code",o.country).order("shipping_zones(priority_order)",{ascending:!0});if(z||!l||l.length===0)return[];let _=null;if(o.state){const n=l.find(i=>i.state_code===o.state);n&&(_=n.zone_id)}if(!_){const n=l.find(i=>!i.state_code&&!i.postal_code);n&&(_=n.zone_id)}_||(_=l[0].zone_id);const{data:f,error:A}=await r.from("shipping_zone_methods").select("id, zone_id, method_type, cost_amount, cost_amounts, cost_currency, currency_pricing_mode, min_order_amount, min_order_amounts, name, name_translations").eq("zone_id",_);if(A||!f)return[];const g=f.filter(n=>{const i=M({amountMap:n.min_order_amounts||{},fallbackAmount:n.min_order_amount||0,sourceCurrencyCode:n.cost_currency||a.code,mode:n.currency_pricing_mode,currencyCode:d,currencies:s});return e>=i}).map(n=>({id:n.id,name:b.resolveTranslatedText(n.name,n.name_translations||null,t),amount:M({amountMap:n.cost_amounts||{},fallbackAmount:n.cost_amount||0,sourceCurrencyCode:n.cost_currency||a.code,mode:n.currency_pricing_mode,currencyCode:d,currencies:s}),currency:d,type:n.method_type})).sort((n,i)=>n.amount-i.amount)[0];return g?[g]:[]}exports.normalizeShippingRateCurrencyMode=p;exports.resolveShippingOptions=R;exports.sanitizeShippingRateAmountMaps=O;