@nextblock-cms/ecom 0.22.0 → 0.22.2
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 +1 -1
- package/index.es.js +31 -31
- package/lib/components/Checkout.cjs +1 -1
- package/lib/components/Checkout.es.js +2 -1
- package/lib/coupon-server.cjs +1 -1
- package/lib/coupon-server.d.ts +65 -1
- package/lib/coupon-server.es.js +151 -91
- package/lib/coupons.cjs +1 -1
- package/lib/coupons.d.ts +13 -0
- package/lib/coupons.es.js +5 -4
- package/lib/order-access-token.cjs +1 -0
- package/lib/order-access-token.d.ts +2 -0
- package/lib/order-access-token.es.js +18 -0
- package/lib/pages/cms/orders/actions.cjs +3 -3
- package/lib/pages/cms/orders/actions.es.js +48 -26
- package/lib/pages/cms/orders/export-actions.cjs +1 -1
- package/lib/pages/cms/orders/export-actions.d.ts +42 -1
- package/lib/pages/cms/orders/export-actions.es.js +18 -16
- package/lib/pages/cms/orders/server-actions.cjs +1 -1
- package/lib/pages/cms/orders/server-actions.es.js +25 -15
- package/lib/pages/cms/products/actions.cjs +4 -4
- package/lib/pages/cms/products/actions.es.js +44 -41
- package/lib/pages/cms/products/inventory/actions.cjs +2 -2
- package/lib/pages/cms/products/inventory/actions.es.js +23 -23
- package/lib/pages/cms/products/server-actions.cjs +1 -1
- package/lib/pages/cms/products/server-actions.es.js +109 -108
- package/lib/pages/cms/shipping/server-actions.cjs +1 -1
- package/lib/pages/cms/shipping/server-actions.es.js +27 -26
- package/lib/payment-config.cjs +1 -1
- package/lib/payment-config.d.ts +4 -2
- package/lib/payment-config.es.js +73 -72
- package/lib/product-actions.cjs +1 -1
- package/lib/product-actions.es.js +6 -6
- package/lib/providers/checkout-items.cjs +1 -0
- package/lib/providers/checkout-items.d.ts +30 -0
- package/lib/providers/checkout-items.es.js +43 -0
- package/lib/providers/freemius.cjs +1 -1
- package/lib/providers/freemius.d.ts +1 -1
- package/lib/providers/freemius.es.js +166 -144
- package/lib/providers/stripe.cjs +1 -1
- package/lib/providers/stripe.d.ts +1 -1
- package/lib/providers/stripe.es.js +191 -146
- package/lib/server-actions/coupon-actions.cjs +1 -1
- package/lib/server-actions/coupon-actions.d.ts +11 -12
- package/lib/server-actions/coupon-actions.es.js +19 -11
- package/lib/server-actions/require-ecom-role.cjs +1 -0
- package/lib/server-actions/require-ecom-role.d.ts +5825 -0
- package/lib/server-actions/require-ecom-role.es.js +24 -0
- package/lib/server-actions/shipping-actions.cjs +1 -1
- package/lib/server-actions/shipping-actions.es.js +4 -4
- package/lib/server-actions/tax-actions.cjs +1 -1
- package/lib/server-actions/tax-actions.es.js +22 -18
- package/lib/shipping/checkout-shipping-method.cjs +7 -0
- package/lib/shipping/checkout-shipping-method.d.ts +115 -0
- package/lib/shipping/checkout-shipping-method.es.js +167 -0
- package/lib/shipping/resolver.cjs +1 -7
- package/lib/shipping/resolver.d.ts +7 -12
- package/lib/shipping/resolver.es.js +13 -38
- package/package.json +4 -4
- package/server.cjs +1 -1
- package/server.d.ts +1 -0
- package/server.es.js +49 -48
|
@@ -18,10 +18,16 @@ async function a() {
|
|
|
18
18
|
let { data: i, error: a } = await e.from("profiles").select("role").eq("id", t.id).single();
|
|
19
19
|
return a || !i || !["ADMIN", "WRITER"].includes(i.role) ? { error: "Forbidden" } : {
|
|
20
20
|
supabase: e,
|
|
21
|
-
user: t
|
|
21
|
+
user: t,
|
|
22
|
+
role: i.role
|
|
22
23
|
};
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
+
var o = /* @__PURE__ */ new Set([
|
|
26
|
+
"paid",
|
|
27
|
+
"cancelled",
|
|
28
|
+
"refunded"
|
|
29
|
+
]);
|
|
30
|
+
function s() {
|
|
25
31
|
let e = process.env.SUPABASE_SERVICE_ROLE_KEY || process.env.SUPABASE_SECRET_KEY, t = process.env.NEXT_PUBLIC_SUPABASE_URL || process.env.SUPABASE_URL;
|
|
26
32
|
if (!e || !t) throw Error("Server configuration error");
|
|
27
33
|
return r(t, e, { auth: {
|
|
@@ -29,36 +35,40 @@ function o() {
|
|
|
29
35
|
persistSession: !1
|
|
30
36
|
} });
|
|
31
37
|
}
|
|
32
|
-
async function
|
|
33
|
-
let
|
|
34
|
-
if ("error" in
|
|
38
|
+
async function c(n, r) {
|
|
39
|
+
let c = await a();
|
|
40
|
+
if ("error" in c) return {
|
|
35
41
|
success: !1,
|
|
36
|
-
error:
|
|
42
|
+
error: c.error
|
|
37
43
|
};
|
|
38
44
|
if (!i.includes(r)) return {
|
|
39
45
|
success: !1,
|
|
40
46
|
error: "Invalid order status"
|
|
41
47
|
};
|
|
42
|
-
|
|
48
|
+
if (o.has(r) && c.role !== "ADMIN") return {
|
|
49
|
+
success: !1,
|
|
50
|
+
error: "Forbidden"
|
|
51
|
+
};
|
|
52
|
+
let l;
|
|
43
53
|
try {
|
|
44
|
-
|
|
54
|
+
l = s();
|
|
45
55
|
} catch (e) {
|
|
46
56
|
return {
|
|
47
57
|
success: !1,
|
|
48
58
|
error: e instanceof Error ? e.message : "Server configuration error"
|
|
49
59
|
};
|
|
50
60
|
}
|
|
51
|
-
let { data:
|
|
52
|
-
return
|
|
61
|
+
let { data: u, error: d } = await l.from("orders").update({ status: r }).eq("id", n).select();
|
|
62
|
+
return d ? (console.error("Order status update error:", d), {
|
|
53
63
|
success: !1,
|
|
54
|
-
error:
|
|
55
|
-
}) : !
|
|
64
|
+
error: d.message
|
|
65
|
+
}) : !u || u.length === 0 ? {
|
|
56
66
|
success: !1,
|
|
57
67
|
error: "Order not found or update failed."
|
|
58
68
|
} : (r === "paid" && (await t({
|
|
59
69
|
orderId: n,
|
|
60
|
-
client:
|
|
61
|
-
}), await e(
|
|
70
|
+
client: l
|
|
71
|
+
}), await e(l, n)), { success: !0 });
|
|
62
72
|
}
|
|
63
73
|
//#endregion
|
|
64
|
-
export {
|
|
74
|
+
export { c as updateOrderStatus };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use server";const e=require("../../../
|
|
1
|
+
"use server";const e=require("../../../server-actions/require-ecom-role.cjs"),t=require("../../../product-actions.cjs"),n=require("../../../providers/freemius.cjs");let r=require("@nextblock-cms/db/server"),i=require("next/cache");async function a(e){let n=(0,r.createClient)(),{data:i,count:a,error:o}=await await t.getProducts(n,{...e,status:`all`});if(o)throw Error(o.message);return{data:i,count:a}}async function o(e){let n=(0,r.createClient)(),{data:i,error:a}=await t.getProduct(n,e);if(a)throw Error(a.message);return i}async function s(){try{await e.requireEcomAdmin();let t=await n.syncFreemiusProductsToSupabase();return(0,i.revalidatePath)(`/cms/products`,`page`),{success:!0,data:t}}catch(e){return{error:e.message||`Failed to sync with Freemius`}}}async function c(t){try{await e.requireEcomAdmin();let r=await n.syncSingleFreemiusProduct(t);return(0,i.revalidatePath)(`/cms/products`,`page`),(0,i.revalidatePath)(`/cms/products/[id]/edit`,`page`),{success:!0,data:r}}catch(e){return{error:e.message||`Failed to sync product with Freemius`}}}async function l(e,n){try{let a=(0,r.createClient)();return await t.copyProductFromLanguage(a,e,n),(0,i.revalidatePath)(`/cms/products`),(0,i.revalidatePath)(`/cms/products/${e}/edit`),{success:!0}}catch(e){return{success:!1,error:e.message||`Failed to copy product content`}}}async function u(e){let{data:t,error:n}=await(0,r.createClient)().from(`products`).select(`id, title, language_id, slug`).eq(`translation_group_id`,e);if(n)throw Error(n.message);return t}async function d(){let{data:e,error:t}=await(0,r.getServiceRoleSupabaseClient)().from(`product_attributes`).select(`
|
|
2
2
|
id,
|
|
3
3
|
name,
|
|
4
4
|
name_translations,
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
sort_order,
|
|
12
12
|
value_translations
|
|
13
13
|
)
|
|
14
|
-
`);if(t)throw Error(t.message);return(e||[]).map(e=>({...e,product_attribute_terms:(e.product_attribute_terms||[]).sort((e,t)=>(e.sort_order??2**53-1)-(t.sort_order??2**53-1)||e.value.localeCompare(t.value))})).sort((e,t)=>e.name.localeCompare(t.name))}async function
|
|
14
|
+
`);if(t)throw Error(t.message);return(e||[]).map(e=>({...e,product_attribute_terms:(e.product_attribute_terms||[]).sort((e,t)=>(e.sort_order??2**53-1)-(t.sort_order??2**53-1)||e.value.localeCompare(t.value))})).sort((e,t)=>e.name.localeCompare(t.name))}async function f(e){let{data:t,error:n}=await(0,r.createClient)().from(`freemius_plans`).select(`
|
|
15
15
|
id,
|
|
16
16
|
name,
|
|
17
17
|
title,
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
override_lifetime_price,
|
|
27
27
|
is_active
|
|
28
28
|
)
|
|
29
|
-
`).eq(`product_id`,e);if(
|
|
29
|
+
`).eq(`product_id`,e);if(n)throw Error(n.message);return t}async function p(e,t){let{error:n}=await(0,r.createClient)().from(`freemius_pricing`).update({...t,updated_at:new Date().toISOString()}).eq(`id`,e);return n?{success:!1,error:n.message}:((0,i.revalidatePath)(`/cms/products`),{success:!0})}async function m(e){let t=(0,r.createClient)(),{data:n}=await t.from(`products`).select(`translation_group_id`).eq(`id`,e).maybeSingle(),i=[e];if(n?.translation_group_id){let{data:e}=await t.from(`products`).select(`id`).eq(`translation_group_id`,n.translation_group_id);e&&e.length>0&&(i=e.map(e=>e.id))}let{data:a,error:o}=await t.from(`freemius_plans`).select(`
|
|
30
30
|
id,
|
|
31
31
|
name,
|
|
32
32
|
title,
|
|
@@ -41,4 +41,4 @@
|
|
|
41
41
|
override_lifetime_price,
|
|
42
42
|
is_active
|
|
43
43
|
)
|
|
44
|
-
`).in(`product_id`,i);if(o)throw Error(o.message);return(a||[]).map(e=>({id:e.id,name:e.name,title:e.title,pricing:(e.freemius_pricing||[]).filter(e=>e.is_active!==!1).map(e=>({id:e.id,license_quota:e.license_quota,monthly_price:e.override_monthly_price??e.api_monthly_price,annual_price:e.override_annual_price??e.api_annual_price,lifetime_price:e.override_lifetime_price??e.api_lifetime_price,is_active:e.is_active}))}))}async function
|
|
44
|
+
`).in(`product_id`,i);if(o)throw Error(o.message);return(a||[]).map(e=>({id:e.id,name:e.name,title:e.title,pricing:(e.freemius_pricing||[]).filter(e=>e.is_active!==!1).map(e=>({id:e.id,license_quota:e.license_quota,monthly_price:e.override_monthly_price??e.api_monthly_price,annual_price:e.override_annual_price??e.api_annual_price,lifetime_price:e.override_lifetime_price??e.api_lifetime_price,is_active:e.is_active}))}))}async function h(){let{data:e,error:t}=await(0,r.createClient)().from(`categories`).select(`id, name, slug, description, created_at, name_translations, description_translations, product_categories(count)`).order(`name`,{ascending:!0});if(t)throw Error(t.message);return(e||[]).map(e=>{let t=e.product_categories?.[0]?.count??e.product_categories?.count??0;return{id:e.id,name:e.name,slug:e.slug,description:e.description??``,created_at:e.created_at,productCount:Number(t),name_translations:e.name_translations||{},description_translations:e.description_translations||{}}})}async function g(e){let{data:t,error:n}=await(0,r.createClient)().from(`categories`).select(`id, name, slug, description, created_at`).eq(`slug`,e).maybeSingle();if(n)throw Error(n.message);return t}async function _(e){let{data:t,error:n}=await(0,r.createClient)().from(`product_categories`).select(`category:categories(id, name, slug, description, name_translations, description_translations, created_at)`).eq(`product_id`,e);if(n)throw Error(n.message);return(t||[]).map(e=>e.category).filter(Boolean)}exports.copyProductFromLanguageAction=l,exports.getCategoriesWithCount=h,exports.getCategoryBySlug=g,exports.getFreemiusPricingByProductId=f,exports.getGlobalProductAttributes=d,exports.getProduct=o,exports.getProductCategories=_,exports.getProductTranslations=u,exports.getProducts=a,exports.getPublicFreemiusPricing=m,exports.triggerFreemiusSync=s,exports.triggerSingleProductSync=c,exports.updateFreemiusOverride=p;
|
|
@@ -1,51 +1,54 @@
|
|
|
1
1
|
"use server";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { requireEcomAdmin as e } from "../../../server-actions/require-ecom-role.es.js";
|
|
3
|
+
import { copyProductFromLanguage as t, getProduct as n, getProducts as r } from "../../../product-actions.es.js";
|
|
4
|
+
import { syncFreemiusProductsToSupabase as i, syncSingleFreemiusProduct as a } from "../../../providers/freemius.es.js";
|
|
5
|
+
import { createClient as o, getServiceRoleSupabaseClient as s } from "@nextblock-cms/db/server";
|
|
6
|
+
import { revalidatePath as c } from "next/cache";
|
|
6
7
|
//#region src/lib/pages/cms/products/actions.ts
|
|
7
|
-
async function
|
|
8
|
-
let t =
|
|
8
|
+
async function l(e) {
|
|
9
|
+
let t = o(), { data: n, count: i, error: a } = await await r(t, {
|
|
9
10
|
...e,
|
|
10
11
|
status: "all"
|
|
11
12
|
});
|
|
12
|
-
if (
|
|
13
|
+
if (a) throw Error(a.message);
|
|
13
14
|
return {
|
|
14
|
-
data:
|
|
15
|
+
data: n,
|
|
15
16
|
count: i
|
|
16
17
|
};
|
|
17
18
|
}
|
|
18
|
-
async function
|
|
19
|
-
let
|
|
19
|
+
async function u(e) {
|
|
20
|
+
let t = o(), { data: r, error: i } = await n(t, e);
|
|
20
21
|
if (i) throw Error(i.message);
|
|
21
22
|
return r;
|
|
22
23
|
}
|
|
23
|
-
async function
|
|
24
|
+
async function d() {
|
|
24
25
|
try {
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
await e();
|
|
27
|
+
let t = await i();
|
|
28
|
+
return c("/cms/products", "page"), {
|
|
27
29
|
success: !0,
|
|
28
|
-
data:
|
|
30
|
+
data: t
|
|
29
31
|
};
|
|
30
32
|
} catch (e) {
|
|
31
33
|
return { error: e.message || "Failed to sync with Freemius" };
|
|
32
34
|
}
|
|
33
35
|
}
|
|
34
|
-
async function
|
|
36
|
+
async function f(t) {
|
|
35
37
|
try {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
await e();
|
|
39
|
+
let n = await a(t);
|
|
40
|
+
return c("/cms/products", "page"), c("/cms/products/[id]/edit", "page"), {
|
|
38
41
|
success: !0,
|
|
39
|
-
data:
|
|
42
|
+
data: n
|
|
40
43
|
};
|
|
41
44
|
} catch (e) {
|
|
42
45
|
return { error: e.message || "Failed to sync product with Freemius" };
|
|
43
46
|
}
|
|
44
47
|
}
|
|
45
|
-
async function
|
|
48
|
+
async function p(e, n) {
|
|
46
49
|
try {
|
|
47
|
-
let r =
|
|
48
|
-
return await
|
|
50
|
+
let r = o();
|
|
51
|
+
return await t(r, e, n), c("/cms/products"), c(`/cms/products/${e}/edit`), { success: !0 };
|
|
49
52
|
} catch (e) {
|
|
50
53
|
return {
|
|
51
54
|
success: !1,
|
|
@@ -53,42 +56,42 @@ async function f(t, n) {
|
|
|
53
56
|
};
|
|
54
57
|
}
|
|
55
58
|
}
|
|
56
|
-
async function
|
|
57
|
-
let { data: t, error: n } = await
|
|
59
|
+
async function m(e) {
|
|
60
|
+
let { data: t, error: n } = await o().from("products").select("id, title, language_id, slug").eq("translation_group_id", e);
|
|
58
61
|
if (n) throw Error(n.message);
|
|
59
62
|
return t;
|
|
60
63
|
}
|
|
61
|
-
async function
|
|
62
|
-
let { data: e, error: t } = await
|
|
64
|
+
async function h() {
|
|
65
|
+
let { data: e, error: t } = await s().from("product_attributes").select("\n id,\n name,\n name_translations,\n slug,\n product_attribute_terms (\n id,\n attribute_id,\n value,\n slug,\n sort_order,\n value_translations\n )\n ");
|
|
63
66
|
if (t) throw Error(t.message);
|
|
64
67
|
return (e || []).map((e) => ({
|
|
65
68
|
...e,
|
|
66
69
|
product_attribute_terms: (e.product_attribute_terms || []).sort((e, t) => (e.sort_order ?? 2 ** 53 - 1) - (t.sort_order ?? 2 ** 53 - 1) || e.value.localeCompare(t.value))
|
|
67
70
|
})).sort((e, t) => e.name.localeCompare(t.name));
|
|
68
71
|
}
|
|
69
|
-
async function
|
|
70
|
-
let { data: t, error: n } = await
|
|
72
|
+
async function g(e) {
|
|
73
|
+
let { data: t, error: n } = await o().from("freemius_plans").select("\n id,\n name,\n title,\n freemius_pricing (\n id,\n license_quota,\n api_monthly_price,\n api_annual_price,\n api_lifetime_price,\n override_monthly_price,\n override_annual_price,\n override_lifetime_price,\n is_active\n )\n ").eq("product_id", e);
|
|
71
74
|
if (n) throw Error(n.message);
|
|
72
75
|
return t;
|
|
73
76
|
}
|
|
74
|
-
async function
|
|
75
|
-
let { error: n } = await
|
|
77
|
+
async function _(e, t) {
|
|
78
|
+
let { error: n } = await o().from("freemius_pricing").update({
|
|
76
79
|
...t,
|
|
77
80
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
78
81
|
}).eq("id", e);
|
|
79
82
|
return n ? {
|
|
80
83
|
success: !1,
|
|
81
84
|
error: n.message
|
|
82
|
-
} : (
|
|
85
|
+
} : (c("/cms/products"), { success: !0 });
|
|
83
86
|
}
|
|
84
|
-
async function
|
|
85
|
-
let t =
|
|
87
|
+
async function v(e) {
|
|
88
|
+
let t = o(), { data: n } = await t.from("products").select("translation_group_id").eq("id", e).maybeSingle(), r = [e];
|
|
86
89
|
if (n?.translation_group_id) {
|
|
87
90
|
let { data: e } = await t.from("products").select("id").eq("translation_group_id", n.translation_group_id);
|
|
88
91
|
e && e.length > 0 && (r = e.map((e) => e.id));
|
|
89
92
|
}
|
|
90
|
-
let { data: i, error:
|
|
91
|
-
if (
|
|
93
|
+
let { data: i, error: a } = await t.from("freemius_plans").select("\n id,\n name,\n title,\n freemius_pricing (\n id,\n license_quota,\n api_monthly_price,\n api_annual_price,\n api_lifetime_price,\n override_monthly_price,\n override_annual_price,\n override_lifetime_price,\n is_active\n )\n ").in("product_id", r);
|
|
94
|
+
if (a) throw Error(a.message);
|
|
92
95
|
return (i || []).map((e) => ({
|
|
93
96
|
id: e.id,
|
|
94
97
|
name: e.name,
|
|
@@ -103,8 +106,8 @@ async function _(e) {
|
|
|
103
106
|
}))
|
|
104
107
|
}));
|
|
105
108
|
}
|
|
106
|
-
async function
|
|
107
|
-
let { data: e, error: t } = await
|
|
109
|
+
async function y() {
|
|
110
|
+
let { data: e, error: t } = await o().from("categories").select("id, name, slug, description, created_at, name_translations, description_translations, product_categories(count)").order("name", { ascending: !0 });
|
|
108
111
|
if (t) throw Error(t.message);
|
|
109
112
|
return (e || []).map((e) => {
|
|
110
113
|
let t = e.product_categories?.[0]?.count ?? e.product_categories?.count ?? 0;
|
|
@@ -120,15 +123,15 @@ async function v() {
|
|
|
120
123
|
};
|
|
121
124
|
});
|
|
122
125
|
}
|
|
123
|
-
async function
|
|
124
|
-
let { data: t, error: n } = await
|
|
126
|
+
async function b(e) {
|
|
127
|
+
let { data: t, error: n } = await o().from("categories").select("id, name, slug, description, created_at").eq("slug", e).maybeSingle();
|
|
125
128
|
if (n) throw Error(n.message);
|
|
126
129
|
return t;
|
|
127
130
|
}
|
|
128
|
-
async function
|
|
129
|
-
let { data: t, error: n } = await
|
|
131
|
+
async function x(e) {
|
|
132
|
+
let { data: t, error: n } = await o().from("product_categories").select("category:categories(id, name, slug, description, name_translations, description_translations, created_at)").eq("product_id", e);
|
|
130
133
|
if (n) throw Error(n.message);
|
|
131
134
|
return (t || []).map((e) => e.category).filter(Boolean);
|
|
132
135
|
}
|
|
133
136
|
//#endregion
|
|
134
|
-
export {
|
|
137
|
+
export { p as copyProductFromLanguageAction, y as getCategoriesWithCount, b as getCategoryBySlug, g as getFreemiusPricingByProductId, h as getGlobalProductAttributes, u as getProduct, x as getProductCategories, m as getProductTranslations, l as getProducts, v as getPublicFreemiusPricing, d as triggerFreemiusSync, f as triggerSingleProductSync, _ as updateFreemiusOverride };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use server";const e=require("../../../../
|
|
1
|
+
"use server";const e=require("../../../../server-actions/require-ecom-role.cjs"),t=require("../../../../shared-inventory.cjs");let n=require("next/cache");function r(e){let t=[],n=[],r=``,i=!1;for(let a=0;a<e.length;a+=1){let o=e[a],s=e[a+1];if(o===`"`){i&&s===`"`?(r+=`"`,a+=1):i=!i;continue}if(o===`,`&&!i){n.push(r),r=``;continue}if((o===`
|
|
2
2
|
`||o===`\r`)&&!i){o===`\r`&&s===`
|
|
3
|
-
`&&(a+=1),(r.length>0||n.length>0)&&(n.push(r),t.push(n)),n=[],r=``;continue}r+=o}return(r.length>0||n.length>0)&&(n.push(r),t.push(n)),t.filter(e=>e.some(e=>e.trim().length>0))}function i(e){return e.sku}async function a(
|
|
3
|
+
`&&(a+=1),(r.length>0||n.length>0)&&(n.push(r),t.push(n)),n=[],r=``;continue}r+=o}return(r.length>0||n.length>0)&&(n.push(r),t.push(n)),t.filter(e=>e.some(e=>e.trim().length>0))}function i(e){return e.sku}async function a(r){try{let a=await e.requireEcomAdmin(),o=Math.max(0,Math.trunc(r.stock));return await t.setSharedInventoryQuantity({sku:r.sku,stock:o},a),(0,n.revalidatePath)(`/cms/products`),(0,n.revalidatePath)(`/cms/products/inventory`),{success:!0,itemKey:i(r),stock:o}}catch(e){return{success:!1,error:e instanceof Error?e.message:`Failed to update inventory.`}}}async function o(i){try{let a=await e.requireEcomAdmin(),o=r(i);if(o.length<2)return{success:!1,error:`The CSV file is empty.`};let[s,...c]=o,l=s.map(e=>e.trim().toLowerCase()),u=l.indexOf(`sku`),d=l.indexOf(`stock`);if(u===-1||d===-1)return{success:!1,error:`The CSV format is invalid. Please import a file exported from the Inventory page.`};let f=0;for(let e of c){let n=(e[u]||``).trim(),r=Number.parseInt((e[d]||``).trim(),10);if(n){if(!Number.isFinite(r)||r<0)return{success:!1,error:`Invalid stock value for SKU "${n}".`};await t.setSharedInventoryQuantity({sku:n,stock:r},a),f+=1}}let p=await t.getInventoryItems(a);return(0,n.revalidatePath)(`/cms/products`),(0,n.revalidatePath)(`/cms/products/inventory`),{success:!0,updatedCount:f,items:p}}catch(e){return{success:!1,error:e instanceof Error?e.message:`Failed to import inventory CSV.`}}}exports.importInventoryCsvAction=o,exports.updateInventoryQuantityAction=a;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use server";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { requireEcomAdmin as e } from "../../../../server-actions/require-ecom-role.es.js";
|
|
3
|
+
import { getInventoryItems as t, setSharedInventoryQuantity as n } from "../../../../shared-inventory.es.js";
|
|
4
4
|
import { revalidatePath as r } from "next/cache";
|
|
5
5
|
//#region src/lib/pages/cms/products/inventory/actions.ts
|
|
6
6
|
function i(e) {
|
|
@@ -26,16 +26,16 @@ function i(e) {
|
|
|
26
26
|
function a(e) {
|
|
27
27
|
return e.sku;
|
|
28
28
|
}
|
|
29
|
-
async function o(
|
|
29
|
+
async function o(t) {
|
|
30
30
|
try {
|
|
31
|
-
let
|
|
32
|
-
return await
|
|
33
|
-
sku:
|
|
34
|
-
stock:
|
|
35
|
-
}), r("/cms/products"), r("/cms/products/inventory"), {
|
|
31
|
+
let i = await e(), o = Math.max(0, Math.trunc(t.stock));
|
|
32
|
+
return await n({
|
|
33
|
+
sku: t.sku,
|
|
34
|
+
stock: o
|
|
35
|
+
}, i), r("/cms/products"), r("/cms/products/inventory"), {
|
|
36
36
|
success: !0,
|
|
37
|
-
itemKey: a(
|
|
38
|
-
stock:
|
|
37
|
+
itemKey: a(t),
|
|
38
|
+
stock: o
|
|
39
39
|
};
|
|
40
40
|
} catch (e) {
|
|
41
41
|
return {
|
|
@@ -46,31 +46,31 @@ async function o(e) {
|
|
|
46
46
|
}
|
|
47
47
|
async function s(a) {
|
|
48
48
|
try {
|
|
49
|
-
let o = i(a);
|
|
50
|
-
if (
|
|
49
|
+
let o = await e(), s = i(a);
|
|
50
|
+
if (s.length < 2) return {
|
|
51
51
|
success: !1,
|
|
52
52
|
error: "The CSV file is empty."
|
|
53
53
|
};
|
|
54
|
-
let [
|
|
55
|
-
if (
|
|
54
|
+
let [c, ...l] = s, u = c.map((e) => e.trim().toLowerCase()), d = u.indexOf("sku"), f = u.indexOf("stock");
|
|
55
|
+
if (d === -1 || f === -1) return {
|
|
56
56
|
success: !1,
|
|
57
57
|
error: "The CSV format is invalid. Please import a file exported from the Inventory page."
|
|
58
58
|
};
|
|
59
|
-
let
|
|
60
|
-
for (let e of
|
|
61
|
-
let
|
|
62
|
-
if (
|
|
59
|
+
let p = 0;
|
|
60
|
+
for (let e of l) {
|
|
61
|
+
let t = (e[d] || "").trim(), r = Number.parseInt((e[f] || "").trim(), 10);
|
|
62
|
+
if (t) {
|
|
63
63
|
if (!Number.isFinite(r) || r < 0) return {
|
|
64
64
|
success: !1,
|
|
65
|
-
error: `Invalid stock value for SKU "${
|
|
65
|
+
error: `Invalid stock value for SKU "${t}".`
|
|
66
66
|
};
|
|
67
|
-
await
|
|
68
|
-
sku:
|
|
67
|
+
await n({
|
|
68
|
+
sku: t,
|
|
69
69
|
stock: r
|
|
70
|
-
},
|
|
70
|
+
}, o), p += 1;
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
let m = await
|
|
73
|
+
let m = await t(o);
|
|
74
74
|
return r("/cms/products"), r("/cms/products/inventory"), {
|
|
75
75
|
success: !0,
|
|
76
76
|
updatedCount: p,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use server";const e=require("../../../currency.cjs"),t=require("../../../
|
|
1
|
+
"use server";const e=require("../../../currency.cjs"),t=require("../../../server-actions/require-ecom-role.cjs"),n=require("../../../product-actions.cjs"),r=require("./product-price-sync.cjs"),i=require("../../../freemius-coupons.cjs");let a=require("@nextblock-cms/db/server"),o=require("next/navigation"),s=require("next/cache");function c(e){return e.toLowerCase().trim().replace(/\s+/g,`-`).replace(/[^a-z0-9-]/g,``).replace(/-+/g,`-`).replace(/^-|-$/g,``)}async function l(t){let c=(0,a.createClient)(),{data:l}=await c.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}),u=r.sanitizeProductFormValuesForStoreManagedCurrencies(t,(l||[]).map(t=>e.normalizeCurrencyRecord(t))),d=await n.createProduct(c,u);if(u.payment_provider===`freemius`&&d?.id)try{await i.syncProductSaleCouponToFreemius({productId:d.id,client:(0,a.getServiceRoleSupabaseClient)()})}catch(e){console.error(`Failed to sync Freemius sale coupon on create:`,e)}(0,s.revalidatePath)(`/cms/products`),(0,o.redirect)(`/cms/products`)}async function u(e,t,n,r){let i={author_id:n,meta:r,updated_at:new Date().toISOString()},a=async()=>{let{error:n}=await e.from(`product_drafts`).update(i).eq(`product_id`,t);if(n)throw Error(`Failed to save product draft: ${n.message}`)},{data:o,error:s}=await e.from(`product_drafts`).select(`id`).eq(`product_id`,t).maybeSingle();if(s)throw Error(`Failed to read product draft: ${s.message}`);if(o){await a();return}let{data:c,error:l}=await e.from(`blocks`).select(`id, page_id, post_id, product_id, language_id, block_type, content, order, created_at, updated_at`).eq(`product_id`,t).order(`order`,{ascending:!0});if(l)throw Error(`Failed to read product blocks: ${l.message}`);let{error:u}=await e.from(`product_drafts`).insert({...i,blocks:c??[],product_id:t});if(u){if(u.code===`23505`){await a();return}throw Error(`Failed to save product draft: ${u.message}`)}}async function d(t,n){let i=(0,a.createClient)(),{data:{user:o}}=await i.auth.getUser();if(!o)throw Error(`User not authenticated.`);let{data:s}=await i.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}),c=r.sanitizeProductFormValuesForStoreManagedCurrencies(n,(s||[]).map(t=>e.normalizeCurrencyRecord(t)));return await u(i,t,o.id,c),{success:!0}}async function f(e){let t=(0,a.createClient)();await n.deleteProduct(t,e),(0,s.revalidatePath)(`/cms/products`)}function p(e){return Array.from(new Set(e.map(e=>e.trim()).filter(Boolean)))}async function m(e){let t=p(e);if(t.length===0)return{success:!1,error:`Select at least one product.`};let{error:n}=await(0,a.createClient)().from(`products`).delete().in(`id`,t);return n?{success:!1,error:n.message}:((0,s.revalidatePath)(`/cms/products`),{success:!0,count:t.length})}async function h(e){let t=p(e);if(t.length===0)return{success:!1,error:`Select at least one product.`};let{error:n}=await(0,a.createClient)().from(`products`).update({status:`draft`,updated_at:new Date().toISOString()}).in(`id`,t);return n?{success:!1,error:n.message}:((0,s.revalidatePath)(`/cms/products`),{success:!0,count:t.length})}async function g(e){let t=String(e||``).trim();if(!t)return{error:`Missing product id.`};let n=(0,a.createClient)(),{data:{user:r}}=await n.auth.getUser();if(!r)return{error:`You must be signed in to publish a product.`};let{data:i,error:o}=await n.from(`products`).update({status:`active`,updated_at:new Date().toISOString()}).eq(`id`,t).select(`slug`).single();return o||!i?{error:o?.message||`Could not publish the product.`}:((0,s.revalidatePath)(`/cms/products`),(0,s.revalidatePath)(`/cms/products/${t}/edit`),i.slug&&(0,s.revalidatePath)(`/product/${i.slug}`),{})}async function _(e){let n=await t.requireEcomStaff(),r=e.name.trim(),i=c(e.slug?.trim()||e.name);if(!r||!i)return{success:!1,error:`Attribute name is required.`};let{error:a}=await n.from(`product_attributes`).insert({name:r,slug:i});return a?{success:!1,error:a.message}:((0,s.revalidatePath)(`/cms/products/attributes`),(0,s.revalidatePath)(`/cms/products/new`),(0,s.revalidatePath)(`/cms/products`),{success:!0})}async function v(e){let{error:n}=await(await t.requireEcomStaff()).from(`product_attributes`).delete().eq(`id`,e);return n?{success:!1,error:n.message}:((0,s.revalidatePath)(`/cms/products/attributes`),(0,s.revalidatePath)(`/cms/products/new`),(0,s.revalidatePath)(`/cms/products`),{success:!0})}async function y(e){let n=await t.requireEcomStaff(),r=e.value.trim(),i=c(e.slug?.trim()||e.value);if(!r||!i)return{success:!1,error:`Term value is required.`};let{data:a}=await n.from(`product_attribute_terms`).select(`sort_order`).eq(`attribute_id`,e.attributeId).order(`sort_order`,{ascending:!1}).limit(1),o=typeof a?.[0]?.sort_order==`number`?a[0].sort_order+1:0,{error:l}=await n.from(`product_attribute_terms`).insert({attribute_id:e.attributeId,value:r,slug:i,sort_order:o});return l?{success:!1,error:l.message}:((0,s.revalidatePath)(`/cms/products/attributes`),(0,s.revalidatePath)(`/cms/products/new`),(0,s.revalidatePath)(`/cms/products`),{success:!0})}async function b(e){let n=await t.requireEcomStaff();for(let[t,r]of e.orderedTermIds.entries()){let{error:i}=await n.from(`product_attribute_terms`).update({sort_order:t,updated_at:new Date().toISOString()}).eq(`id`,r).eq(`attribute_id`,e.attributeId);if(i)return{success:!1,error:i.message}}return(0,s.revalidatePath)(`/cms/products/attributes`),(0,s.revalidatePath)(`/cms/products/new`),(0,s.revalidatePath)(`/cms/products`),{success:!0}}async function x(e){let n=await t.requireEcomStaff(),{error:r}=await n.from(`product_attributes`).update({name_translations:e.nameTranslations,updated_at:new Date().toISOString()}).eq(`id`,e.attributeId);if(r)return{success:!1,error:r.message};for(let t of e.termTranslations){let{error:r}=await n.from(`product_attribute_terms`).update({value_translations:t.valueTranslations,updated_at:new Date().toISOString()}).eq(`id`,t.termId).eq(`attribute_id`,e.attributeId);if(r)return{success:!1,error:r.message}}return(0,s.revalidatePath)(`/cms/products/attributes`),(0,s.revalidatePath)(`/cms/products/new`),(0,s.revalidatePath)(`/cms/products`),{success:!0}}async function S(e){let{error:n}=await(await t.requireEcomStaff()).from(`product_attribute_terms`).delete().eq(`id`,e);return n?{success:!1,error:n.message}:((0,s.revalidatePath)(`/cms/products/attributes`),(0,s.revalidatePath)(`/cms/products/new`),(0,s.revalidatePath)(`/cms/products`),{success:!0})}async function C(e){let n=await t.requireEcomStaff(),r=e.name.trim(),i=c(e.slug?.trim()||e.name);if(!r||!i)return{success:!1,error:`Category name is required.`};let{data:a,error:o}=await n.from(`categories`).insert({name:r,slug:i,description:e.description?.trim()||null,name_translations:e.nameTranslations||{},description_translations:e.descriptionTranslations||{}}).select().single();return o?{success:!1,error:o.message}:((0,s.revalidatePath)(`/cms/products/categories`),(0,s.revalidatePath)(`/cms/products/new`),(0,s.revalidatePath)(`/cms/products`),{success:!0,category:a})}async function w(e,n){let r=await t.requireEcomStaff(),i=n.name.trim(),a=c(n.slug?.trim()||n.name);if(!i||!a)return{success:!1,error:`Category name is required.`};let{error:o}=await r.from(`categories`).update({name:i,slug:a,description:n.description?.trim()||null,name_translations:n.nameTranslations||{},description_translations:n.descriptionTranslations||{}}).eq(`id`,e);return o?{success:!1,error:o.message}:((0,s.revalidatePath)(`/cms/products/categories`),(0,s.revalidatePath)(`/cms/products/new`),(0,s.revalidatePath)(`/cms/products`),{success:!0})}async function T(e){let{error:n}=await(await t.requireEcomStaff()).from(`categories`).delete().eq(`id`,e);return n?{success:!1,error:n.message}:((0,s.revalidatePath)(`/cms/products/categories`),(0,s.revalidatePath)(`/cms/products/new`),(0,s.revalidatePath)(`/cms/products`),{success:!0})}async function E(e,r){let i=await t.requireEcomStaff();try{return await n.syncCategoriesForTranslationGroup(i,e,r),(0,s.revalidatePath)(`/cms/products`),(0,s.revalidatePath)(`/cms/products/${e}/edit`),{success:!0}}catch(e){return{success:!1,error:e.message}}}exports.bulkDeleteProductsAction=m,exports.bulkDraftProductsAction=h,exports.createCategoryAction=C,exports.createProductAction=l,exports.createProductAttributeAction=_,exports.createProductAttributeTermAction=y,exports.deleteCategoryAction=T,exports.deleteProductAction=f,exports.deleteProductAttributeAction=v,exports.deleteProductAttributeTermAction=S,exports.publishProductAction=g,exports.reorderProductAttributeTermsAction=b,exports.syncProductCategoriesAction=E,exports.updateCategoryAction=w,exports.updateProductAction=d,exports.updateProductAttributeTranslationsAction=x;
|