@nextblock-cms/ecom 0.14.3 → 0.14.5

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.
@@ -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"),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(`
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"),C=require("@nextblock-cms/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 E(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:E(e.variants)}}async function j(e,t,r){const i=a=>{const o=typeof a=="string"?a.trim():"";return o||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(),a=s?.sku??r.sku,{data:o}=await e.from("products").select("id").eq("sku",a),c=(o??[]).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 A(e,{page:t=1,limit:r=10,offset:i,search:s="",languageId:a,categoryId:o,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)),a&&(d=d.eq("language_id",a)),_){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:o?[o]:[];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 T(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 T(e,t,r){const{data:i,error:s}=await e.from("products").select(`
84
+ `).eq("id",t).single()}async function R(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(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(`
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 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 $(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,c)=>({product_id:r,media_id:o.media_id,sort_order:c}));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 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: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 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,a,r.is_taxable),await j(e,a,r),await F(e,a,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(a,r),r.category_ids!==void 0&&await k(e,t,r.category_ids);const{data:f}=await e.from("products").select("*").eq("id",a).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 O(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: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: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:a.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: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(l=>`"${l}"`).join(",")})`),s.length>0&&o.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",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;
237
+ `).eq("language_id",a.id).eq("status","active").or(C.buildPublishedAtOrFilter());return o.length>0&&(c=c.or(o.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 o=r.map(c=>({product_id:t,category_id:c}));await e.from("product_categories").insert(o)}return}const{data:s}=await e.from("products").select("id").eq("translation_group_id",i.translation_group_id),a=s&&s.length>0?s.map(o=>o.id):[t];if(await e.from("product_categories").delete().in("product_id",a),r.length>0){const o=[];for(const c of a)for(const _ of r)o.push({product_id:c,category_id:_});await e.from("product_categories").insert(o)}}exports.copyProductFromLanguage=O;exports.createProduct=$;exports.deleteProduct=N;exports.fetchTranslatedProductsForCartInternal=B;exports.getProduct=T;exports.getProductBySlug=R;exports.getProducts=A;exports.syncCategoriesForTranslationGroup=k;exports.updateProduct=D;
@@ -117,6 +117,7 @@ export declare function getProduct(supabase: SupabaseClient<Database>, id: strin
117
117
  price: number;
118
118
  prices: import('../../../db/src/index.ts').Json;
119
119
  product_type: string;
120
+ published_at: string | null;
120
121
  sale_end_at: string | null;
121
122
  sale_price: number | null;
122
123
  sale_prices: import('../../../db/src/index.ts').Json | null;
@@ -242,6 +243,7 @@ export declare function createProduct(supabase: SupabaseClient<Database>, data:
242
243
  price: number;
243
244
  prices: import('../../../db/src/index.ts').Json;
244
245
  product_type: string;
246
+ published_at: string | null;
245
247
  sale_end_at: string | null;
246
248
  sale_price: number | null;
247
249
  sale_prices: import('../../../db/src/index.ts').Json | null;
@@ -279,6 +281,7 @@ export declare function updateProduct(supabase: SupabaseClient<Database>, id: st
279
281
  price: number;
280
282
  prices: import('../../../db/src/index.ts').Json;
281
283
  product_type: string;
284
+ published_at: string | null;
282
285
  sale_end_at: string | null;
283
286
  sale_price: number | null;
284
287
  sale_prices: import('../../../db/src/index.ts').Json | null;
@@ -1,14 +1,15 @@
1
- import { deleteMediaFiles as E } from "@nextblock-cms/utils/server";
1
+ import { deleteMediaFiles as F } from "@nextblock-cms/utils/server";
2
2
  import { syncSharedInventoryForSavedProduct as k } from "./shared-inventory.es.js";
3
- import { normalizeCurrencyCode as F } from "@nextblock-cms/utils/utils";
3
+ import { normalizeCurrencyCode as E } from "@nextblock-cms/utils/utils";
4
+ import { buildPublishedAtOrFilter as M } from "@nextblock-cms/utils";
4
5
  const w = (e) => Math.round(e * 100);
5
6
  function v(e) {
6
7
  return Object.entries(e || {}).reduce(
7
- (t, [i, r]) => (typeof r == "number" && Number.isFinite(r) && r >= 0 && (t[F(i)] = w(r)), t),
8
+ (t, [i, r]) => (typeof r == "number" && Number.isFinite(r) && r >= 0 && (t[E(i)] = w(r)), t),
8
9
  {}
9
10
  );
10
11
  }
11
- function M(e) {
12
+ function A(e) {
12
13
  return (e || []).map((t) => ({
13
14
  id: t.id,
14
15
  sku: t.sku,
@@ -24,7 +25,7 @@ function M(e) {
24
25
  attribute_term_ids: t.attribute_term_ids
25
26
  }));
26
27
  }
27
- function j(e, t) {
28
+ function P(e, t) {
28
29
  const r = e.product_type === "digital" && e.payment_provider === "freemius" ? Math.max(0, Number(e.trial_period_days ?? 0)) : 0;
29
30
  return {
30
31
  id: t,
@@ -52,10 +53,10 @@ function j(e, t) {
52
53
  is_taxable: e.is_taxable,
53
54
  language_id: e.language_id,
54
55
  translation_group_id: e.translation_group_id || void 0,
55
- variants: M(e.variants)
56
+ variants: A(e.variants)
56
57
  };
57
58
  }
58
- async function P(e, t, i) {
59
+ async function j(e, t, i) {
59
60
  const r = (o) => {
60
61
  const a = typeof o == "string" ? o.trim() : "";
61
62
  return a || null;
@@ -109,7 +110,7 @@ async function S(e, t, i) {
109
110
  throw d;
110
111
  }
111
112
  }
112
- async function T(e, {
113
+ async function N(e, {
113
114
  page: t = 1,
114
115
  limit: i = 10,
115
116
  offset: r,
@@ -152,7 +153,7 @@ async function T(e, {
152
153
  }
153
154
  return s && (d = d.or(`title.ilike.%${s}%,sku.ilike.%${s}%`)), d;
154
155
  }
155
- async function D(e, t) {
156
+ async function $(e, t) {
156
157
  return e.from("products").select(
157
158
  `
158
159
  *,
@@ -240,7 +241,7 @@ async function D(e, t) {
240
241
  `
241
242
  ).eq("id", t).single();
242
243
  }
243
- async function N(e, t, i) {
244
+ async function O(e, t, i) {
244
245
  const { data: r, error: s } = await e.from("products").select(
245
246
  `
246
247
  *,
@@ -336,9 +337,9 @@ async function N(e, t, i) {
336
337
  let o = null;
337
338
  return i && (o = r.find((a) => (Array.isArray(a.languages) ? a.languages[0] : a.languages)?.code === i)), o || (o = r.find((a) => (Array.isArray(a.languages) ? a.languages[0] : a.languages)?.is_default)), o || (o = r.find((a) => (Array.isArray(a.languages) ? a.languages[0] : a.languages)?.code === "en")), o || (o = r[0]), { data: o, error: null };
338
339
  }
339
- async function $(e, t) {
340
+ async function z(e, t) {
340
341
  const { data: i, error: r } = await e.rpc("upsert_product_with_variants", {
341
- product_payload: j(t)
342
+ product_payload: P(t)
342
343
  });
343
344
  if (r || !i) throw r || new Error("Failed to create product");
344
345
  if (t.product_media && t.product_media.length > 0) {
@@ -353,13 +354,13 @@ async function $(e, t) {
353
354
  media_id: t.media_id,
354
355
  sort_order: 0
355
356
  });
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
+ await x(e, i, t.is_taxable), await j(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
358
  const { data: s } = await e.from("products").select("*").eq("id", i).single();
358
359
  return s;
359
360
  }
360
- async function z(e, t, i) {
361
+ async function B(e, t, i) {
361
362
  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", {
362
- product_payload: j(i, t)
363
+ product_payload: P(i, t)
363
364
  });
364
365
  if (a || !o) throw a || new Error("Failed to update product");
365
366
  if (i.product_media) {
@@ -376,7 +377,7 @@ async function z(e, t, i) {
376
377
  media_id: i.media_id,
377
378
  sort_order: 0
378
379
  }));
379
- await x(e, o, i.is_taxable), await P(e, o, i), await S(e, o, i);
380
+ await x(e, o, i.is_taxable), await j(e, o, i), await S(e, o, i);
380
381
  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
382
  if (p.length > 0)
382
383
  for (const n of p) {
@@ -393,19 +394,19 @@ async function z(e, t, i) {
393
394
  const y = [m.object_key];
394
395
  m.variants && Array.isArray(m.variants) && m.variants.forEach((q) => {
395
396
  q.objectKey && y.push(q.objectKey);
396
- }), await E(y), await e.from("media").delete().eq("id", n);
397
+ }), await F(y), await e.from("media").delete().eq("id", n);
397
398
  }
398
399
  }
399
400
  await k(o, i), i.category_ids !== void 0 && await b(e, t, i.category_ids);
400
401
  const { data: f } = await e.from("products").select("*").eq("id", o).single();
401
402
  return f;
402
403
  }
403
- async function B(e, t) {
404
+ async function U(e, t) {
404
405
  const { error: i } = await e.from("products").delete().eq("id", t);
405
406
  if (i) throw i;
406
407
  return !0;
407
408
  }
408
- async function O(e, t, i) {
409
+ async function V(e, t, i) {
409
410
  const { data: r, error: s } = await e.from("products").select("*").eq("id", i).single();
410
411
  if (s || !r)
411
412
  throw new Error(s?.message || "Source product not found");
@@ -443,7 +444,7 @@ async function O(e, t, i) {
443
444
  }
444
445
  return { success: !0 };
445
446
  }
446
- async function U(e, t, i, r = [], s = []) {
447
+ async function K(e, t, i, r = [], s = []) {
447
448
  const { data: o } = await e.from("languages").select("id").eq("code", i).single();
448
449
  if (!o)
449
450
  return { data: [], error: "Language not found" };
@@ -517,7 +518,7 @@ async function U(e, t, i, r = [], s = []) {
517
518
  )
518
519
  ),
519
520
  translation_group_id
520
- `).eq("language_id", o.id).eq("status", "active");
521
+ `).eq("language_id", o.id).eq("status", "active").or(M());
521
522
  return a.length > 0 && (c = c.or(a.join(","))), await c.order("id");
522
523
  }
523
524
  async function b(e, t, i) {
@@ -545,13 +546,13 @@ async function b(e, t, i) {
545
546
  }
546
547
  }
547
548
  export {
548
- O as copyProductFromLanguage,
549
- $ as createProduct,
550
- B as deleteProduct,
551
- U as fetchTranslatedProductsForCartInternal,
552
- D as getProduct,
553
- N as getProductBySlug,
554
- T as getProducts,
549
+ V as copyProductFromLanguage,
550
+ z as createProduct,
551
+ U as deleteProduct,
552
+ K as fetchTranslatedProductsForCartInternal,
553
+ $ as getProduct,
554
+ O as getProductBySlug,
555
+ N as getProducts,
555
556
  b as syncCategoriesForTranslationGroup,
556
- z as updateProduct
557
+ B as updateProduct
557
558
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nextblock-cms/ecom",
3
- "version": "0.14.3",
3
+ "version": "0.14.5",
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.14.3",
14
- "@nextblock-cms/ui": "^0.14.3",
15
- "@nextblock-cms/utils": "^0.14.3",
13
+ "@nextblock-cms/db": "^0.14.5",
14
+ "@nextblock-cms/ui": "^0.14.5",
15
+ "@nextblock-cms/utils": "^0.14.5",
16
16
  "@freemius/checkout": "^1.4.1",
17
17
  "@freemius/sdk": "^0.3.0",
18
18
  "@hookform/resolvers": "^5.2.2",