@nextblock-cms/ecom 0.8.1 → 0.8.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.cjs.js +1 -1
- package/index.es.js +136 -3224
- package/lib/CurrencyProvider.cjs.js +1 -0
- package/lib/CurrencyProvider.es.js +99 -0
- package/lib/cart-store.cjs.js +1 -0
- package/lib/cart-store.es.js +116 -0
- package/lib/components/AccountNavigationMenu.cjs.js +1 -0
- package/lib/components/AccountNavigationMenu.es.js +44 -0
- package/lib/components/AddToCartButton.cjs.js +1 -0
- package/lib/components/AddToCartButton.es.js +60 -0
- package/lib/components/Cart.cjs.js +1 -0
- package/lib/components/Cart.es.js +148 -0
- package/lib/components/CartDrawer.cjs.js +1 -0
- package/lib/components/CartDrawer.es.js +119 -0
- package/lib/components/CartIcon.cjs.js +1 -0
- package/lib/components/CartIcon.es.js +34 -0
- package/lib/components/Checkout.cjs.js +1 -0
- package/lib/components/Checkout.es.js +987 -0
- package/lib/components/CouponForm.cjs.js +1 -0
- package/lib/components/CouponForm.es.js +136 -0
- package/lib/components/CurrencySwitcher.cjs.js +1 -0
- package/lib/components/CurrencySwitcher.es.js +18 -0
- package/lib/components/CustomerProfileForm.cjs.js +1 -0
- package/lib/components/CustomerProfileForm.es.js +355 -0
- package/lib/components/FeaturedProduct.cjs.js +1 -0
- package/lib/components/FeaturedProduct.es.js +72 -0
- package/lib/components/InvoiceDocument.cjs.js +1 -0
- package/lib/components/InvoiceDocument.es.js +196 -0
- package/lib/components/InvoiceViewerShell.cjs.js +29 -0
- package/lib/components/InvoiceViewerShell.es.js +100 -0
- package/lib/components/ProductCard.cjs.js +1 -0
- package/lib/components/ProductCard.es.js +119 -0
- package/lib/components/ProductDetailsLayout.cjs.js +1 -0
- package/lib/components/ProductDetailsLayout.es.js +361 -0
- package/lib/components/ProductGallery.cjs.js +1 -0
- package/lib/components/ProductGallery.es.js +41 -0
- package/lib/components/ProductGrid.cjs.js +1 -0
- package/lib/components/ProductGrid.es.js +19 -0
- package/lib/components/ShippingEstimator.cjs.js +1 -0
- package/lib/components/ShippingEstimator.es.js +125 -0
- package/lib/components/SimpleTiptapRenderer.cjs.js +1 -0
- package/lib/components/SimpleTiptapRenderer.es.js +187 -0
- package/lib/components/SubscriptionSelector.cjs.js +1 -0
- package/lib/components/SubscriptionSelector.es.js +140 -0
- package/lib/countries.cjs.js +1 -0
- package/lib/countries.es.js +77 -0
- package/lib/coupon-server.cjs.js +1 -0
- package/lib/coupon-server.es.js +234 -0
- package/lib/coupons.cjs.js +1 -0
- package/lib/coupons.es.js +17 -0
- package/lib/currency-constants.cjs.js +1 -0
- package/lib/currency-constants.es.js +4 -0
- package/lib/currency-store.cjs.js +1 -0
- package/lib/currency-store.es.js +27 -0
- package/lib/currency-sync.cjs.js +1 -0
- package/lib/currency-sync.es.js +180 -0
- package/lib/currency.cjs.js +1 -0
- package/lib/currency.es.js +312 -0
- package/lib/customer-addresses.cjs.js +1 -0
- package/lib/customer-addresses.es.js +116 -0
- package/lib/customer-orders.cjs.js +1 -0
- package/lib/customer-orders.es.js +45 -0
- package/lib/customer.cjs.js +1 -0
- package/lib/customer.es.js +58 -0
- package/lib/export-helpers.cjs.js +1 -0
- package/lib/export-helpers.es.js +56 -0
- package/lib/factory.cjs.js +1 -0
- package/lib/factory.es.js +8 -0
- package/lib/freemius-coupons.cjs.js +1 -0
- package/lib/freemius-coupons.es.js +272 -0
- package/lib/freemius-order-sync.cjs.js +1 -0
- package/lib/freemius-order-sync.es.js +284 -0
- package/lib/inventory-settings.cjs.js +1 -0
- package/lib/inventory-settings.es.js +86 -0
- package/lib/invoice-server.cjs.js +33 -0
- package/lib/invoice-server.es.js +141 -0
- package/lib/invoice-ui.cjs.js +1 -0
- package/lib/invoice-ui.es.js +71 -0
- package/lib/invoice.cjs.js +1 -0
- package/lib/invoice.es.js +102 -0
- package/lib/order-inventory.cjs.js +61 -0
- package/lib/order-inventory.es.js +128 -0
- package/lib/order-tax-details.cjs.js +1 -0
- package/lib/order-tax-details.es.js +164 -0
- package/lib/pages/cms/coupons/CouponEditorForm.cjs.js +1 -0
- package/lib/pages/cms/coupons/CouponEditorForm.es.js +189 -0
- package/lib/pages/cms/coupons/CouponsPage.cjs.js +5 -0
- package/lib/pages/cms/coupons/CouponsPage.es.js +171 -0
- package/lib/pages/cms/coupons/CreateCouponDialog.cjs.js +1 -0
- package/lib/pages/cms/coupons/CreateCouponDialog.es.js +35 -0
- package/lib/pages/cms/coupons/EditCouponPage.cjs.js +1 -0
- package/lib/pages/cms/coupons/EditCouponPage.es.js +48 -0
- package/lib/pages/cms/coupons/ProductScopePicker.cjs.js +1 -0
- package/lib/pages/cms/coupons/ProductScopePicker.es.js +120 -0
- package/lib/pages/cms/coupons/actions.cjs.js +1 -0
- package/lib/pages/cms/coupons/actions.es.js +128 -0
- package/lib/pages/cms/coupons/product-options.cjs.js +1 -0
- package/lib/pages/cms/coupons/product-options.es.js +27 -0
- package/lib/pages/cms/orders/ExportReportsDialog.cjs.js +1 -0
- package/lib/pages/cms/orders/ExportReportsDialog.es.js +130 -0
- package/lib/pages/cms/orders/OrderDetailPage.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderDetailPage.es.js +137 -0
- package/lib/pages/cms/orders/OrderPrintButton.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderPrintButton.es.js +22 -0
- package/lib/pages/cms/orders/OrderStatusForm.cjs.js +1 -0
- package/lib/pages/cms/orders/OrderStatusForm.es.js +71 -0
- package/lib/pages/cms/orders/OrdersPage.cjs.js +1 -0
- package/lib/pages/cms/orders/OrdersPage.es.js +104 -0
- package/lib/pages/cms/orders/actions.cjs.js +7 -0
- package/lib/pages/cms/orders/actions.es.js +71 -0
- package/lib/pages/cms/orders/export-actions.cjs.js +1 -0
- package/lib/pages/cms/orders/export-actions.es.js +27 -0
- package/lib/pages/cms/orders/server-actions.cjs.js +1 -0
- package/lib/pages/cms/orders/server-actions.es.js +55 -0
- package/lib/pages/cms/payments/PaymentsClient.cjs.js +1 -0
- package/lib/pages/cms/payments/PaymentsClient.es.js +188 -0
- package/lib/pages/cms/payments/PaymentsPage.cjs.js +1 -0
- package/lib/pages/cms/payments/PaymentsPage.es.js +29 -0
- package/lib/pages/cms/payments/actions.cjs.js +1 -0
- package/lib/pages/cms/payments/actions.es.js +23 -0
- package/lib/pages/cms/payments/queries.cjs.js +1 -0
- package/lib/pages/cms/payments/queries.es.js +30 -0
- package/lib/pages/cms/products/ProductsPage.cjs.js +1 -0
- package/lib/pages/cms/products/ProductsPage.es.js +46 -0
- package/lib/pages/cms/products/_id_/edit/EditProductPage.cjs.js +1 -0
- package/lib/pages/cms/products/_id_/edit/EditProductPage.es.js +156 -0
- package/lib/pages/cms/products/actions.cjs.js +44 -0
- package/lib/pages/cms/products/actions.es.js +175 -0
- package/lib/pages/cms/products/attributes/AttributeManagementPage.cjs.js +1 -0
- package/lib/pages/cms/products/attributes/AttributeManagementPage.es.js +48 -0
- package/lib/pages/cms/products/attributes/components/AttributeManager.cjs.js +1 -0
- package/lib/pages/cms/products/attributes/components/AttributeManager.es.js +291 -0
- package/lib/pages/cms/products/categories/CategoryManagementPage.cjs.js +1 -0
- package/lib/pages/cms/products/categories/CategoryManagementPage.es.js +36 -0
- package/lib/pages/cms/products/categories/components/CategoryManager.cjs.js +1 -0
- package/lib/pages/cms/products/categories/components/CategoryManager.es.js +263 -0
- package/lib/pages/cms/products/components/CopyProductFromLanguage.cjs.js +1 -0
- package/lib/pages/cms/products/components/CopyProductFromLanguage.es.js +114 -0
- package/lib/pages/cms/products/components/CurrencyPriceFields.cjs.js +1 -0
- package/lib/pages/cms/products/components/CurrencyPriceFields.es.js +107 -0
- package/lib/pages/cms/products/components/DeleteProductButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/DeleteProductButton.es.js +48 -0
- package/lib/pages/cms/products/components/FreemiusPricingDashboard.cjs.js +1 -0
- package/lib/pages/cms/products/components/FreemiusPricingDashboard.es.js +86 -0
- package/lib/pages/cms/products/components/ProductCategorySelector.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductCategorySelector.es.js +160 -0
- package/lib/pages/cms/products/components/ProductForm.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductForm.es.js +702 -0
- package/lib/pages/cms/products/components/ProductMediaManager.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductMediaManager.es.js +104 -0
- package/lib/pages/cms/products/components/ProductsBulkTable.cjs.js +1 -0
- package/lib/pages/cms/products/components/ProductsBulkTable.es.js +228 -0
- package/lib/pages/cms/products/components/SaleScheduleFields.cjs.js +1 -0
- package/lib/pages/cms/products/components/SaleScheduleFields.es.js +137 -0
- package/lib/pages/cms/products/components/SyncFreemiusButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/SyncFreemiusButton.es.js +36 -0
- package/lib/pages/cms/products/components/SyncFreemiusPricingButton.cjs.js +1 -0
- package/lib/pages/cms/products/components/SyncFreemiusPricingButton.es.js +40 -0
- package/lib/pages/cms/products/components/VariationsEditor.cjs.js +1 -0
- package/lib/pages/cms/products/components/VariationsEditor.es.js +408 -0
- package/lib/pages/cms/products/inventory/InventoryPage.cjs.js +1 -0
- package/lib/pages/cms/products/inventory/InventoryPage.es.js +16 -0
- package/lib/pages/cms/products/inventory/InventoryTableClient.cjs.js +2 -0
- package/lib/pages/cms/products/inventory/InventoryTableClient.es.js +214 -0
- package/lib/pages/cms/products/inventory/actions.cjs.js +3 -0
- package/lib/pages/cms/products/inventory/actions.es.js +95 -0
- package/lib/pages/cms/products/new/NewProductPage.cjs.js +1 -0
- package/lib/pages/cms/products/new/NewProductPage.es.js +76 -0
- package/lib/pages/cms/products/product-price-sync.cjs.js +1 -0
- package/lib/pages/cms/products/product-price-sync.es.js +98 -0
- package/lib/pages/cms/products/server-actions.cjs.js +1 -0
- package/lib/pages/cms/products/server-actions.es.js +193 -0
- package/lib/pages/cms/shipping/ShippingPage.cjs.js +5 -0
- package/lib/pages/cms/shipping/ShippingPage.es.js +211 -0
- package/lib/pages/cms/shipping/components/RateForm.cjs.js +1 -0
- package/lib/pages/cms/shipping/components/RateForm.es.js +441 -0
- package/lib/pages/cms/shipping/components/ZoneForm.cjs.js +1 -0
- package/lib/pages/cms/shipping/components/ZoneForm.es.js +228 -0
- package/lib/pages/cms/shipping/server-actions.cjs.js +1 -0
- package/lib/pages/cms/shipping/server-actions.es.js +159 -0
- package/lib/pages/cms/taxes/TaxesPage.cjs.js +1 -0
- package/lib/pages/cms/taxes/TaxesPage.es.js +172 -0
- package/lib/pages/cms/taxes/actions.cjs.js +1 -0
- package/lib/pages/cms/taxes/actions.es.js +57 -0
- package/lib/pages/cms/taxes/components/TaxRateForm.cjs.js +1 -0
- package/lib/pages/cms/taxes/components/TaxRateForm.es.js +100 -0
- package/lib/product-actions.cjs.js +237 -0
- package/lib/product-actions.es.js +524 -0
- package/lib/product-context.cjs.js +1 -0
- package/lib/product-context.es.js +16 -0
- package/lib/product-schema.cjs.js +1 -0
- package/lib/product-schema.es.js +134 -0
- package/lib/providers/freemius.cjs.js +5 -0
- package/lib/providers/freemius.es.js +538 -0
- package/lib/providers/stripe.cjs.js +1 -0
- package/lib/providers/stripe.es.js +473 -0
- package/lib/server-actions/coupon-actions.cjs.js +1 -0
- package/lib/server-actions/coupon-actions.es.js +23 -0
- package/lib/server-actions/customer-actions.cjs.js +1 -0
- package/lib/server-actions/customer-actions.es.js +30 -0
- package/lib/server-actions/product-actions.cjs.js +1 -0
- package/lib/server-actions/product-actions.es.js +16 -0
- package/lib/server-actions/shipping-actions.cjs.js +1 -0
- package/lib/server-actions/shipping-actions.es.js +27 -0
- package/lib/server-actions/tax-actions.cjs.js +1 -0
- package/lib/server-actions/tax-actions.es.js +40 -0
- package/lib/shared-inventory.cjs.js +15 -0
- package/lib/shared-inventory.es.js +171 -0
- package/lib/shipping/resolver.cjs.js +7 -0
- package/lib/shipping/resolver.es.js +62 -0
- package/lib/shipping-rate-currency.cjs.js +1 -0
- package/lib/shipping-rate-currency.es.js +72 -0
- package/lib/states.cjs.js +1 -0
- package/lib/states.es.js +127 -0
- package/lib/stripe/checkout.cjs.js +1 -0
- package/lib/stripe/checkout.es.js +182 -0
- package/lib/stripe/client.cjs.js +1 -0
- package/lib/stripe/client.es.js +7 -0
- package/lib/stripe/order-sync.cjs.js +1 -0
- package/lib/stripe/order-sync.es.js +130 -0
- package/lib/stripe/webhooks.cjs.js +1 -0
- package/lib/stripe/webhooks.es.js +30 -0
- package/lib/tax-calculation.cjs.js +1 -0
- package/lib/tax-calculation.es.js +101 -0
- package/lib/trials.cjs.js +1 -0
- package/lib/trials.es.js +29 -0
- package/lib/types.cjs.js +1 -0
- package/lib/types.es.js +33 -0
- package/lib/use-cart.cjs.js +1 -0
- package/lib/use-cart.es.js +44 -0
- package/lib/variation-utils.cjs.js +1 -0
- package/lib/variation-utils.es.js +246 -0
- package/lib/zod-config.cjs.js +1 -0
- package/lib/zod-config.es.js +6 -0
- package/package.json +10 -5
- package/server.cjs.js +1 -110
- package/server.es.js +178 -6037
- package/invoice-ui-BJZfedDd.cjs +0 -332
- package/invoice-ui-GJ1ADAjn.js +0 -5759
- /package/{currency-rest-client-uolJxUkL.cjs → lib/currency-rest-client.cjs.js} +0 -0
- /package/{currency-rest-client-CwoqdgAP.js → lib/currency-rest-client.es.js} +0 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const S=require("@nextblock-cms/db/server"),h=require("./customer.cjs.js"),u=require("./invoice.cjs.js"),E=require("./order-tax-details.cjs.js");function U(e){return e?e.startsWith("http")||e.startsWith("/")?e:process.env.NEXT_PUBLIC_R2_BASE_URL?`${process.env.NEXT_PUBLIC_R2_BASE_URL}/${e}`:process.env.NEXT_PUBLIC_SUPABASE_URL?`${process.env.NEXT_PUBLIC_SUPABASE_URL}/storage/v1/object/public/media/${e}`:e:null}function s(e){return e??S.getServiceRoleSupabaseClient()}function k(e){if(!e)return null;const n=e.media?.object_key??e.media?.file_path??null;return{id:e.id,name:e.name??null,url:U(n),width:e.media?.width??null,height:e.media?.height??null}}async function p(e){const n=s(e),[r,t]=await Promise.all([n.from("site_settings").select("value").eq("key",u.INVOICE_SETTINGS_KEY).maybeSingle(),n.from("logos").select(`
|
|
2
|
+
id,
|
|
3
|
+
name,
|
|
4
|
+
media:media_id (
|
|
5
|
+
object_key,
|
|
6
|
+
file_path,
|
|
7
|
+
width,
|
|
8
|
+
height
|
|
9
|
+
)
|
|
10
|
+
`).order("created_at",{ascending:!1}).limit(1).maybeSingle()]);return{settings:u.normalizeInvoiceSettings(r.data?.value??u.DEFAULT_INVOICE_SETTINGS),logo:k(t.data)}}async function B(e){const n=s(e.client),{data:r,error:t}=await n.rpc("assign_order_invoice_metadata",{p_order_id:e.orderId,p_paid_at:e.paidAt??null}).single();if(t)throw new Error(t.message);return{invoiceNumber:r?.invoice_number??null,paidAt:r?.paid_at??null}}async function m(e,n){const r=s(n),{data:t,error:i}=await r.from("orders").select(`
|
|
11
|
+
id,
|
|
12
|
+
invoice_number,
|
|
13
|
+
paid_at,
|
|
14
|
+
created_at,
|
|
15
|
+
currency,
|
|
16
|
+
status,
|
|
17
|
+
provider,
|
|
18
|
+
subtotal,
|
|
19
|
+
shipping_total,
|
|
20
|
+
discount_total,
|
|
21
|
+
coupon_code,
|
|
22
|
+
tax_total,
|
|
23
|
+
total,
|
|
24
|
+
customer_details,
|
|
25
|
+
tax_details,
|
|
26
|
+
order_items (
|
|
27
|
+
id,
|
|
28
|
+
product_id,
|
|
29
|
+
variant_id,
|
|
30
|
+
quantity,
|
|
31
|
+
price_at_purchase
|
|
32
|
+
)
|
|
33
|
+
`).eq("id",e).single();if(i||!t)throw new Error(i?.message||"Order not found");const l=(t.order_items||[]).map(a=>a.product_id).filter(Boolean),c=(t.order_items||[]).map(a=>a.variant_id).filter(Boolean),[g,v]=await Promise.all([l.length?r.from("products").select("id, title, sku").in("id",l):Promise.resolve({data:[],error:null}),c.length?r.from("product_variants").select("id, sku").in("id",c):Promise.resolve({data:[],error:null})]),b=new Map((g.data||[]).map(a=>[a.id,a])),f=new Map((v.data||[]).map(a=>[a.id,a])),_=(t.order_items||[]).map(a=>{const o=a.product_id?b.get(a.product_id):null,d=a.variant_id?f.get(a.variant_id):null,y=o?.title||"Product",I=d?.sku?`SKU: ${d.sku}`:o?.sku?`SKU: ${o.sku}`:null;return{id:a.id,product_id:a.product_id??null,variant_id:a.variant_id??null,title:y,description:I,quantity:a.quantity,unit_amount:a.price_at_purchase,total_amount:a.price_at_purchase*a.quantity,sku:d?.sku??o?.sku??null}});return{id:t.id,invoice_number:t.invoice_number??null,paid_at:t.paid_at??null,created_at:t.created_at??null,currency:t.currency||"usd",status:t.status,provider:t.provider??null,subtotal:typeof t.subtotal=="number"?t.subtotal:_.reduce((a,o)=>a+o.total_amount,0),shipping_total:typeof t.shipping_total=="number"?t.shipping_total:0,discount_total:typeof t.discount_total=="number"?t.discount_total:0,coupon_code:t.coupon_code??null,tax_total:typeof t.tax_total=="number"?t.tax_total:0,total:t.total,customer_details:h.normalizeOrderCustomerDetails(t.customer_details??{}),tax_details:E.normalizeOrderTaxDetails(t.tax_details),items:_}}async function R(e,n){const r=s(n),[t,i]=await Promise.all([m(e,r),p(r)]);return{order:t,settings:i.settings,logo:i.logo}}exports.assignInvoiceMetadata=B;exports.getInvoiceBrandingData=p;exports.getInvoiceOrder=m;exports.getInvoicePresentationData=R;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { getServiceRoleSupabaseClient as f } from "@nextblock-cms/db/server";
|
|
2
|
+
import { normalizeOrderCustomerDetails as y } from "./customer.es.js";
|
|
3
|
+
import { INVOICE_SETTINGS_KEY as h, normalizeInvoiceSettings as I, DEFAULT_INVOICE_SETTINGS as S } from "./invoice.es.js";
|
|
4
|
+
import { normalizeOrderTaxDetails as E } from "./order-tax-details.es.js";
|
|
5
|
+
function U(e) {
|
|
6
|
+
return e ? e.startsWith("http") || e.startsWith("/") ? e : process.env.NEXT_PUBLIC_R2_BASE_URL ? `${process.env.NEXT_PUBLIC_R2_BASE_URL}/${e}` : process.env.NEXT_PUBLIC_SUPABASE_URL ? `${process.env.NEXT_PUBLIC_SUPABASE_URL}/storage/v1/object/public/media/${e}` : e : null;
|
|
7
|
+
}
|
|
8
|
+
function s(e) {
|
|
9
|
+
return e ?? f();
|
|
10
|
+
}
|
|
11
|
+
function k(e) {
|
|
12
|
+
if (!e)
|
|
13
|
+
return null;
|
|
14
|
+
const o = e.media?.object_key ?? e.media?.file_path ?? null;
|
|
15
|
+
return {
|
|
16
|
+
id: e.id,
|
|
17
|
+
name: e.name ?? null,
|
|
18
|
+
url: U(o),
|
|
19
|
+
width: e.media?.width ?? null,
|
|
20
|
+
height: e.media?.height ?? null
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
async function R(e) {
|
|
24
|
+
const o = s(e), [r, t] = await Promise.all([
|
|
25
|
+
o.from("site_settings").select("value").eq("key", h).maybeSingle(),
|
|
26
|
+
o.from("logos").select(
|
|
27
|
+
`
|
|
28
|
+
id,
|
|
29
|
+
name,
|
|
30
|
+
media:media_id (
|
|
31
|
+
object_key,
|
|
32
|
+
file_path,
|
|
33
|
+
width,
|
|
34
|
+
height
|
|
35
|
+
)
|
|
36
|
+
`
|
|
37
|
+
).order("created_at", { ascending: !1 }).limit(1).maybeSingle()
|
|
38
|
+
]);
|
|
39
|
+
return {
|
|
40
|
+
settings: I(r.data?.value ?? S),
|
|
41
|
+
logo: k(t.data)
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
async function C(e) {
|
|
45
|
+
const o = s(e.client), { data: r, error: t } = await o.rpc("assign_order_invoice_metadata", {
|
|
46
|
+
p_order_id: e.orderId,
|
|
47
|
+
p_paid_at: e.paidAt ?? null
|
|
48
|
+
}).single();
|
|
49
|
+
if (t)
|
|
50
|
+
throw new Error(t.message);
|
|
51
|
+
return {
|
|
52
|
+
invoiceNumber: r?.invoice_number ?? null,
|
|
53
|
+
paidAt: r?.paid_at ?? null
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async function B(e, o) {
|
|
57
|
+
const r = s(o), { data: t, error: i } = await r.from("orders").select(
|
|
58
|
+
`
|
|
59
|
+
id,
|
|
60
|
+
invoice_number,
|
|
61
|
+
paid_at,
|
|
62
|
+
created_at,
|
|
63
|
+
currency,
|
|
64
|
+
status,
|
|
65
|
+
provider,
|
|
66
|
+
subtotal,
|
|
67
|
+
shipping_total,
|
|
68
|
+
discount_total,
|
|
69
|
+
coupon_code,
|
|
70
|
+
tax_total,
|
|
71
|
+
total,
|
|
72
|
+
customer_details,
|
|
73
|
+
tax_details,
|
|
74
|
+
order_items (
|
|
75
|
+
id,
|
|
76
|
+
product_id,
|
|
77
|
+
variant_id,
|
|
78
|
+
quantity,
|
|
79
|
+
price_at_purchase
|
|
80
|
+
)
|
|
81
|
+
`
|
|
82
|
+
).eq("id", e).single();
|
|
83
|
+
if (i || !t)
|
|
84
|
+
throw new Error(i?.message || "Order not found");
|
|
85
|
+
const u = (t.order_items || []).map((a) => a.product_id).filter(Boolean), l = (t.order_items || []).map((a) => a.variant_id).filter(Boolean), [_, p] = await Promise.all([
|
|
86
|
+
u.length ? r.from("products").select("id, title, sku").in("id", u) : Promise.resolve({ data: [], error: null }),
|
|
87
|
+
l.length ? r.from("product_variants").select("id, sku").in("id", l) : Promise.resolve({ data: [], error: null })
|
|
88
|
+
]), m = new Map(
|
|
89
|
+
(_.data || []).map((a) => [a.id, a])
|
|
90
|
+
), g = new Map(
|
|
91
|
+
(p.data || []).map((a) => [a.id, a])
|
|
92
|
+
), c = (t.order_items || []).map((a) => {
|
|
93
|
+
const n = a.product_id ? m.get(a.product_id) : null, d = a.variant_id ? g.get(a.variant_id) : null, v = n?.title || "Product", b = d?.sku ? `SKU: ${d.sku}` : n?.sku ? `SKU: ${n.sku}` : null;
|
|
94
|
+
return {
|
|
95
|
+
id: a.id,
|
|
96
|
+
product_id: a.product_id ?? null,
|
|
97
|
+
variant_id: a.variant_id ?? null,
|
|
98
|
+
title: v,
|
|
99
|
+
description: b,
|
|
100
|
+
quantity: a.quantity,
|
|
101
|
+
unit_amount: a.price_at_purchase,
|
|
102
|
+
total_amount: a.price_at_purchase * a.quantity,
|
|
103
|
+
sku: d?.sku ?? n?.sku ?? null
|
|
104
|
+
};
|
|
105
|
+
});
|
|
106
|
+
return {
|
|
107
|
+
id: t.id,
|
|
108
|
+
invoice_number: t.invoice_number ?? null,
|
|
109
|
+
paid_at: t.paid_at ?? null,
|
|
110
|
+
created_at: t.created_at ?? null,
|
|
111
|
+
currency: t.currency || "usd",
|
|
112
|
+
status: t.status,
|
|
113
|
+
provider: t.provider ?? null,
|
|
114
|
+
subtotal: typeof t.subtotal == "number" ? t.subtotal : c.reduce((a, n) => a + n.total_amount, 0),
|
|
115
|
+
shipping_total: typeof t.shipping_total == "number" ? t.shipping_total : 0,
|
|
116
|
+
discount_total: typeof t.discount_total == "number" ? t.discount_total : 0,
|
|
117
|
+
coupon_code: t.coupon_code ?? null,
|
|
118
|
+
tax_total: typeof t.tax_total == "number" ? t.tax_total : 0,
|
|
119
|
+
total: t.total,
|
|
120
|
+
customer_details: y(t.customer_details ?? {}),
|
|
121
|
+
tax_details: E(t.tax_details),
|
|
122
|
+
items: c
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
async function N(e, o) {
|
|
126
|
+
const r = s(o), [t, i] = await Promise.all([
|
|
127
|
+
B(e, r),
|
|
128
|
+
R(r)
|
|
129
|
+
]);
|
|
130
|
+
return {
|
|
131
|
+
order: t,
|
|
132
|
+
settings: i.settings,
|
|
133
|
+
logo: i.logo
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
export {
|
|
137
|
+
C as assignInvoiceMetadata,
|
|
138
|
+
R as getInvoiceBrandingData,
|
|
139
|
+
B as getInvoiceOrder,
|
|
140
|
+
N as getInvoicePresentationData
|
|
141
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});function r(e,t,a){const n=e(t);return n===t?a:n}function i(e){return{invoice:r(e,"invoice","Invoice"),invoiceNumber:r(e,"invoice_number","Invoice #"),orderNumber:r(e,"order_number","Order #"),paidOn:r(e,"paid_on","Paid on"),status:r(e,"status","Status"),from:r(e,"from","From"),billTo:r(e,"bill_to","Bill to"),shipTo:r(e,"ship_to","Ship to"),item:r(e,"product","Item"),details:r(e,"details","Details"),quantity:r(e,"ecommerce.qty","Qty"),price:r(e,"price","Price"),amount:r(e,"amount","Amount"),subtotal:r(e,"ecommerce.subtotal","Subtotal"),discount:r(e,"ecommerce.discount","Discount"),shipping:r(e,"ecommerce.shipping","Shipping"),tax:r(e,"ecommerce.tax","Tax"),total:r(e,"ecommerce.total","Total"),taxBreakdown:r(e,"tax_breakdown","Tax breakdown"),taxRegistrations:r(e,"tax_registrations","Tax registrations")}}function s(e){return e==="fr"?"fr-CA":"en-US"}function o(e,t){if(!e)return"";switch(e.toLowerCase()){case"paid":return r(t,"order_status_paid","Paid");case"pending":return r(t,"order_status_pending","Pending");case"trial":return r(t,"order_status_trial","Trial");case"shipped":return r(t,"order_status_shipped","Shipped");case"cancelled":return r(t,"order_status_cancelled","Cancelled");case"refunded":return r(t,"order_status_refunded","Refunded");default:return e}}function c(e,t){return e?{...e,order:{...e.order,status:o(e.order.status,t)}}:null}exports.buildInvoiceDocumentLabels=i;exports.getInvoiceLocale=s;exports.localizeInvoicePresentationData=c;exports.translateOrFallback=r;exports.translateOrderStatus=o;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
function r(e, t, n) {
|
|
2
|
+
const a = e(t);
|
|
3
|
+
return a === t ? n : a;
|
|
4
|
+
}
|
|
5
|
+
function i(e) {
|
|
6
|
+
return {
|
|
7
|
+
invoice: r(e, "invoice", "Invoice"),
|
|
8
|
+
invoiceNumber: r(e, "invoice_number", "Invoice #"),
|
|
9
|
+
orderNumber: r(e, "order_number", "Order #"),
|
|
10
|
+
paidOn: r(e, "paid_on", "Paid on"),
|
|
11
|
+
status: r(e, "status", "Status"),
|
|
12
|
+
from: r(e, "from", "From"),
|
|
13
|
+
billTo: r(e, "bill_to", "Bill to"),
|
|
14
|
+
shipTo: r(e, "ship_to", "Ship to"),
|
|
15
|
+
item: r(e, "product", "Item"),
|
|
16
|
+
details: r(e, "details", "Details"),
|
|
17
|
+
quantity: r(e, "ecommerce.qty", "Qty"),
|
|
18
|
+
price: r(e, "price", "Price"),
|
|
19
|
+
amount: r(e, "amount", "Amount"),
|
|
20
|
+
subtotal: r(e, "ecommerce.subtotal", "Subtotal"),
|
|
21
|
+
discount: r(e, "ecommerce.discount", "Discount"),
|
|
22
|
+
shipping: r(e, "ecommerce.shipping", "Shipping"),
|
|
23
|
+
tax: r(e, "ecommerce.tax", "Tax"),
|
|
24
|
+
total: r(e, "ecommerce.total", "Total"),
|
|
25
|
+
taxBreakdown: r(e, "tax_breakdown", "Tax breakdown"),
|
|
26
|
+
taxRegistrations: r(
|
|
27
|
+
e,
|
|
28
|
+
"tax_registrations",
|
|
29
|
+
"Tax registrations"
|
|
30
|
+
)
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function s(e) {
|
|
34
|
+
return e === "fr" ? "fr-CA" : "en-US";
|
|
35
|
+
}
|
|
36
|
+
function o(e, t) {
|
|
37
|
+
if (!e)
|
|
38
|
+
return "";
|
|
39
|
+
switch (e.toLowerCase()) {
|
|
40
|
+
case "paid":
|
|
41
|
+
return r(t, "order_status_paid", "Paid");
|
|
42
|
+
case "pending":
|
|
43
|
+
return r(t, "order_status_pending", "Pending");
|
|
44
|
+
case "trial":
|
|
45
|
+
return r(t, "order_status_trial", "Trial");
|
|
46
|
+
case "shipped":
|
|
47
|
+
return r(t, "order_status_shipped", "Shipped");
|
|
48
|
+
case "cancelled":
|
|
49
|
+
return r(t, "order_status_cancelled", "Cancelled");
|
|
50
|
+
case "refunded":
|
|
51
|
+
return r(t, "order_status_refunded", "Refunded");
|
|
52
|
+
default:
|
|
53
|
+
return e;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function u(e, t) {
|
|
57
|
+
return e ? {
|
|
58
|
+
...e,
|
|
59
|
+
order: {
|
|
60
|
+
...e.order,
|
|
61
|
+
status: o(e.order.status, t)
|
|
62
|
+
}
|
|
63
|
+
} : null;
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
i as buildInvoiceDocumentLabels,
|
|
67
|
+
s as getInvoiceLocale,
|
|
68
|
+
u as localizeInvoicePresentationData,
|
|
69
|
+
r as translateOrFallback,
|
|
70
|
+
o as translateOrderStatus
|
|
71
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u="invoice_settings",r={businessName:"",email:"",phone:"",address:{line1:"",line2:"",city:"",state:"",postal_code:"",country_code:"CA"},taxRegistrations:[]};function t(e){return typeof e!="string"?"":e.trim()}function s(e){if(!e||typeof e!="object"||Array.isArray(e))return{...r.address};const n=e;return{line1:t(n.line1),line2:t(n.line2),city:t(n.city),state:t(n.state),postal_code:t(n.postal_code),country_code:t(n.country_code).toUpperCase()||"CA"}}function c(e){return Array.isArray(e)?e.map(n=>{if(!n||typeof n!="object"||Array.isArray(n))return null;const i=n,o=t(i.label),a=t(i.value);return!o&&!a?null:{label:o,value:a}}).filter(n=>!!n):[]}function m(e){if(!e||typeof e!="object"||Array.isArray(e))return{...r};const n=e;return{businessName:t(n.business_name??n.businessName),email:t(n.email),phone:t(n.phone),address:s(n.address),taxRegistrations:c(n.tax_registrations??n.taxRegistrations)}}function l(e){return{business_name:t(e.businessName),email:t(e.email),phone:t(e.phone),address:s(e.address),tax_registrations:c(e.taxRegistrations)}}function f(e,n="usd",i="en-US"){return new Intl.NumberFormat(i,{style:"currency",currency:n.toUpperCase()}).format(e/100)}function y(e,n="en-US"){if(!e)return"";const i=new Date(e);return Number.isNaN(i.getTime())?"":new Intl.DateTimeFormat(n,{year:"numeric",month:"short",day:"numeric"}).format(i)}function p(e){if(!e)return[];const n=[e.city,e.state,e.postal_code].filter(Boolean).join(", ").replace(", ,",",").trim();return["company_name"in e&&e.company_name||"","recipient_name"in e&&e.recipient_name||"",e.line1||"",e.line2||"",n,e.country_code||""].filter(Boolean)}exports.DEFAULT_INVOICE_SETTINGS=r;exports.INVOICE_SETTINGS_KEY=u;exports.formatInvoiceCurrency=f;exports.formatInvoiceDate=y;exports.getInvoiceAddressLines=p;exports.normalizeInvoiceSettings=m;exports.serializeInvoiceSettings=l;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
const u = "invoice_settings", a = {
|
|
2
|
+
businessName: "",
|
|
3
|
+
email: "",
|
|
4
|
+
phone: "",
|
|
5
|
+
address: {
|
|
6
|
+
line1: "",
|
|
7
|
+
line2: "",
|
|
8
|
+
city: "",
|
|
9
|
+
state: "",
|
|
10
|
+
postal_code: "",
|
|
11
|
+
country_code: "CA"
|
|
12
|
+
},
|
|
13
|
+
taxRegistrations: []
|
|
14
|
+
};
|
|
15
|
+
function t(e) {
|
|
16
|
+
return typeof e != "string" ? "" : e.trim();
|
|
17
|
+
}
|
|
18
|
+
function s(e) {
|
|
19
|
+
if (!e || typeof e != "object" || Array.isArray(e))
|
|
20
|
+
return { ...a.address };
|
|
21
|
+
const n = e;
|
|
22
|
+
return {
|
|
23
|
+
line1: t(n.line1),
|
|
24
|
+
line2: t(n.line2),
|
|
25
|
+
city: t(n.city),
|
|
26
|
+
state: t(n.state),
|
|
27
|
+
postal_code: t(n.postal_code),
|
|
28
|
+
country_code: t(n.country_code).toUpperCase() || "CA"
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function c(e) {
|
|
32
|
+
return Array.isArray(e) ? e.map((n) => {
|
|
33
|
+
if (!n || typeof n != "object" || Array.isArray(n))
|
|
34
|
+
return null;
|
|
35
|
+
const r = n, i = t(r.label), o = t(r.value);
|
|
36
|
+
return !i && !o ? null : {
|
|
37
|
+
label: i,
|
|
38
|
+
value: o
|
|
39
|
+
};
|
|
40
|
+
}).filter((n) => !!n) : [];
|
|
41
|
+
}
|
|
42
|
+
function m(e) {
|
|
43
|
+
if (!e || typeof e != "object" || Array.isArray(e))
|
|
44
|
+
return { ...a };
|
|
45
|
+
const n = e;
|
|
46
|
+
return {
|
|
47
|
+
businessName: t(n.business_name ?? n.businessName),
|
|
48
|
+
email: t(n.email),
|
|
49
|
+
phone: t(n.phone),
|
|
50
|
+
address: s(n.address),
|
|
51
|
+
taxRegistrations: c(
|
|
52
|
+
n.tax_registrations ?? n.taxRegistrations
|
|
53
|
+
)
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function l(e) {
|
|
57
|
+
return {
|
|
58
|
+
business_name: t(e.businessName),
|
|
59
|
+
email: t(e.email),
|
|
60
|
+
phone: t(e.phone),
|
|
61
|
+
address: s(e.address),
|
|
62
|
+
tax_registrations: c(e.taxRegistrations)
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function f(e, n = "usd", r = "en-US") {
|
|
66
|
+
return new Intl.NumberFormat(r, {
|
|
67
|
+
style: "currency",
|
|
68
|
+
currency: n.toUpperCase()
|
|
69
|
+
}).format(e / 100);
|
|
70
|
+
}
|
|
71
|
+
function y(e, n = "en-US") {
|
|
72
|
+
if (!e)
|
|
73
|
+
return "";
|
|
74
|
+
const r = new Date(e);
|
|
75
|
+
return Number.isNaN(r.getTime()) ? "" : new Intl.DateTimeFormat(n, {
|
|
76
|
+
year: "numeric",
|
|
77
|
+
month: "short",
|
|
78
|
+
day: "numeric"
|
|
79
|
+
}).format(r);
|
|
80
|
+
}
|
|
81
|
+
function p(e) {
|
|
82
|
+
if (!e)
|
|
83
|
+
return [];
|
|
84
|
+
const n = [e.city, e.state, e.postal_code].filter(Boolean).join(", ").replace(", ,", ",").trim();
|
|
85
|
+
return [
|
|
86
|
+
"company_name" in e && e.company_name || "",
|
|
87
|
+
"recipient_name" in e && e.recipient_name || "",
|
|
88
|
+
e.line1 || "",
|
|
89
|
+
e.line2 || "",
|
|
90
|
+
n,
|
|
91
|
+
e.country_code || ""
|
|
92
|
+
].filter(Boolean);
|
|
93
|
+
}
|
|
94
|
+
export {
|
|
95
|
+
a as DEFAULT_INVOICE_SETTINGS,
|
|
96
|
+
u as INVOICE_SETTINGS_KEY,
|
|
97
|
+
f as formatInvoiceCurrency,
|
|
98
|
+
y as formatInvoiceDate,
|
|
99
|
+
p as getInvoiceAddressLines,
|
|
100
|
+
m as normalizeInvoiceSettings,
|
|
101
|
+
l as serializeInvoiceSettings
|
|
102
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("server-only");const l=require("postgres"),c=require("./inventory-settings.cjs.js");function y(){return process.env.POSTGRES_URL||process.env.DATABASE_URL||null}async function _(r){const n=y();if(!n)throw new Error("Missing POSTGRES_URL or DATABASE_URL for inventory fallback");const e=l(n,{ssl:"require",onnotice:()=>{}});try{await e.begin(async o=>{const t=o,s=(await t`
|
|
2
|
+
SELECT inventory_deducted_at
|
|
3
|
+
FROM public.orders
|
|
4
|
+
WHERE id = ${r}
|
|
5
|
+
FOR UPDATE
|
|
6
|
+
`)[0];if(!s||s.inventory_deducted_at)return;const u=await t`
|
|
7
|
+
SELECT value
|
|
8
|
+
FROM public.site_settings
|
|
9
|
+
WHERE key = ${c.ECOMMERCE_INVENTORY_SETTINGS_KEY}
|
|
10
|
+
LIMIT 1
|
|
11
|
+
`;if(!c.normalizeEcommerceInventorySettings(u[0]?.value).trackQuantities){await t`
|
|
12
|
+
UPDATE public.orders
|
|
13
|
+
SET inventory_deducted_at = now()
|
|
14
|
+
WHERE id = ${r}
|
|
15
|
+
`;return}const E=await t`
|
|
16
|
+
SELECT
|
|
17
|
+
product_id,
|
|
18
|
+
variant_id,
|
|
19
|
+
SUM(quantity)::integer AS quantity
|
|
20
|
+
FROM public.order_items
|
|
21
|
+
WHERE order_id = ${r}
|
|
22
|
+
GROUP BY product_id, variant_id
|
|
23
|
+
`;for(const a of E){if(a.variant_id){const i=(await t`
|
|
24
|
+
SELECT
|
|
25
|
+
sku,
|
|
26
|
+
stock_quantity
|
|
27
|
+
FROM public.product_variants
|
|
28
|
+
WHERE id = ${a.variant_id}
|
|
29
|
+
LIMIT 1
|
|
30
|
+
`)[0];if(!i?.sku)continue;await t`
|
|
31
|
+
INSERT INTO public.inventory_items (sku, quantity)
|
|
32
|
+
VALUES (${i.sku}, ${Math.max(0,i.stock_quantity??0)})
|
|
33
|
+
ON CONFLICT (sku) DO NOTHING
|
|
34
|
+
`,await t`
|
|
35
|
+
UPDATE public.inventory_items
|
|
36
|
+
SET
|
|
37
|
+
quantity = GREATEST(COALESCE(quantity, 0) - ${a.quantity}, 0),
|
|
38
|
+
updated_at = now()
|
|
39
|
+
WHERE sku = ${i.sku}
|
|
40
|
+
`;continue}if(a.product_id){const i=(await t`
|
|
41
|
+
SELECT
|
|
42
|
+
sku,
|
|
43
|
+
stock
|
|
44
|
+
FROM public.products
|
|
45
|
+
WHERE id = ${a.product_id}
|
|
46
|
+
LIMIT 1
|
|
47
|
+
`)[0];if(!i?.sku)continue;await t`
|
|
48
|
+
INSERT INTO public.inventory_items (sku, quantity)
|
|
49
|
+
VALUES (${i.sku}, ${Math.max(0,i.stock??0)})
|
|
50
|
+
ON CONFLICT (sku) DO NOTHING
|
|
51
|
+
`,await t`
|
|
52
|
+
UPDATE public.inventory_items
|
|
53
|
+
SET
|
|
54
|
+
quantity = GREATEST(COALESCE(quantity, 0) - ${a.quantity}, 0),
|
|
55
|
+
updated_at = now()
|
|
56
|
+
WHERE sku = ${i.sku}
|
|
57
|
+
`}}await t`
|
|
58
|
+
UPDATE public.orders
|
|
59
|
+
SET inventory_deducted_at = now()
|
|
60
|
+
WHERE id = ${r}
|
|
61
|
+
`})}finally{await e.end()}}async function p(r,n){const{error:e}=await r.rpc("apply_order_inventory_deduction",{p_order_id:n});if(!e)return{method:"rpc"};console.error(`[Inventory] RPC deduction failed for order ${n}:`,e);try{return await _(n),{method:"sql-fallback"}}catch(o){const t=o instanceof Error?o.message:String(o);throw new Error(`Failed to reconcile inventory for order ${n}. RPC error: ${e.message}. Fallback error: ${t}`)}}exports.applyOrderInventoryDeduction=p;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import "server-only";
|
|
2
|
+
import d from "postgres";
|
|
3
|
+
import { ECOMMERCE_INVENTORY_SETTINGS_KEY as l, normalizeEcommerceInventorySettings as _ } from "./inventory-settings.es.js";
|
|
4
|
+
function p() {
|
|
5
|
+
return process.env.POSTGRES_URL || process.env.DATABASE_URL || null;
|
|
6
|
+
}
|
|
7
|
+
async function y(r) {
|
|
8
|
+
const n = p();
|
|
9
|
+
if (!n)
|
|
10
|
+
throw new Error("Missing POSTGRES_URL or DATABASE_URL for inventory fallback");
|
|
11
|
+
const a = d(n, {
|
|
12
|
+
ssl: "require",
|
|
13
|
+
onnotice: () => {
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
try {
|
|
17
|
+
await a.begin(async (e) => {
|
|
18
|
+
const t = e, s = (await t`
|
|
19
|
+
SELECT inventory_deducted_at
|
|
20
|
+
FROM public.orders
|
|
21
|
+
WHERE id = ${r}
|
|
22
|
+
FOR UPDATE
|
|
23
|
+
`)[0];
|
|
24
|
+
if (!s || s.inventory_deducted_at)
|
|
25
|
+
return;
|
|
26
|
+
const c = await t`
|
|
27
|
+
SELECT value
|
|
28
|
+
FROM public.site_settings
|
|
29
|
+
WHERE key = ${l}
|
|
30
|
+
LIMIT 1
|
|
31
|
+
`;
|
|
32
|
+
if (!_(
|
|
33
|
+
c[0]?.value
|
|
34
|
+
).trackQuantities) {
|
|
35
|
+
await t`
|
|
36
|
+
UPDATE public.orders
|
|
37
|
+
SET inventory_deducted_at = now()
|
|
38
|
+
WHERE id = ${r}
|
|
39
|
+
`;
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const u = await t`
|
|
43
|
+
SELECT
|
|
44
|
+
product_id,
|
|
45
|
+
variant_id,
|
|
46
|
+
SUM(quantity)::integer AS quantity
|
|
47
|
+
FROM public.order_items
|
|
48
|
+
WHERE order_id = ${r}
|
|
49
|
+
GROUP BY product_id, variant_id
|
|
50
|
+
`;
|
|
51
|
+
for (const o of u) {
|
|
52
|
+
if (o.variant_id) {
|
|
53
|
+
const i = (await t`
|
|
54
|
+
SELECT
|
|
55
|
+
sku,
|
|
56
|
+
stock_quantity
|
|
57
|
+
FROM public.product_variants
|
|
58
|
+
WHERE id = ${o.variant_id}
|
|
59
|
+
LIMIT 1
|
|
60
|
+
`)[0];
|
|
61
|
+
if (!i?.sku)
|
|
62
|
+
continue;
|
|
63
|
+
await t`
|
|
64
|
+
INSERT INTO public.inventory_items (sku, quantity)
|
|
65
|
+
VALUES (${i.sku}, ${Math.max(0, i.stock_quantity ?? 0)})
|
|
66
|
+
ON CONFLICT (sku) DO NOTHING
|
|
67
|
+
`, await t`
|
|
68
|
+
UPDATE public.inventory_items
|
|
69
|
+
SET
|
|
70
|
+
quantity = GREATEST(COALESCE(quantity, 0) - ${o.quantity}, 0),
|
|
71
|
+
updated_at = now()
|
|
72
|
+
WHERE sku = ${i.sku}
|
|
73
|
+
`;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (o.product_id) {
|
|
77
|
+
const i = (await t`
|
|
78
|
+
SELECT
|
|
79
|
+
sku,
|
|
80
|
+
stock
|
|
81
|
+
FROM public.products
|
|
82
|
+
WHERE id = ${o.product_id}
|
|
83
|
+
LIMIT 1
|
|
84
|
+
`)[0];
|
|
85
|
+
if (!i?.sku)
|
|
86
|
+
continue;
|
|
87
|
+
await t`
|
|
88
|
+
INSERT INTO public.inventory_items (sku, quantity)
|
|
89
|
+
VALUES (${i.sku}, ${Math.max(0, i.stock ?? 0)})
|
|
90
|
+
ON CONFLICT (sku) DO NOTHING
|
|
91
|
+
`, await t`
|
|
92
|
+
UPDATE public.inventory_items
|
|
93
|
+
SET
|
|
94
|
+
quantity = GREATEST(COALESCE(quantity, 0) - ${o.quantity}, 0),
|
|
95
|
+
updated_at = now()
|
|
96
|
+
WHERE sku = ${i.sku}
|
|
97
|
+
`;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
await t`
|
|
101
|
+
UPDATE public.orders
|
|
102
|
+
SET inventory_deducted_at = now()
|
|
103
|
+
WHERE id = ${r}
|
|
104
|
+
`;
|
|
105
|
+
});
|
|
106
|
+
} finally {
|
|
107
|
+
await a.end();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
async function v(r, n) {
|
|
111
|
+
const { error: a } = await r.rpc("apply_order_inventory_deduction", {
|
|
112
|
+
p_order_id: n
|
|
113
|
+
});
|
|
114
|
+
if (!a)
|
|
115
|
+
return { method: "rpc" };
|
|
116
|
+
console.error(`[Inventory] RPC deduction failed for order ${n}:`, a);
|
|
117
|
+
try {
|
|
118
|
+
return await y(n), { method: "sql-fallback" };
|
|
119
|
+
} catch (e) {
|
|
120
|
+
const t = e instanceof Error ? e.message : String(e);
|
|
121
|
+
throw new Error(
|
|
122
|
+
`Failed to reconcile inventory for order ${n}. RPC error: ${a.message}. Fallback error: ${t}`
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export {
|
|
127
|
+
v as applyOrderInventoryDeduction
|
|
128
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const m={gst:"GST",qst:"QST",hst:"HST",pst:"PST",vat:"VAT",sales_tax:"Sales Tax",amusement_tax:"Amusement Tax",retail_delivery_fee:"Retail Delivery Fee"};function c(){return new Date().toISOString()}function l(t){const e=t.tax_type?.trim().toLowerCase();return e&&m[e]?m[e]:t.display_name||"Tax"}function s(t){return typeof t.effective_percentage=="number"?t.effective_percentage:typeof t.percentage=="number"?t.percentage:null}function f(t){return t.jurisdiction?t.jurisdiction:[t.state,t.country].filter(Boolean).join(", ")||null}function p(t){return JSON.stringify({source:t.source,rate_id:t.rate.id??null,display_name:l(t.rate),percentage:s(t.rate),jurisdiction:f(t.rate),jurisdiction_level:t.rate.jurisdiction_level??null,country:t.rate.country??null,state:t.rate.state??null,tax_type:t.rate.tax_type??null,inclusive:t.rate.inclusive??null})}function u(t){const e=new Map;for(const o of t){const n=p(o),a=e.get(n);if(!a){e.set(n,{...o,scope:"aggregate",line_item_id:null});continue}a.amount+=o.amount,typeof a.taxable_amount=="number"||typeof o.taxable_amount=="number"?a.taxable_amount=(typeof a.taxable_amount=="number"?a.taxable_amount:0)+(typeof o.taxable_amount=="number"?o.taxable_amount:0):a.taxable_amount=null,a.description!==o.description&&(a.description=null),a.taxability_reason!==o.taxability_reason&&(a.taxability_reason=null)}return[...e.values()].sort((o,n)=>{const a=l(o.rate).localeCompare(l(n.rate));return a!==0?a:(s(o.rate)??0)-(s(n.rate)??0)})}function x(t){if(!t||typeof t!="object"||Array.isArray(t))return null;const e=t;return typeof e.currency!="string"?null:{source:e.source==="manual"||e.source==="stripe_checkout"||e.source==="none"?e.source:"none",mode:e.mode==="automatic"?"automatic":"manual",currency:e.currency,subtotal:typeof e.subtotal=="number"?e.subtotal:0,shipping_total:typeof e.shipping_total=="number"?e.shipping_total:0,tax_total:typeof e.tax_total=="number"?e.tax_total:0,total:typeof e.total=="number"?e.total:0,is_finalized:!!e.is_finalized,lines:Array.isArray(e.lines)?u(e.lines):[],updated_at:typeof e.updated_at=="string"?e.updated_at:c()}}function g(t){const e=t.currency||"usd",o=t.calculation.mode==="manual"&&!t.calculation.isPendingExternalCalculation;return{source:t.calculation.enabled&&t.calculation.mode==="manual"?"manual":t.calculation.mode==="automatic"?"stripe_checkout":"none",mode:t.calculation.mode,currency:e,subtotal:t.subtotal,shipping_total:t.shippingTotal,tax_total:t.calculation.amount,total:t.total,is_finalized:o,lines:t.calculation.enabled&&t.calculation.mode==="manual"?t.calculation.lines.map(n=>({scope:"aggregate",source:"manual",amount:n.amount,taxable_amount:t.calculation.taxableSubtotal,description:n.name,rate:{id:n.id??null,display_name:n.name,percentage:n.rate,country:n.country_code,state:n.state_code??null}})):[],updated_at:c()}}function _(t){return{id:t.id,display_name:t.display_name,percentage:t.percentage,effective_percentage:t.effective_percentage,jurisdiction:t.jurisdiction,jurisdiction_level:t.jurisdiction_level,country:t.country,state:t.state,tax_type:t.tax_type,inclusive:t.inclusive,description:t.description}}function y(t){const e=t.currency||t.session.currency||"usd",o=typeof t.session.amount_total=="number"?t.session.amount_total:0,n=t.session.total_details?.amount_tax??0,a=[];for(const r of t.lineItems)for(const i of r.taxes||[])a.push({scope:"line_item",source:"stripe_checkout",amount:i.amount,taxable_amount:i.taxable_amount,description:r.description||void 0,line_item_id:r.id,taxability_reason:i.taxability_reason,rate:_(i.rate)});if(a.length===0)for(const r of t.session.total_details?.breakdown?.taxes||[])a.push({scope:"aggregate",source:"stripe_checkout",amount:r.amount,taxable_amount:r.taxable_amount,description:r.rate.display_name,taxability_reason:r.taxability_reason,rate:_(r.rate)});const d=u(a);return{source:n>0||d.length>0?"stripe_checkout":"none",mode:t.fallbackMode==="manual"?"manual":"automatic",currency:e,subtotal:t.subtotal,shipping_total:t.shippingTotal,tax_total:n,total:o,is_finalized:!0,lines:d,updated_at:c()}}exports.aggregateOrderTaxLines=u;exports.buildOrderTaxDetailsFromCalculation=g;exports.buildOrderTaxDetailsFromStripeSession=y;exports.getOrderTaxRateJurisdiction=f;exports.getOrderTaxRateLabel=l;exports.getOrderTaxRatePercentage=s;exports.normalizeOrderTaxDetails=x;
|