@nextblock-cms/ecom 0.12.12 → 0.12.14
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.d.ts +2 -0
- package/index.es.js +108 -106
- package/lib/components/Checkout.cjs.js +1 -1
- package/lib/components/Checkout.es.js +32 -33
- package/lib/components/CustomerProfileForm.cjs.js +1 -1
- package/lib/components/CustomerProfileForm.es.js +185 -238
- package/lib/components/FreemiusLicensePanel.cjs.js +1 -0
- package/lib/components/FreemiusLicensePanel.d.ts +6 -0
- package/lib/components/FreemiusLicensePanel.es.js +66 -0
- package/lib/components/InvoiceViewerShell.cjs.js +2 -2
- package/lib/components/InvoiceViewerShell.d.ts +3 -1
- package/lib/components/InvoiceViewerShell.es.js +13 -11
- package/lib/customer-orders.cjs.js +1 -1
- package/lib/customer-orders.d.ts +2 -0
- package/lib/customer-orders.es.js +30 -27
- package/lib/freemius-license-types.d.ts +19 -0
- package/lib/freemius-license.cjs.js +1 -0
- package/lib/freemius-license.d.ts +20 -0
- package/lib/freemius-license.es.js +41 -0
- package/lib/freemius-order-sync.cjs.js +1 -1
- package/lib/freemius-order-sync.es.js +62 -52
- package/lib/pages/cms/orders/types.d.ts +0 -1
- package/lib/pages/cms/products/actions.cjs.js +1 -1
- package/lib/pages/cms/products/actions.es.js +1 -1
- package/lib/pages/cms/products/components/CurrencyPriceFields.cjs.js +1 -1
- package/lib/pages/cms/products/components/CurrencyPriceFields.es.js +104 -62
- package/lib/product-actions.cjs.js +3 -3
- package/lib/product-actions.d.ts +2 -1
- package/lib/product-actions.es.js +45 -44
- package/lib/server-actions/customer-actions.cjs.js +1 -1
- package/lib/server-actions/customer-actions.d.ts +0 -1
- package/lib/server-actions/customer-actions.es.js +12 -13
- package/package.json +4 -4
- package/server.cjs.js +1 -1
- package/server.d.ts +1 -0
- package/server.es.js +77 -75
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import f from "crypto";
|
|
2
|
-
import { Freemius as
|
|
3
|
-
import { createClient as
|
|
4
|
-
import { assignInvoiceMetadata as
|
|
5
|
-
import { applyOrderInventoryDeduction as
|
|
6
|
-
import { readFreemiusEnvValue as m, parseFreemiusCheckoutCredentialsMap as
|
|
2
|
+
import { Freemius as C } from "@freemius/sdk";
|
|
3
|
+
import { createClient as k } from "@supabase/supabase-js";
|
|
4
|
+
import { assignInvoiceMetadata as A } from "./invoice-server.es.js";
|
|
5
|
+
import { applyOrderInventoryDeduction as O } from "./order-inventory.es.js";
|
|
6
|
+
import { readFreemiusEnvValue as m, parseFreemiusCheckoutCredentialsMap as R, resolveFreemiusCheckoutCredentials as K } from "./providers/freemius.es.js";
|
|
7
7
|
function s(e) {
|
|
8
8
|
return e == null || e === "" ? null : String(e);
|
|
9
9
|
}
|
|
@@ -14,7 +14,7 @@ function a(e) {
|
|
|
14
14
|
const t = r.includes("T") ? r : r.replace(" ", "T") + "Z", i = new Date(t);
|
|
15
15
|
return Number.isNaN(i.getTime()) ? r : i.toISOString();
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function j(e) {
|
|
18
18
|
if (e == null || e === "")
|
|
19
19
|
return null;
|
|
20
20
|
const r = Number(e);
|
|
@@ -22,11 +22,11 @@ function R(e) {
|
|
|
22
22
|
}
|
|
23
23
|
function _(...e) {
|
|
24
24
|
return e.some((r) => {
|
|
25
|
-
const t =
|
|
25
|
+
const t = j(r);
|
|
26
26
|
return t !== null && t > 0;
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
-
function
|
|
29
|
+
function o(e) {
|
|
30
30
|
return e === "pending" || e === "trial";
|
|
31
31
|
}
|
|
32
32
|
function p(e) {
|
|
@@ -40,10 +40,10 @@ function p(e) {
|
|
|
40
40
|
"trial.expired"
|
|
41
41
|
].includes(e || "");
|
|
42
42
|
}
|
|
43
|
-
function
|
|
43
|
+
function x(e, r) {
|
|
44
44
|
return e === "license.extended" && (r?.is_renewal === !0 || r?.is_renewal === 1 || r?.is_renewal === "1" || r?.is_renewal === "true");
|
|
45
45
|
}
|
|
46
|
-
function
|
|
46
|
+
function B(e) {
|
|
47
47
|
const r = e.objects?.payment ?? e.data?.payment ?? e.data;
|
|
48
48
|
return e.type === "payment.created" && _(
|
|
49
49
|
r?.gross,
|
|
@@ -52,7 +52,7 @@ function j(e) {
|
|
|
52
52
|
r?.initial_amount
|
|
53
53
|
);
|
|
54
54
|
}
|
|
55
|
-
function
|
|
55
|
+
function N(e) {
|
|
56
56
|
const r = e?.purchase ?? null, t = e?.trial ?? null;
|
|
57
57
|
return {
|
|
58
58
|
freemius_product_id: s(r?.plugin_id) ?? s(r?.product_id) ?? s(r?.productId) ?? s(t?.plugin_id) ?? s(t?.product_id) ?? s(t?.productId),
|
|
@@ -64,9 +64,9 @@ function x(e) {
|
|
|
64
64
|
freemius_trial_ends_at: a(t?.trial_ends_at) ?? a(t?.trialEndsAt) ?? a(r?.trial_ends) ?? a(r?.trialEnds)
|
|
65
65
|
};
|
|
66
66
|
}
|
|
67
|
-
function
|
|
67
|
+
function M(e, r = "pending") {
|
|
68
68
|
const t = e?.purchase ?? null, i = e?.trial ?? null;
|
|
69
|
-
return r === "paid" ? "paid" : i && (i.canceled_at || i.canceledAt) &&
|
|
69
|
+
return r === "paid" ? "paid" : i && (i.canceled_at || i.canceledAt) && o(r) ? "cancelled" : i ? "trial" : t ? (t.canceled_at || t.canceledAt) && o(r) ? "cancelled" : _(
|
|
70
70
|
t.initial_amount,
|
|
71
71
|
t.initialAmount,
|
|
72
72
|
t.gross,
|
|
@@ -76,15 +76,21 @@ function N(e, r = "pending") {
|
|
|
76
76
|
}
|
|
77
77
|
function y(e) {
|
|
78
78
|
const { event: r, currentStatus: t, purchaseData: i } = e;
|
|
79
|
-
return t === "paid" && p(r.type) ? "paid" : p(r.type) ?
|
|
79
|
+
return t === "paid" && p(r.type) ? "paid" : p(r.type) ? o(t) ? "cancelled" : t : B(r) || x(r.type, r.data) || i && _(
|
|
80
80
|
i.initialAmount,
|
|
81
81
|
i.initial_amount,
|
|
82
82
|
i.gross,
|
|
83
83
|
i.totalGross,
|
|
84
84
|
i.total_gross
|
|
85
|
-
) ? "paid" : i?.canceled === !0 &&
|
|
85
|
+
) ? "paid" : i?.canceled === !0 && o(t) ? "cancelled" : t === "pending" && (i?.trialEndsAt || i?.trial_ends_at || i?.trialEnds || i?.trial_ends) || (i?.subscriptionId || i?.subscription_id || r.type === "subscription.created" || r.type === "license.created") && t === "pending" ? "trial" : t;
|
|
86
|
+
}
|
|
87
|
+
function P(e, ...r) {
|
|
88
|
+
const t = r.filter(
|
|
89
|
+
(i) => !!i
|
|
90
|
+
);
|
|
91
|
+
return e === "paid" || t.includes("paid") ? "paid" : t.includes("cancelled") ? "cancelled" : t.includes("trial") ? "trial" : t.includes("shipped") ? "shipped" : t.includes("refunded") ? "refunded" : e;
|
|
86
92
|
}
|
|
87
|
-
function
|
|
93
|
+
function T(e, r) {
|
|
88
94
|
const t = {
|
|
89
95
|
freemius_last_event_type: r,
|
|
90
96
|
freemius_last_synced_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
@@ -97,14 +103,14 @@ function b() {
|
|
|
97
103
|
const e = process.env.NEXT_PUBLIC_SUPABASE_URL || process.env.SUPABASE_URL, r = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_SECRET_KEY;
|
|
98
104
|
if (!e || !r)
|
|
99
105
|
throw new Error("Missing Supabase Service Role environment variables");
|
|
100
|
-
return
|
|
106
|
+
return k(e, r, {
|
|
101
107
|
auth: {
|
|
102
108
|
autoRefreshToken: !1,
|
|
103
109
|
persistSession: !1
|
|
104
110
|
}
|
|
105
111
|
});
|
|
106
112
|
}
|
|
107
|
-
async function
|
|
113
|
+
async function U(e, r) {
|
|
108
114
|
const { data: t, error: i } = await e.from("orders").select(
|
|
109
115
|
"id, status, provider, freemius_product_id, freemius_plan_id, freemius_license_id, freemius_subscription_id, freemius_trial_id, freemius_user_id, freemius_trial_ends_at"
|
|
110
116
|
).eq("id", r).single();
|
|
@@ -112,7 +118,7 @@ async function M(e, r) {
|
|
|
112
118
|
throw new Error(i?.message || "Order not found");
|
|
113
119
|
return t;
|
|
114
120
|
}
|
|
115
|
-
async function
|
|
121
|
+
async function L(e, r) {
|
|
116
122
|
const { data: t, error: i } = await e.from("orders").select(
|
|
117
123
|
"id, status, provider, freemius_product_id, freemius_plan_id, freemius_license_id, freemius_subscription_id, freemius_trial_id, freemius_user_id, freemius_trial_ends_at"
|
|
118
124
|
).eq("freemius_license_id", r).order("created_at", { ascending: !1 }).limit(1).maybeSingle();
|
|
@@ -121,24 +127,24 @@ async function P(e, r) {
|
|
|
121
127
|
return t ?? null;
|
|
122
128
|
}
|
|
123
129
|
async function S(e) {
|
|
124
|
-
const r =
|
|
130
|
+
const r = T(e.metadata, e.eventType), t = e.order.status, i = t === "paid" && e.nextStatus !== "paid" ? "paid" : e.nextStatus;
|
|
125
131
|
i !== t && (r.status = i);
|
|
126
132
|
const { error: n } = await e.client.from("orders").update(r).eq("id", e.order.id);
|
|
127
133
|
if (n)
|
|
128
134
|
throw new Error(n.message);
|
|
129
|
-
return i === "paid" && t !== "paid" && (await
|
|
135
|
+
return i === "paid" && t !== "paid" && (await A({
|
|
130
136
|
orderId: e.order.id,
|
|
131
137
|
client: e.client
|
|
132
|
-
}), await
|
|
138
|
+
}), await O(e.client, e.order.id)), i;
|
|
133
139
|
}
|
|
134
|
-
async function
|
|
135
|
-
const r = e.client ?? b(), t = await
|
|
140
|
+
async function H(e) {
|
|
141
|
+
const r = e.client ?? b(), t = await U(r, e.orderId);
|
|
136
142
|
if (t.provider !== "freemius")
|
|
137
143
|
return {
|
|
138
144
|
success: !1,
|
|
139
145
|
error: "Only Freemius orders can be synced from Freemius checkout"
|
|
140
146
|
};
|
|
141
|
-
const i =
|
|
147
|
+
const i = N(e.checkoutResponse);
|
|
142
148
|
let n = null;
|
|
143
149
|
if (i.freemius_license_id)
|
|
144
150
|
try {
|
|
@@ -146,35 +152,39 @@ async function V(e) {
|
|
|
146
152
|
i.freemius_product_id ?? t.freemius_product_id,
|
|
147
153
|
i.freemius_license_id
|
|
148
154
|
);
|
|
149
|
-
} catch (
|
|
150
|
-
console.error("[Freemius Checkout Sync] Failed to retrieve purchase data:",
|
|
155
|
+
} catch (F) {
|
|
156
|
+
console.error("[Freemius Checkout Sync] Failed to retrieve purchase data:", F);
|
|
151
157
|
}
|
|
152
158
|
const c = {
|
|
153
159
|
type: "checkout.purchaseCompleted",
|
|
154
160
|
data: { license_id: i.freemius_license_id },
|
|
155
161
|
objects: {}
|
|
156
|
-
},
|
|
162
|
+
}, u = {
|
|
157
163
|
...i,
|
|
158
164
|
...g(c, n)
|
|
159
|
-
},
|
|
165
|
+
}, d = M(
|
|
160
166
|
e.checkoutResponse,
|
|
161
167
|
t.status
|
|
162
168
|
), l = n ? y({
|
|
163
169
|
event: c,
|
|
164
170
|
currentStatus: t.status,
|
|
165
171
|
purchaseData: n
|
|
166
|
-
}) :
|
|
172
|
+
}) : null, I = d === "paid" ? t.status : d, w = P(
|
|
173
|
+
t.status,
|
|
174
|
+
I,
|
|
175
|
+
l
|
|
176
|
+
), v = await S({
|
|
167
177
|
client: r,
|
|
168
178
|
order: t,
|
|
169
|
-
nextStatus:
|
|
170
|
-
metadata:
|
|
179
|
+
nextStatus: w,
|
|
180
|
+
metadata: u,
|
|
171
181
|
eventType: "checkout.purchaseCompleted"
|
|
172
182
|
});
|
|
173
183
|
return {
|
|
174
184
|
success: !0,
|
|
175
185
|
orderId: t.id,
|
|
176
|
-
status:
|
|
177
|
-
metadata:
|
|
186
|
+
status: v,
|
|
187
|
+
metadata: u
|
|
178
188
|
};
|
|
179
189
|
}
|
|
180
190
|
function E(e) {
|
|
@@ -195,15 +205,15 @@ function g(e, r) {
|
|
|
195
205
|
async function h(e, r) {
|
|
196
206
|
if (!e)
|
|
197
207
|
return null;
|
|
198
|
-
const t =
|
|
199
|
-
return !t.apiKey || !t.secretKey || !t.publicKey ? null : new
|
|
208
|
+
const t = K(e);
|
|
209
|
+
return !t.apiKey || !t.secretKey || !t.publicKey ? null : new C({
|
|
200
210
|
productId: Number(e),
|
|
201
211
|
apiKey: t.apiKey,
|
|
202
212
|
secretKey: t.secretKey,
|
|
203
213
|
publicKey: t.publicKey
|
|
204
214
|
}).purchase.retrievePurchaseData(r);
|
|
205
215
|
}
|
|
206
|
-
async function
|
|
216
|
+
async function Z(e) {
|
|
207
217
|
const r = e.client ?? b(), t = E(e.event);
|
|
208
218
|
if (!t)
|
|
209
219
|
return {
|
|
@@ -212,7 +222,7 @@ async function X(e) {
|
|
|
212
222
|
reason: "missing_license_id",
|
|
213
223
|
type: e.event.type
|
|
214
224
|
};
|
|
215
|
-
const i = await
|
|
225
|
+
const i = await L(r, t);
|
|
216
226
|
if (!i)
|
|
217
227
|
return {
|
|
218
228
|
success: !0,
|
|
@@ -230,29 +240,29 @@ async function X(e) {
|
|
|
230
240
|
} catch (l) {
|
|
231
241
|
console.error("[Freemius Sync] Failed to retrieve purchase data:", l);
|
|
232
242
|
}
|
|
233
|
-
const c = g(e.event, n),
|
|
243
|
+
const c = g(e.event, n), u = y({
|
|
234
244
|
event: e.event,
|
|
235
245
|
currentStatus: i.status,
|
|
236
246
|
purchaseData: n
|
|
237
|
-
}),
|
|
247
|
+
}), d = await S({
|
|
238
248
|
client: r,
|
|
239
249
|
order: i,
|
|
240
|
-
nextStatus:
|
|
250
|
+
nextStatus: u,
|
|
241
251
|
metadata: c,
|
|
242
252
|
eventType: e.event.type ?? null
|
|
243
253
|
});
|
|
244
254
|
return {
|
|
245
255
|
success: !0,
|
|
246
256
|
orderId: i.id,
|
|
247
|
-
status:
|
|
257
|
+
status: d,
|
|
248
258
|
type: e.event.type
|
|
249
259
|
};
|
|
250
260
|
}
|
|
251
|
-
function
|
|
261
|
+
function q() {
|
|
252
262
|
const e = [
|
|
253
263
|
m("FREEMIUS_SECRET_KEY"),
|
|
254
264
|
m("FREEMIUS_ECOMMERCE_SANDBOX_SECRET_KEY"),
|
|
255
|
-
...Object.values(
|
|
265
|
+
...Object.values(R() ?? {}).map(
|
|
256
266
|
(r) => r.secretKey ?? null
|
|
257
267
|
)
|
|
258
268
|
];
|
|
@@ -260,8 +270,8 @@ function T() {
|
|
|
260
270
|
new Set(e.filter((r) => !!r))
|
|
261
271
|
);
|
|
262
272
|
}
|
|
263
|
-
function
|
|
264
|
-
return r ?
|
|
273
|
+
function J(e, r) {
|
|
274
|
+
return r ? q().some((t) => {
|
|
265
275
|
const i = f.createHmac("sha256", t).update(e).digest("hex");
|
|
266
276
|
try {
|
|
267
277
|
return f.timingSafeEqual(
|
|
@@ -274,11 +284,11 @@ function z(e, r) {
|
|
|
274
284
|
}) : !1;
|
|
275
285
|
}
|
|
276
286
|
export {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
287
|
+
N as extractFreemiusCheckoutMetadata,
|
|
288
|
+
q as getFreemiusWebhookSecretCandidates,
|
|
289
|
+
M as resolveFreemiusStatusFromCheckoutResponse,
|
|
280
290
|
y as resolveFreemiusStatusFromWebhookEvent,
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
291
|
+
H as syncFreemiusCheckoutOrder,
|
|
292
|
+
Z as syncFreemiusOrderFromWebhookEvent,
|
|
293
|
+
J as verifyFreemiusWebhookSignature
|
|
284
294
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("@nextblock-cms/db/server"),d=require("../../../product-actions.cjs.js"),l=require("../../../providers/freemius.cjs.js"),u=require("next/cache");async function _(t){const r=o.createClient(),{data:a,count:e,error:i}=await await d.getProducts(r,t);if(i)throw new Error(i.message);return{data:a,count:e}}async function m(t){const r=o.createClient(),{data:a,error:e}=await d.getProduct(r,t);if(e)throw new Error(e.message);return a}async function p(){try{const t=await l.syncFreemiusProductsToSupabase();return u.revalidatePath("/cms/products","page"),{success:!0,data:t}}catch(t){return{error:t.message||"Failed to sync with Freemius"}}}async function g(t){try{const r=await l.syncSingleFreemiusProduct(t);return u.revalidatePath("/cms/products","page"),{success:!0,data:r}}catch(r){return{error:r.message||"Failed to sync product with Freemius"}}}async function f(t,r){try{const a=o.createClient();return await d.copyProductFromLanguage(a,t,r),u.revalidatePath("/cms/products"),u.revalidatePath(`/cms/products/${t}/edit`),{success:!0}}catch(a){return{success:!1,error:a.message||"Failed to copy product content"}}}async function y(t){const r=o.createClient(),{data:a,error:e}=await r.from("products").select("id, title, language_id, slug").eq("translation_group_id",t);if(e)throw new Error(e.message);return a}async function w(){const t=o.getServiceRoleSupabaseClient(),{data:r,error:a}=await t.from("product_attributes").select(`
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("@nextblock-cms/db/server"),d=require("../../../product-actions.cjs.js"),l=require("../../../providers/freemius.cjs.js"),u=require("next/cache");async function _(t){const r=o.createClient(),{data:a,count:e,error:i}=await await d.getProducts(r,{...t,status:"all"});if(i)throw new Error(i.message);return{data:a,count:e}}async function m(t){const r=o.createClient(),{data:a,error:e}=await d.getProduct(r,t);if(e)throw new Error(e.message);return a}async function p(){try{const t=await l.syncFreemiusProductsToSupabase();return u.revalidatePath("/cms/products","page"),{success:!0,data:t}}catch(t){return{error:t.message||"Failed to sync with Freemius"}}}async function g(t){try{const r=await l.syncSingleFreemiusProduct(t);return u.revalidatePath("/cms/products","page"),{success:!0,data:r}}catch(r){return{error:r.message||"Failed to sync product with Freemius"}}}async function f(t,r){try{const a=o.createClient();return await d.copyProductFromLanguage(a,t,r),u.revalidatePath("/cms/products"),u.revalidatePath(`/cms/products/${t}/edit`),{success:!0}}catch(a){return{success:!1,error:a.message||"Failed to copy product content"}}}async function y(t){const r=o.createClient(),{data:a,error:e}=await r.from("products").select("id, title, language_id, slug").eq("translation_group_id",t);if(e)throw new Error(e.message);return a}async function w(){const t=o.getServiceRoleSupabaseClient(),{data:r,error:a}=await t.from("product_attributes").select(`
|
|
2
2
|
id,
|
|
3
3
|
name,
|
|
4
4
|
name_translations,
|
|
@@ -4,7 +4,7 @@ import { getProduct as l, getProducts as _, copyProductFromLanguage as m } from
|
|
|
4
4
|
import { syncSingleFreemiusProduct as p, syncFreemiusProductsToSupabase as g } from "../../../providers/freemius.es.js";
|
|
5
5
|
import { revalidatePath as u } from "next/cache";
|
|
6
6
|
async function v(r) {
|
|
7
|
-
const t = o(), { data: a, count: e, error: s } = await await _(t, r);
|
|
7
|
+
const t = o(), { data: a, count: e, error: s } = await await _(t, { ...r, status: "all" });
|
|
8
8
|
if (s) throw new Error(s.message);
|
|
9
9
|
return { data: a, count: e };
|
|
10
10
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("react/jsx-runtime"),N=require("react"),y=require("@nextblock-cms/ui"),w=require("@nextblock-cms/utils"),v=require("../../../../currency.cjs.js");function C(s){return w.getCurrencyMinorUnitFactor(s)===1?0:2}function h(s,d){return s==null||Number.isNaN(s)?"":s.toFixed(d)}function j({id:s,currencyCode:d,value:n,disabled:x,placeholder:p,className:f,allowEmpty:g=!1,onValueChange:i}){const c=C(d),[u,m]=N.useState(!1),[b,l]=N.useState(()=>h(n,c));return N.useEffect(()=>{u||l(h(n,c))},[n,c,u]),t.jsx(y.Input,{id:s,type:"text",inputMode:"decimal",value:b,disabled:x,placeholder:p,className:f,onFocus:()=>m(!0),onChange:e=>{let r=e.target.value.replace(/[^0-9.]/g,"");const o=r.indexOf(".");if(o!==-1&&(r=r.slice(0,o+1)+r.slice(o+1).replace(/\./g,"")),l(r),r===""||r==="."){i(g?null:0);return}const a=Number(r);Number.isNaN(a)||i(a)},onBlur:()=>{if(m(!1),n!=null&&!Number.isNaN(n)){const e=Number(n.toFixed(c));e!==n&&i(e)}}})}function k({idPrefix:s="currency",currencies:d,prices:n,salePrices:x,managedCurrencyCodes:p=[],onPriceChange:f,onSalePriceChange:g,readOnly:i=!1,helperText:c,trailing:u}){const m=d.find(l=>l.is_default)??d[0],b=new Set(p);return m?t.jsxs("div",{className:"flex flex-col gap-2",children:[c&&t.jsx("p",{className:"text-xs text-muted-foreground bg-muted/20 p-2 rounded-md border",children:c}),t.jsx("div",{className:"divide-y divide-muted/50",children:d.map(l=>{const e=v.normalizeCurrencyRecord(l),r=e.is_default!==!0&&b.has(e.code),o=i||r;return t.jsxs("div",{className:"flex flex-wrap items-stretch gap-4 py-3 first:pt-0 last:pb-0",children:[t.jsxs("div",{className:"flex items-center gap-2 w-[160px] shrink-0 self-stretch border-r border-border/50 pr-4",children:[t.jsx("span",{className:"text-sm font-black tracking-tighter text-foreground",children:e.code}),t.jsx("span",{className:"text-[11px] text-muted-foreground font-medium",children:e.symbol}),e.is_default?t.jsx("span",{className:"text-[9px] uppercase font-bold bg-primary/10 text-primary px-1.5 py-0.5 rounded tracking-widest leading-none",children:"Default"}):r?t.jsx("span",{className:"text-[9px] uppercase font-bold bg-muted text-muted-foreground px-1.5 py-0.5 rounded tracking-widest leading-none",children:"Auto"}):null,!e.is_default&&t.jsxs("span",{className:"text-[9px] text-muted-foreground/80 font-medium tracking-wide leading-none ml-auto",children:["x",e.exchange_rate]})]}),t.jsxs("div",{className:"grid flex-1 grid-cols-2 items-end gap-3 sm:grid-cols-4",children:[t.jsxs("div",{className:"min-w-0 space-y-1",children:[t.jsx(y.Label,{htmlFor:`${s}-price-${e.code}`,className:"block text-[10px] uppercase font-bold text-muted-foreground tracking-widest",children:"Price"}),t.jsxs("div",{className:"relative",children:[t.jsx("span",{className:"absolute left-2.5 top-1/2 -translate-y-1/2 text-xs text-muted-foreground font-bold pointer-events-none",children:e.symbol}),t.jsx(j,{id:`${s}-price-${e.code}`,currencyCode:e.code,value:n[e.code],disabled:o,className:"h-8 w-full text-sm pl-6",onValueChange:a=>f(e.code,a??0)})]})]}),t.jsxs("div",{className:"min-w-0 space-y-1",children:[t.jsx(y.Label,{htmlFor:`${s}-sale-price-${e.code}`,className:"block text-[10px] uppercase font-bold text-muted-foreground tracking-widest",children:"Sale"}),t.jsxs("div",{className:"relative",children:[t.jsx("span",{className:"absolute left-2.5 top-1/2 -translate-y-1/2 text-xs text-muted-foreground font-bold pointer-events-none",children:e.symbol}),t.jsx(j,{id:`${s}-sale-price-${e.code}`,currencyCode:e.code,value:x[e.code],disabled:o,placeholder:r?"Auto":"—",allowEmpty:!0,className:"h-8 w-full text-sm pl-6",onValueChange:a=>g(e.code,a)})]})]}),u&&e.is_default?u:null]})]},e.code)})})]}):null}exports.CurrencyPriceFields=k;
|
|
@@ -1,99 +1,141 @@
|
|
|
1
1
|
'use client';
|
|
2
|
-
import { jsxs as
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
import { jsxs as d, jsx as r } from "react/jsx-runtime";
|
|
3
|
+
import { useState as h, useEffect as k } from "react";
|
|
4
|
+
import { Label as y, Input as C } from "@nextblock-cms/ui";
|
|
5
|
+
import { getCurrencyMinorUnitFactor as F } from "@nextblock-cms/utils";
|
|
6
|
+
import { normalizeCurrencyRecord as $ } from "../../../../currency.es.js";
|
|
7
|
+
function _(t) {
|
|
8
|
+
return F(t) === 1 ? 0 : 2;
|
|
9
|
+
}
|
|
10
|
+
function w(t, o) {
|
|
11
|
+
return t == null || Number.isNaN(t) ? "" : t.toFixed(o);
|
|
12
|
+
}
|
|
13
|
+
function v({
|
|
14
|
+
id: t,
|
|
15
|
+
currencyCode: o,
|
|
16
|
+
value: s,
|
|
17
|
+
disabled: f,
|
|
18
|
+
placeholder: x,
|
|
19
|
+
className: g,
|
|
20
|
+
allowEmpty: N = !1,
|
|
21
|
+
onValueChange: u
|
|
22
|
+
}) {
|
|
23
|
+
const a = _(o), [m, p] = h(!1), [b, l] = h(() => w(s, a));
|
|
24
|
+
return k(() => {
|
|
25
|
+
m || l(w(s, a));
|
|
26
|
+
}, [s, a, m]), /* @__PURE__ */ r(
|
|
27
|
+
C,
|
|
28
|
+
{
|
|
29
|
+
id: t,
|
|
30
|
+
type: "text",
|
|
31
|
+
inputMode: "decimal",
|
|
32
|
+
value: b,
|
|
33
|
+
disabled: f,
|
|
34
|
+
placeholder: x,
|
|
35
|
+
className: g,
|
|
36
|
+
onFocus: () => p(!0),
|
|
37
|
+
onChange: (e) => {
|
|
38
|
+
let n = e.target.value.replace(/[^0-9.]/g, "");
|
|
39
|
+
const c = n.indexOf(".");
|
|
40
|
+
if (c !== -1 && (n = n.slice(0, c + 1) + n.slice(c + 1).replace(/\./g, "")), l(n), n === "" || n === ".") {
|
|
41
|
+
u(N ? null : 0);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const i = Number(n);
|
|
45
|
+
Number.isNaN(i) || u(i);
|
|
46
|
+
},
|
|
47
|
+
onBlur: () => {
|
|
48
|
+
if (p(!1), s != null && !Number.isNaN(s)) {
|
|
49
|
+
const e = Number(s.toFixed(a));
|
|
50
|
+
e !== s && u(e);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
function z({
|
|
57
|
+
idPrefix: t = "currency",
|
|
58
|
+
currencies: o,
|
|
59
|
+
prices: s,
|
|
9
60
|
salePrices: f,
|
|
10
61
|
managedCurrencyCodes: x = [],
|
|
11
62
|
onPriceChange: g,
|
|
12
|
-
onSalePriceChange:
|
|
13
|
-
readOnly:
|
|
14
|
-
helperText:
|
|
15
|
-
trailing:
|
|
63
|
+
onSalePriceChange: N,
|
|
64
|
+
readOnly: u = !1,
|
|
65
|
+
helperText: a,
|
|
66
|
+
trailing: m
|
|
16
67
|
}) {
|
|
17
|
-
const
|
|
18
|
-
return
|
|
19
|
-
|
|
20
|
-
/* @__PURE__ */
|
|
21
|
-
const e =
|
|
22
|
-
return /* @__PURE__ */
|
|
68
|
+
const p = o.find((l) => l.is_default) ?? o[0], b = new Set(x);
|
|
69
|
+
return p ? /* @__PURE__ */ d("div", { className: "flex flex-col gap-2", children: [
|
|
70
|
+
a && /* @__PURE__ */ r("p", { className: "text-xs text-muted-foreground bg-muted/20 p-2 rounded-md border", children: a }),
|
|
71
|
+
/* @__PURE__ */ r("div", { className: "divide-y divide-muted/50", children: o.map((l) => {
|
|
72
|
+
const e = $(l), n = e.is_default !== !0 && b.has(e.code), c = u || n;
|
|
73
|
+
return /* @__PURE__ */ d(
|
|
23
74
|
"div",
|
|
24
75
|
{
|
|
25
76
|
className: "flex flex-wrap items-stretch gap-4 py-3 first:pt-0 last:pb-0",
|
|
26
77
|
children: [
|
|
27
|
-
/* @__PURE__ */
|
|
28
|
-
/* @__PURE__ */
|
|
29
|
-
/* @__PURE__ */
|
|
30
|
-
e.is_default ? /* @__PURE__ */
|
|
31
|
-
!e.is_default && /* @__PURE__ */
|
|
78
|
+
/* @__PURE__ */ d("div", { className: "flex items-center gap-2 w-[160px] shrink-0 self-stretch border-r border-border/50 pr-4", children: [
|
|
79
|
+
/* @__PURE__ */ r("span", { className: "text-sm font-black tracking-tighter text-foreground", children: e.code }),
|
|
80
|
+
/* @__PURE__ */ r("span", { className: "text-[11px] text-muted-foreground font-medium", children: e.symbol }),
|
|
81
|
+
e.is_default ? /* @__PURE__ */ r("span", { className: "text-[9px] uppercase font-bold bg-primary/10 text-primary px-1.5 py-0.5 rounded tracking-widest leading-none", children: "Default" }) : n ? /* @__PURE__ */ r("span", { className: "text-[9px] uppercase font-bold bg-muted text-muted-foreground px-1.5 py-0.5 rounded tracking-widest leading-none", children: "Auto" }) : null,
|
|
82
|
+
!e.is_default && /* @__PURE__ */ d("span", { className: "text-[9px] text-muted-foreground/80 font-medium tracking-wide leading-none ml-auto", children: [
|
|
32
83
|
"x",
|
|
33
84
|
e.exchange_rate
|
|
34
85
|
] })
|
|
35
86
|
] }),
|
|
36
|
-
/* @__PURE__ */
|
|
37
|
-
/* @__PURE__ */
|
|
38
|
-
/* @__PURE__ */
|
|
39
|
-
|
|
87
|
+
/* @__PURE__ */ d("div", { className: "grid flex-1 grid-cols-2 items-end gap-3 sm:grid-cols-4", children: [
|
|
88
|
+
/* @__PURE__ */ d("div", { className: "min-w-0 space-y-1", children: [
|
|
89
|
+
/* @__PURE__ */ r(
|
|
90
|
+
y,
|
|
40
91
|
{
|
|
41
|
-
htmlFor: `${
|
|
92
|
+
htmlFor: `${t}-price-${e.code}`,
|
|
42
93
|
className: "block text-[10px] uppercase font-bold text-muted-foreground tracking-widest",
|
|
43
94
|
children: "Price"
|
|
44
95
|
}
|
|
45
96
|
),
|
|
46
|
-
/* @__PURE__ */
|
|
47
|
-
/* @__PURE__ */
|
|
48
|
-
/* @__PURE__ */
|
|
49
|
-
|
|
97
|
+
/* @__PURE__ */ d("div", { className: "relative", children: [
|
|
98
|
+
/* @__PURE__ */ r("span", { className: "absolute left-2.5 top-1/2 -translate-y-1/2 text-xs text-muted-foreground font-bold pointer-events-none", children: e.symbol }),
|
|
99
|
+
/* @__PURE__ */ r(
|
|
100
|
+
v,
|
|
50
101
|
{
|
|
51
|
-
id: `${
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
value: p[e.code] ?? "",
|
|
56
|
-
disabled: i,
|
|
102
|
+
id: `${t}-price-${e.code}`,
|
|
103
|
+
currencyCode: e.code,
|
|
104
|
+
value: s[e.code],
|
|
105
|
+
disabled: c,
|
|
57
106
|
className: "h-8 w-full text-sm pl-6",
|
|
58
|
-
|
|
59
|
-
e.code,
|
|
60
|
-
Number(l.target.value || 0)
|
|
61
|
-
)
|
|
107
|
+
onValueChange: (i) => g(e.code, i ?? 0)
|
|
62
108
|
}
|
|
63
109
|
)
|
|
64
110
|
] })
|
|
65
111
|
] }),
|
|
66
|
-
/* @__PURE__ */
|
|
67
|
-
/* @__PURE__ */
|
|
68
|
-
|
|
112
|
+
/* @__PURE__ */ d("div", { className: "min-w-0 space-y-1", children: [
|
|
113
|
+
/* @__PURE__ */ r(
|
|
114
|
+
y,
|
|
69
115
|
{
|
|
70
|
-
htmlFor: `${
|
|
116
|
+
htmlFor: `${t}-sale-price-${e.code}`,
|
|
71
117
|
className: "block text-[10px] uppercase font-bold text-muted-foreground tracking-widest",
|
|
72
118
|
children: "Sale"
|
|
73
119
|
}
|
|
74
120
|
),
|
|
75
|
-
/* @__PURE__ */
|
|
76
|
-
/* @__PURE__ */
|
|
77
|
-
/* @__PURE__ */
|
|
78
|
-
|
|
121
|
+
/* @__PURE__ */ d("div", { className: "relative", children: [
|
|
122
|
+
/* @__PURE__ */ r("span", { className: "absolute left-2.5 top-1/2 -translate-y-1/2 text-xs text-muted-foreground font-bold pointer-events-none", children: e.symbol }),
|
|
123
|
+
/* @__PURE__ */ r(
|
|
124
|
+
v,
|
|
79
125
|
{
|
|
80
|
-
id: `${
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
placeholder: s ? "Auto" : "—",
|
|
126
|
+
id: `${t}-sale-price-${e.code}`,
|
|
127
|
+
currencyCode: e.code,
|
|
128
|
+
value: f[e.code],
|
|
129
|
+
disabled: c,
|
|
130
|
+
placeholder: n ? "Auto" : "—",
|
|
131
|
+
allowEmpty: !0,
|
|
87
132
|
className: "h-8 w-full text-sm pl-6",
|
|
88
|
-
|
|
89
|
-
e.code,
|
|
90
|
-
l.target.value === "" ? null : Number(l.target.value)
|
|
91
|
-
)
|
|
133
|
+
onValueChange: (i) => N(e.code, i)
|
|
92
134
|
}
|
|
93
135
|
)
|
|
94
136
|
] })
|
|
95
137
|
] }),
|
|
96
|
-
|
|
138
|
+
m && e.is_default ? m : null
|
|
97
139
|
] })
|
|
98
140
|
]
|
|
99
141
|
},
|
|
@@ -103,5 +145,5 @@ function $({
|
|
|
103
145
|
] }) : null;
|
|
104
146
|
}
|
|
105
147
|
export {
|
|
106
|
-
|
|
148
|
+
z as CurrencyPriceFields
|
|
107
149
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("@nextblock-cms/utils/server"),P=require("./shared-inventory.cjs.js"),E=require("@nextblock-cms/utils/utils"),g=e=>Math.round(e*100);function y(e){return Object.entries(e||{}).reduce((t,[r,i])=>(typeof i=="number"&&Number.isFinite(i)&&i>=0&&(t[E.normalizeCurrencyCode(r)]=g(i)),t),{})}function M(e){return(e||[]).map(t=>({id:t.id,sku:t.sku,upc:t.upc??null,price:g(t.price),sale_price:typeof t.sale_price=="number"&&!isNaN(t.sale_price)?g(t.sale_price):null,prices:y(t.prices),sale_prices:y(t.sale_prices),sale_start_at:t.sale_start_at??null,sale_end_at:t.sale_end_at??null,stock_quantity:t.stock_quantity,main_media_id:t.main_media_id??null,attribute_term_ids:t.attribute_term_ids}))}function S(e,t){const i=e.product_type==="digital"&&e.payment_provider==="freemius"?Math.max(0,Number(e.trial_period_days??0)):0;return{id:t,product_type:e.product_type,payment_provider:e.payment_provider,title:e.title,slug:e.slug,sku:e.sku,upc:e.upc??null,stock:e.stock,status:e.status,short_description:e.short_description??null,description_json:e.description_json??null,metadata:{},price:g(e.price),sale_price:typeof e.sale_price=="number"&&!isNaN(e.sale_price)?g(e.sale_price):null,prices:y(e.prices),sale_prices:y(e.sale_prices),sale_start_at:e.sale_start_at??null,sale_end_at:e.sale_end_at??null,freemius_plan_id:e.freemius_plan_id??null,freemius_product_id:e.freemius_product_id??null,trial_period_days:i,trial_requires_payment_method:i>0?e.trial_requires_payment_method??!1:!1,is_taxable:e.is_taxable,language_id:e.language_id,translation_group_id:e.translation_group_id||void 0,variants:M(e.variants)}}async function j(e,t,r){const i=a=>{const o=typeof a=="string"?a.trim():"";return o||null},{error:c}=await e.from("products").update({meta_title:i(r.meta_title),meta_description:i(r.meta_description),custom_canonical:i(r.custom_canonical),updated_at:new Date().toISOString()}).eq("id",t);if(c)throw c}async function x(e,t,r){const{error:i}=await e.from("products").update({is_taxable:r,updated_at:new Date().toISOString()}).eq("id",t);if(i)throw i}async function F(e,t,r){const i=new Date().toISOString(),{data:c}=await e.from("products").select("sku, sale_price, sale_prices").eq("id",t).maybeSingle(),a=c?.sku??r.sku,{data:o}=await e.from("products").select("id").eq("sku",a),d=(o??[]).map(s=>s.id);d.length===0&&d.push(t);const{error:
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("@nextblock-cms/utils/server"),P=require("./shared-inventory.cjs.js"),E=require("@nextblock-cms/utils/utils"),g=e=>Math.round(e*100);function y(e){return Object.entries(e||{}).reduce((t,[r,i])=>(typeof i=="number"&&Number.isFinite(i)&&i>=0&&(t[E.normalizeCurrencyCode(r)]=g(i)),t),{})}function M(e){return(e||[]).map(t=>({id:t.id,sku:t.sku,upc:t.upc??null,price:g(t.price),sale_price:typeof t.sale_price=="number"&&!isNaN(t.sale_price)?g(t.sale_price):null,prices:y(t.prices),sale_prices:y(t.sale_prices),sale_start_at:t.sale_start_at??null,sale_end_at:t.sale_end_at??null,stock_quantity:t.stock_quantity,main_media_id:t.main_media_id??null,attribute_term_ids:t.attribute_term_ids}))}function S(e,t){const i=e.product_type==="digital"&&e.payment_provider==="freemius"?Math.max(0,Number(e.trial_period_days??0)):0;return{id:t,product_type:e.product_type,payment_provider:e.payment_provider,title:e.title,slug:e.slug,sku:e.sku,upc:e.upc??null,stock:e.stock,status:e.status,short_description:e.short_description??null,description_json:e.description_json??null,metadata:{},price:g(e.price),sale_price:typeof e.sale_price=="number"&&!isNaN(e.sale_price)?g(e.sale_price):null,prices:y(e.prices),sale_prices:y(e.sale_prices),sale_start_at:e.sale_start_at??null,sale_end_at:e.sale_end_at??null,freemius_plan_id:e.freemius_plan_id??null,freemius_product_id:e.freemius_product_id??null,trial_period_days:i,trial_requires_payment_method:i>0?e.trial_requires_payment_method??!1:!1,is_taxable:e.is_taxable,language_id:e.language_id,translation_group_id:e.translation_group_id||void 0,variants:M(e.variants)}}async function j(e,t,r){const i=a=>{const o=typeof a=="string"?a.trim():"";return o||null},{error:c}=await e.from("products").update({meta_title:i(r.meta_title),meta_description:i(r.meta_description),custom_canonical:i(r.custom_canonical),updated_at:new Date().toISOString()}).eq("id",t);if(c)throw c}async function x(e,t,r){const{error:i}=await e.from("products").update({is_taxable:r,updated_at:new Date().toISOString()}).eq("id",t);if(i)throw i}async function F(e,t,r){const i=new Date().toISOString(),{data:c}=await e.from("products").select("sku, sale_price, sale_prices").eq("id",t).maybeSingle(),a=c?.sku??r.sku,{data:o}=await e.from("products").select("id").eq("sku",a),d=(o??[]).map(s=>s.id);d.length===0&&d.push(t);const{error:_}=await e.from("products").update({sale_price:c?.sale_price??null,sale_prices:c?.sale_prices??null,sale_start_at:r.sale_start_at??null,sale_end_at:r.sale_end_at??null,updated_at:i}).in("id",d);if(_)throw _;const n=r.variants??[];if(n.length===0)return;const{data:u}=await e.from("product_variants").select("sku, sale_price, sale_prices").eq("product_id",t),p=new Map((u??[]).map(s=>[s.sku,s]));for(const s of n){const l=p.get(s.sku);if(!l)continue;const{error:m}=await e.from("product_variants").update({sale_price:l.sale_price??null,sale_prices:l.sale_prices??null,sale_start_at:s.sale_start_at??null,sale_end_at:s.sale_end_at??null,updated_at:i}).eq("sku",s.sku).in("product_id",d);if(m)throw m}}async function C(e,{page:t=1,limit:r=10,search:i="",languageId:c,categoryId:a,status:o="active"}={}){const d=(t-1)*r,_=d+r-1;let n=e.from("products").select("id, title, average_rating, total_reviews, 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))",{count:"exact"}).range(d,_).order("created_at",{ascending:!1});if(o!=="all"&&(n=n.eq("status",o)),c&&(n=n.eq("language_id",c)),a){const{data:u,error:p}=await e.from("product_categories").select("product_id").eq("category_id",a);if(p)return{data:[],error:p,count:0};const s=(u||[]).map(l=>l.product_id).filter(Boolean);if(s.length===0)return{data:[],error:null,count:0};n=n.in("id",s)}return i&&(n=n.or(`title.ilike.%${i}%,sku.ilike.%${i}%`)),n}async function T(e,t){return e.from("products").select(`
|
|
2
2
|
*,
|
|
3
3
|
languages (
|
|
4
4
|
code
|
|
@@ -166,7 +166,7 @@
|
|
|
166
166
|
description_translations
|
|
167
167
|
)
|
|
168
168
|
)
|
|
169
|
-
`).eq("slug",t);if(c)return{data:null,error:c};if(!i||i.length===0)return{data:null,error:{message:"Product not found",code:"PGRST116"}};let a=null;return r&&(a=i.find(o=>(Array.isArray(o.languages)?o.languages[0]:o.languages)?.code===r)),a||(a=i.find(o=>(Array.isArray(o.languages)?o.languages[0]:o.languages)?.is_default)),a||(a=i.find(o=>(Array.isArray(o.languages)?o.languages[0]:o.languages)?.code==="en")),a||(a=i[0]),{data:a,error:null}}async function R(e,t){const{data:r,error:i}=await e.rpc("upsert_product_with_variants",{product_payload:S(t)});if(i||!r)throw i||new Error("Failed to create product");if(t.product_media&&t.product_media.length>0){const a=t.product_media.map((o,d)=>({product_id:r,media_id:o.media_id,sort_order:d}));await e.from("product_media").insert(a)}else t.media_id&&await e.from("product_media").insert({product_id:r,media_id:t.media_id,sort_order:0});await x(e,r,t.is_taxable),await j(e,r,t),await F(e,r,t),await P.syncSharedInventoryForSavedProduct(r,t),t.category_ids!==void 0&&await q(e,r,t.category_ids);const{data:c}=await e.from("products").select("*").eq("id",r).single();return c}async function I(e,t,r){const{data:i}=await e.from("product_media").select("media_id").eq("product_id",t),c=i?.map(s=>s.media_id)||[],{data:a,error:o}=await e.rpc("upsert_product_with_variants",{product_payload:S(r,t)});if(o||!a)throw o||new Error("Failed to update product");if(r.product_media){if(await e.from("product_media").delete().eq("product_id",t),r.product_media.length>0){const s=r.product_media.map((
|
|
169
|
+
`).eq("slug",t);if(c)return{data:null,error:c};if(!i||i.length===0)return{data:null,error:{message:"Product not found",code:"PGRST116"}};let a=null;return r&&(a=i.find(o=>(Array.isArray(o.languages)?o.languages[0]:o.languages)?.code===r)),a||(a=i.find(o=>(Array.isArray(o.languages)?o.languages[0]:o.languages)?.is_default)),a||(a=i.find(o=>(Array.isArray(o.languages)?o.languages[0]:o.languages)?.code==="en")),a||(a=i[0]),{data:a,error:null}}async function R(e,t){const{data:r,error:i}=await e.rpc("upsert_product_with_variants",{product_payload:S(t)});if(i||!r)throw i||new Error("Failed to create product");if(t.product_media&&t.product_media.length>0){const a=t.product_media.map((o,d)=>({product_id:r,media_id:o.media_id,sort_order:d}));await e.from("product_media").insert(a)}else t.media_id&&await e.from("product_media").insert({product_id:r,media_id:t.media_id,sort_order:0});await x(e,r,t.is_taxable),await j(e,r,t),await F(e,r,t),await P.syncSharedInventoryForSavedProduct(r,t),t.category_ids!==void 0&&await q(e,r,t.category_ids);const{data:c}=await e.from("products").select("*").eq("id",r).single();return c}async function I(e,t,r){const{data:i}=await e.from("product_media").select("media_id").eq("product_id",t),c=i?.map(s=>s.media_id)||[],{data:a,error:o}=await e.rpc("upsert_product_with_variants",{product_payload:S(r,t)});if(o||!a)throw o||new Error("Failed to update product");if(r.product_media){if(await e.from("product_media").delete().eq("product_id",t),r.product_media.length>0){const s=r.product_media.map((l,m)=>({product_id:t,media_id:l.media_id,sort_order:m}));await e.from("product_media").insert(s)}}else r.media_id&&(await e.from("product_media").delete().eq("product_id",t),await e.from("product_media").insert({product_id:t,media_id:r.media_id,sort_order:0}));await x(e,a,r.is_taxable),await j(e,a,r),await F(e,a,r);const d=r.product_media?r.product_media.map(s=>s.media_id):r.media_id?[r.media_id]:[],_=r.explicitly_removed_media_ids||[],n=c.filter(s=>!d.includes(s)),u=Array.from(new Set([...n,..._]));if(u.length>0)for(const s of u){const{count:l}=await e.from("product_media").select("*",{count:"exact",head:!0}).eq("media_id",s);if(l&&l>0)continue;const{count:m}=await e.from("posts").select("*",{count:"exact",head:!0}).eq("feature_image_id",s);if(m&&m>0)continue;const{count:h}=await e.from("logos").select("*",{count:"exact",head:!0}).eq("media_id",s);if(h&&h>0)continue;const{count:w}=await e.from("product_variants").select("*",{count:"exact",head:!0}).eq("main_media_id",s);if(w&&w>0)continue;const{data:f}=await e.from("media").select("object_key, variants").eq("id",s).single();if(f){const v=[f.object_key];f.variants&&Array.isArray(f.variants)&&f.variants.forEach(k=>{k.objectKey&&v.push(k.objectKey)}),await b.deleteMediaFiles(v),await e.from("media").delete().eq("id",s)}}await P.syncSharedInventoryForSavedProduct(a,r),r.category_ids!==void 0&&await q(e,t,r.category_ids);const{data:p}=await e.from("products").select("*").eq("id",a).single();return p}async function D(e,t){const{error:r}=await e.from("products").delete().eq("id",t);if(r)throw r;return!0}async function N(e,t,r){const{data:i,error:c}=await e.from("products").select("*").eq("id",r).single();if(c||!i)throw new Error(c?.message||"Source product not found");const{data:a,error:o}=await e.from("products").select("language_id").eq("id",t).single();if(o||!a)throw new Error(o?.message||"Target product not found");const{error:d}=await e.from("products").update({title:i.title,short_description:i.short_description,description_json:i.description_json}).eq("id",t);if(d)throw d;const{data:_,error:n}=await e.from("blocks").select("*").eq("product_id",r).order("order",{ascending:!0});if(n)throw n;if(await e.from("blocks").delete().eq("product_id",t),_&&_.length>0){const p=_.map(l=>{const{id:m,created_at:h,updated_at:w,...f}=l;return{...f,product_id:t,language_id:a.language_id}}),{error:s}=await e.from("blocks").insert(p);if(s)throw s}await e.from("product_drafts").delete().eq("product_id",t),await e.from("product_media").delete().eq("product_id",t);const{data:u}=await e.from("product_media").select("media_id, sort_order").eq("product_id",r);if(u&&u.length>0){const p=u.map(s=>({product_id:t,media_id:s.media_id,sort_order:s.sort_order}));await e.from("product_media").insert(p)}return{success:!0}}async function $(e,t,r,i=[],c=[]){const{data:a}=await e.from("languages").select("id").eq("code",r).single();if(!a)return{data:[],error:"Language not found"};const o=[];t.length>0&&o.push(`translation_group_id.in.(${t.join(",")})`),i.length>0&&o.push(`sku.in.(${i.map(n=>`"${n}"`).join(",")})`),c.length>0&&o.push(`id.in.(${c.join(",")})`);let d=e.from("products").select(`
|
|
170
170
|
id,
|
|
171
171
|
title,
|
|
172
172
|
sku,
|
|
@@ -234,4 +234,4 @@
|
|
|
234
234
|
)
|
|
235
235
|
),
|
|
236
236
|
translation_group_id
|
|
237
|
-
`).eq("language_id",a.id).eq("status","active");return o.length>0&&(d=d.or(o.join(","))),await d.order("id")}async function q(e,t,r){const{data:i}=await e.from("products").select("translation_group_id").eq("id",t).single();if(!i||!i.translation_group_id){if(await e.from("product_categories").delete().eq("product_id",t),r.length>0){const o=r.map(d=>({product_id:t,category_id:d}));await e.from("product_categories").insert(o)}return}const{data:c}=await e.from("products").select("id").eq("translation_group_id",i.translation_group_id),a=c&&c.length>0?c.map(o=>o.id):[t];if(await e.from("product_categories").delete().in("product_id",a),r.length>0){const o=[];for(const d of a)for(const
|
|
237
|
+
`).eq("language_id",a.id).eq("status","active");return o.length>0&&(d=d.or(o.join(","))),await d.order("id")}async function q(e,t,r){const{data:i}=await e.from("products").select("translation_group_id").eq("id",t).single();if(!i||!i.translation_group_id){if(await e.from("product_categories").delete().eq("product_id",t),r.length>0){const o=r.map(d=>({product_id:t,category_id:d}));await e.from("product_categories").insert(o)}return}const{data:c}=await e.from("products").select("id").eq("translation_group_id",i.translation_group_id),a=c&&c.length>0?c.map(o=>o.id):[t];if(await e.from("product_categories").delete().in("product_id",a),r.length>0){const o=[];for(const d of a)for(const _ of r)o.push({product_id:d,category_id:_});await e.from("product_categories").insert(o)}}exports.copyProductFromLanguage=N;exports.createProduct=R;exports.deleteProduct=D;exports.fetchTranslatedProductsForCartInternal=$;exports.getProduct=T;exports.getProductBySlug=A;exports.getProducts=C;exports.syncCategoriesForTranslationGroup=q;exports.updateProduct=I;
|