@nextblock-cms/ecom 0.13.8 → 0.13.9
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/lib/product-actions.cjs.js +4 -4
- package/lib/product-actions.d.ts +13 -3
- package/lib/product-actions.es.js +134 -115
- package/package.json +4 -4
|
@@ -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"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=require("@nextblock-cms/utils/server"),P=require("./shared-inventory.cjs.js"),M=require("@nextblock-cms/utils/utils"),w=e=>Math.round(e*100);function q(e){return Object.entries(e||{}).reduce((t,[r,i])=>(typeof i=="number"&&Number.isFinite(i)&&i>=0&&(t[M.normalizeCurrencyCode(r)]=w(i)),t),{})}function C(e){return(e||[]).map(t=>({id:t.id,sku:t.sku,upc:t.upc??null,price:w(t.price),sale_price:typeof t.sale_price=="number"&&!isNaN(t.sale_price)?w(t.sale_price):null,prices:q(t.prices),sale_prices:q(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:w(e.price),sale_price:typeof e.sale_price=="number"&&!isNaN(e.sale_price)?w(e.sale_price):null,prices:q(e.prices),sale_prices:q(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:C(e.variants)}}async function j(e,t,r){const i=o=>{const a=typeof o=="string"?o.trim():"";return a||null},{error:s}=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(s)throw s}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:s}=await e.from("products").select("sku, sale_price, sale_prices").eq("id",t).maybeSingle(),o=s?.sku??r.sku,{data:a}=await e.from("products").select("id").eq("sku",o),c=(a??[]).map(n=>n.id);c.length===0&&c.push(t);const{error:_}=await e.from("products").update({sale_price:s?.sale_price??null,sale_prices:s?.sale_prices??null,sale_start_at:r.sale_start_at??null,sale_end_at:r.sale_end_at??null,updated_at:i}).in("id",c);if(_)throw _;const l=r.variants??[];if(l.length===0)return;const{data:p}=await e.from("product_variants").select("sku, sale_price, sale_prices").eq("product_id",t),f=new Map((p??[]).map(n=>[n.sku,n]));for(const n of l){const u=f.get(n.sku);if(!u)continue;const{error:d}=await e.from("product_variants").update({sale_price:u.sale_price??null,sale_prices:u.sale_prices??null,sale_start_at:n.sale_start_at??null,sale_end_at:n.sale_end_at??null,updated_at:i}).eq("sku",n.sku).in("product_id",c);if(d)throw d}}async function E(e,{page:t=1,limit:r=10,offset:i,search:s="",languageId:o,categoryId:a,categoryIds:c,productIds:_,translationGroupIds:l,status:p="active"}={}){const f=r>0,n=typeof i=="number"?Math.max(0,i):(t-1)*r,u=n+r-1;let d=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"}).order("created_at",{ascending:!1});if(f&&(d=d.range(n,u)),p!=="all"&&(d=d.eq("status",p)),o&&(d=d.eq("language_id",o)),_){if(_.length===0)return{data:[],error:null,count:0};d=d.in("id",_)}if(l){if(l.length===0)return{data:[],error:null,count:0};d=d.in("translation_group_id",l)}const g=c?.length?c:a?[a]:[];if(g.length>0){const{data:y,error:m}=await e.from("product_categories").select("product_id").in("category_id",g);if(m)return{data:[],error:m,count:0};const h=Array.from(new Set((y||[]).map(v=>v.product_id).filter(Boolean)));if(h.length===0)return{data:[],error:null,count:0};d=d.in("id",h)}return s&&(d=d.or(`title.ilike.%${s}%,sku.ilike.%${s}%`)),d}async function A(e,t){return e.from("products").select(`
|
|
2
2
|
*,
|
|
3
3
|
languages (
|
|
4
4
|
code
|
|
@@ -81,7 +81,7 @@
|
|
|
81
81
|
description_translations
|
|
82
82
|
)
|
|
83
83
|
)
|
|
84
|
-
`).eq("id",t).single()}async function
|
|
84
|
+
`).eq("id",t).single()}async function T(e,t,r){const{data:i,error:s}=await e.from("products").select(`
|
|
85
85
|
*,
|
|
86
86
|
languages (
|
|
87
87
|
id,
|
|
@@ -166,7 +166,7 @@
|
|
|
166
166
|
description_translations
|
|
167
167
|
)
|
|
168
168
|
)
|
|
169
|
-
`).eq("slug",t);if(
|
|
169
|
+
`).eq("slug",t);if(s)return{data:null,error:s};if(!i||i.length===0)return{data:null,error:{message:"Product not found",code:"PGRST116"}};let o=null;return r&&(o=i.find(a=>(Array.isArray(a.languages)?a.languages[0]:a.languages)?.code===r)),o||(o=i.find(a=>(Array.isArray(a.languages)?a.languages[0]:a.languages)?.is_default)),o||(o=i.find(a=>(Array.isArray(a.languages)?a.languages[0]:a.languages)?.code==="en")),o||(o=i[0]),{data:o,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 o=t.product_media.map((a,c)=>({product_id:r,media_id:a.media_id,sort_order:c}));await e.from("product_media").insert(o)}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 k(e,r,t.category_ids);const{data:s}=await e.from("products").select("*").eq("id",r).single();return s}async function D(e,t,r){const{data:i}=await e.from("product_media").select("media_id").eq("product_id",t),s=i?.map(n=>n.media_id)||[],{data:o,error:a}=await e.rpc("upsert_product_with_variants",{product_payload:S(r,t)});if(a||!o)throw a||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 n=r.product_media.map((u,d)=>({product_id:t,media_id:u.media_id,sort_order:d}));await e.from("product_media").insert(n)}}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,o,r.is_taxable),await j(e,o,r),await F(e,o,r);const c=r.product_media?r.product_media.map(n=>n.media_id):r.media_id?[r.media_id]:[],_=r.explicitly_removed_media_ids||[],l=s.filter(n=>!c.includes(n)),p=Array.from(new Set([...l,..._]));if(p.length>0)for(const n of p){const{count:u}=await e.from("product_media").select("*",{count:"exact",head:!0}).eq("media_id",n);if(u&&u>0)continue;const{count:d}=await e.from("posts").select("*",{count:"exact",head:!0}).eq("feature_image_id",n);if(d&&d>0)continue;const{count:g}=await e.from("logos").select("*",{count:"exact",head:!0}).eq("media_id",n);if(g&&g>0)continue;const{count:y}=await e.from("product_variants").select("*",{count:"exact",head:!0}).eq("main_media_id",n);if(y&&y>0)continue;const{data:m}=await e.from("media").select("object_key, variants").eq("id",n).single();if(m){const h=[m.object_key];m.variants&&Array.isArray(m.variants)&&m.variants.forEach(v=>{v.objectKey&&h.push(v.objectKey)}),await b.deleteMediaFiles(h),await e.from("media").delete().eq("id",n)}}await P.syncSharedInventoryForSavedProduct(o,r),r.category_ids!==void 0&&await k(e,t,r.category_ids);const{data:f}=await e.from("products").select("*").eq("id",o).single();return f}async function N(e,t){const{error:r}=await e.from("products").delete().eq("id",t);if(r)throw r;return!0}async function $(e,t,r){const{data:i,error:s}=await e.from("products").select("*").eq("id",r).single();if(s||!i)throw new Error(s?.message||"Source product not found");const{data:o,error:a}=await e.from("products").select("language_id").eq("id",t).single();if(a||!o)throw new Error(a?.message||"Target product not found");const{error:c}=await e.from("products").update({title:i.title,short_description:i.short_description,description_json:i.description_json}).eq("id",t);if(c)throw c;const{data:_,error:l}=await e.from("blocks").select("*").eq("product_id",r).order("order",{ascending:!0});if(l)throw l;if(await e.from("blocks").delete().eq("product_id",t),_&&_.length>0){const f=_.map(u=>{const{id:d,created_at:g,updated_at:y,...m}=u;return{...m,product_id:t,language_id:o.language_id}}),{error:n}=await e.from("blocks").insert(f);if(n)throw n}await e.from("product_drafts").delete().eq("product_id",t),await e.from("product_media").delete().eq("product_id",t);const{data:p}=await e.from("product_media").select("media_id, sort_order").eq("product_id",r);if(p&&p.length>0){const f=p.map(n=>({product_id:t,media_id:n.media_id,sort_order:n.sort_order}));await e.from("product_media").insert(f)}return{success:!0}}async function B(e,t,r,i=[],s=[]){const{data:o}=await e.from("languages").select("id").eq("code",r).single();if(!o)return{data:[],error:"Language not found"};const a=[];t.length>0&&a.push(`translation_group_id.in.(${t.join(",")})`),i.length>0&&a.push(`sku.in.(${i.map(l=>`"${l}"`).join(",")})`),s.length>0&&a.push(`id.in.(${s.join(",")})`);let c=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",
|
|
237
|
+
`).eq("language_id",o.id).eq("status","active");return a.length>0&&(c=c.or(a.join(","))),await c.order("id")}async function k(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 a=r.map(c=>({product_id:t,category_id:c}));await e.from("product_categories").insert(a)}return}const{data:s}=await e.from("products").select("id").eq("translation_group_id",i.translation_group_id),o=s&&s.length>0?s.map(a=>a.id):[t];if(await e.from("product_categories").delete().in("product_id",o),r.length>0){const a=[];for(const c of o)for(const _ of r)a.push({product_id:c,category_id:_});await e.from("product_categories").insert(a)}}exports.copyProductFromLanguage=$;exports.createProduct=R;exports.deleteProduct=N;exports.fetchTranslatedProductsForCartInternal=B;exports.getProduct=A;exports.getProductBySlug=T;exports.getProducts=E;exports.syncCategoriesForTranslationGroup=k;exports.updateProduct=D;
|
package/lib/product-actions.d.ts
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
import { SupabaseClient } from '@supabase/supabase-js';
|
|
2
2
|
import { Database } from '../../../db/src/lib/supabase/types.ts';
|
|
3
3
|
import { ProductFormValues } from './product-schema';
|
|
4
|
-
export declare function getProducts(supabase: SupabaseClient<Database>, { page, limit, search, languageId, categoryId, status, }?: {
|
|
4
|
+
export declare function getProducts(supabase: SupabaseClient<Database>, { page, limit, offset, search, languageId, categoryId, categoryIds, productIds, translationGroupIds, status, }?: {
|
|
5
5
|
page?: number;
|
|
6
|
+
/** Rows per page. `0` (or less) returns every match, with no range applied. */
|
|
6
7
|
limit?: number;
|
|
8
|
+
/** Explicit row offset, overriding the one derived from `page`. */
|
|
9
|
+
offset?: number;
|
|
7
10
|
search?: string;
|
|
8
11
|
languageId?: number;
|
|
12
|
+
/** Legacy single-category filter. Superseded by `categoryIds`, which wins when both are set. */
|
|
9
13
|
categoryId?: string;
|
|
14
|
+
/** Products in ANY of these categories. */
|
|
15
|
+
categoryIds?: string[];
|
|
16
|
+
/** Restrict to an explicit set of product ids (hand-picked selections). */
|
|
17
|
+
productIds?: string[];
|
|
18
|
+
/** Restrict to products belonging to these translation groups. */
|
|
19
|
+
translationGroupIds?: string[];
|
|
10
20
|
status?: 'active' | 'draft' | 'archived' | 'all';
|
|
11
21
|
}): Promise<import('@supabase/postgrest-js').PostgrestResponseFailure | import('@supabase/postgrest-js').PostgrestResponseSuccess<{
|
|
12
22
|
id: string;
|
|
@@ -83,11 +93,11 @@ export declare function getProducts(supabase: SupabaseClient<Database>, { page,
|
|
|
83
93
|
}[];
|
|
84
94
|
}[]> | {
|
|
85
95
|
data: never[];
|
|
86
|
-
error:
|
|
96
|
+
error: null;
|
|
87
97
|
count: number;
|
|
88
98
|
} | {
|
|
89
99
|
data: never[];
|
|
90
|
-
error:
|
|
100
|
+
error: import('@supabase/postgrest-js').PostgrestError;
|
|
91
101
|
count: number;
|
|
92
102
|
}>;
|
|
93
103
|
export declare function getProduct(supabase: SupabaseClient<Database>, id: string): Promise<import('@supabase/postgrest-js').PostgrestSingleResponse<{
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { deleteMediaFiles as
|
|
1
|
+
import { deleteMediaFiles as E } from "@nextblock-cms/utils/server";
|
|
2
2
|
import { syncSharedInventoryForSavedProduct as k } from "./shared-inventory.es.js";
|
|
3
|
-
import { normalizeCurrencyCode as
|
|
4
|
-
const
|
|
5
|
-
function
|
|
3
|
+
import { normalizeCurrencyCode as F } from "@nextblock-cms/utils/utils";
|
|
4
|
+
const w = (e) => Math.round(e * 100);
|
|
5
|
+
function v(e) {
|
|
6
6
|
return Object.entries(e || {}).reduce(
|
|
7
|
-
(t, [i, r]) => (typeof r == "number" && Number.isFinite(r) && r >= 0 && (t[
|
|
7
|
+
(t, [i, r]) => (typeof r == "number" && Number.isFinite(r) && r >= 0 && (t[F(i)] = w(r)), t),
|
|
8
8
|
{}
|
|
9
9
|
);
|
|
10
10
|
}
|
|
@@ -13,10 +13,10 @@ function M(e) {
|
|
|
13
13
|
id: t.id,
|
|
14
14
|
sku: t.sku,
|
|
15
15
|
upc: t.upc ?? null,
|
|
16
|
-
price:
|
|
17
|
-
sale_price: typeof t.sale_price == "number" && !isNaN(t.sale_price) ?
|
|
18
|
-
prices:
|
|
19
|
-
sale_prices:
|
|
16
|
+
price: w(t.price),
|
|
17
|
+
sale_price: typeof t.sale_price == "number" && !isNaN(t.sale_price) ? w(t.sale_price) : null,
|
|
18
|
+
prices: v(t.prices),
|
|
19
|
+
sale_prices: v(t.sale_prices),
|
|
20
20
|
sale_start_at: t.sale_start_at ?? null,
|
|
21
21
|
sale_end_at: t.sale_end_at ?? null,
|
|
22
22
|
stock_quantity: t.stock_quantity,
|
|
@@ -39,10 +39,10 @@ function j(e, t) {
|
|
|
39
39
|
short_description: e.short_description ?? null,
|
|
40
40
|
description_json: e.description_json ?? null,
|
|
41
41
|
metadata: {},
|
|
42
|
-
price:
|
|
43
|
-
sale_price: typeof e.sale_price == "number" && !isNaN(e.sale_price) ?
|
|
44
|
-
prices:
|
|
45
|
-
sale_prices:
|
|
42
|
+
price: w(e.price),
|
|
43
|
+
sale_price: typeof e.sale_price == "number" && !isNaN(e.sale_price) ? w(e.sale_price) : null,
|
|
44
|
+
prices: v(e.prices),
|
|
45
|
+
sale_prices: v(e.sale_prices),
|
|
46
46
|
sale_start_at: e.sale_start_at ?? null,
|
|
47
47
|
sale_end_at: e.sale_end_at ?? null,
|
|
48
48
|
freemius_plan_id: e.freemius_plan_id ?? null,
|
|
@@ -59,14 +59,14 @@ async function P(e, t, i) {
|
|
|
59
59
|
const r = (o) => {
|
|
60
60
|
const a = typeof o == "string" ? o.trim() : "";
|
|
61
61
|
return a || null;
|
|
62
|
-
}, { error:
|
|
62
|
+
}, { error: s } = await e.from("products").update({
|
|
63
63
|
meta_title: r(i.meta_title),
|
|
64
64
|
meta_description: r(i.meta_description),
|
|
65
65
|
custom_canonical: r(i.custom_canonical),
|
|
66
66
|
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
67
67
|
}).eq("id", t);
|
|
68
|
-
if (
|
|
69
|
-
throw
|
|
68
|
+
if (s)
|
|
69
|
+
throw s;
|
|
70
70
|
}
|
|
71
71
|
async function x(e, t, i) {
|
|
72
72
|
const { error: r } = await e.from("products").update({
|
|
@@ -77,61 +77,80 @@ async function x(e, t, i) {
|
|
|
77
77
|
throw r;
|
|
78
78
|
}
|
|
79
79
|
async function S(e, t, i) {
|
|
80
|
-
const r = (/* @__PURE__ */ new Date()).toISOString(), { data:
|
|
81
|
-
|
|
80
|
+
const r = (/* @__PURE__ */ new Date()).toISOString(), { data: s } = await e.from("products").select("sku, sale_price, sale_prices").eq("id", t).maybeSingle(), o = s?.sku ?? i.sku, { data: a } = await e.from("products").select("id").eq("sku", o), c = (a ?? []).map((n) => n.id);
|
|
81
|
+
c.length === 0 && c.push(t);
|
|
82
82
|
const { error: _ } = await e.from("products").update({
|
|
83
|
-
sale_price:
|
|
84
|
-
sale_prices:
|
|
83
|
+
sale_price: s?.sale_price ?? null,
|
|
84
|
+
sale_prices: s?.sale_prices ?? null,
|
|
85
85
|
sale_start_at: i.sale_start_at ?? null,
|
|
86
86
|
sale_end_at: i.sale_end_at ?? null,
|
|
87
87
|
updated_at: r
|
|
88
|
-
}).in("id",
|
|
88
|
+
}).in("id", c);
|
|
89
89
|
if (_)
|
|
90
90
|
throw _;
|
|
91
|
-
const
|
|
92
|
-
if (
|
|
91
|
+
const l = i.variants ?? [];
|
|
92
|
+
if (l.length === 0)
|
|
93
93
|
return;
|
|
94
|
-
const { data:
|
|
95
|
-
(
|
|
94
|
+
const { data: p } = await e.from("product_variants").select("sku, sale_price, sale_prices").eq("product_id", t), f = new Map(
|
|
95
|
+
(p ?? []).map((n) => [n.sku, n])
|
|
96
96
|
);
|
|
97
|
-
for (const
|
|
98
|
-
const
|
|
99
|
-
if (!
|
|
97
|
+
for (const n of l) {
|
|
98
|
+
const u = f.get(n.sku);
|
|
99
|
+
if (!u)
|
|
100
100
|
continue;
|
|
101
|
-
const { error:
|
|
102
|
-
sale_price:
|
|
103
|
-
sale_prices:
|
|
104
|
-
sale_start_at:
|
|
105
|
-
sale_end_at:
|
|
101
|
+
const { error: d } = await e.from("product_variants").update({
|
|
102
|
+
sale_price: u.sale_price ?? null,
|
|
103
|
+
sale_prices: u.sale_prices ?? null,
|
|
104
|
+
sale_start_at: n.sale_start_at ?? null,
|
|
105
|
+
sale_end_at: n.sale_end_at ?? null,
|
|
106
106
|
updated_at: r
|
|
107
|
-
}).eq("sku",
|
|
108
|
-
if (
|
|
109
|
-
throw
|
|
107
|
+
}).eq("sku", n.sku).in("product_id", c);
|
|
108
|
+
if (d)
|
|
109
|
+
throw d;
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
async function T(e, {
|
|
113
113
|
page: t = 1,
|
|
114
114
|
limit: i = 10,
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
115
|
+
offset: r,
|
|
116
|
+
search: s = "",
|
|
117
|
+
languageId: o,
|
|
118
|
+
categoryId: a,
|
|
119
|
+
categoryIds: c,
|
|
120
|
+
productIds: _,
|
|
121
|
+
translationGroupIds: l,
|
|
122
|
+
status: p = "active"
|
|
119
123
|
} = {}) {
|
|
120
|
-
const
|
|
121
|
-
let
|
|
124
|
+
const f = i > 0, n = typeof r == "number" ? Math.max(0, r) : (t - 1) * i, u = n + i - 1;
|
|
125
|
+
let d = e.from("products").select(
|
|
122
126
|
"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))",
|
|
123
127
|
{ count: "exact" }
|
|
124
|
-
).
|
|
125
|
-
if (
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
128
|
+
).order("created_at", { ascending: !1 });
|
|
129
|
+
if (f && (d = d.range(n, u)), p !== "all" && (d = d.eq("status", p)), o && (d = d.eq("language_id", o)), _) {
|
|
130
|
+
if (_.length === 0)
|
|
131
|
+
return { data: [], error: null, count: 0 };
|
|
132
|
+
d = d.in("id", _);
|
|
133
|
+
}
|
|
134
|
+
if (l) {
|
|
135
|
+
if (l.length === 0)
|
|
136
|
+
return { data: [], error: null, count: 0 };
|
|
137
|
+
d = d.in("translation_group_id", l);
|
|
138
|
+
}
|
|
139
|
+
const g = c?.length ? c : a ? [a] : [];
|
|
140
|
+
if (g.length > 0) {
|
|
141
|
+
const { data: h, error: m } = await e.from("product_categories").select("product_id").in("category_id", g);
|
|
142
|
+
if (m)
|
|
143
|
+
return { data: [], error: m, count: 0 };
|
|
144
|
+
const y = Array.from(
|
|
145
|
+
new Set(
|
|
146
|
+
(h || []).map((q) => q.product_id).filter(Boolean)
|
|
147
|
+
)
|
|
148
|
+
);
|
|
149
|
+
if (y.length === 0)
|
|
131
150
|
return { data: [], error: null, count: 0 };
|
|
132
|
-
|
|
151
|
+
d = d.in("id", y);
|
|
133
152
|
}
|
|
134
|
-
return
|
|
153
|
+
return s && (d = d.or(`title.ilike.%${s}%,sku.ilike.%${s}%`)), d;
|
|
135
154
|
}
|
|
136
155
|
async function D(e, t) {
|
|
137
156
|
return e.from("products").select(
|
|
@@ -222,7 +241,7 @@ async function D(e, t) {
|
|
|
222
241
|
).eq("id", t).single();
|
|
223
242
|
}
|
|
224
243
|
async function N(e, t, i) {
|
|
225
|
-
const { data: r, error:
|
|
244
|
+
const { data: r, error: s } = await e.from("products").select(
|
|
226
245
|
`
|
|
227
246
|
*,
|
|
228
247
|
languages (
|
|
@@ -310,8 +329,8 @@ async function N(e, t, i) {
|
|
|
310
329
|
)
|
|
311
330
|
`
|
|
312
331
|
).eq("slug", t);
|
|
313
|
-
if (
|
|
314
|
-
return { data: null, error:
|
|
332
|
+
if (s)
|
|
333
|
+
return { data: null, error: s };
|
|
315
334
|
if (!r || r.length === 0)
|
|
316
335
|
return { data: null, error: { message: "Product not found", code: "PGRST116" } };
|
|
317
336
|
let o = null;
|
|
@@ -323,10 +342,10 @@ async function $(e, t) {
|
|
|
323
342
|
});
|
|
324
343
|
if (r || !i) throw r || new Error("Failed to create product");
|
|
325
344
|
if (t.product_media && t.product_media.length > 0) {
|
|
326
|
-
const o = t.product_media.map((a,
|
|
345
|
+
const o = t.product_media.map((a, c) => ({
|
|
327
346
|
product_id: i,
|
|
328
347
|
media_id: a.media_id,
|
|
329
|
-
sort_order:
|
|
348
|
+
sort_order: c
|
|
330
349
|
}));
|
|
331
350
|
await e.from("product_media").insert(o);
|
|
332
351
|
} else t.media_id && await e.from("product_media").insert({
|
|
@@ -334,23 +353,23 @@ async function $(e, t) {
|
|
|
334
353
|
media_id: t.media_id,
|
|
335
354
|
sort_order: 0
|
|
336
355
|
});
|
|
337
|
-
await x(e, i, t.is_taxable), await P(e, i, t), await S(e, i, t), await k(i, t), t.category_ids !== void 0 && await
|
|
338
|
-
const { data:
|
|
339
|
-
return
|
|
356
|
+
await x(e, i, t.is_taxable), await P(e, i, t), await S(e, i, t), await k(i, t), t.category_ids !== void 0 && await b(e, i, t.category_ids);
|
|
357
|
+
const { data: s } = await e.from("products").select("*").eq("id", i).single();
|
|
358
|
+
return s;
|
|
340
359
|
}
|
|
341
360
|
async function z(e, t, i) {
|
|
342
|
-
const { data: r } = await e.from("product_media").select("media_id").eq("product_id", t),
|
|
361
|
+
const { data: r } = await e.from("product_media").select("media_id").eq("product_id", t), s = r?.map((n) => n.media_id) || [], { data: o, error: a } = await e.rpc("upsert_product_with_variants", {
|
|
343
362
|
product_payload: j(i, t)
|
|
344
363
|
});
|
|
345
364
|
if (a || !o) throw a || new Error("Failed to update product");
|
|
346
365
|
if (i.product_media) {
|
|
347
366
|
if (await e.from("product_media").delete().eq("product_id", t), i.product_media.length > 0) {
|
|
348
|
-
const
|
|
367
|
+
const n = i.product_media.map((u, d) => ({
|
|
349
368
|
product_id: t,
|
|
350
|
-
media_id:
|
|
351
|
-
sort_order:
|
|
369
|
+
media_id: u.media_id,
|
|
370
|
+
sort_order: d
|
|
352
371
|
}));
|
|
353
|
-
await e.from("product_media").insert(
|
|
372
|
+
await e.from("product_media").insert(n);
|
|
354
373
|
}
|
|
355
374
|
} else i.media_id && (await e.from("product_media").delete().eq("product_id", t), await e.from("product_media").insert({
|
|
356
375
|
product_id: t,
|
|
@@ -358,79 +377,79 @@ async function z(e, t, i) {
|
|
|
358
377
|
sort_order: 0
|
|
359
378
|
}));
|
|
360
379
|
await x(e, o, i.is_taxable), await P(e, o, i), await S(e, o, i);
|
|
361
|
-
const
|
|
362
|
-
if (
|
|
363
|
-
for (const
|
|
364
|
-
const { count:
|
|
365
|
-
if (
|
|
366
|
-
const { count:
|
|
367
|
-
if (
|
|
368
|
-
const { count:
|
|
380
|
+
const c = i.product_media ? i.product_media.map((n) => n.media_id) : i.media_id ? [i.media_id] : [], _ = i.explicitly_removed_media_ids || [], l = s.filter((n) => !c.includes(n)), p = Array.from(/* @__PURE__ */ new Set([...l, ..._]));
|
|
381
|
+
if (p.length > 0)
|
|
382
|
+
for (const n of p) {
|
|
383
|
+
const { count: u } = await e.from("product_media").select("*", { count: "exact", head: !0 }).eq("media_id", n);
|
|
384
|
+
if (u && u > 0) continue;
|
|
385
|
+
const { count: d } = await e.from("posts").select("*", { count: "exact", head: !0 }).eq("feature_image_id", n);
|
|
386
|
+
if (d && d > 0) continue;
|
|
387
|
+
const { count: g } = await e.from("logos").select("*", { count: "exact", head: !0 }).eq("media_id", n);
|
|
388
|
+
if (g && g > 0) continue;
|
|
389
|
+
const { count: h } = await e.from("product_variants").select("*", { count: "exact", head: !0 }).eq("main_media_id", n);
|
|
369
390
|
if (h && h > 0) continue;
|
|
370
|
-
const {
|
|
371
|
-
if (
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
v.objectKey && q.push(v.objectKey);
|
|
377
|
-
}), await F(q), await e.from("media").delete().eq("id", s);
|
|
391
|
+
const { data: m } = await e.from("media").select("object_key, variants").eq("id", n).single();
|
|
392
|
+
if (m) {
|
|
393
|
+
const y = [m.object_key];
|
|
394
|
+
m.variants && Array.isArray(m.variants) && m.variants.forEach((q) => {
|
|
395
|
+
q.objectKey && y.push(q.objectKey);
|
|
396
|
+
}), await E(y), await e.from("media").delete().eq("id", n);
|
|
378
397
|
}
|
|
379
398
|
}
|
|
380
|
-
await k(o, i), i.category_ids !== void 0 && await
|
|
381
|
-
const { data:
|
|
382
|
-
return
|
|
399
|
+
await k(o, i), i.category_ids !== void 0 && await b(e, t, i.category_ids);
|
|
400
|
+
const { data: f } = await e.from("products").select("*").eq("id", o).single();
|
|
401
|
+
return f;
|
|
383
402
|
}
|
|
384
403
|
async function B(e, t) {
|
|
385
404
|
const { error: i } = await e.from("products").delete().eq("id", t);
|
|
386
405
|
if (i) throw i;
|
|
387
406
|
return !0;
|
|
388
407
|
}
|
|
389
|
-
async function
|
|
390
|
-
const { data: r, error:
|
|
391
|
-
if (
|
|
392
|
-
throw new Error(
|
|
408
|
+
async function O(e, t, i) {
|
|
409
|
+
const { data: r, error: s } = await e.from("products").select("*").eq("id", i).single();
|
|
410
|
+
if (s || !r)
|
|
411
|
+
throw new Error(s?.message || "Source product not found");
|
|
393
412
|
const { data: o, error: a } = await e.from("products").select("language_id").eq("id", t).single();
|
|
394
413
|
if (a || !o)
|
|
395
414
|
throw new Error(a?.message || "Target product not found");
|
|
396
|
-
const { error:
|
|
415
|
+
const { error: c } = await e.from("products").update({
|
|
397
416
|
title: r.title,
|
|
398
417
|
short_description: r.short_description,
|
|
399
418
|
description_json: r.description_json
|
|
400
419
|
}).eq("id", t);
|
|
401
|
-
if (
|
|
402
|
-
const { data: _, error:
|
|
403
|
-
if (
|
|
420
|
+
if (c) throw c;
|
|
421
|
+
const { data: _, error: l } = await e.from("blocks").select("*").eq("product_id", i).order("order", { ascending: !0 });
|
|
422
|
+
if (l) throw l;
|
|
404
423
|
if (await e.from("blocks").delete().eq("product_id", t), _ && _.length > 0) {
|
|
405
|
-
const
|
|
406
|
-
const { id:
|
|
424
|
+
const f = _.map((u) => {
|
|
425
|
+
const { id: d, created_at: g, updated_at: h, ...m } = u;
|
|
407
426
|
return {
|
|
408
|
-
...
|
|
427
|
+
...m,
|
|
409
428
|
product_id: t,
|
|
410
429
|
language_id: o.language_id
|
|
411
430
|
};
|
|
412
|
-
}), { error:
|
|
413
|
-
if (
|
|
431
|
+
}), { error: n } = await e.from("blocks").insert(f);
|
|
432
|
+
if (n) throw n;
|
|
414
433
|
}
|
|
415
434
|
await e.from("product_drafts").delete().eq("product_id", t), await e.from("product_media").delete().eq("product_id", t);
|
|
416
|
-
const { data:
|
|
417
|
-
if (
|
|
418
|
-
const
|
|
435
|
+
const { data: p } = await e.from("product_media").select("media_id, sort_order").eq("product_id", i);
|
|
436
|
+
if (p && p.length > 0) {
|
|
437
|
+
const f = p.map((n) => ({
|
|
419
438
|
product_id: t,
|
|
420
|
-
media_id:
|
|
421
|
-
sort_order:
|
|
439
|
+
media_id: n.media_id,
|
|
440
|
+
sort_order: n.sort_order
|
|
422
441
|
}));
|
|
423
|
-
await e.from("product_media").insert(
|
|
442
|
+
await e.from("product_media").insert(f);
|
|
424
443
|
}
|
|
425
444
|
return { success: !0 };
|
|
426
445
|
}
|
|
427
|
-
async function
|
|
446
|
+
async function U(e, t, i, r = [], s = []) {
|
|
428
447
|
const { data: o } = await e.from("languages").select("id").eq("code", i).single();
|
|
429
448
|
if (!o)
|
|
430
449
|
return { data: [], error: "Language not found" };
|
|
431
450
|
const a = [];
|
|
432
|
-
t.length > 0 && a.push(`translation_group_id.in.(${t.join(",")})`), r.length > 0 && a.push(`sku.in.(${r.map((
|
|
433
|
-
let
|
|
451
|
+
t.length > 0 && a.push(`translation_group_id.in.(${t.join(",")})`), r.length > 0 && a.push(`sku.in.(${r.map((l) => `"${l}"`).join(",")})`), s.length > 0 && a.push(`id.in.(${s.join(",")})`);
|
|
452
|
+
let c = e.from("products").select(`
|
|
434
453
|
id,
|
|
435
454
|
title,
|
|
436
455
|
sku,
|
|
@@ -499,40 +518,40 @@ async function O(e, t, i, r = [], c = []) {
|
|
|
499
518
|
),
|
|
500
519
|
translation_group_id
|
|
501
520
|
`).eq("language_id", o.id).eq("status", "active");
|
|
502
|
-
return a.length > 0 && (
|
|
521
|
+
return a.length > 0 && (c = c.or(a.join(","))), await c.order("id");
|
|
503
522
|
}
|
|
504
|
-
async function
|
|
523
|
+
async function b(e, t, i) {
|
|
505
524
|
const { data: r } = await e.from("products").select("translation_group_id").eq("id", t).single();
|
|
506
525
|
if (!r || !r.translation_group_id) {
|
|
507
526
|
if (await e.from("product_categories").delete().eq("product_id", t), i.length > 0) {
|
|
508
|
-
const a = i.map((
|
|
527
|
+
const a = i.map((c) => ({
|
|
509
528
|
product_id: t,
|
|
510
|
-
category_id:
|
|
529
|
+
category_id: c
|
|
511
530
|
}));
|
|
512
531
|
await e.from("product_categories").insert(a);
|
|
513
532
|
}
|
|
514
533
|
return;
|
|
515
534
|
}
|
|
516
|
-
const { data:
|
|
535
|
+
const { data: s } = await e.from("products").select("id").eq("translation_group_id", r.translation_group_id), o = s && s.length > 0 ? s.map((a) => a.id) : [t];
|
|
517
536
|
if (await e.from("product_categories").delete().in("product_id", o), i.length > 0) {
|
|
518
537
|
const a = [];
|
|
519
|
-
for (const
|
|
538
|
+
for (const c of o)
|
|
520
539
|
for (const _ of i)
|
|
521
540
|
a.push({
|
|
522
|
-
product_id:
|
|
541
|
+
product_id: c,
|
|
523
542
|
category_id: _
|
|
524
543
|
});
|
|
525
544
|
await e.from("product_categories").insert(a);
|
|
526
545
|
}
|
|
527
546
|
}
|
|
528
547
|
export {
|
|
529
|
-
|
|
548
|
+
O as copyProductFromLanguage,
|
|
530
549
|
$ as createProduct,
|
|
531
550
|
B as deleteProduct,
|
|
532
|
-
|
|
551
|
+
U as fetchTranslatedProductsForCartInternal,
|
|
533
552
|
D as getProduct,
|
|
534
553
|
N as getProductBySlug,
|
|
535
554
|
T as getProducts,
|
|
536
|
-
|
|
555
|
+
b as syncCategoriesForTranslationGroup,
|
|
537
556
|
z as updateProduct
|
|
538
557
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nextblock-cms/ecom",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.9",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
},
|
|
11
11
|
"type": "module",
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@nextblock-cms/db": "^0.13.
|
|
14
|
-
"@nextblock-cms/ui": "^0.13.
|
|
15
|
-
"@nextblock-cms/utils": "^0.13.
|
|
13
|
+
"@nextblock-cms/db": "^0.13.9",
|
|
14
|
+
"@nextblock-cms/ui": "^0.13.9",
|
|
15
|
+
"@nextblock-cms/utils": "^0.13.9",
|
|
16
16
|
"@freemius/checkout": "^1.4.1",
|
|
17
17
|
"@freemius/sdk": "^0.3.0",
|
|
18
18
|
"@hookform/resolvers": "^5.2.2",
|