Orders
ordersCarts on anonymous or signed-in users, checkout priced from the catalog, confirmed orders from lines or an accepted quote with stock allocations, gapless order and invoice numbers, payments, fulfillments, returns and refunds, with an optional kitchen display. With invoices, a paid order gets an issued, paid invoice.
Billing & commercemanagedcustom
Invoice INV-0042paid
Pro plan€49.00
Seats × 3€27.00
Usage€4.20
Total€80.20
Install
The command also installs updated-at, memberships, permissions, catalog, the modules it needs.
npx better-supabase add ordersConfigure
The blocks entries in your config, with the default mode.
import { defineConfig } from "better-supabase/config";
export default defineConfig({
blocks: {
// Required by orders; add installs them too.
"updated-at": {},
memberships: {},
permissions: {},
catalog: {},
// Modes: "managed", "custom". The default is "managed".
orders: { mode: "managed" },
},
});Migrations
The files
npx better-supabase add orders writes into your declarative schema with the default layout.11 files
supabase/schemas/900_better_supabase_084_orders.sqlschema2953 lines-- better-supabase module: orders
-- @bsb-block orders@5 managed
-- @bsb-hash sha256:9e5d183e48b33d6e4d9f34750722fdfd8492af0dbdb62aa8b2dac8ad111cf0c5
-- Carts on anonymous or signed-in users, checkout priced from the catalog, confirmed orders from lines or an accepted quote with stock allocations, gapless order and invoice numbers, payments, fulfillments, returns and refunds, with an optional kitchen display. With invoices, a paid order gets an issued, paid invoice.
-- Managed by `better-supabase add`; re-running it rewrites this file unless it was edited by hand.
-- Change it through `blocks` in better-supabase.config.ts and the module's SQL hooks.
-- Carts on any signed-in user (anonymous sign-ins included), checkout into
-- orders with numbers, payments, fulfillments, returns and refunds.
create table "better_supabase"."order_shipping_rates" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"name" text not null constraint bsb_order_shipping_rates_name_check check (length("name") >= 1 and length("name") <= 200),
"countries" text[] not null default '{}',
"currency" char(3) not null constraint bsb_order_shipping_rates_currency_check check ("currency" ~ '^[A-Z]{3}$'),
"kind" text not null default 'flat' constraint bsb_order_shipping_rates_kind_check check ("kind" in ('flat', 'weight')),
"amount" bigint not null default 0 constraint bsb_order_shipping_rates_amount_check check ("amount" >= 0),
"amount_per_kg" bigint not null default 0 constraint bsb_order_shipping_rates_amount_per_kg_check check ("amount_per_kg" >= 0),
"free_over" bigint constraint bsb_order_shipping_rates_free_over_check check ("free_over" >= 0),
"active" boolean not null default true,
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now()
);
create index "order_shipping_rates_tenant_idx" on "better_supabase"."order_shipping_rates" ("tenant_id", "currency");
alter table "better_supabase"."order_shipping_rates" enable row level security;
revoke all on "better_supabase"."order_shipping_rates" from public, anon, authenticated;
grant select on "better_supabase"."order_shipping_rates" to authenticated;
grant all on "better_supabase"."order_shipping_rates" to service_role;
create policy bsb_order_shipping_rates_read on "better_supabase"."order_shipping_rates"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')));
create table "better_supabase"."carts" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"user_id" uuid not null references auth.users (id) on delete cascade,
"currency" char(3) not null constraint bsb_carts_currency_check check ("currency" ~ '^[A-Z]{3}$'),
"price_list_id" uuid references "better_supabase"."catalog_price_lists" ("id") on delete set null,
"status" text not null default 'open' constraint bsb_carts_status_check check ("status" in ('open', 'converted', 'abandoned')),
"email" text constraint bsb_carts_email_check check (length("email") <= 320),
"shipping_address" jsonb constraint bsb_carts_shipping_address_check check (jsonb_typeof("shipping_address") = 'object'),
"billing_address" jsonb constraint bsb_carts_billing_address_check check (jsonb_typeof("billing_address") = 'object'),
"shipping_rate_id" uuid references "better_supabase"."order_shipping_rates" ("id") on delete set null,
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now(),
"converted_at" timestamptz
);
create unique index carts_open_idx on "better_supabase"."carts" ("tenant_id", "user_id", "currency") where "status" = 'open';
create index carts_user_idx on "better_supabase"."carts" ("user_id");
create index carts_rate_idx on "better_supabase"."carts" ("shipping_rate_id");
create index carts_price_list_idx on "better_supabase"."carts" ("price_list_id");
alter table "better_supabase"."carts" enable row level security;
revoke all on "better_supabase"."carts" from public, anon, authenticated;
grant select on "better_supabase"."carts" to authenticated;
grant all on "better_supabase"."carts" to service_role;
create policy bsb_carts_read on "better_supabase"."carts"
for select to authenticated
using ("user_id" = (select auth.uid()) or "tenant_id" in (select better_supabase.tenant_ids_with('orders.read')));
create table "better_supabase"."cart_lines" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"cart_id" uuid not null references "better_supabase"."carts" ("id") on delete cascade,
"variant_id" uuid not null references "better_supabase"."product_variants" ("id") on delete cascade,
"quantity" integer not null constraint bsb_cart_lines_quantity_check check ("quantity" >= 1 and "quantity" <= 10000),
"unit_amount" bigint constraint bsb_cart_lines_unit_amount_check check ("unit_amount" >= 0),
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now(),
unique ("cart_id", "variant_id")
);
create index cart_lines_variant_idx on "better_supabase"."cart_lines" ("variant_id");
alter table "better_supabase"."cart_lines" enable row level security;
revoke all on "better_supabase"."cart_lines" from public, anon, authenticated;
grant select on "better_supabase"."cart_lines" to authenticated;
grant all on "better_supabase"."cart_lines" to service_role;
create policy bsb_cart_lines_read on "better_supabase"."cart_lines"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."carts" x where x."id" = "cart_id" and x."user_id" = (select auth.uid())));
create table "better_supabase"."orders" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"number" text not null,
"sequence" bigint not null,
"cart_id" uuid unique references "better_supabase"."carts" ("id") on delete set null,
"customer_id" uuid references auth.users (id) on delete set null,
"email" text,
"status" text not null default 'pending_payment',
"currency" char(3) not null constraint bsb_orders_currency_check check ("currency" ~ '^[A-Z]{3}$'),
"subtotal" bigint not null default 0 constraint bsb_orders_subtotal_check check ("subtotal" >= 0),
"discount_total" bigint not null default 0 constraint bsb_orders_discount_total_check check ("discount_total" >= 0),
"shipping_total" bigint not null default 0 constraint bsb_orders_shipping_total_check check ("shipping_total" >= 0),
"tax_total" bigint not null default 0 constraint bsb_orders_tax_total_check check ("tax_total" >= 0),
"total" bigint not null default 0 constraint bsb_orders_total_check check ("total" >= 0),
"paid_total" bigint not null default 0 constraint bsb_orders_paid_total_check check ("paid_total" >= 0),
"refunded_total" bigint not null default 0 constraint bsb_orders_refunded_total_check check ("refunded_total" >= 0),
"tax_inclusive" boolean not null default false,
"shipping_address" jsonb,
"billing_address" jsonb,
"shipping" jsonb,
"promotions" jsonb not null default '[]',
"tax" jsonb,
"tax_transaction" jsonb,
"invoice_number" text,
"invoice_sequence" bigint,
"invoiced_at" timestamptz,
"placed_at" timestamptz not null default now(),
"paid_at" timestamptz,
"fulfilled_at" timestamptz,
"completed_at" timestamptz,
"cancelled_at" timestamptz,
"cancel_reason" text,
"held_at" timestamptz,
"held_from" text,
"hold_reason" text constraint bsb_orders_hold_reason_check check (length("hold_reason") <= 2000),
"origin_type" text constraint bsb_orders_origin_type_check check ("origin_type" ~ '^[a-z][a-z0-9_]{0,62}$'),
"origin_id" text constraint bsb_orders_origin_id_check check (length("origin_id") >= 1 and length("origin_id") <= 200),
"buyer" jsonb,
"variant_of" uuid references "better_supabase"."orders" ("id") on delete set null,
"variant_label" text constraint bsb_orders_variant_label_check check (length("variant_label") <= 200),
"is_selected" boolean not null default true,
"channel" text constraint bsb_orders_channel_check check ("channel" ~ '^[a-z][a-z0-9_]{0,62}$'),
"source" text constraint bsb_orders_source_check check ("source" ~ '^[a-z][a-z0-9_]{0,62}$'),
"external_id" text constraint bsb_orders_external_id_check check (length("external_id") >= 1 and length("external_id") <= 200),
"invoicing_policy" text not null default 'on_order' constraint bsb_orders_invoicing_policy_check check ("invoicing_policy" in ('on_order', 'on_delivery')),
"delivery_method" text constraint bsb_orders_delivery_method_check check ("delivery_method" ~ '^[a-z][a-z0-9_]{0,62}$'),
"quote_id" uuid,
"order_discount" bigint not null default 0 constraint bsb_orders_order_discount_check check ("order_discount" >= 0),
"promotion_id" uuid,
"payment_required" boolean not null default false,
"deposit_amount" bigint constraint bsb_orders_deposit_amount_check check ("deposit_amount" >= 0),
"party_type" text constraint bsb_orders_party_type_check check ("party_type" ~ '^[a-z][a-z0-9_]{0,62}$'),
"party_id" text constraint bsb_orders_party_id_check check (length("party_id") >= 1 and length("party_id") <= 200),
"updated_at" timestamptz not null default now(),
unique ("tenant_id", "number"),
unique ("tenant_id", "invoice_number")
);
create index orders_status_idx on "better_supabase"."orders" ("tenant_id", "status", "placed_at");
create index orders_customer_idx on "better_supabase"."orders" ("customer_id");
create index orders_sequence_idx on "better_supabase"."orders" ("tenant_id", "sequence");
create unique index orders_external_idx on "better_supabase"."orders" ("tenant_id", "source", "external_id") where "external_id" is not null;
alter table "better_supabase"."orders" add constraint bsb_orders_external_source_check check ("external_id" is null or "source" is not null);
create index orders_channel_idx on "better_supabase"."orders" ("tenant_id", "channel", "placed_at");
create index orders_quote_idx on "better_supabase"."orders" ("quote_id") where "quote_id" is not null;
create index orders_party_idx on "better_supabase"."orders" ("tenant_id", "party_type", "party_id") where "party_id" is not null;
alter table "better_supabase"."orders" add constraint bsb_orders_party_pair_check check (("party_type" is null) = ("party_id" is null));
create unique index orders_origin_idx on "better_supabase"."orders" ("tenant_id", "origin_type", "origin_id") where "origin_id" is not null and "status" <> 'cancelled';
create index orders_variant_idx on "better_supabase"."orders" ("variant_of") where "variant_of" is not null;
alter table "better_supabase"."orders" enable row level security;
revoke all on "better_supabase"."orders" from public, anon, authenticated;
grant select on "better_supabase"."orders" to authenticated;
grant all on "better_supabase"."orders" to service_role;
create policy bsb_orders_read on "better_supabase"."orders"
for select to authenticated
using ("customer_id" = (select auth.uid()) or "tenant_id" in (select better_supabase.tenant_ids_with('orders.read')));
create table "better_supabase"."order_lines" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"order_id" uuid not null references "better_supabase"."orders" ("id") on delete cascade,
"position" integer not null default 0,
"variant_id" uuid references "better_supabase"."product_variants" ("id") on delete set null,
"product_id" uuid,
"sku" text not null,
"title" text not null,
"quantity" numeric(18, 0) not null constraint bsb_order_lines_quantity_check check ("quantity" > 0),
"unit" text constraint bsb_order_lines_unit_check check (length("unit") >= 1 and length("unit") <= 32),
"description" text constraint bsb_order_lines_description_check check (length("description") <= 4000),
"line_type" text not null default 'item' constraint bsb_order_lines_line_type_check check ("line_type" in ('item', 'service', 'fee')),
"unit_amount" bigint not null default 0 constraint bsb_order_lines_unit_amount_check check ("unit_amount" >= 0),
"subtotal" bigint not null default 0 constraint bsb_order_lines_subtotal_check check ("subtotal" >= 0),
"discount" bigint not null default 0 constraint bsb_order_lines_discount_check check ("discount" >= 0),
"tax" bigint not null default 0 constraint bsb_order_lines_tax_check check ("tax" >= 0),
"total" bigint not null default 0 constraint bsb_order_lines_total_check check ("total" >= 0),
"requires_shipping" boolean not null default true,
"location_id" uuid,
"reservation_id" uuid,
"tax_code_id" uuid,
"unit_cost" bigint constraint bsb_order_lines_unit_cost_check check ("unit_cost" >= 0),
"cost_source" text constraint bsb_order_lines_cost_source_check check ("cost_source" in ('catalog', 'average', 'manual', 'imported')),
"bin_id" uuid,
"billing_party" text not null default 'customer' constraint bsb_order_lines_billing_party_check check ("billing_party" in ('customer', 'internal', 'own_warranty', 'supplier_warranty', 'cost_account')),
"billing_party_id" text,
"billing_account_id" uuid,
"quantity_fulfilled" numeric(18, 0) not null default 0 constraint bsb_order_lines_check check ("quantity_fulfilled" >= 0 and "quantity_fulfilled" <= "quantity"),
"quantity_returned" numeric(18, 0) not null default 0 constraint bsb_order_lines_check1 check ("quantity_returned" >= 0 and "quantity_returned" <= "quantity"),
"backordered" boolean not null default false
);
create index order_lines_order_idx on "better_supabase"."order_lines" ("order_id");
create index order_lines_variant_idx on "better_supabase"."order_lines" ("variant_id");
alter table "better_supabase"."order_lines" enable row level security;
revoke all on "better_supabase"."order_lines" from public, anon, authenticated;
grant select on "better_supabase"."order_lines" to authenticated;
grant all on "better_supabase"."order_lines" to service_role;
create policy bsb_order_lines_read on "better_supabase"."order_lines"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."orders" o where o."id" = "order_id" and o."customer_id" = (select auth.uid())));
create table "better_supabase"."order_payments" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"order_id" uuid not null references "better_supabase"."orders" ("id") on delete cascade,
"provider" text not null,
"reference" text not null,
"status" text not null constraint bsb_order_payments_status_check check ("status" in ('requires_action', 'processing', 'authorized', 'succeeded', 'failed', 'canceled')),
"currency" char(3) not null constraint bsb_order_payments_currency_check check ("currency" ~ '^[A-Z]{3}$'),
"amount" bigint not null default 0 constraint bsb_order_payments_amount_check check ("amount" >= 0),
"captured" bigint not null default 0 constraint bsb_order_payments_captured_check check ("captured" >= 0),
"refunded" bigint not null default 0 constraint bsb_order_payments_refunded_check check ("refunded" >= 0),
"created_at" timestamptz not null default now(),
"updated_at" timestamptz not null default now(),
unique ("provider", "reference")
);
create index order_payments_order_idx on "better_supabase"."order_payments" ("order_id");
alter table "better_supabase"."order_payments" enable row level security;
revoke all on "better_supabase"."order_payments" from public, anon, authenticated;
grant select on "better_supabase"."order_payments" to authenticated;
grant all on "better_supabase"."order_payments" to service_role;
create policy bsb_order_payments_read on "better_supabase"."order_payments"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."orders" o where o."id" = "order_id" and o."customer_id" = (select auth.uid())));
create table "better_supabase"."order_fulfillments" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"order_id" uuid not null references "better_supabase"."orders" ("id") on delete cascade,
"location_id" uuid,
"status" text not null default 'pending' constraint bsb_order_fulfillments_status_check check ("status" in ('pending', 'shipped', 'delivered', 'cancelled')),
"carrier" text,
"tracking_number" text,
"tracking_url" text constraint bsb_order_fulfillments_tracking_url_check check ("tracking_url" ~ '^https://'),
"created_at" timestamptz not null default now(),
"shipped_at" timestamptz,
"delivered_at" timestamptz
);
create index "order_fulfillments_order_idx" on "better_supabase"."order_fulfillments" ("order_id");
alter table "better_supabase"."order_fulfillments" enable row level security;
revoke all on "better_supabase"."order_fulfillments" from public, anon, authenticated;
grant select on "better_supabase"."order_fulfillments" to authenticated;
grant all on "better_supabase"."order_fulfillments" to service_role;
create policy bsb_order_fulfillments_read on "better_supabase"."order_fulfillments"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."orders" o where o."id" = "order_id" and o."customer_id" = (select auth.uid())));
create table "better_supabase"."order_fulfillment_lines" (
"tenant_id" uuid not null,
"fulfillment_id" uuid not null references "better_supabase"."order_fulfillments" ("id") on delete cascade,
"order_line_id" uuid not null references "better_supabase"."order_lines" ("id") on delete cascade,
"quantity" numeric(18, 0) not null constraint bsb_order_fulfillment_lines_quantity_check check ("quantity" > 0),
"detail" jsonb constraint bsb_order_fulfillment_lines_detail_check check ("detail" is null or jsonb_typeof("detail") = 'object'),
primary key ("fulfillment_id", "order_line_id")
);
create index "order_fulfillment_lines_line_idx" on "better_supabase"."order_fulfillment_lines" ("order_line_id");
alter table "better_supabase"."order_fulfillment_lines" enable row level security;
revoke all on "better_supabase"."order_fulfillment_lines" from public, anon, authenticated;
grant select on "better_supabase"."order_fulfillment_lines" to authenticated;
grant all on "better_supabase"."order_fulfillment_lines" to service_role;
create policy bsb_order_fulfillment_lines_read on "better_supabase"."order_fulfillment_lines"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')));
create table "better_supabase"."order_returns" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"order_id" uuid not null references "better_supabase"."orders" ("id") on delete cascade,
"status" text not null default 'requested' constraint bsb_order_returns_status_check check ("status" in ('requested', 'received', 'rejected')),
"reason" text constraint bsb_order_returns_reason_check check (length("reason") <= 2000),
"requested_by" uuid default auth.uid(),
"created_at" timestamptz not null default now(),
"received_at" timestamptz
);
create index order_returns_order_idx on "better_supabase"."order_returns" ("order_id");
alter table "better_supabase"."order_returns" enable row level security;
revoke all on "better_supabase"."order_returns" from public, anon, authenticated;
grant select on "better_supabase"."order_returns" to authenticated;
grant all on "better_supabase"."order_returns" to service_role;
create policy bsb_order_returns_read on "better_supabase"."order_returns"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."orders" o where o."id" = "order_id" and o."customer_id" = (select auth.uid())));
create table "better_supabase"."order_return_lines" (
"tenant_id" uuid not null,
"return_id" uuid not null references "better_supabase"."order_returns" ("id") on delete cascade,
"order_line_id" uuid not null references "better_supabase"."order_lines" ("id") on delete cascade,
"quantity" numeric(18, 0) not null constraint bsb_order_return_lines_quantity_check check ("quantity" > 0),
primary key ("return_id", "order_line_id")
);
create index order_return_lines_line_idx on "better_supabase"."order_return_lines" ("order_line_id");
alter table "better_supabase"."order_return_lines" enable row level security;
revoke all on "better_supabase"."order_return_lines" from public, anon, authenticated;
grant select on "better_supabase"."order_return_lines" to authenticated;
grant all on "better_supabase"."order_return_lines" to service_role;
create policy bsb_order_return_lines_read on "better_supabase"."order_return_lines"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')));
create table "better_supabase"."order_refunds" (
"id" uuid primary key default gen_random_uuid(),
"tenant_id" uuid not null,
"order_id" uuid not null references "better_supabase"."orders" ("id") on delete cascade,
"payment_id" uuid not null references "better_supabase"."order_payments" ("id") on delete cascade,
"return_id" uuid references "better_supabase"."order_returns" ("id") on delete set null,
"amount" bigint not null constraint bsb_order_refunds_amount_check check ("amount" > 0),
"currency" char(3) not null constraint bsb_order_refunds_currency_check check ("currency" ~ '^[A-Z]{3}$'),
"status" text not null default 'pending' constraint bsb_order_refunds_status_check check ("status" in ('pending', 'succeeded', 'failed')),
"reason" text constraint bsb_order_refunds_reason_check check (length("reason") <= 2000),
"provider_reference" text,
"created_at" timestamptz not null default now(),
"completed_at" timestamptz
);
create index order_refunds_order_idx on "better_supabase"."order_refunds" ("order_id");
create index order_refunds_payment_idx on "better_supabase"."order_refunds" ("payment_id");
create index order_refunds_return_idx on "better_supabase"."order_refunds" ("return_id");
create unique index order_refunds_provider_reference_idx on "better_supabase"."order_refunds" ("provider_reference") where "provider_reference" is not null;
alter table "better_supabase"."order_refunds" enable row level security;
revoke all on "better_supabase"."order_refunds" from public, anon, authenticated;
grant select on "better_supabase"."order_refunds" to authenticated;
grant all on "better_supabase"."order_refunds" to service_role;
create policy bsb_order_refunds_read on "better_supabase"."order_refunds"
for select to authenticated
using ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."orders" o where o."id" = "order_id" and o."customer_id" = (select auth.uid())));
-- Creates or updates a shipping rate: a flat amount, plus an amount per
-- started kilogram for weight rates, free from an order subtotal when
-- free_over is set. Countries are ISO 3166-1 alpha-2; none means anywhere.
-- Amounts are text in minor units.
create or replace function "better_supabase"."upsert_shipping_rate"(tenant uuid, rate uuid, data jsonb)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_id uuid;
v_countries text[];
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', upsert_shipping_rate.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if jsonb_typeof(data) is distinct from 'object' then
raise exception 'the rate is an object' using errcode = '22023', hint = 'ORDERS_RATE_INVALID';
end if;
if "better_supabase"."currency_minor_units"(data ->> 'currency') is null then
raise exception 'the currency is not an ISO 4217 code' using errcode = '22023', hint = 'ORDERS_CURRENCY_INVALID';
end if;
if (case when (data ->> 'amount') ~ '^[0-9]{1,18}$' then (data ->> 'amount')::bigint end) is null
or (data ? 'amountPerKg' and (case when (data ->> 'amountPerKg') ~ '^[0-9]{1,18}$' then (data ->> 'amountPerKg')::bigint end) is null)
or (data ->> 'freeOver' is not null and (case when (data ->> 'freeOver') ~ '^[0-9]{1,18}$' then (data ->> 'freeOver')::bigint end) is null) then
raise exception 'amounts are whole minor units of zero or more, as text' using errcode = '22023', hint = 'ORDERS_AMOUNT_INVALID';
end if;
select coalesce(array_agg(value order by value), '{}') into v_countries
from jsonb_array_elements_text(coalesce(data -> 'countries', '[]'::jsonb));
if exists (select 1 from unnest(v_countries) as c(value) where c.value !~ '^[A-Z]{2}$') then
raise exception 'countries are ISO 3166-1 alpha-2 codes' using errcode = '22023', hint = 'ORDERS_ADDRESS_INVALID';
end if;
if upsert_shipping_rate.rate is null then
insert into "better_supabase"."order_shipping_rates" ("tenant_id", "name", "countries", "currency", "kind", "amount", "amount_per_kg", "free_over", "active")
values (upsert_shipping_rate.tenant, data ->> 'name', v_countries, data ->> 'currency', coalesce(data ->> 'kind', 'flat'),
(case when (data ->> 'amount') ~ '^[0-9]{1,18}$' then (data ->> 'amount')::bigint end), coalesce((case when (data ->> 'amountPerKg') ~ '^[0-9]{1,18}$' then (data ->> 'amountPerKg')::bigint end), 0), (case when (data ->> 'freeOver') ~ '^[0-9]{1,18}$' then (data ->> 'freeOver')::bigint end), coalesce((data ->> 'active')::boolean, true))
returning "id" into v_id;
else
update "better_supabase"."order_shipping_rates" x set "name" = data ->> 'name', "countries" = v_countries, "currency" = data ->> 'currency',
"kind" = coalesce(data ->> 'kind', 'flat'), "amount" = (case when (data ->> 'amount') ~ '^[0-9]{1,18}$' then (data ->> 'amount')::bigint end), "amount_per_kg" = coalesce((case when (data ->> 'amountPerKg') ~ '^[0-9]{1,18}$' then (data ->> 'amountPerKg')::bigint end), 0),
"free_over" = (case when (data ->> 'freeOver') ~ '^[0-9]{1,18}$' then (data ->> 'freeOver')::bigint end), "active" = coalesce((data ->> 'active')::boolean, true), "updated_at" = now()
where x."id" = upsert_shipping_rate.rate and x."tenant_id" = upsert_shipping_rate.tenant
returning x."id" into v_id;
if v_id is null then
raise exception 'shipping rate not found' using errcode = 'P0002', hint = 'ORDERS_RATE_NOT_FOUND';
end if;
end if;
return jsonb_build_object('id', v_id);
end;
$$;
revoke execute on function "better_supabase"."upsert_shipping_rate"(uuid, uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."upsert_shipping_rate"(uuid, uuid, jsonb) to authenticated, service_role;
create or replace function "better_supabase"."delete_shipping_rate"(rate uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_tenant uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."tenant_id" into v_tenant from "better_supabase"."order_shipping_rates" x where x."id" = delete_shipping_rate.rate;
if not found then
return false;
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
delete from "better_supabase"."order_shipping_rates" x where x."id" = delete_shipping_rate.rate;
return true;
end;
$$;
revoke execute on function "better_supabase"."delete_shipping_rate"(uuid) from public, anon;
grant execute on function "better_supabase"."delete_shipping_rate"(uuid) to authenticated, service_role;
-- A cart's lines at today's catalog prices, its discounts, shipping and the
-- problems that keep it from checkout. Refreshes each line's price
-- snapshot. Internal: price_cart and place_order call it after their checks.
-- Amounts are text: JSON numbers lose bigint precision.
create or replace function "better_supabase"."cart_totals"(p_cart uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_cart record;
v_line record;
v_rate record;
v_amount bigint;
v_lines jsonb := '[]';
v_problems jsonb := '[]';
v_subtotal bigint := 0;
v_discount bigint := 0;
v_shipping bigint := 0;
v_weight bigint := 0;
v_ships boolean := false;
v_free boolean := false;
v_promo jsonb;
v_discounts jsonb := '{}';
v_applied jsonb := '[]';
v_snapshot jsonb;
v_country text;
begin
select x."tenant_id" as tenant, x."currency" as currency, x."price_list_id" as price_list,
x."shipping_address" as shipping_address, x."shipping_rate_id" as shipping_rate,
x."user_id" as owner, x."status" as status, x."email" as email, x."billing_address" as billing_address
into v_cart from "better_supabase"."carts" x where x."id" = p_cart;
for v_line in
select l."id" as id, l."variant_id" as variant, l."quantity" as quantity, l."unit_amount" as unit_amount,
cv."sku" as sku, concat_ws(' / ', cp."title", nullif(cv."title", '')) as title, cp."id" as product, cp."status" as status,
cp."kind" as kind, cv."weight_grams" as weight
from "better_supabase"."cart_lines" l
join "better_supabase"."product_variants" cv on cv."id" = l."variant_id"
join "better_supabase"."products" cp on cp."id" = cv."product_id"
where l."cart_id" = p_cart
order by l."created_at", l."id"
loop
v_amount := ("better_supabase"."variant_price"(v_line.variant, v_cart.currency, v_cart.price_list, null) ->> 'amount')::bigint;
if v_line.status <> 'active' or v_amount is null then
v_problems := v_problems || jsonb_build_array(jsonb_build_object('lineId', v_line.id, 'variantId', v_line.variant,
'problem', case when v_line.status <> 'active' then 'unavailable' else 'unpriced' end));
continue;
end if;
if v_line.unit_amount is distinct from v_amount then
update "better_supabase"."cart_lines" x set "unit_amount" = v_amount, "updated_at" = now() where x."id" = v_line.id;
end if;
v_lines := v_lines || jsonb_build_array(jsonb_build_object(
'id', v_line.id, 'variantId', v_line.variant, 'productId', v_line.product, 'sku', v_line.sku, 'title', v_line.title,
'quantity', v_line.quantity, 'unitAmount', v_amount::text, 'previousUnitAmount', v_line.unit_amount::text,
'subtotal', (v_amount * v_line.quantity)::text, 'requiresShipping', v_line.kind = 'physical'));
v_subtotal := v_subtotal + v_amount * v_line.quantity;
if v_line.kind = 'physical' then
v_ships := true;
v_weight := v_weight + coalesce(v_line.weight, 0)::bigint * v_line.quantity;
end if;
end loop;
select coalesce(jsonb_agg(e.l || jsonb_build_object(
'discount', coalesce(v_discounts ->> (e.l ->> 'id'), '0'),
'total', ((e.l ->> 'subtotal')::bigint - coalesce((v_discounts ->> (e.l ->> 'id'))::bigint, 0))::text) order by e.n), '[]'::jsonb),
coalesce(sum(coalesce((v_discounts ->> (e.l ->> 'id'))::bigint, 0)), 0)
into v_lines, v_discount
from jsonb_array_elements(v_lines) with ordinality as e(l, n);
v_country := v_cart.shipping_address ->> 'country';
if v_ships and v_cart.shipping_rate is not null then
select x."id" as id, x."name" as name, x."countries" as countries, x."kind" as kind,
x."amount" as amount, x."amount_per_kg" as per_kg, x."free_over" as free_over
into v_rate from "better_supabase"."order_shipping_rates" x
where x."id" = v_cart.shipping_rate and x."active" and x."currency" = v_cart.currency;
if not found or (cardinality(v_rate.countries) > 0 and (v_country is null or not v_country = any (v_rate.countries))) then
v_problems := v_problems || jsonb_build_array(jsonb_build_object('problem', 'shipping_unavailable'));
else
v_shipping := v_rate.amount + case when v_rate.kind = 'weight' then (v_rate.per_kg * v_weight + 999) / 1000 else 0 end;
if v_free or (v_rate.free_over is not null and v_subtotal - v_discount >= v_rate.free_over) then
v_shipping := 0;
end if;
v_snapshot := jsonb_build_object('rateId', v_rate.id, 'name', v_rate.name, 'amount', v_shipping::text);
end if;
elsif v_ships and exists (
select 1 from "better_supabase"."order_shipping_rates" x
where x."tenant_id" = v_cart.tenant and x."active" and x."currency" = v_cart.currency
) then
v_problems := v_problems || jsonb_build_array(jsonb_build_object('problem', 'shipping_required'));
end if;
return jsonb_build_object(
'cartId', p_cart, 'organizationId', v_cart.tenant, 'customerId', v_cart.owner, 'status', v_cart.status,
'email', v_cart.email, 'shippingAddress', v_cart.shipping_address, 'billingAddress', v_cart.billing_address,
'currency', v_cart.currency, 'lines', v_lines,
'subtotal', v_subtotal::text, 'discountTotal', v_discount::text, 'shippingTotal', v_shipping::text,
'total', (v_subtotal - v_discount + v_shipping)::text, 'requiresShipping', v_ships,
'weightGrams', v_weight::text, 'shipping', v_snapshot, 'promotions', v_applied, 'problems', v_problems);
end;
$$;
revoke execute on function "better_supabase"."cart_totals"(uuid) from public, anon, authenticated;
-- The caller's open cart in an organization and currency, created on first
-- use. Every signed-in user can have one, anonymous sign-ins included;
-- merge_carts moves it when the user signs up. Only staff set a price list.
create or replace function "better_supabase"."create_cart"(tenant uuid, currency text, price_list uuid default null, email text default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_id uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
if auth.uid() is null then
raise exception 'sign in (anonymously is enough) to keep a cart' using errcode = '42501', hint = 'ORDERS_SIGN_IN_REQUIRED';
end if;
if "better_supabase"."currency_minor_units"(create_cart.currency) is null then
raise exception 'the currency is not an ISO 4217 code' using errcode = '22023', hint = 'ORDERS_CURRENCY_INVALID';
end if;
if create_cart.price_list is not null then
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', create_cart.tenant, 'orders.manage'), false)) then
raise exception 'only staff can put a cart on a price list' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if not exists (select 1 from "better_supabase"."catalog_price_lists" x where x."id" = create_cart.price_list and x."tenant_id" = create_cart.tenant) then
raise exception 'price list not found' using errcode = 'P0002', hint = 'ORDERS_PRICE_LIST_NOT_FOUND';
end if;
end if;
insert into "better_supabase"."carts" as x ("tenant_id", "user_id", "currency", "price_list_id", "email")
values (create_cart.tenant, auth.uid(), create_cart.currency, create_cart.price_list, create_cart.email)
on conflict ("tenant_id", "user_id", "currency") where "status" = 'open' do update
set "price_list_id" = coalesce(excluded."price_list_id", x."price_list_id"),
"email" = coalesce(excluded."email", x."email"), "updated_at" = now()
returning x."id" into v_id;
return jsonb_build_object('id', v_id, 'currency', create_cart.currency, 'status', 'open');
end;
$$;
revoke execute on function "better_supabase"."create_cart"(uuid, text, uuid, text) from public, anon;
grant execute on function "better_supabase"."create_cart"(uuid, text, uuid, text) to authenticated, service_role;
-- Adds units of a variant to the cart; a variant already in it gets more.
create or replace function "better_supabase"."add_to_cart"(cart uuid, variant uuid, quantity integer)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_cart record;
v_id uuid;
v_quantity integer;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."tenant_id" as tenant, x."user_id" as owner, x."status" as status,
x."currency" as currency, x."price_list_id" as price_list, x."shipping_address" as shipping_address
into v_cart from "better_supabase"."carts" x where x."id" = add_to_cart.cart for update;
if not found or not (auth.role() = 'service_role' or coalesce(v_cart.owner = auth.uid(), false)) then
raise exception 'cart not found' using errcode = 'P0002', hint = 'ORDERS_CART_NOT_FOUND';
end if;
if v_cart.status <> 'open' then
raise exception 'the cart is no longer open' using errcode = '55000', hint = 'ORDERS_CART_CLOSED';
end if;
if add_to_cart.quantity is null or add_to_cart.quantity < 1 then
raise exception 'the quantity must be a positive whole number' using errcode = '22023', hint = 'ORDERS_QUANTITY_INVALID';
end if;
if not exists (
select 1 from "better_supabase"."product_variants" cv join "better_supabase"."products" cp on cp."id" = cv."product_id"
where cv."id" = add_to_cart.variant and cv."tenant_id" = v_cart.tenant and cp."status" = 'active'
) then
raise exception 'this variant is not for sale' using errcode = 'P0002', hint = 'ORDERS_VARIANT_UNAVAILABLE';
end if;
select x."quantity" + add_to_cart.quantity into v_quantity from "better_supabase"."cart_lines" x
where x."cart_id" = add_to_cart.cart and x."variant_id" = add_to_cart.variant;
if coalesce(v_quantity, add_to_cart.quantity) > 10000 then
raise exception 'a cart line holds at most 10000 units' using errcode = '22023', hint = 'ORDERS_QUANTITY_INVALID';
end if;
insert into "better_supabase"."cart_lines" as x ("tenant_id", "cart_id", "variant_id", "quantity")
values (v_cart.tenant, add_to_cart.cart, add_to_cart.variant, add_to_cart.quantity)
on conflict ("cart_id", "variant_id") do update
set "quantity" = x."quantity" + excluded."quantity", "updated_at" = now()
returning x."id", x."quantity" into v_id, v_quantity;
update "better_supabase"."carts" x set "updated_at" = now() where x."id" = add_to_cart.cart;
return jsonb_build_object('lineId', v_id, 'quantity', v_quantity);
end;
$$;
revoke execute on function "better_supabase"."add_to_cart"(uuid, uuid, integer) from public, anon;
grant execute on function "better_supabase"."add_to_cart"(uuid, uuid, integer) to authenticated, service_role;
-- Sets a variant's quantity in the cart; zero removes the line.
create or replace function "better_supabase"."set_cart_line"(cart uuid, variant uuid, quantity integer)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_cart record;
v_id uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."tenant_id" as tenant, x."user_id" as owner, x."status" as status,
x."currency" as currency, x."price_list_id" as price_list, x."shipping_address" as shipping_address
into v_cart from "better_supabase"."carts" x where x."id" = set_cart_line.cart for update;
if not found or not (auth.role() = 'service_role' or coalesce(v_cart.owner = auth.uid(), false)) then
raise exception 'cart not found' using errcode = 'P0002', hint = 'ORDERS_CART_NOT_FOUND';
end if;
if v_cart.status <> 'open' then
raise exception 'the cart is no longer open' using errcode = '55000', hint = 'ORDERS_CART_CLOSED';
end if;
if set_cart_line.quantity is null or set_cart_line.quantity < 0 or set_cart_line.quantity > 10000 then
raise exception 'the quantity is a whole number from 0 to 10000' using errcode = '22023', hint = 'ORDERS_QUANTITY_INVALID';
end if;
if set_cart_line.quantity = 0 then
delete from "better_supabase"."cart_lines" x where x."cart_id" = set_cart_line.cart and x."variant_id" = set_cart_line.variant;
update "better_supabase"."carts" x set "updated_at" = now() where x."id" = set_cart_line.cart;
return jsonb_build_object('lineId', null, 'quantity', 0);
end if;
if not exists (
select 1 from "better_supabase"."product_variants" cv join "better_supabase"."products" cp on cp."id" = cv."product_id"
where cv."id" = set_cart_line.variant and cv."tenant_id" = v_cart.tenant and cp."status" = 'active'
) then
raise exception 'this variant is not for sale' using errcode = 'P0002', hint = 'ORDERS_VARIANT_UNAVAILABLE';
end if;
insert into "better_supabase"."cart_lines" as x ("tenant_id", "cart_id", "variant_id", "quantity")
values (v_cart.tenant, set_cart_line.cart, set_cart_line.variant, set_cart_line.quantity)
on conflict ("cart_id", "variant_id") do update
set "quantity" = excluded."quantity", "updated_at" = now()
returning x."id" into v_id;
update "better_supabase"."carts" x set "updated_at" = now() where x."id" = set_cart_line.cart;
return jsonb_build_object('lineId', v_id, 'quantity', set_cart_line.quantity);
end;
$$;
revoke execute on function "better_supabase"."set_cart_line"(uuid, uuid, integer) from public, anon;
grant execute on function "better_supabase"."set_cart_line"(uuid, uuid, integer) to authenticated, service_role;
-- Sets the cart's email and addresses; a null argument keeps the value.
create or replace function "better_supabase"."set_cart_details"(cart uuid, email text default null, shipping_address jsonb default null, billing_address jsonb default null)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_cart record;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."tenant_id" as tenant, x."user_id" as owner, x."status" as status,
x."currency" as currency, x."price_list_id" as price_list, x."shipping_address" as shipping_address
into v_cart from "better_supabase"."carts" x where x."id" = set_cart_details.cart for update;
if not found or not (auth.role() = 'service_role' or coalesce(v_cart.owner = auth.uid(), false)) then
raise exception 'cart not found' using errcode = 'P0002', hint = 'ORDERS_CART_NOT_FOUND';
end if;
if v_cart.status <> 'open' then
raise exception 'the cart is no longer open' using errcode = '55000', hint = 'ORDERS_CART_CLOSED';
end if;
if set_cart_details.shipping_address is not null and (jsonb_typeof(set_cart_details.shipping_address) <> 'object' or (set_cart_details.shipping_address ? 'country' and coalesce(set_cart_details.shipping_address ->> 'country', '') !~ '^[A-Z]{2}$')) then
raise exception 'an address is an object whose country is an ISO 3166-1 alpha-2 code' using errcode = '22023', hint = 'ORDERS_ADDRESS_INVALID';
end if;
if set_cart_details.billing_address is not null and (jsonb_typeof(set_cart_details.billing_address) <> 'object' or (set_cart_details.billing_address ? 'country' and coalesce(set_cart_details.billing_address ->> 'country', '') !~ '^[A-Z]{2}$')) then
raise exception 'an address is an object whose country is an ISO 3166-1 alpha-2 code' using errcode = '22023', hint = 'ORDERS_ADDRESS_INVALID';
end if;
update "better_supabase"."carts" x set
"email" = coalesce(set_cart_details.email, x."email"),
"shipping_address" = coalesce(set_cart_details.shipping_address, x."shipping_address"),
"billing_address" = coalesce(set_cart_details.billing_address, x."billing_address"),
"updated_at" = now()
where x."id" = set_cart_details.cart;
return true;
end;
$$;
revoke execute on function "better_supabase"."set_cart_details"(uuid, text, jsonb, jsonb) from public, anon;
grant execute on function "better_supabase"."set_cart_details"(uuid, text, jsonb, jsonb) to authenticated, service_role;
-- Picks one of the cart's shipping options, or clears it with null.
create or replace function "better_supabase"."set_cart_shipping_rate"(cart uuid, rate uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_cart record;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."tenant_id" as tenant, x."user_id" as owner, x."status" as status,
x."currency" as currency, x."price_list_id" as price_list, x."shipping_address" as shipping_address
into v_cart from "better_supabase"."carts" x where x."id" = set_cart_shipping_rate.cart for update;
if not found or not (auth.role() = 'service_role' or coalesce(v_cart.owner = auth.uid(), false)) then
raise exception 'cart not found' using errcode = 'P0002', hint = 'ORDERS_CART_NOT_FOUND';
end if;
if v_cart.status <> 'open' then
raise exception 'the cart is no longer open' using errcode = '55000', hint = 'ORDERS_CART_CLOSED';
end if;
if set_cart_shipping_rate.rate is not null and not exists (
select 1 from "better_supabase"."order_shipping_rates" x
where x."id" = set_cart_shipping_rate.rate and x."tenant_id" = v_cart.tenant and x."active"
and x."currency" = v_cart.currency
and (cardinality(x."countries") = 0 or (v_cart.shipping_address ->> 'country') = any (x."countries"))
) then
raise exception 'this shipping rate doesn''t serve the cart' using errcode = 'P0002', hint = 'ORDERS_RATE_NOT_FOUND';
end if;
update "better_supabase"."carts" x set "shipping_rate_id" = set_cart_shipping_rate.rate, "updated_at" = now()
where x."id" = set_cart_shipping_rate.cart;
return true;
end;
$$;
revoke execute on function "better_supabase"."set_cart_shipping_rate"(uuid, uuid) from public, anon;
grant execute on function "better_supabase"."set_cart_shipping_rate"(uuid, uuid) to authenticated, service_role;
-- The active rates that serve the cart's currency and shipping country,
-- each with what it costs for this cart.
create or replace function "better_supabase"."shipping_options"(cart uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_cart record;
v_totals jsonb;
v_after_discount bigint;
v_weight bigint;
begin
select x."tenant_id" as tenant, x."user_id" as owner, x."status" as status,
x."currency" as currency, x."price_list_id" as price_list, x."shipping_address" as shipping_address
into v_cart from "better_supabase"."carts" x where x."id" = shipping_options.cart;
if not found or not (auth.role() = 'service_role' or coalesce(v_cart.owner = auth.uid(), false) or coalesce(better_supabase.can('tenant', v_cart.tenant, 'orders.read'), false)) then
raise exception 'cart not found' using errcode = 'P0002', hint = 'ORDERS_CART_NOT_FOUND';
end if;
v_totals := "better_supabase"."cart_totals"(shipping_options.cart);
v_after_discount := (v_totals ->> 'subtotal')::bigint - (v_totals ->> 'discountTotal')::bigint;
v_weight := (v_totals ->> 'weightGrams')::bigint;
return coalesce((
select jsonb_agg(jsonb_build_object(
'id', x."id", 'name', x."name", 'kind', x."kind", 'currency', x."currency",
'amount', (case
when x."free_over" is not null and v_after_discount >= x."free_over" then 0
else x."amount" + case when x."kind" = 'weight' then (x."amount_per_kg" * v_weight + 999) / 1000 else 0 end
end)::text) order by x."amount", x."name")
from "better_supabase"."order_shipping_rates" x
where x."tenant_id" = v_cart.tenant and x."active" and x."currency" = v_cart.currency
and (cardinality(x."countries") = 0 or (v_cart.shipping_address ->> 'country') = any (x."countries"))
), '[]'::jsonb);
end;
$$;
revoke execute on function "better_supabase"."shipping_options"(uuid) from public, anon;
grant execute on function "better_supabase"."shipping_options"(uuid) to authenticated, service_role;
-- The cart at today's prices: lines, discounts, shipping, the pre-tax
-- total and what keeps it from checkout. Its owner and the organization's
-- order readers can price it.
create or replace function "better_supabase"."price_cart"(cart uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_cart record;
begin
select x."tenant_id" as tenant, x."user_id" as owner, x."status" as status,
x."currency" as currency, x."price_list_id" as price_list, x."shipping_address" as shipping_address
into v_cart from "better_supabase"."carts" x where x."id" = price_cart.cart;
if not found or not (auth.role() = 'service_role' or coalesce(v_cart.owner = auth.uid(), false) or coalesce(better_supabase.can('tenant', v_cart.tenant, 'orders.read'), false)) then
raise exception 'cart not found' using errcode = 'P0002', hint = 'ORDERS_CART_NOT_FOUND';
end if;
return "better_supabase"."cart_totals"(price_cart.cart);
end;
$$;
revoke execute on function "better_supabase"."price_cart"(uuid) from public, anon;
grant execute on function "better_supabase"."price_cart"(uuid) to authenticated, service_role;
-- Moves a user's open carts to another user, such as an anonymous user who
-- signed up or signed in. A cart in the same organization and currency
-- takes the lines, adding up quantities, and its promotion codes. Returns
-- the carts moved or merged.
create or replace function "better_supabase"."merge_carts"(from_user uuid, to_user uuid)
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
v_from record;
v_to uuid;
v_count integer := 0;
begin
if auth.role() is distinct from 'service_role' then
raise exception 'only the service role can do this' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if merge_carts.from_user = merge_carts.to_user then
return 0;
end if;
for v_from in
select x."id" as id, x."tenant_id" as tenant, x."currency" as currency
from "better_supabase"."carts" x where x."user_id" = merge_carts.from_user and x."status" = 'open'
for update
loop
select x."id" into v_to from "better_supabase"."carts" x
where x."user_id" = merge_carts.to_user and x."tenant_id" = v_from.tenant
and x."currency" = v_from.currency and x."status" = 'open'
for update;
if v_to is null then
update "better_supabase"."carts" x set "user_id" = merge_carts.to_user, "updated_at" = now() where x."id" = v_from.id;
else
insert into "better_supabase"."cart_lines" as x ("tenant_id", "cart_id", "variant_id", "quantity", "unit_amount")
select l."tenant_id", v_to, l."variant_id", l."quantity", l."unit_amount"
from "better_supabase"."cart_lines" l where l."cart_id" = v_from.id
on conflict ("cart_id", "variant_id") do update
set "quantity" = least(x."quantity" + excluded."quantity", 10000), "updated_at" = now();
update "better_supabase"."carts" x set "status" = 'abandoned', "updated_at" = now() where x."id" = v_from.id;
end if;
v_to := null;
v_count := v_count + 1;
end loop;
return v_count;
end;
$$;
revoke execute on function "better_supabase"."merge_carts"(uuid, uuid) from public, anon, authenticated;
grant execute on function "better_supabase"."merge_carts"(uuid, uuid) to service_role;
-- Moves an order to p_to and stamps when it happened. Internal: the order
-- functions call it after their own checks.
create or replace function "better_supabase"."order_transition"(p_order uuid, p_to text)
returns text
language plpgsql
security definer
set search_path = ''
as $$
declare
v_from text;
begin
select x."status" into v_from from "better_supabase"."orders" x where x."id" = p_order for update;
if v_from = p_to then
return v_from;
end if;
if not (case v_from
when 'draft' then p_to = any ('{confirmed,cancelled}'::text[])
when 'pending_payment' then p_to = any ('{paid,cancelled,on_hold}'::text[])
when 'confirmed' then p_to = any ('{paid,fulfilled,cancelled,on_hold}'::text[])
when 'on_hold' then p_to = any ('{pending_payment,confirmed,cancelled}'::text[])
when 'paid' then p_to = any ('{fulfilled,refunded}'::text[])
when 'fulfilled' then p_to = any ('{paid,completed,refunded}'::text[])
when 'completed' then p_to = any ('{refunded}'::text[])
when 'cancelled' then p_to = any ('{}'::text[])
when 'refunded' then p_to = any ('{}'::text[])
else false end) then
raise exception 'the order can''t move to this status' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
update "better_supabase"."orders" x set "status" = p_to, "updated_at" = now(),
"paid_at" = case when p_to = 'paid' then coalesce(x."paid_at", now()) else x."paid_at" end,
"fulfilled_at" = case when p_to = 'fulfilled' then now() when p_to = 'paid' then null else x."fulfilled_at" end,
"completed_at" = case when p_to = 'completed' then now() else x."completed_at" end,
"cancelled_at" = case when p_to = 'cancelled' then now() else x."cancelled_at" end
where x."id" = p_order;
return v_from;
end;
$$;
revoke execute on function "better_supabase"."order_transition"(uuid, text) from public, anon, authenticated;
-- The order as one object. Amounts are text. Internal: get_order and
-- place_order return it after their checks.
create or replace function "better_supabase"."order_json"(p_order uuid)
returns jsonb
language sql
stable
security definer
set search_path = ''
as $$
select jsonb_build_object(
'id', x."id", 'organizationId', x."tenant_id", 'number', x."number", 'cartId', x."cart_id",
'customerId', x."customer_id", 'email', x."email", 'status', x."status", 'currency', x."currency",
'subtotal', x."subtotal"::text, 'discountTotal', x."discount_total"::text,
'shippingTotal', x."shipping_total"::text, 'taxTotal', x."tax_total"::text, 'total', x."total"::text,
'paidTotal', x."paid_total"::text, 'refundedTotal', x."refunded_total"::text, 'taxInclusive', x."tax_inclusive",
'shippingAddress', x."shipping_address", 'billingAddress', x."billing_address", 'shipping', x."shipping",
'promotions', x."promotions", 'tax', x."tax", 'taxTransaction', x."tax_transaction",
'invoiceNumber', x."invoice_number", 'invoicedAt', x."invoiced_at", 'placedAt', x."placed_at",
'paidAt', x."paid_at", 'fulfilledAt', x."fulfilled_at", 'completedAt', x."completed_at",
'cancelledAt', x."cancelled_at", 'cancelReason', x."cancel_reason",
'heldAt', x."held_at", 'holdReason', x."hold_reason",
'lines', coalesce((select jsonb_agg(jsonb_build_object(
'id', l."id", 'variantId', l."variant_id", 'productId', l."product_id", 'sku', l."sku",
'title', l."title", 'quantity', l."quantity", 'unitAmount', l."unit_amount"::text,
'subtotal', l."subtotal"::text, 'discount', l."discount"::text, 'tax', l."tax"::text,
'total', l."total"::text, 'requiresShipping', l."requires_shipping", 'locationId', l."location_id",
'reservationId', l."reservation_id", 'fulfilled', l."quantity_fulfilled", 'returned', l."quantity_returned",
'backordered', l."backordered", 'unit', l."unit", 'description', l."description", 'lineType', l."line_type") order by l."position")
from "better_supabase"."order_lines" l where l."order_id" = x."id"), '[]'::jsonb),
'payments', coalesce((select jsonb_agg(jsonb_build_object(
'id', y."id", 'provider', y."provider", 'reference', y."reference", 'status', y."status",
'amount', y."amount"::text, 'captured', y."captured"::text, 'refunded', y."refunded"::text,
'createdAt', y."created_at") order by y."created_at")
from "better_supabase"."order_payments" y where y."order_id" = x."id"), '[]'::jsonb),
'fulfillments', coalesce((select jsonb_agg(jsonb_build_object(
'id', y."id", 'status', y."status", 'locationId', y."location_id", 'carrier', y."carrier",
'trackingNumber', y."tracking_number", 'trackingUrl', y."tracking_url", 'createdAt', y."created_at",
'shippedAt', y."shipped_at", 'deliveredAt', y."delivered_at",
'lines', coalesce((select jsonb_agg(jsonb_build_object('lineId', z."order_line_id", 'quantity', z."quantity"))
from "better_supabase"."order_fulfillment_lines" z where z."fulfillment_id" = y."id"), '[]'::jsonb)) order by y."created_at")
from "better_supabase"."order_fulfillments" y where y."order_id" = x."id"), '[]'::jsonb),
'returns', coalesce((select jsonb_agg(jsonb_build_object(
'id', y."id", 'status', y."status", 'reason', y."reason", 'createdAt', y."created_at",
'receivedAt', y."received_at",
'lines', coalesce((select jsonb_agg(jsonb_build_object('lineId', z."order_line_id", 'quantity', z."quantity"))
from "better_supabase"."order_return_lines" z where z."return_id" = y."id"), '[]'::jsonb)) order by y."created_at")
from "better_supabase"."order_returns" y where y."order_id" = x."id"), '[]'::jsonb),
'refunds', coalesce((select jsonb_agg(jsonb_build_object(
'id', y."id", 'paymentId', y."payment_id", 'returnId', y."return_id", 'amount', y."amount"::text,
'status', y."status", 'reason', y."reason", 'providerReference', y."provider_reference",
'createdAt', y."created_at", 'completedAt', y."completed_at") order by y."created_at")
from "better_supabase"."order_refunds" y where y."order_id" = x."id"), '[]'::jsonb)) || jsonb_build_object('party', case when x."party_id" is not null then jsonb_build_object('type', x."party_type", 'id', x."party_id") end, 'channel', x."channel", 'source', x."source", 'externalId', x."external_id", 'invoicingPolicy', x."invoicing_policy", 'deliveryMethod', x."delivery_method", 'quoteId', x."quote_id", 'orderDiscount', x."order_discount"::text, 'promotionId', x."promotion_id", 'paymentRequired', x."payment_required", 'deposit', x."deposit_amount"::text)
from "better_supabase"."orders" x where x."id" = p_order
$$;
revoke execute on function "better_supabase"."order_json"(uuid) from public, anon, authenticated;
-- Turns an open cart into an order: prices it again, refuses a total that
-- moved since the caller quoted tax (ORDERS_PRICE_CHANGED), numbers the
-- order, reserves its stock for the payment window and redeems its
-- promotions. tax is the TaxQuote as JSON (amounts as text) or null.
-- Placing the same cart again returns its order. Service only: the
-- TypeScript checkout calls the tax and payment providers around it.
create or replace function "better_supabase"."place_order"(cart uuid, tax jsonb default null, expected_total bigint default null, location uuid default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_cart record;
v_order record;
v_line record;
v_priced jsonb;
v_tax bigint := 0;
v_line_tax bigint;
v_invalid boolean;
v_inclusive boolean := false;
v_total bigint;
v_seq bigint;
v_number text;
v_id uuid;
v_location uuid;
v_reservation jsonb;
v_claims text;
v_warnings jsonb;
begin
if auth.role() is distinct from 'service_role' then
raise exception 'only the service role can do this' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
select x."tenant_id" as tenant, x."user_id" as owner, x."status" as status, x."currency" as currency,
x."email" as email, x."shipping_address" as shipping_address, x."billing_address" as billing_address
into v_cart from "better_supabase"."carts" x where x."id" = place_order.cart for update;
if not found then
raise exception 'cart not found' using errcode = 'P0002', hint = 'ORDERS_CART_NOT_FOUND';
end if;
if v_cart.status = 'converted' then
select x."id" into v_id from "better_supabase"."orders" x where x."cart_id" = place_order.cart;
if v_id is not null then
return "better_supabase"."order_json"(v_id) || jsonb_build_object('created', false);
end if;
end if;
if v_cart.status <> 'open' then
raise exception 'the cart is no longer open' using errcode = '55000', hint = 'ORDERS_CART_CLOSED';
end if;
declare
v_hook regprocedure := to_regprocedure('"public"."before_order_place"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using place_order.cart;
end if;
end;
v_priced := "better_supabase"."cart_totals"(place_order.cart);
if jsonb_array_length(v_priced -> 'lines') = 0 then
raise exception 'the cart has no lines to order' using errcode = '55000', hint = 'ORDERS_CART_EMPTY';
end if;
if jsonb_array_length(v_priced -> 'problems') > 0 then
raise exception 'the cart can''t be ordered: %', v_priced -> 'problems' using errcode = '55000', hint = 'ORDERS_CART_INVALID';
end if;
if place_order.expected_total is not null and (v_priced ->> 'total')::bigint <> place_order.expected_total then
raise exception 'the cart''s total changed; price it and quote tax again' using errcode = '40001', hint = 'ORDERS_PRICE_CHANGED';
end if;
if place_order.tax is not null then
if jsonb_typeof(place_order.tax) <> 'object' or (case when (place_order.tax ->> 'totalTax') ~ '^[0-9]{1,18}$' then (place_order.tax ->> 'totalTax')::bigint end) is null
or coalesce(place_order.tax ->> 'currency', v_cart.currency) <> v_cart.currency then
raise exception 'tax is a quote in the cart''s currency with a totalTax of zero or more' using errcode = '22023', hint = 'ORDERS_TAX_INVALID';
end if;
v_tax := (case when (place_order.tax ->> 'totalTax') ~ '^[0-9]{1,18}$' then (place_order.tax ->> 'totalTax')::bigint end);
v_inclusive := coalesce((place_order.tax ->> 'inclusive')::boolean, false);
select coalesce(sum((case when (e ->> 'tax') ~ '^[0-9]{1,18}$' then (e ->> 'tax')::bigint end)), 0), coalesce(bool_or((case when (e ->> 'tax') ~ '^[0-9]{1,18}$' then (e ->> 'tax')::bigint end) is null), false)
into v_line_tax, v_invalid
from jsonb_array_elements(coalesce(place_order.tax -> 'lines', '[]'::jsonb)) e;
if v_invalid or (jsonb_array_length(coalesce(place_order.tax -> 'lines', '[]'::jsonb)) > 0 and v_line_tax <> v_tax) then
raise exception 'the tax lines don''t add up to totalTax' using errcode = '22023', hint = 'ORDERS_TAX_INVALID';
end if;
end if;
v_total := (v_priced ->> 'total')::bigint + case when v_inclusive then 0 else v_tax end;
if (case when split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 1) = 'order' then nullif(split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 2), '') end) is not null then
v_number := (case when split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 1) = 'order' then nullif(split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 2), '') end);
if better_supabase.import_run() is null then
raise exception 'a fixed number is only accepted during an import' using errcode = '42501', hint = 'NUMBER_FIXED_FORBIDDEN';
end if;
v_seq := coalesce(substring(v_number from '([0-9]+)$')::bigint, 0);
perform set_config('better_supabase.fixed_number', '', true);
else
perform pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended(v_cart.tenant::text || ':commerce:order', 0));
select coalesce(max(x."sequence"), 0) + 1 into v_seq from "better_supabase"."orders" x where x."tenant_id" = v_cart.tenant;
v_number := v_seq::text;
end if;
insert into "better_supabase"."orders" ("tenant_id", "number", "sequence", "cart_id", "customer_id", "email", "currency",
"subtotal", "discount_total", "shipping_total", "tax_total", "total", "tax_inclusive",
"shipping_address", "billing_address", "shipping", "promotions", "tax")
values (v_cart.tenant, v_number, v_seq, place_order.cart, v_cart.owner, v_cart.email, v_cart.currency,
(v_priced ->> 'subtotal')::bigint, (v_priced ->> 'discountTotal')::bigint, (v_priced ->> 'shippingTotal')::bigint,
v_tax, v_total, v_inclusive, v_cart.shipping_address, v_cart.billing_address, v_priced -> 'shipping',
v_priced -> 'promotions', place_order.tax)
returning "id" into v_id;
insert into "better_supabase"."order_lines" ("tenant_id", "order_id", "position", "variant_id", "product_id", "sku", "title",
"quantity", "unit_amount", "subtotal", "discount", "tax", "total", "requires_shipping")
select v_cart.tenant, v_id, e.n, (e.l ->> 'variantId')::uuid, (e.l ->> 'productId')::uuid, e.l ->> 'sku', e.l ->> 'title',
(e.l ->> 'quantity')::integer, (e.l ->> 'unitAmount')::bigint, (e.l ->> 'subtotal')::bigint, (e.l ->> 'discount')::bigint,
coalesce((select (case when (t ->> 'tax') ~ '^[0-9]{1,18}$' then (t ->> 'tax')::bigint end) from jsonb_array_elements(coalesce(place_order.tax -> 'lines', '[]'::jsonb)) t
where t ->> 'lineId' = e.l ->> 'id' limit 1), 0),
(e.l ->> 'total')::bigint, (e.l ->> 'requiresShipping')::boolean
from jsonb_array_elements(v_priced -> 'lines') with ordinality as e(l, n);
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = v_id;
update "better_supabase"."carts" x set "status" = 'converted', "converted_at" = now(), "updated_at" = now()
where x."id" = place_order.cart;
null;
declare
v_hook regprocedure := to_regprocedure('"public"."after_order_place"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_order.id;
end if;
end;
return "better_supabase"."order_json"(v_order.id) || jsonb_build_object('created', true)
|| case when v_warnings is null then '{}'::jsonb else jsonb_build_object('warnings', v_warnings) end;
end;
$$;
revoke execute on function "better_supabase"."place_order"(uuid, jsonb, bigint, uuid) from public, anon, authenticated;
grant execute on function "better_supabase"."place_order"(uuid, jsonb, bigint, uuid) to service_role;
-- Records what a payment provider reports for an order's payment, keyed on
-- provider and reference, so a redelivered webhook changes nothing. A late
-- event never moves a settled payment back. Once the succeeded payments
-- cover the total, the order is paid: its reservations become sales, it
-- gets a gapless invoice number and order.paid is recorded. order may be
-- null for a payment recorded before. Amounts are minor units.
create or replace function "better_supabase"."record_payment"(order_id uuid, provider text, reference text, status text, currency text,
amount bigint, captured bigint default 0, refunded bigint default 0)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_order record;
v_line record;
v_id uuid := record_payment.order_id;
v_paid bigint;
v_newly boolean := false;
v_seq bigint;
v_number text;
v_claims text;
begin
if auth.role() is distinct from 'service_role' then
raise exception 'only the service role can do this' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_id is null then
select x."order_id" into v_id from "better_supabase"."order_payments" x
where x."provider" = record_payment.provider and x."reference" = record_payment.reference;
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = v_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if record_payment.status is null or record_payment.status not in ('requires_action', 'processing', 'authorized', 'succeeded', 'failed', 'canceled') then
raise exception 'unknown payment status' using errcode = '22023', hint = 'ORDERS_PAYMENT_STATUS_INVALID';
end if;
if record_payment.currency is distinct from v_order.currency then
raise exception 'the payment is in another currency than the order' using errcode = '22023', hint = 'ORDERS_CURRENCY_MISMATCH';
end if;
if coalesce(record_payment.amount, -1) < 0 or coalesce(record_payment.captured, 0) < 0 or coalesce(record_payment.refunded, 0) < 0 then
raise exception 'payment amounts are zero or more' using errcode = '22023', hint = 'ORDERS_AMOUNT_INVALID';
end if;
insert into "better_supabase"."order_payments" as x ("tenant_id", "order_id", "provider", "reference", "status", "currency",
"amount", "captured", "refunded")
values (v_order.tenant, v_order.id, record_payment.provider, record_payment.reference, record_payment.status, record_payment.currency,
record_payment.amount, coalesce(record_payment.captured, 0), coalesce(record_payment.refunded, 0))
on conflict ("provider", "reference") do update set
"status" = case when x."status" in ('succeeded', 'failed', 'canceled') and excluded."status" not in ('succeeded', 'failed', 'canceled') then x."status" else excluded."status" end,
"amount" = excluded."amount",
"captured" = greatest(x."captured", excluded."captured"),
"refunded" = greatest(x."refunded", excluded."refunded"),
"updated_at" = now()
where x."order_id" = excluded."order_id";
if not found then
raise exception 'this payment reference belongs to another order' using errcode = '23505', hint = 'ORDERS_PAYMENT_CONFLICT';
end if;
select coalesce(sum(x."captured"), 0) into v_paid from "better_supabase"."order_payments" x
where x."order_id" = v_order.id and x."status" = 'succeeded';
update "better_supabase"."orders" x set "paid_total" = v_paid, "updated_at" = now() where x."id" = v_order.id;
if v_order.status::text in ('pending_payment', 'confirmed') and v_paid >= v_order.total then
perform "better_supabase"."order_transition"(v_order.id, 'paid');
if (case when split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 1) = 'invoice' then nullif(split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 2), '') end) is not null then
v_number := (case when split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 1) = 'invoice' then nullif(split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 2), '') end);
if better_supabase.import_run() is null then
raise exception 'a fixed number is only accepted during an import' using errcode = '42501', hint = 'NUMBER_FIXED_FORBIDDEN';
end if;
v_seq := coalesce(substring(v_number from '([0-9]+)$')::bigint, 0);
perform set_config('better_supabase.fixed_number', '', true);
else
perform pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended(v_order.tenant::text || ':commerce:invoice', 0));
select coalesce(max(x."invoice_sequence"), 0) + 1 into v_seq from "better_supabase"."orders" x where x."tenant_id" = v_order.tenant;
v_number := v_seq::text;
end if;
update "better_supabase"."orders" x set "invoice_number" = v_number, "invoice_sequence" = v_seq, "invoiced_at" = now()
where x."id" = v_order.id;
null;
v_newly := true;
end if;
return jsonb_build_object('orderId', v_order.id, 'status', (select x."status" from "better_supabase"."orders" x where x."id" = v_order.id),
'paidTotal', v_paid::text, 'newlyPaid', v_newly,
'invoiceNumber', coalesce(v_number, v_order.invoice_number));
end;
$$;
revoke execute on function "better_supabase"."record_payment"(uuid, text, text, text, text, bigint, bigint, bigint) from public, anon, authenticated;
grant execute on function "better_supabase"."record_payment"(uuid, text, text, text, text, bigint, bigint, bigint) to service_role;
-- Stores the tax provider's transaction for the sale, to reverse it on a refund.
create or replace function "better_supabase"."set_order_tax_transaction"(order_id uuid, tax_transaction jsonb)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
begin
if auth.role() is distinct from 'service_role' then
raise exception 'only the service role can do this' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
update "better_supabase"."orders" x set "tax_transaction" = set_order_tax_transaction.tax_transaction, "updated_at" = now()
where x."id" = set_order_tax_transaction.order_id;
return found;
end;
$$;
revoke execute on function "better_supabase"."set_order_tax_transaction"(uuid, jsonb) from public, anon, authenticated;
grant execute on function "better_supabase"."set_order_tax_transaction"(uuid, jsonb) to service_role;
-- Cancels an order waiting for payment or confirmed: releases its stock
-- reservations and allocations and its promotion redemptions. Internal: cancel_order and expire_orders check first.
create or replace function "better_supabase"."order_cancel"(p_order uuid, p_reason text)
returns void
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
v_line record;
v_claims text;
begin
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = p_order for update;
perform "better_supabase"."order_transition"(v_order.id, 'cancelled');
update "better_supabase"."orders" x set "cancel_reason" = p_reason where x."id" = v_order.id;
null;
end;
$$;
revoke execute on function "better_supabase"."order_cancel"(uuid, text) from public, anon, authenticated;
-- Cancels a draft, an order that is waiting for payment, a confirmed one or
-- one on hold, before anything shipped. Staff with orders.manage and the customer who placed
-- it can.
create or replace function "better_supabase"."cancel_order"(order_id uuid, reason text default null)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
begin
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = cancel_order.order_id for update;
if not found or not (auth.role() = 'service_role' or coalesce(v_order.customer = auth.uid(), false) or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.read'), false)) then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(v_order.customer = auth.uid(), false) or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to cancel this order' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_order.status = 'cancelled' then
return false;
end if;
if v_order.status::text not in ('draft', 'pending_payment', 'confirmed', 'on_hold') or exists (select 1 from "better_supabase"."order_fulfillments" y
where y."order_id" = v_order.id and y."status" in ('shipped', 'delivered')) then
raise exception 'only a draft, an order waiting for payment, a confirmed one or one on hold, with nothing shipped, can be cancelled; refund a paid one' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
perform "better_supabase"."order_cancel"(v_order.id, cancel_order.reason);
return true;
end;
$$;
revoke execute on function "better_supabase"."cancel_order"(uuid, text) from public, anon;
grant execute on function "better_supabase"."cancel_order"(uuid, text) to authenticated, service_role;
-- Cancels orders still waiting for payment once the payment window
-- (30 minutes) is over. Service only; run it from a job.
create or replace function "better_supabase"."expire_orders"(max_rows integer default 100)
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
v_id uuid;
v_count integer := 0;
begin
if auth.role() is distinct from 'service_role' then
raise exception 'only the service role can do this' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
for v_id in
select x."id" from "better_supabase"."orders" x
where x."status" = 'pending_payment' and x."placed_at" < now() - interval '30 minutes'
order by x."placed_at"
limit greatest(coalesce(expire_orders.max_rows, 100), 1)
for update skip locked
loop
perform "better_supabase"."order_cancel"(v_id, 'payment_expired');
v_count := v_count + 1;
end loop;
return v_count;
end;
$$;
revoke execute on function "better_supabase"."expire_orders"(integer) from public, anon, authenticated;
grant execute on function "better_supabase"."expire_orders"(integer) to service_role;
-- The order with its lines, payments, fulfillments, returns and refunds,
-- for its customer and the organization's order readers.
create or replace function "better_supabase"."get_order"(order_id uuid)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
v_order record;
begin
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = get_order.order_id;
if not found or not (auth.role() = 'service_role' or coalesce(v_order.customer = auth.uid(), false) or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.read'), false)) then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
return "better_supabase"."order_json"(v_order.id);
end;
$$;
revoke execute on function "better_supabase"."get_order"(uuid) from public, anon;
grant execute on function "better_supabase"."get_order"(uuid) to authenticated, service_role;
-- Inserts a confirmed order with computed lines and totals, allocates its
-- stock and records order.placed. With p_attrs.draft the order is a draft:
-- it holds no stock and records order.drafted until confirm_draft_order.
-- p_attrs carries the totals as text and an optional origin; an order with the same origin is returned instead.
-- Internal: order_from_lines and order_from_quote check first.
create or replace function "better_supabase"."order_from_document"(p_tenant uuid, p_attrs jsonb, p_lines jsonb, p_location uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
v_line record;
v_allocation jsonb;
v_seq bigint;
v_number text;
v_id uuid;
v_claims text;
begin
if p_attrs -> 'origin' is not null then
select x."id" into v_id from "better_supabase"."orders" x
where x."tenant_id" = p_tenant and x."origin_type" = p_attrs #>> '{origin,type}' and x."origin_id" = p_attrs #>> '{origin,id}'
and x."status" <> 'cancelled'
limit 1;
if v_id is not null then
return "better_supabase"."order_json"(v_id) || jsonb_build_object('created', false);
end if;
end if;
if nullif(p_attrs ->> 'externalId', '') is not null then
select x."id" into v_id from "better_supabase"."orders" x
where x."tenant_id" = p_tenant and x."source" = p_attrs ->> 'source' and x."external_id" = p_attrs ->> 'externalId';
if v_id is not null then
return "better_supabase"."order_json"(v_id) || jsonb_build_object('created', false);
end if;
end if;
if (case when split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 1) = 'order' then nullif(split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 2), '') end) is not null then
v_number := (case when split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 1) = 'order' then nullif(split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 2), '') end);
if better_supabase.import_run() is null then
raise exception 'a fixed number is only accepted during an import' using errcode = '42501', hint = 'NUMBER_FIXED_FORBIDDEN';
end if;
v_seq := coalesce(substring(v_number from '([0-9]+)$')::bigint, 0);
perform set_config('better_supabase.fixed_number', '', true);
else
perform pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended(p_tenant::text || ':commerce:order', 0));
select coalesce(max(x."sequence"), 0) + 1 into v_seq from "better_supabase"."orders" x where x."tenant_id" = p_tenant;
v_number := v_seq::text;
end if;
insert into "better_supabase"."orders" ("tenant_id", "number", "sequence", "customer_id", "email", "status", "currency",
"subtotal", "discount_total", "shipping_total", "tax_total", "total", "tax_inclusive",
"shipping_address", "billing_address", "origin_type", "origin_id", "channel", "source", "external_id", "invoicing_policy", "delivery_method", "quote_id", "order_discount", "promotion_id", "payment_required", "deposit_amount", "party_type", "party_id")
values (p_tenant, v_number, v_seq, nullif(p_attrs ->> 'customerId', '')::uuid, p_attrs ->> 'email', case when coalesce((p_attrs ->> 'draft')::boolean, false) then 'draft' else 'confirmed' end, p_attrs ->> 'currency',
(p_attrs ->> 'subtotal')::bigint, (p_attrs ->> 'discountTotal')::bigint, (p_attrs ->> 'shippingTotal')::bigint,
(p_attrs ->> 'taxTotal')::bigint, (p_attrs ->> 'total')::bigint, coalesce((p_attrs ->> 'taxInclusive')::boolean, false),
p_attrs -> 'shippingAddress', p_attrs -> 'billingAddress', p_attrs #>> '{origin,type}', p_attrs #>> '{origin,id}', nullif(p_attrs ->> 'channel', ''), nullif(p_attrs ->> 'source', ''), nullif(p_attrs ->> 'externalId', ''), coalesce(nullif(p_attrs ->> 'invoicingPolicy', ''), 'on_order'), nullif(p_attrs ->> 'deliveryMethod', ''), nullif(p_attrs ->> 'quoteId', '')::uuid, coalesce((p_attrs ->> 'orderDiscount')::bigint, 0), nullif(p_attrs ->> 'promotionId', '')::uuid, coalesce((p_attrs ->> 'paymentRequired')::boolean, false), nullif(p_attrs ->> 'deposit', '')::bigint, nullif(p_attrs #>> '{party,type}', ''), nullif(p_attrs #>> '{party,id}', ''))
returning "id" into v_id;
insert into "better_supabase"."order_lines" ("tenant_id", "order_id", "position", "variant_id", "product_id", "sku", "title",
"quantity", "unit_amount", "subtotal", "discount", "tax", "total", "requires_shipping", "unit", "description", "line_type")
select p_tenant, v_id, e.n, nullif(e.l ->> 'variantId', '')::uuid, nullif(e.l ->> 'productId', '')::uuid, e.l ->> 'sku', e.l ->> 'title',
(e.l ->> 'quantity')::numeric, (e.l ->> 'unitAmount')::bigint, (e.l ->> 'subtotal')::bigint, (e.l ->> 'discount')::bigint,
(e.l ->> 'tax')::bigint, (e.l ->> 'total')::bigint, (e.l ->> 'requiresShipping')::boolean, nullif(e.l ->> 'unit', ''), nullif(e.l ->> 'description', ''), coalesce(nullif(e.l ->> 'lineType', ''), 'item')
from jsonb_array_elements(p_lines) with ordinality as e(l, n);
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = v_id;
if not coalesce((p_attrs ->> 'draft')::boolean, false) then
end if;
if coalesce((p_attrs ->> 'draft')::boolean, false) then
null;
else
null;
end if;
return "better_supabase"."order_json"(v_id) || jsonb_build_object('created', true);
end;
$$;
revoke execute on function "better_supabase"."order_from_document"(uuid, jsonb, jsonb, uuid) from public, anon, authenticated;
-- A confirmed order from lines staff enter, for sales that don't go through
-- a cart: phone, B2B and wholesale orders. lines: [{ variantId?, title?,
-- sku?, quantity, unitAmount?, discount?, tax? }]; a catalog line without a
-- unitAmount takes the variant's price. attrs: { currency, customerId?,
-- email?, shippingAddress?, billingAddress?, shippingTotal?, taxInclusive?,
-- locationId?, origin? { type, id } }. Amounts are minor units as text.
create or replace function "better_supabase"."order_from_lines"(tenant uuid, lines jsonb, attrs jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
#variable_conflict use_column
declare
v_attrs jsonb := coalesce(order_from_lines.attrs, '{}'::jsonb);
v_currency text := v_attrs ->> 'currency';
v_inclusive boolean := coalesce((v_attrs ->> 'taxInclusive')::boolean, false);
v_shipping bigint := coalesce(case when coalesce(v_attrs ->> 'shippingTotal', '') ~ '^[0-9]{1,18}$' then (v_attrs ->> 'shippingTotal')::bigint end, 0);
v_item record;
v_lines jsonb := '[]';
v_unit bigint;
v_rule jsonb;
v_disc bigint;
v_subtotal bigint := 0;
v_discount bigint := 0;
v_tax bigint := 0;
v_order_discount bigint;
begin
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', order_from_lines.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_currency is null or v_currency !~ '^[A-Z]{3}$' then
raise exception 'attrs.currency is an ISO 4217 code' using errcode = '22023', hint = 'ORDERS_CURRENCY_INVALID';
end if;
if jsonb_typeof(order_from_lines.lines) is distinct from 'array' or jsonb_array_length(order_from_lines.lines) = 0 then
raise exception 'lines is a non-empty list' using errcode = '22023', hint = 'ORDERS_LINES_INVALID';
end if;
if (v_attrs ? 'shippingTotal' and case when coalesce(v_attrs ->> 'shippingTotal', '') ~ '^[0-9]{1,18}$' then (v_attrs ->> 'shippingTotal')::bigint end is null) then
raise exception 'amounts are minor units of zero or more' using errcode = '22023', hint = 'ORDERS_AMOUNT_INVALID';
end if;
if (v_attrs ? 'orderDiscount' and coalesce(v_attrs ->> 'orderDiscount', '') !~ '^[0-9]{1,18}$') or (v_attrs ? 'deposit' and coalesce(v_attrs ->> 'deposit', '') !~ '^[0-9]{1,18}$') then
raise exception 'amounts are minor units of zero or more' using errcode = '22023', hint = 'ORDERS_AMOUNT_INVALID';
end if;
if nullif(v_attrs ->> 'externalId', '') is not null and nullif(v_attrs ->> 'source', '') is null then
raise exception 'attrs.externalId needs attrs.source, the system the id comes from' using errcode = '22023', hint = 'ORDERS_SOURCE_REQUIRED';
end if;
if v_attrs -> 'party' is not null and (nullif(v_attrs #>> '{party,type}', '') is null or nullif(v_attrs #>> '{party,id}', '') is null) then
raise exception 'attrs.party is { type, id }' using errcode = '22023', hint = 'ORDERS_PARTY_INVALID';
end if;
v_order_discount := coalesce(case when coalesce(v_attrs ->> 'orderDiscount', '') ~ '^[0-9]{1,18}$' then (v_attrs ->> 'orderDiscount')::bigint end, 0);
for v_item in
select e as raw, nullif(e ->> 'variantId', '')::uuid as variant, e ->> 'quantity' as quantity,
cv."id" as found, cv."tenant_id" as variant_tenant, cv."sku" as sku, concat_ws(' / ', cp."title", nullif(cv."title", '')) as title, cp."id" as product, cp."kind" as kind
from jsonb_array_elements(order_from_lines.lines) e
left join "better_supabase"."product_variants" cv on cv."id" = nullif(e ->> 'variantId', '')::uuid
left join "better_supabase"."products" cp on cp."id" = cv."product_id"
loop
if not (case when coalesce(v_item.quantity, '') ~ '^[0-9]{1,12}$' then (v_item.quantity)::numeric > 0 else false end) then
raise exception 'each quantity is a whole number above zero' using errcode = '22023', hint = 'ORDERS_QUANTITY_INVALID';
end if;
if coalesce(v_item.raw ->> 'lineType', 'item') not in ('item', 'service', 'fee') then
raise exception 'a line''s lineType is one of item, service, fee' using errcode = '22023', hint = 'ORDERS_LINES_INVALID';
end if;
if v_item.variant is not null and (v_item.found is null or v_item.variant_tenant <> order_from_lines.tenant) then
raise exception 'a line''s variant isn''t in this organization' using errcode = 'P0002', hint = 'ORDERS_VARIANT_NOT_FOUND';
end if;
if v_item.variant is null and coalesce(v_item.raw ->> 'title', '') = '' then
raise exception 'a line without a variant needs a title' using errcode = '22023', hint = 'ORDERS_LINES_INVALID';
end if;
if (v_item.raw ? 'unitAmount' and case when coalesce(v_item.raw ->> 'unitAmount', '') ~ '^[0-9]{1,18}$' then (v_item.raw ->> 'unitAmount')::bigint end is null)
or (v_item.raw ? 'discount' and case when coalesce(v_item.raw ->> 'discount', '') ~ '^[0-9]{1,18}$' then (v_item.raw ->> 'discount')::bigint end is null)
or (v_item.raw ? 'tax' and case when coalesce(v_item.raw ->> 'tax', '') ~ '^[0-9]{1,18}$' then (v_item.raw ->> 'tax')::bigint end is null) then
raise exception 'amounts are minor units of zero or more' using errcode = '22023', hint = 'ORDERS_AMOUNT_INVALID';
end if;
v_rule := null;
v_unit := coalesce(case when coalesce(v_item.raw ->> 'unitAmount', '') ~ '^[0-9]{1,18}$' then (v_item.raw ->> 'unitAmount')::bigint end, (v_rule ->> 'fixedPrice')::bigint,
case when v_item.variant is not null then ("better_supabase"."variant_price"(v_item.variant, v_currency, null, null) ->> 'amount')::bigint end);
if v_unit is null then
raise exception 'a line has no unitAmount and its variant no price in this currency' using errcode = '22023', hint = 'ORDERS_LINE_UNPRICED';
end if;
v_disc := coalesce(case when coalesce(v_item.raw ->> 'discount', '') ~ '^[0-9]{1,18}$' then (v_item.raw ->> 'discount')::bigint end,
case when v_rule is not null and not v_item.raw ? 'discount' then round(round(v_unit * v_item.quantity::numeric)::bigint * (v_rule ->> 'discountBps')::integer / 10000.0)::bigint end, 0);
if v_disc > round(v_unit * v_item.quantity::numeric)::bigint then
raise exception 'a line''s discount is more than its subtotal' using errcode = '22023', hint = 'ORDERS_AMOUNT_INVALID';
end if;
v_lines := v_lines || jsonb_build_array(jsonb_build_object(
'variantId', v_item.variant, 'productId', v_item.product,
'sku', coalesce(v_item.raw ->> 'sku', v_item.sku, ''), 'title', coalesce(nullif(v_item.raw ->> 'title', ''), v_item.title),
'quantity', v_item.quantity, 'unitAmount', v_unit::text, 'subtotal', (round(v_unit * v_item.quantity::numeric)::bigint)::text,
'discount', v_disc::text, 'tax', coalesce(case when coalesce(v_item.raw ->> 'tax', '') ~ '^[0-9]{1,18}$' then (v_item.raw ->> 'tax')::bigint end, 0)::text,
'total', (round(v_unit * v_item.quantity::numeric)::bigint - v_disc)::text,
'requiresShipping', case when v_item.variant is null then coalesce((v_item.raw ->> 'requiresShipping')::boolean, false) else v_item.kind = 'physical' end)
|| jsonb_strip_nulls(jsonb_build_object('unit', v_item.raw -> 'unit', 'description', v_item.raw -> 'description',
'lineType', v_item.raw -> 'lineType', 'fields', v_item.raw -> 'fields')));
v_subtotal := v_subtotal + round(v_unit * v_item.quantity::numeric)::bigint;
v_discount := v_discount + v_disc;
v_tax := v_tax + coalesce(case when coalesce(v_item.raw ->> 'tax', '') ~ '^[0-9]{1,18}$' then (v_item.raw ->> 'tax')::bigint end, 0);
end loop;
if v_order_discount > v_subtotal - v_discount then
raise exception 'attrs.orderDiscount is more than the lines'' net subtotal' using errcode = '22023', hint = 'ORDERS_AMOUNT_INVALID';
end if;
v_discount := v_discount + v_order_discount;
return "better_supabase"."order_from_document"(order_from_lines.tenant, v_attrs || jsonb_build_object(
'subtotal', v_subtotal::text, 'discountTotal', v_discount::text, 'shippingTotal', v_shipping::text, 'taxTotal', v_tax::text,
'total', (v_subtotal - v_discount + v_shipping + case when v_inclusive then 0 else v_tax end)::text),
v_lines, nullif(v_attrs ->> 'locationId', '')::uuid);
end;
$$;
revoke execute on function "better_supabase"."order_from_lines"(uuid, jsonb, jsonb) from public, anon;
grant execute on function "better_supabase"."order_from_lines"(uuid, jsonb, jsonb) to authenticated, service_role;
create or replace function "better_supabase"."order_from_quote"(quote uuid, attrs jsonb default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
begin
raise exception 'install the quotes block to order a quote' using errcode = '22023', hint = 'ORDERS_QUOTES_MISSING';
end;
$$;
revoke execute on function "better_supabase"."order_from_quote"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."order_from_quote"(uuid, jsonb) to authenticated, service_role;
-- Confirms a draft order: it is allocated stock at the best location (or
-- location), the rest on backorder, and records order.placed. A draft that
-- was never confirmed can be cancelled with cancel_order.
create or replace function "better_supabase"."confirm_draft_order"(order_id uuid, location uuid default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
v_line record;
v_allocation jsonb;
v_claims text;
begin
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = confirm_draft_order.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_order.status::text <> 'draft' then
raise exception 'only a draft order can be confirmed' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
perform "better_supabase"."order_transition"(v_order.id, 'confirmed');
null;
return "better_supabase"."order_json"(v_order.id);
end;
$$;
revoke execute on function "better_supabase"."confirm_draft_order"(uuid, uuid) from public, anon;
grant execute on function "better_supabase"."confirm_draft_order"(uuid, uuid) to authenticated, service_role;
-- Puts an order that waits for payment, or a confirmed one, on hold: it can't
-- be paid into, fulfilled or expire until the hold is released. Its stock
-- stays reserved. reason is for the people who release it.
create or replace function "better_supabase"."hold_order"(order_id uuid, reason text default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
v_from text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = hold_order.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_order.status::text not in ('pending_payment', 'confirmed') then
raise exception 'only an order waiting for payment or a confirmed one can be put on hold' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
v_from := "better_supabase"."order_transition"(v_order.id, 'on_hold');
update "better_supabase"."orders" x set "held_from" = v_from, "held_at" = now(), "hold_reason" = nullif(trim(hold_order.reason), ''), "updated_at" = now()
where x."id" = v_order.id;
null;
return "better_supabase"."order_json"(v_order.id);
end;
$$;
revoke execute on function "better_supabase"."hold_order"(uuid, text) from public, anon;
grant execute on function "better_supabase"."hold_order"(uuid, text) to authenticated, service_role;
-- Releases a hold: the order goes back to the status it was on hold from.
create or replace function "better_supabase"."release_order_hold"(order_id uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
v_target text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = release_order_hold.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_order.status::text <> 'on_hold' then
raise exception 'the order isn''t on hold' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
select coalesce(x."held_from", 'confirmed') into v_target from "better_supabase"."orders" x where x."id" = v_order.id;
perform "better_supabase"."order_transition"(v_order.id, v_target);
update "better_supabase"."orders" x set "held_from" = null, "held_at" = null, "hold_reason" = null, "updated_at" = now()
where x."id" = v_order.id;
null;
return "better_supabase"."order_json"(v_order.id);
end;
$$;
revoke execute on function "better_supabase"."release_order_hold"(uuid) from public, anon;
grant execute on function "better_supabase"."release_order_hold"(uuid) to authenticated, service_role;
-- Puts an order into the variant group of another order (variant_of is the
-- group's first order), or takes it out of its group when variant_of is null.
-- Both orders belong to the same organization and use the same currency.
create or replace function "better_supabase"."set_order_variant"(id uuid, variant_of uuid, label text default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
v_root record;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_order from "better_supabase"."orders" x where x."id" = set_order_variant.id for update;
if not found then
raise exception 'no such order' using errcode = 'P0002', hint = 'ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order."tenant_id", 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if set_order_variant.variant_of is null then
update "better_supabase"."orders" x set "variant_of" = null, "variant_label" = null, "is_selected" = true
where x."id" = v_order."id";
else
select * into v_root from "better_supabase"."orders" x where x."id" = set_order_variant.variant_of for update;
if not found or v_root."tenant_id" <> v_order."tenant_id" then
raise exception 'no such order to group with' using errcode = 'P0002', hint = 'ORDER_NOT_FOUND';
end if;
if v_root."variant_of" is not null or v_root."id" = v_order."id" or exists (
select 1 from "better_supabase"."orders" s where s."variant_of" = v_order."id") then
raise exception 'group with the first order of a group, and not an order that has variants itself' using errcode = '22023', hint = 'ORDER_VARIANT_INVALID';
end if;
if v_root."currency" <> v_order."currency" then
raise exception 'variants share one currency' using errcode = '22023', hint = 'ORDER_VARIANT_INVALID';
end if;
update "better_supabase"."orders" x set "variant_of" = v_root."id",
"variant_label" = coalesce(nullif(trim(set_order_variant.label), ''), x."variant_label"),
"is_selected" = false
where x."id" = v_order."id";
update "better_supabase"."orders" x set "variant_label" = coalesce(nullif(x."variant_label", ''), 'Original')
where x."id" = v_root."id";
end if;
return (select jsonb_build_object('id', x."id", 'number', x."number", 'variantOf', x."variant_of", 'variantLabel', x."variant_label", 'isSelected', x."is_selected", 'total', x."total"::text, 'currency', x."currency") from "better_supabase"."orders" x where x."id" = v_order."id");
end;
$$;
revoke execute on function "better_supabase"."set_order_variant"(uuid, uuid, text) from public, anon;
grant execute on function "better_supabase"."set_order_variant"(uuid, uuid, text) to authenticated, service_role;
-- Makes an order the selected one of its variant group and unselects the others.
create or replace function "better_supabase"."select_order_variant"(id uuid)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select * into v_order from "better_supabase"."orders" x where x."id" = select_order_variant.id;
if not found then
raise exception 'no such order' using errcode = 'P0002', hint = 'ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order."tenant_id", 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if coalesce(v_order."variant_of", v_order."id") = v_order."id" and not exists (
select 1 from "better_supabase"."orders" s where s."variant_of" = v_order."id") then
raise exception 'this order has no variants' using errcode = '22023', hint = 'ORDER_VARIANT_INVALID';
end if;
if v_order."status" in ('cancelled', 'refunded') then
raise exception 'a cancelled or refunded order can''t be selected' using errcode = '55000', hint = 'ORDER_VARIANT_NOT_SELECTABLE';
end if;
perform 1 from "better_supabase"."orders" x where coalesce(x."variant_of", x."id") = coalesce(v_order."variant_of", v_order."id") order by x."id" for update;
update "better_supabase"."orders" x set "is_selected" = (x."id" = v_order."id")
where coalesce(x."variant_of", x."id") = coalesce(v_order."variant_of", v_order."id");
return jsonb_build_object('order', (select jsonb_build_object('id', x."id", 'number', x."number", 'variantOf', x."variant_of", 'variantLabel', x."variant_label", 'isSelected', x."is_selected", 'total', x."total"::text, 'currency', x."currency") from "better_supabase"."orders" x where x."id" = v_order."id"),
'variants', (select coalesce(jsonb_agg(jsonb_build_object('id', x."id", 'number', x."number", 'variantOf', x."variant_of", 'variantLabel', x."variant_label", 'isSelected', x."is_selected", 'total', x."total"::text, 'currency', x."currency") order by x."placed_at", x."id"), '[]'::jsonb)
from "better_supabase"."orders" x where coalesce(x."variant_of", x."id") = coalesce(v_order."variant_of", v_order."id")));
end;
$$;
revoke execute on function "better_supabase"."select_order_variant"(uuid) from public, anon;
grant execute on function "better_supabase"."select_order_variant"(uuid) to authenticated, service_role;
-- The orders of one variant group: the first order and its variants.
create or replace function "better_supabase"."list_order_variants"(id uuid)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
v_order record;
begin
select * into v_order from "better_supabase"."orders" x where x."id" = list_order_variants.id;
if not found then
raise exception 'no such order' using errcode = 'P0002', hint = 'ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order."tenant_id", 'orders.read'), false)) then
raise exception 'not allowed to read these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
return (select coalesce(jsonb_agg(jsonb_build_object('id', x."id", 'number', x."number", 'variantOf', x."variant_of", 'variantLabel', x."variant_label", 'isSelected', x."is_selected", 'total', x."total"::text, 'currency', x."currency") order by x."placed_at", x."id"), '[]'::jsonb)
from "better_supabase"."orders" x where coalesce(x."variant_of", x."id") = coalesce(v_order."variant_of", v_order."id"));
end;
$$;
revoke execute on function "better_supabase"."list_order_variants"(uuid) from public, anon;
grant execute on function "better_supabase"."list_order_variants"(uuid) to authenticated, service_role;
-- Fulfills lines of a paid or confirmed order from one location. lines is
-- [{lineId, quantity, detail?}]; null fulfills everything not yet
-- fulfilled. detail names the bin, lot or serial numbers a confirmed
-- order's line ships from, as for inventory's move_stock.
create or replace function "better_supabase"."create_fulfillment"(order_id uuid, lines jsonb default null, location uuid default null,
carrier text default null, tracking_number text default null, tracking_url text default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
v_item record;
v_requested jsonb;
v_id uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = create_fulfillment.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_order.status::text not in ('paid', 'confirmed') then
raise exception 'only a paid or confirmed order can be fulfilled' using errcode = '55000', hint = 'ORDERS_NOT_PAID';
end if;
if exists (select 1 from "better_supabase"."orders" x where x."id" = v_order.id and x."payment_required"
and x."paid_total" < coalesce(x."deposit_amount", x."total")) then
raise exception 'the order ships once its deposit, or its total without one, is paid' using errcode = '55000', hint = 'ORDERS_PAYMENT_REQUIRED';
end if;
declare
v_hook regprocedure := to_regprocedure('"public"."before_fulfillment_create"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_order.id;
end if;
end;
v_requested := coalesce(create_fulfillment.lines, (
select jsonb_agg(jsonb_build_object('lineId', l."id", 'quantity', l."quantity" - l."quantity_fulfilled") order by l."position")
from "better_supabase"."order_lines" l where l."order_id" = v_order.id and l."quantity" > l."quantity_fulfilled"));
if v_requested is null or jsonb_typeof(v_requested) <> 'array' or jsonb_array_length(v_requested) = 0 then
raise exception 'no lines left to fulfill' using errcode = '55000', hint = 'ORDERS_NOTHING_TO_FULFILL';
end if;
insert into "better_supabase"."order_fulfillments" ("tenant_id", "order_id", "location_id", "carrier", "tracking_number", "tracking_url")
values (v_order.tenant, v_order.id,
coalesce(create_fulfillment.location, (select l."location_id" from "better_supabase"."order_lines" l where l."order_id" = v_order.id and l."location_id" is not null order by l."position" limit 1)),
create_fulfillment.carrier, create_fulfillment.tracking_number, create_fulfillment.tracking_url)
returning "id" into v_id;
for v_item in
select (e ->> 'lineId')::uuid as line, case when (case when coalesce(e ->> 'quantity', '') ~ '^[0-9]{1,12}$' then (e ->> 'quantity')::numeric > 0 else false end) then (e ->> 'quantity')::numeric end as quantity,
case when jsonb_typeof(e -> 'detail') = 'object' then e -> 'detail' end as detail
from jsonb_array_elements(v_requested) e
loop
if v_item.quantity is null or v_item.quantity <= 0 then
raise exception 'each quantity is a whole number above zero' using errcode = '22023', hint = 'ORDERS_QUANTITY_INVALID';
end if;
update "better_supabase"."order_lines" x set "quantity_fulfilled" = x."quantity_fulfilled" + v_item.quantity
where x."id" = v_item.line and x."order_id" = v_order.id and x."quantity_fulfilled" + v_item.quantity <= x."quantity";
if not found then
raise exception 'a line isn''t on this order or has less left to fulfill' using errcode = '22023', hint = 'ORDERS_FULFILLMENT_INVALID';
end if;
insert into "better_supabase"."order_fulfillment_lines" as x ("tenant_id", "fulfillment_id", "order_line_id", "quantity", "detail")
values (v_order.tenant, v_id, v_item.line, v_item.quantity, v_item.detail)
on conflict ("fulfillment_id", "order_line_id") do update set "quantity" = x."quantity" + excluded."quantity",
"detail" = coalesce(excluded."detail", x."detail");
end loop;
declare
v_hook regprocedure := to_regprocedure('"public"."after_fulfillment_create"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_id;
end if;
end;
return jsonb_build_object('id', v_id, 'orderId', v_order.id, 'status', 'pending');
end;
$$;
revoke execute on function "better_supabase"."create_fulfillment"(uuid, jsonb, uuid, text, text, text) from public, anon;
grant execute on function "better_supabase"."create_fulfillment"(uuid, jsonb, uuid, text, text, text) to authenticated, service_role;
-- Marks a fulfillment shipped. A confirmed order's lines take their stock
-- through their allocations here. Once every line has shipped, the order
-- is fulfilled.
create or replace function "better_supabase"."ship_fulfillment"(fulfillment uuid, carrier text default null, tracking_number text default null, tracking_url text default null)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_fulfillment record;
v_order record;
v_line record;
v_claims text;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."id" as id, x."order_id" as order_id, x."status" as status into v_fulfillment
from "better_supabase"."order_fulfillments" x where x."id" = ship_fulfillment.fulfillment for update;
if not found then
raise exception 'fulfillment not found' using errcode = 'P0002', hint = 'ORDERS_FULFILLMENT_NOT_FOUND';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = v_fulfillment.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_fulfillment.status = 'shipped' then
return false;
end if;
if v_fulfillment.status <> 'pending' then
raise exception 'only a pending fulfillment can ship' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
update "better_supabase"."order_fulfillments" x set "status" = 'shipped', "shipped_at" = now(),
"carrier" = coalesce(ship_fulfillment.carrier, x."carrier"),
"tracking_number" = coalesce(ship_fulfillment.tracking_number, x."tracking_number"),
"tracking_url" = coalesce(ship_fulfillment.tracking_url, x."tracking_url")
where x."id" = v_fulfillment.id;
null;
if v_order.status::text in ('paid', 'confirmed')
and not exists (select 1 from "better_supabase"."order_lines" l where l."order_id" = v_order.id and l."quantity_fulfilled" < l."quantity")
and not exists (select 1 from "better_supabase"."order_fulfillments" x where x."order_id" = v_order.id and x."status" = 'pending') then
perform "better_supabase"."order_transition"(v_order.id, 'fulfilled');
null;
end if;
return true;
end;
$$;
revoke execute on function "better_supabase"."ship_fulfillment"(uuid, text, text, text) from public, anon;
grant execute on function "better_supabase"."ship_fulfillment"(uuid, text, text, text) to authenticated, service_role;
-- Marks a shipped fulfillment delivered, from staff or a carrier webhook.
-- Once everything is delivered, the order is completed.
create or replace function "better_supabase"."deliver_fulfillment"(fulfillment uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_fulfillment record;
v_order record;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."id" as id, x."order_id" as order_id, x."status" as status into v_fulfillment
from "better_supabase"."order_fulfillments" x where x."id" = deliver_fulfillment.fulfillment for update;
if not found then
raise exception 'fulfillment not found' using errcode = 'P0002', hint = 'ORDERS_FULFILLMENT_NOT_FOUND';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = v_fulfillment.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_fulfillment.status = 'delivered' then
return false;
end if;
if v_fulfillment.status <> 'shipped' then
raise exception 'only a shipped fulfillment can be delivered' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
update "better_supabase"."order_fulfillments" x set "status" = 'delivered', "delivered_at" = now() where x."id" = v_fulfillment.id;
null;
if v_order.status = 'fulfilled'
and not exists (select 1 from "better_supabase"."order_fulfillments" x where x."order_id" = v_order.id and x."status" in ('pending', 'shipped')) then
perform "better_supabase"."order_transition"(v_order.id, 'completed');
null;
end if;
return true;
end;
$$;
revoke execute on function "better_supabase"."deliver_fulfillment"(uuid) from public, anon;
grant execute on function "better_supabase"."deliver_fulfillment"(uuid) to authenticated, service_role;
-- Cancels a fulfillment that hasn't shipped; its lines can be fulfilled again.
create or replace function "better_supabase"."cancel_fulfillment"(fulfillment uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_fulfillment record;
v_order record;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."id" as id, x."order_id" as order_id, x."status" as status into v_fulfillment
from "better_supabase"."order_fulfillments" x where x."id" = cancel_fulfillment.fulfillment for update;
if not found then
raise exception 'fulfillment not found' using errcode = 'P0002', hint = 'ORDERS_FULFILLMENT_NOT_FOUND';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = v_fulfillment.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_fulfillment.status = 'cancelled' then
return false;
end if;
if v_fulfillment.status <> 'pending' then
raise exception 'a shipped fulfillment can''t be cancelled; request a return' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
update "better_supabase"."order_lines" x set "quantity_fulfilled" = x."quantity_fulfilled" - y."quantity"
from "better_supabase"."order_fulfillment_lines" y
where y."fulfillment_id" = v_fulfillment.id and x."id" = y."order_line_id";
update "better_supabase"."order_fulfillments" x set "status" = 'cancelled' where x."id" = v_fulfillment.id;
return true;
end;
$$;
revoke execute on function "better_supabase"."cancel_fulfillment"(uuid) from public, anon;
grant execute on function "better_supabase"."cancel_fulfillment"(uuid) to authenticated, service_role;
-- Requests a return of fulfilled lines. lines is [{lineId, quantity}].
-- The customer who placed the order and staff with orders.manage can.
create or replace function "better_supabase"."request_return"(order_id uuid, lines jsonb, reason text default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
v_item record;
v_id uuid;
v_open integer;
v_units integer := 0;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = request_return.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(v_order.customer = auth.uid(), false) or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to return this order' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_order.status not in ('paid', 'fulfilled', 'completed') then
raise exception 'only a paid order can be returned' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
if request_return.lines is null or jsonb_typeof(request_return.lines) <> 'array' or jsonb_array_length(request_return.lines) = 0 then
raise exception 'lines is a non-empty array of {lineId, quantity}' using errcode = '22023', hint = 'ORDERS_RETURN_INVALID';
end if;
insert into "better_supabase"."order_returns" ("tenant_id", "order_id", "reason")
values (v_order.tenant, v_order.id, request_return.reason)
returning "id" into v_id;
for v_item in
select (e ->> 'lineId')::uuid as line, case when (case when coalesce(e ->> 'quantity', '') ~ '^[0-9]{1,12}$' then (e ->> 'quantity')::numeric > 0 else false end) then (e ->> 'quantity')::numeric end as quantity from jsonb_array_elements(request_return.lines) e
loop
if v_item.quantity is null or v_item.quantity <= 0 then
raise exception 'each quantity is a whole number above zero' using errcode = '22023', hint = 'ORDERS_QUANTITY_INVALID';
end if;
select coalesce(sum(y."quantity"), 0) into v_open
from "better_supabase"."order_return_lines" y join "better_supabase"."order_returns" z on z."id" = y."return_id"
where y."order_line_id" = v_item.line and z."status" = 'requested';
if not exists (select 1 from "better_supabase"."order_lines" l where l."id" = v_item.line and l."order_id" = v_order.id
and l."quantity_fulfilled" - l."quantity_returned" - v_open >= v_item.quantity) then
raise exception 'a line isn''t on this order or has fewer fulfilled units left to return' using errcode = '22023', hint = 'ORDERS_RETURN_INVALID';
end if;
insert into "better_supabase"."order_return_lines" as x ("tenant_id", "return_id", "order_line_id", "quantity")
values (v_order.tenant, v_id, v_item.line, v_item.quantity)
on conflict ("return_id", "order_line_id") do update set "quantity" = x."quantity" + excluded."quantity";
v_units := v_units + v_item.quantity;
end loop;
null;
return jsonb_build_object('id', v_id, 'orderId', v_order.id, 'status', 'requested', 'quantity', v_units);
end;
$$;
revoke execute on function "better_supabase"."request_return"(uuid, jsonb, text) from public, anon;
grant execute on function "better_supabase"."request_return"(uuid, jsonb, text) to authenticated, service_role;
-- Receives a requested return. With restock, tracked units go back on hand
-- at location (or where the line was reserved). Refund it with create_refund.
create or replace function "better_supabase"."receive_return"(return_id uuid, restock boolean default true, location uuid default null)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_return record;
v_order record;
v_line record;
v_units integer := 0;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."id" as id, x."order_id" as order_id, x."status" as status into v_return
from "better_supabase"."order_returns" x where x."id" = receive_return.return_id for update;
if not found then
raise exception 'return not found' using errcode = 'P0002', hint = 'ORDERS_RETURN_NOT_FOUND';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = v_return.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_return.status = 'received' then
return false;
end if;
if v_return.status <> 'requested' then
raise exception 'only a requested return can be received' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
for v_line in
select y."order_line_id" as line, y."quantity" as quantity, l."variant_id" as variant, l."location_id" as location
from "better_supabase"."order_return_lines" y join "better_supabase"."order_lines" l on l."id" = y."order_line_id"
where y."return_id" = v_return.id
loop
update "better_supabase"."order_lines" x set "quantity_returned" = x."quantity_returned" + v_line.quantity where x."id" = v_line.line;
v_units := v_units + v_line.quantity;
end loop;
update "better_supabase"."order_returns" x set "status" = 'received', "received_at" = now() where x."id" = v_return.id;
null;
return true;
end;
$$;
revoke execute on function "better_supabase"."receive_return"(uuid, boolean, uuid) from public, anon;
grant execute on function "better_supabase"."receive_return"(uuid, boolean, uuid) to authenticated, service_role;
create or replace function "better_supabase"."reject_return"(return_id uuid)
returns boolean
language plpgsql
security definer
set search_path = ''
as $$
declare
v_return record;
v_order record;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."id" as id, x."order_id" as order_id, x."status" as status into v_return
from "better_supabase"."order_returns" x where x."id" = reject_return.return_id for update;
if not found then
raise exception 'return not found' using errcode = 'P0002', hint = 'ORDERS_RETURN_NOT_FOUND';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = v_return.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if v_return.status <> 'requested' then
raise exception 'only a requested return can be rejected' using errcode = '55000', hint = 'ORDERS_TRANSITION_INVALID';
end if;
update "better_supabase"."order_returns" x set "status" = 'rejected' where x."id" = v_return.id;
return true;
end;
$$;
revoke execute on function "better_supabase"."reject_return"(uuid) from public, anon;
grant execute on function "better_supabase"."reject_return"(uuid) to authenticated, service_role;
-- Starts a refund against the order's captured payment: amount null
-- refunds what is left. The TypeScript block sends it to the payment
-- provider and finishes it with complete_refund.
create or replace function "better_supabase"."create_refund"(order_id uuid, amount bigint default null, return_id uuid default null, reason text default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_order record;
v_pending bigint;
v_left bigint;
v_amount bigint;
v_payment record;
v_id uuid;
begin
if not (better_supabase.actor_may_write()) then
raise exception 'This session is read-only' using errcode = '42501', hint = 'ACTOR_READ_ONLY';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = create_refund.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if not (auth.role() = 'service_role' or coalesce(better_supabase.can('tenant', v_order.tenant, 'orders.manage'), false)) then
raise exception 'not allowed to manage these orders' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
declare
v_hook regprocedure := to_regprocedure('"public"."before_refund_create"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_order.id;
end if;
end;
if create_refund.return_id is not null and not exists (
select 1 from "better_supabase"."order_returns" x where x."id" = create_refund.return_id and x."order_id" = v_order.id) then
raise exception 'return not found on this order' using errcode = 'P0002', hint = 'ORDERS_RETURN_NOT_FOUND';
end if;
select coalesce(sum(x."amount"), 0) into v_pending from "better_supabase"."order_refunds" x
where x."order_id" = v_order.id and x."status" = 'pending';
v_left := v_order.paid_total - v_order.refunded_total - v_pending;
v_amount := coalesce(create_refund.amount, v_left);
if v_amount <= 0 or v_amount > v_left then
raise exception 'the amount is above zero and at most what was paid and not refunded yet' using errcode = '22023', hint = 'ORDERS_REFUND_INVALID';
end if;
select y."id" as id, y."provider" as provider, y."reference" as reference into v_payment
from "better_supabase"."order_payments" y
where y."order_id" = v_order.id and y."status" = 'succeeded'
and y."captured" - y."refunded" - coalesce((select sum(z."amount") from "better_supabase"."order_refunds" z
where z."payment_id" = y."id" and z."status" = 'pending'), 0) >= v_amount
order by y."created_at" desc
limit 1;
if not found then
raise exception 'no single captured payment covers this amount; refund in parts' using errcode = '22023', hint = 'ORDERS_REFUND_INVALID';
end if;
insert into "better_supabase"."order_refunds" ("tenant_id", "order_id", "payment_id", "return_id", "amount", "currency", "reason")
values (v_order.tenant, v_order.id, v_payment.id, create_refund.return_id, v_amount, v_order.currency, create_refund.reason)
returning "id" into v_id;
declare
v_hook regprocedure := to_regprocedure('"public"."after_refund_create"(uuid)');
begin
if v_hook is not null then
execute format('select %s($1::uuid)', v_hook::oid::regproc)
using v_id;
end if;
end;
return jsonb_build_object('id', v_id, 'orderId', v_order.id, 'paymentId', v_payment.id, 'provider', v_payment.provider,
'paymentReference', v_payment.reference, 'amount', v_amount::text, 'currency', v_order.currency, 'status', 'pending');
end;
$$;
revoke execute on function "better_supabase"."create_refund"(uuid, bigint, uuid, text) from public, anon;
grant execute on function "better_supabase"."create_refund"(uuid, bigint, uuid, text) to authenticated, service_role;
-- Records the payment provider's answer to a refund, found by its id or,
-- from a webhook, by the provider's refund id. pending only stores that
-- id. A succeeded refund adds to the order's refunded total and records
-- order.refunded; once everything paid is refunded, the order is
-- refunded. Repeating it changes nothing.
create or replace function "better_supabase"."complete_refund"(refund uuid, status text, provider_reference text default null)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_refund record;
v_order record;
v_total bigint;
begin
if auth.role() is distinct from 'service_role' then
raise exception 'only the service role can do this' using errcode = '42501', hint = 'ORDERS_FORBIDDEN';
end if;
if complete_refund.status is null or complete_refund.status not in ('pending', 'succeeded', 'failed') then
raise exception 'status is pending, succeeded or failed' using errcode = '22023', hint = 'ORDERS_REFUND_STATUS_INVALID';
end if;
select x."id" as id, x."order_id" as order_id, x."payment_id" as payment, x."amount" as amount,
x."status" as status
into v_refund from "better_supabase"."order_refunds" x
where (complete_refund.refund is not null and x."id" = complete_refund.refund)
or (complete_refund.refund is null and x."provider_reference" = complete_refund.provider_reference)
for update;
if not found then
raise exception 'refund not found' using errcode = 'P0002', hint = 'ORDERS_REFUND_NOT_FOUND';
end if;
select x."id" as id, x."tenant_id" as tenant, x."number" as number, x."customer_id" as customer, x."status" as status, x."currency" as currency, x."total" as total, x."paid_total" as paid_total, x."refunded_total" as refunded_total, x."tax_total" as tax_total, x."tax_inclusive" as tax_inclusive, x."invoice_number" as invoice_number, x."placed_at" as placed_at into v_order from "better_supabase"."orders" x where x."id" = v_refund.order_id for update;
if not found then
raise exception 'order not found' using errcode = 'P0002', hint = 'ORDERS_ORDER_NOT_FOUND';
end if;
if v_refund.status = 'pending' and complete_refund.status = 'pending' then
update "better_supabase"."order_refunds" x set "provider_reference" = coalesce(complete_refund.provider_reference, x."provider_reference")
where x."id" = v_refund.id;
elsif v_refund.status = 'pending' then
update "better_supabase"."order_refunds" x set "status" = complete_refund.status, "completed_at" = now(),
"provider_reference" = coalesce(complete_refund.provider_reference, x."provider_reference")
where x."id" = v_refund.id;
if complete_refund.status = 'succeeded' then
update "better_supabase"."order_payments" x set "refunded" = x."refunded" + v_refund.amount, "updated_at" = now()
where x."id" = v_refund.payment;
update "better_supabase"."orders" x set "refunded_total" = x."refunded_total" + v_refund.amount, "updated_at" = now()
where x."id" = v_order.id
returning x."refunded_total" into v_total;
if v_total >= v_order.paid_total and v_order.status in ('paid', 'fulfilled', 'completed') then
perform "better_supabase"."order_transition"(v_order.id, 'refunded');
end if;
null;
end if;
end if;
return (select jsonb_build_object('id', x."id", 'orderId', x."order_id", 'status', x."status",
'amount', x."amount"::text, 'orderStatus', y."status", 'refundedTotal', y."refunded_total"::text)
from "better_supabase"."order_refunds" x join "better_supabase"."orders" y on y."id" = x."order_id" where x."id" = v_refund.id);
end;
$$;
revoke execute on function "better_supabase"."complete_refund"(uuid, text, text) from public, anon, authenticated;
grant execute on function "better_supabase"."complete_refund"(uuid, text, text) to service_role;
-- The import target of the order entity (ADR 0141): item.data is { number,
-- status?, currency, customerId?, email?, taxInclusive?, placedAt?, paidAt?,
-- fulfilledAt?, completedAt?, cancelledAt?, shippingAddress?, billingAddress?,
-- buyer?, legalEntityId?, taxLines?, totals?, lines, payments? }. A line is {
-- variantId?, productId?, sku?, title, quantity, unitAmount, discount?, tax?,
-- locationId?, taxCodeId?, unitCost?, costSource?, binId?, billingParty?,
-- billingPartyId?, billingAccountId? }; a line without a variant is free
-- text. Amounts are minor units as text. totals is { subtotal, discountTotal,
-- shippingTotal, taxTotal, total } and is kept as given; a difference from
-- the lines is a warning. An order that is already imported is kept.
create or replace function "better_supabase"."import_order"(run uuid, item jsonb, subject_id text)
returns jsonb
language plpgsql
security definer
set search_path = ''
as $$
declare
v_tenant uuid := (import_order.item ->> 'tenant')::uuid;
v_data jsonb := import_order.item -> 'data';
v_number text := nullif(btrim(v_data ->> 'number'), '');
v_seq bigint;
v_id uuid;
v_inclusive boolean := coalesce((v_data ->> 'taxInclusive')::boolean, false);
v_subtotal bigint;
v_discount bigint;
v_tax bigint;
v_shipping bigint;
v_total bigint;
v_paid bigint := 0;
v_paid_at timestamptz;
v_given jsonb := v_data -> 'totals';
v_warnings jsonb := '[]'::jsonb;
v_updated timestamptz;
v_pay jsonb;
v_index integer := 0;
begin
if import_order.subject_id is not null then
select x."updated_at" into v_updated from "better_supabase"."orders" x where x."id" = import_order.subject_id::uuid and x."tenant_id" = v_tenant;
if not found then
raise exception 'the order no longer exists' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
if coalesce(v_data ->> 'update', 'keep') is distinct from 'apply' then
return jsonb_build_object('subjectId', import_order.subject_id, 'updatedAt', v_updated, 'warnings', jsonb_build_array(jsonb_build_object('code', 'IMPORT_DOCUMENT_CHANGED', 'message', 'the source order changed after it was imported; an imported order is kept as imported')));
end if;
v_id := import_order.subject_id::uuid;
update "better_supabase"."orders" x set
"status" = case when x."status"::text in ('cancelled', 'paid') and coalesce((v_data ->> 'repost')::boolean, false) = false then x."status" else coalesce(nullif(v_data ->> 'status', ''), x."status"::text) end,
"customer_id" = coalesce(nullif(v_data ->> 'customerId', '')::uuid, x."customer_id"),
"currency" = coalesce(nullif(upper(v_data ->> 'currency'), ''), x."currency"),
"placed_at" = coalesce(nullif(v_data ->> 'placedAt', '')::timestamptz, x."placed_at"),
"subtotal" = coalesce((v_data -> 'totals' ->> 'subtotal')::bigint, x."subtotal"),
"tax_total" = coalesce((v_data -> 'totals' ->> 'taxTotal')::bigint, x."tax_total"),
"total" = coalesce((v_data -> 'totals' ->> 'total')::bigint, x."total")
where x."id" = v_id;
if coalesce((v_data ->> 'repost')::boolean, false) or (select x."status"::text from "better_supabase"."orders" x where x."id" = v_id) not in ('cancelled', 'paid') then
if (select count(*) from "better_supabase"."order_lines" l where l."order_id" = v_id) = jsonb_array_length(coalesce(v_data -> 'lines', '[]'::jsonb)) then
update "better_supabase"."order_lines" l set
"title" = coalesce(nullif(e.l ->> 'title', ''), l."title"),
"tax" = coalesce((e.l ->> 'tax')::bigint, l."tax")
from jsonb_array_elements(v_data -> 'lines') with ordinality as e(l, n)
where l."order_id" = v_id and l."position" = e.n::integer;
elsif jsonb_typeof(v_data -> 'lines') = 'array' then
delete from "better_supabase"."order_lines" l where l."order_id" = v_id;
insert into "better_supabase"."order_lines" ("tenant_id", "order_id", "position", "variant_id", "product_id", "sku", "title", "quantity", "unit_amount", "subtotal", "discount", "tax", "total", "requires_shipping", "location_id", "tax_code_id", "unit_cost", "cost_source", "bin_id", "billing_party", "billing_party_id", "billing_account_id")
select v_tenant, v_id, e.n::integer, nullif(e.l ->> 'variantId', '')::uuid, nullif(e.l ->> 'productId', '')::uuid, coalesce(e.l ->> 'sku', ''), e.l ->> 'title', (e.l ->> 'quantity')::numeric, (e.l ->> 'unitAmount')::bigint, coalesce((e.l ->> 'subtotal')::bigint, round((e.l ->> 'unitAmount')::bigint * (e.l ->> 'quantity')::numeric)::bigint), coalesce((e.l ->> 'discount')::bigint, 0), coalesce((e.l ->> 'tax')::bigint, 0), coalesce((e.l ->> 'total')::bigint, round((e.l ->> 'unitAmount')::bigint * (e.l ->> 'quantity')::numeric)::bigint - coalesce((e.l ->> 'discount')::bigint, 0)), coalesce((e.l ->> 'requiresShipping')::boolean, false), nullif(e.l ->> 'locationId', '')::uuid, nullif(e.l ->> 'taxCodeId', '')::uuid, (e.l ->> 'unitCost')::bigint, nullif(e.l ->> 'costSource', ''), nullif(e.l ->> 'binId', '')::uuid, coalesce(nullif(e.l ->> 'billingParty', ''), 'customer'), nullif(e.l ->> 'billingPartyId', ''), nullif(e.l ->> 'billingAccountId', '')::uuid
from jsonb_array_elements(v_data -> 'lines') with ordinality as e(l, n);
end if;
end if;
select x."updated_at" into v_updated from "better_supabase"."orders" x where x."id" = v_id;
return jsonb_build_object('subjectId', v_id::text, 'updatedAt', v_updated, 'warnings', v_warnings);
end if;
if v_number is null then
raise exception 'an order needs its number' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
if coalesce(v_data ->> 'currency', '') !~ '^[A-Za-z]{3}$' then
raise exception 'currency is an ISO 4217 code' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
if jsonb_typeof(v_data -> 'lines') is distinct from 'array' or jsonb_array_length(v_data -> 'lines') = 0 then
raise exception 'an order needs at least one line' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
if exists (
select 1 from jsonb_array_elements(v_data -> 'lines') l
where coalesce(l.value ->> 'title', '') = '' or not (case when coalesce(l.value ->> 'quantity', '') ~ '^[0-9]{1,12}$' then (l.value ->> 'quantity')::numeric > 0 else false end) or coalesce(l.value ->> 'unitAmount', '') !~ '^[0-9]{1,18}$'
) then
raise exception 'a line needs a title, a whole quantity above zero and a unitAmount' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
if exists (
select 1 from jsonb_array_elements(v_data -> 'lines') l
where l.value ->> 'billingParty' is not null and l.value ->> 'billingParty' not in ('customer', 'internal', 'own_warranty', 'supplier_warranty', 'cost_account')
or l.value ->> 'costSource' is not null and l.value ->> 'costSource' not in ('catalog', 'average', 'manual', 'imported')
) then
raise exception 'a line''s billingParty or costSource is not a known value' using errcode = '22023', hint = 'IMPORT_ITEM_INVALID';
end if;
select coalesce(sum(round((l.value ->> 'unitAmount')::bigint * (l.value ->> 'quantity')::numeric)::bigint), 0),
coalesce(sum(coalesce((l.value ->> 'discount')::bigint, 0)), 0),
coalesce(sum(coalesce((l.value ->> 'tax')::bigint, 0)), 0)
into v_subtotal, v_discount, v_tax
from jsonb_array_elements(v_data -> 'lines') l;
v_shipping := coalesce((v_given ->> 'shippingTotal')::bigint, 0);
v_total := v_subtotal - v_discount + v_shipping + case when v_inclusive then 0 else v_tax end;
if jsonb_typeof(v_given) = 'object' then
if coalesce((v_given ->> 'subtotal')::bigint, v_subtotal) <> v_subtotal
or coalesce((v_given ->> 'discountTotal')::bigint, v_discount) <> v_discount
or coalesce((v_given ->> 'taxTotal')::bigint, v_tax) <> v_tax
or coalesce((v_given ->> 'total')::bigint, v_total) <> v_total then
v_warnings := v_warnings || jsonb_build_array(jsonb_build_object('code', 'IMPORT_TOTALS_MISMATCH', 'message', 'the stored totals differ from the totals computed from the lines; the stored totals were kept',
'computed', jsonb_build_object('subtotal', v_subtotal::text, 'discountTotal', v_discount::text, 'taxTotal', v_tax::text, 'total', v_total::text),
'given', v_given));
end if;
v_subtotal := coalesce((v_given ->> 'subtotal')::bigint, v_subtotal);
v_discount := coalesce((v_given ->> 'discountTotal')::bigint, v_discount);
v_tax := coalesce((v_given ->> 'taxTotal')::bigint, v_tax);
v_total := coalesce((v_given ->> 'total')::bigint, v_total);
end if;
if jsonb_typeof(v_data -> 'payments') = 'array' then
select coalesce(sum((p.value ->> 'amount')::bigint), 0), max((p.value ->> 'paidAt')::timestamptz)
into v_paid, v_paid_at from jsonb_array_elements(v_data -> 'payments') p;
end if;
if nullif(btrim(v_data ->> 'number'), '') is not null then
perform set_config('better_supabase.fixed_number', 'order' || chr(31) || btrim(v_data ->> 'number'), true);
end if;
if (case when split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 1) = 'order' then nullif(split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 2), '') end) is not null then
v_number := (case when split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 1) = 'order' then nullif(split_part(coalesce(current_setting('better_supabase.fixed_number', true), ''), chr(31), 2), '') end);
if better_supabase.import_run() is null then
raise exception 'a fixed number is only accepted during an import' using errcode = '42501', hint = 'NUMBER_FIXED_FORBIDDEN';
end if;
v_seq := coalesce(substring(v_number from '([0-9]+)$')::bigint, 0);
perform set_config('better_supabase.fixed_number', '', true);
else
perform pg_catalog.pg_advisory_xact_lock(pg_catalog.hashtextextended(v_tenant::text || ':commerce:order', 0));
select coalesce(max(x."sequence"), 0) + 1 into v_seq from "better_supabase"."orders" x where x."tenant_id" = v_tenant;
v_number := v_seq::text;
end if;
insert into "better_supabase"."orders" ("tenant_id", "number", "sequence", "customer_id", "email", "status", "currency", "subtotal", "discount_total", "shipping_total", "tax_total", "total", "paid_total", "tax_inclusive", "shipping_address", "billing_address", "tax", "invoice_number", "invoice_sequence", "invoiced_at", "placed_at", "paid_at", "fulfilled_at", "completed_at", "cancelled_at", "cancel_reason", "buyer")
values (v_tenant, v_number, v_seq, nullif(v_data ->> 'customerId', '')::uuid, v_data ->> 'email', coalesce(v_data ->> 'status', 'confirmed'), upper(v_data ->> 'currency'), v_subtotal, v_discount, v_shipping, v_tax, v_total, v_paid, v_inclusive, v_data -> 'shippingAddress', v_data -> 'billingAddress', v_data -> 'taxLines', nullif(v_data ->> 'invoiceNumber', ''), substring(nullif(v_data ->> 'invoiceNumber', '') from '([0-9]+)$')::bigint, nullif(v_data ->> 'invoicedAt', '')::timestamptz, coalesce(nullif(v_data ->> 'placedAt', '')::timestamptz, now()), coalesce(nullif(v_data ->> 'paidAt', '')::timestamptz, v_paid_at), nullif(v_data ->> 'fulfilledAt', '')::timestamptz, nullif(v_data ->> 'completedAt', '')::timestamptz, nullif(v_data ->> 'cancelledAt', '')::timestamptz, nullif(v_data ->> 'cancelReason', ''), case when jsonb_typeof(v_data -> 'buyer') = 'object' then v_data -> 'buyer' end)
returning "id", "updated_at" into v_id, v_updated;
insert into "better_supabase"."order_lines" ("tenant_id", "order_id", "position", "variant_id", "product_id", "sku", "title", "quantity", "unit_amount", "subtotal", "discount", "tax", "total", "requires_shipping", "location_id", "tax_code_id", "unit_cost", "cost_source", "bin_id", "billing_party", "billing_party_id", "billing_account_id")
select v_tenant, v_id, e.n::integer, nullif(e.l ->> 'variantId', '')::uuid, nullif(e.l ->> 'productId', '')::uuid, coalesce(e.l ->> 'sku', ''), e.l ->> 'title', (e.l ->> 'quantity')::numeric, (e.l ->> 'unitAmount')::bigint, coalesce((e.l ->> 'subtotal')::bigint, round((e.l ->> 'unitAmount')::bigint * (e.l ->> 'quantity')::numeric)::bigint), coalesce((e.l ->> 'discount')::bigint, 0), coalesce((e.l ->> 'tax')::bigint, 0), coalesce((e.l ->> 'total')::bigint, round((e.l ->> 'unitAmount')::bigint * (e.l ->> 'quantity')::numeric)::bigint - coalesce((e.l ->> 'discount')::bigint, 0)), coalesce((e.l ->> 'requiresShipping')::boolean, false), nullif(e.l ->> 'locationId', '')::uuid, nullif(e.l ->> 'taxCodeId', '')::uuid, (e.l ->> 'unitCost')::bigint, nullif(e.l ->> 'costSource', ''), nullif(e.l ->> 'binId', '')::uuid, coalesce(nullif(e.l ->> 'billingParty', ''), 'customer'), nullif(e.l ->> 'billingPartyId', ''), nullif(e.l ->> 'billingAccountId', '')::uuid
from jsonb_array_elements(v_data -> 'lines') with ordinality as e(l, n);
if jsonb_typeof(v_data -> 'payments') = 'array' then
for v_pay in select e.value from jsonb_array_elements(v_data -> 'payments') e loop
v_index := v_index + 1;
insert into "better_supabase"."order_payments" ("tenant_id", "order_id", "provider", "reference", "status", "currency", "amount", "captured", "refunded", "created_at", "updated_at")
values (v_tenant, v_id, coalesce(nullif(v_pay ->> 'provider', ''), 'import'),
coalesce(nullif(v_pay ->> 'reference', ''), import_order.item ->> 'key' || ':payment:' || v_index::text),
'succeeded', upper(v_data ->> 'currency'), (v_pay ->> 'amount')::bigint, (v_pay ->> 'amount')::bigint, 0,
coalesce(nullif(v_pay ->> 'paidAt', '')::timestamptz, now()), coalesce(nullif(v_pay ->> 'paidAt', '')::timestamptz, now()));
end loop;
end if;
return jsonb_build_object('subjectId', v_id::text, 'updatedAt', v_updated, 'warnings', v_warnings);
end;
$$;
revoke execute on function "better_supabase"."import_order"(uuid, jsonb, text) from public, anon, authenticated;
grant execute on function "better_supabase"."import_order"(uuid, jsonb, text) to service_role;
-- When an order last changed, for the conflict check of imports.
create or replace function "better_supabase"."order_updated_at"(subject_id text)
returns timestamptz
language sql
stable
security definer
set search_path = ''
as $$
select x."updated_at" from "better_supabase"."orders" x where x."id" = $1::uuid
$$;
revoke execute on function "better_supabase"."order_updated_at"(text) from public, anon, authenticated;
grant execute on function "better_supabase"."order_updated_at"(text) to service_role;
-- The orders page in one call: a column per status with its count, its totals
-- per currency and its latest orders. Amounts are text.
create or replace function "better_supabase"."order_status_board"(tenant uuid, per_status integer default 20)
returns jsonb
language sql
stable
security invoker
set search_path = ''
as $$
select jsonb_build_object('columns', coalesce(jsonb_agg(col order by rank, col ->> 'status'), '[]'::jsonb))
from (
select
array_position(array['draft', 'pending_payment', 'confirmed', 'on_hold', 'paid', 'fulfilled', 'completed', 'cancelled', 'refunded'], g.raw::text) as rank,
jsonb_build_object(
'status', g.label,
'count', g.n,
'totals', g.totals,
'orders', coalesce((
select jsonb_agg(jsonb_build_object(
'id', x."id", 'number', x."number", 'customerId', x."customer_id",
'currency', x."currency", 'total', x."total"::text,
'paidTotal', x."paid_total"::text, 'placedAt', x."placed_at") order by x."placed_at" desc, x."id")
from (
select * from "better_supabase"."orders" y
where y."tenant_id" = order_status_board.tenant and y."status" = g.raw
order by y."placed_at" desc, y."id"
limit greatest(coalesce(order_status_board.per_status, 20), 0)
) x), '[]'::jsonb)
) as col
from (
select s.raw, s.label, sum(s.n)::integer as n,
jsonb_agg(jsonb_build_object('currency', s.currency, 'total', s.total::text, 'paid', s.paid::text) order by s.currency) as totals
from (
select z."status" as raw, z."status" as label,
z."currency" as currency, count(*) as n,
sum(z."total") as total, sum(z."paid_total") as paid
from "better_supabase"."orders" z
where z."tenant_id" = order_status_board.tenant
group by z."status", z."currency"
) s
group by s.raw, s.label
) g
) cols
$$;
revoke execute on function "better_supabase"."order_status_board"(uuid, integer) from public, anon;
grant execute on function "better_supabase"."order_status_board"(uuid, integer) to authenticated, service_role;
-- The organization's orders as a keyset page, newest first (ADR 1077). Row
-- security decides what the caller sees. filters is { status, customerId,
-- partyId, channel, source, currency (each a value or a list), from and to
-- (placed at), search (number, email, external id), sort (placedAt, number,
-- total), direction, facet, count, limit, after, offset, paged, total,
-- totalCap }. Amounts are text.
create or replace function "better_supabase"."page_orders"(tenant uuid, filters jsonb default '{}')
returns jsonb
language plpgsql
stable
security invoker
set search_path = ''
as $$
#variable_conflict use_variable
declare
v_filters jsonb := coalesce(page_orders.filters, '{}'::jsonb);
v_search text := case when nullif(trim(v_filters ->> 'search'), '') is null then null
else '%' || replace(replace(replace(trim(v_filters ->> 'search'), '\', '\\'), '%', '\%'), '_', '\_') || '%' end;
v_limit integer := least(greatest(coalesce((v_filters ->> 'limit')::integer, 50), 1), 200);
v_offset integer := greatest(coalesce((v_filters ->> 'offset')::integer, 0), 0);
v_after jsonb := v_filters -> 'after';
v_paged boolean := coalesce((v_filters ->> 'paged')::boolean, false);
v_total_cap integer := least(greatest(coalesce((v_filters ->> 'totalCap')::integer, 10000), 1), 100000);
v_items jsonb;
v_more boolean;
v_next jsonb;
v_total integer;
v_sort text := nullif(v_filters ->> 'sort', '');
v_dir text := nullif(v_filters ->> 'direction', '');
v_facet text := nullif(v_filters ->> 'facet', '');
v_count boolean := coalesce((v_filters ->> 'count')::boolean, false);
v_result jsonb;
begin
if v_filters ? 'after' and v_filters ? 'offset' then
raise exception 'after and offset are two ways to page; pass one' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
if v_facet is not null then
if v_facet not in ('status', 'currency', 'channel') then
raise exception 'facet is one of status, currency, channel' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
with s as (
select (case v_facet when 'status' then (x."status")::text when 'currency' then (x."currency")::text when 'channel' then (x."channel")::text end) as v
from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
limit v_total_cap + 1
)
select jsonb_build_object('facet', v_facet, 'values', coalesce(jsonb_agg(jsonb_build_object('value', g.v, 'count', jsonb_build_object('value', g.n, 'exact', g.exact)) order by g.n desc, g.v), '[]'::jsonb))
into v_result
from (
select s.v, count(*) as n, (select count(*) from s) <= v_total_cap as exact
from s where s.v is not null group by s.v order by n desc, s.v limit 100
) g;
return v_result;
end if;
if v_count then
select count(*) into v_total from (
select 1 from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
limit v_total_cap + 1
) c;
return jsonb_build_object('value', least(v_total, v_total_cap), 'exact', v_total <= v_total_cap);
end if;
if v_sort is not null and v_sort not in ('placedAt', 'number', 'total') then
raise exception 'sort is one of placedAt, number, total' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
if v_dir is not null and v_dir not in ('asc', 'desc') then
raise exception 'direction is asc or desc' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
if v_sort is null and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object(
'id', x."id", 'number', x."number", 'status', x."status",
'customerId', x."customer_id", 'currency', x."currency", 'total', x."total"::text,
'paidTotal', x."paid_total"::text, 'placedAt', x."placed_at") || jsonb_build_object('party', case when x."party_id" is not null then jsonb_build_object('type', x."party_type", 'id', x."party_id") end, 'channel', x."channel", 'source', x."source", 'externalId', x."external_id", 'invoicingPolicy', x."invoicing_policy", 'deliveryMethod', x."delivery_method", 'quoteId', x."quote_id", 'orderDiscount', x."order_discount"::text, 'promotionId', x."promotion_id", 'paymentRequired', x."payment_required", 'deposit', x."deposit_amount"::text) as j, jsonb_build_array((x."placed_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
and (v_after is null or (x."placed_at" >= (v_after ->> 0)::timestamptz and ((x."placed_at" > (v_after ->> 0)::timestamptz) or (x."placed_at" = (v_after ->> 0)::timestamptz and x."id" < (v_after ->> 1)::uuid))))
order by x."placed_at" asc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort is null and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object(
'id', x."id", 'number', x."number", 'status', x."status",
'customerId', x."customer_id", 'currency', x."currency", 'total', x."total"::text,
'paidTotal', x."paid_total"::text, 'placedAt', x."placed_at") || jsonb_build_object('party', case when x."party_id" is not null then jsonb_build_object('type', x."party_type", 'id', x."party_id") end, 'channel', x."channel", 'source', x."source", 'externalId', x."external_id", 'invoicingPolicy', x."invoicing_policy", 'deliveryMethod', x."delivery_method", 'quoteId', x."quote_id", 'orderDiscount', x."order_discount"::text, 'promotionId', x."promotion_id", 'paymentRequired', x."payment_required", 'deposit', x."deposit_amount"::text) as j, jsonb_build_array((x."placed_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
and (v_after is null or (x."placed_at", x."id") < ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by x."placed_at" desc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'placedAt' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object(
'id', x."id", 'number', x."number", 'status', x."status",
'customerId', x."customer_id", 'currency', x."currency", 'total', x."total"::text,
'paidTotal', x."paid_total"::text, 'placedAt', x."placed_at") || jsonb_build_object('party', case when x."party_id" is not null then jsonb_build_object('type', x."party_type", 'id', x."party_id") end, 'channel', x."channel", 'source', x."source", 'externalId', x."external_id", 'invoicingPolicy', x."invoicing_policy", 'deliveryMethod', x."delivery_method", 'quoteId', x."quote_id", 'orderDiscount', x."order_discount"::text, 'promotionId', x."promotion_id", 'paymentRequired', x."payment_required", 'deposit', x."deposit_amount"::text) as j, jsonb_build_array((x."placed_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
and (v_after is null or (x."placed_at" >= (v_after ->> 0)::timestamptz and ((x."placed_at" > (v_after ->> 0)::timestamptz) or (x."placed_at" = (v_after ->> 0)::timestamptz and x."id" < (v_after ->> 1)::uuid))))
order by x."placed_at" asc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'placedAt' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object(
'id', x."id", 'number', x."number", 'status', x."status",
'customerId', x."customer_id", 'currency', x."currency", 'total', x."total"::text,
'paidTotal', x."paid_total"::text, 'placedAt', x."placed_at") || jsonb_build_object('party', case when x."party_id" is not null then jsonb_build_object('type', x."party_type", 'id', x."party_id") end, 'channel', x."channel", 'source', x."source", 'externalId', x."external_id", 'invoicingPolicy', x."invoicing_policy", 'deliveryMethod', x."delivery_method", 'quoteId', x."quote_id", 'orderDiscount', x."order_discount"::text, 'promotionId', x."promotion_id", 'paymentRequired', x."payment_required", 'deposit', x."deposit_amount"::text) as j, jsonb_build_array((x."placed_at")::text, (x."id")::text) as k
from (
select * from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
and (v_after is null or (x."placed_at", x."id") < ((v_after ->> 0)::timestamptz, (v_after ->> 1)::uuid))
order by x."placed_at" desc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'number' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object(
'id', x."id", 'number', x."number", 'status', x."status",
'customerId', x."customer_id", 'currency', x."currency", 'total', x."total"::text,
'paidTotal', x."paid_total"::text, 'placedAt', x."placed_at") || jsonb_build_object('party', case when x."party_id" is not null then jsonb_build_object('type', x."party_type", 'id', x."party_id") end, 'channel', x."channel", 'source', x."source", 'externalId', x."external_id", 'invoicingPolicy', x."invoicing_policy", 'deliveryMethod', x."delivery_method", 'quoteId', x."quote_id", 'orderDiscount', x."order_discount"::text, 'promotionId', x."promotion_id", 'paymentRequired', x."payment_required", 'deposit', x."deposit_amount"::text) as j, jsonb_build_array((x."sequence")::text, (x."id")::text) as k
from (
select * from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
and (v_after is null or (x."sequence" >= (v_after ->> 0)::bigint and ((x."sequence" > (v_after ->> 0)::bigint) or (x."sequence" = (v_after ->> 0)::bigint and x."id" < (v_after ->> 1)::uuid))))
order by x."sequence" asc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'number' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object(
'id', x."id", 'number', x."number", 'status', x."status",
'customerId', x."customer_id", 'currency', x."currency", 'total', x."total"::text,
'paidTotal', x."paid_total"::text, 'placedAt', x."placed_at") || jsonb_build_object('party', case when x."party_id" is not null then jsonb_build_object('type', x."party_type", 'id', x."party_id") end, 'channel', x."channel", 'source', x."source", 'externalId', x."external_id", 'invoicingPolicy', x."invoicing_policy", 'deliveryMethod', x."delivery_method", 'quoteId', x."quote_id", 'orderDiscount', x."order_discount"::text, 'promotionId', x."promotion_id", 'paymentRequired', x."payment_required", 'deposit', x."deposit_amount"::text) as j, jsonb_build_array((x."sequence")::text, (x."id")::text) as k
from (
select * from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
and (v_after is null or (x."sequence", x."id") < ((v_after ->> 0)::bigint, (v_after ->> 1)::uuid))
order by x."sequence" desc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'total' and coalesce(v_dir, 'desc') = 'asc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object(
'id', x."id", 'number', x."number", 'status', x."status",
'customerId', x."customer_id", 'currency', x."currency", 'total', x."total"::text,
'paidTotal', x."paid_total"::text, 'placedAt', x."placed_at") || jsonb_build_object('party', case when x."party_id" is not null then jsonb_build_object('type', x."party_type", 'id', x."party_id") end, 'channel', x."channel", 'source', x."source", 'externalId', x."external_id", 'invoicingPolicy', x."invoicing_policy", 'deliveryMethod', x."delivery_method", 'quoteId', x."quote_id", 'orderDiscount', x."order_discount"::text, 'promotionId', x."promotion_id", 'paymentRequired', x."payment_required", 'deposit', x."deposit_amount"::text) as j, jsonb_build_array((x."total")::text, (x."id")::text) as k
from (
select * from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
and (v_after is null or (x."total" >= (v_after ->> 0)::bigint and ((x."total" > (v_after ->> 0)::bigint) or (x."total" = (v_after ->> 0)::bigint and x."id" < (v_after ->> 1)::uuid))))
order by x."total" asc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
elsif v_sort = 'total' and coalesce(v_dir, 'desc') = 'desc' then
if v_after is not null and (jsonb_typeof(v_after) <> 'array' or jsonb_array_length(v_after) <> 2) then
raise exception 'after is the nextCursor of the previous page' using errcode = '22023', hint = 'ORDERS_INVALID';
end if;
select coalesce(jsonb_agg(p.j order by p.n) filter (where p.n <= v_limit), '[]'::jsonb),
count(*) > v_limit,
(array_agg(p.k order by p.n) filter (where p.n = v_limit))[1]
into v_items, v_more, v_next
from (
select row_number() over () as n, jsonb_build_object(
'id', x."id", 'number', x."number", 'status', x."status",
'customerId', x."customer_id", 'currency', x."currency", 'total', x."total"::text,
'paidTotal', x."paid_total"::text, 'placedAt', x."placed_at") || jsonb_build_object('party', case when x."party_id" is not null then jsonb_build_object('type', x."party_type", 'id', x."party_id") end, 'channel', x."channel", 'source', x."source", 'externalId', x."external_id", 'invoicingPolicy', x."invoicing_policy", 'deliveryMethod', x."delivery_method", 'quoteId', x."quote_id", 'orderDiscount', x."order_discount"::text, 'promotionId', x."promotion_id", 'paymentRequired', x."payment_required", 'deposit', x."deposit_amount"::text) as j, jsonb_build_array((x."total")::text, (x."id")::text) as k
from (
select * from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
and (v_after is null or (x."total", x."id") < ((v_after ->> 0)::bigint, (v_after ->> 1)::uuid))
order by x."total" desc, x."id" desc
limit v_limit + 1 offset v_offset
) x
) p;
end if;
if not v_paged then
return v_items;
end if;
if coalesce((v_filters ->> 'total')::boolean, false) then
select count(*) into v_total from (
select 1 from "better_supabase"."orders" x
where x."tenant_id" = page_orders.tenant
and (not (v_filters -> 'status' is not null and jsonb_typeof(v_filters -> 'status') <> 'null' and not (jsonb_typeof(v_filters -> 'status') = 'array' and jsonb_array_length(v_filters -> 'status') = 0)) or (x."status")::text = any((case when jsonb_typeof(v_filters -> 'status') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'status')) else array[v_filters ->> 'status'] end)::text[]))
and (not (v_filters -> 'customerId' is not null and jsonb_typeof(v_filters -> 'customerId') <> 'null' and not (jsonb_typeof(v_filters -> 'customerId') = 'array' and jsonb_array_length(v_filters -> 'customerId') = 0)) or x."customer_id" = any((case when jsonb_typeof(v_filters -> 'customerId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'customerId')) else array[v_filters ->> 'customerId'] end)::uuid[]))
and (not (v_filters -> 'partyId' is not null and jsonb_typeof(v_filters -> 'partyId') <> 'null' and not (jsonb_typeof(v_filters -> 'partyId') = 'array' and jsonb_array_length(v_filters -> 'partyId') = 0)) or (x."party_id")::text = any((case when jsonb_typeof(v_filters -> 'partyId') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'partyId')) else array[v_filters ->> 'partyId'] end)::text[]))
and (not (v_filters -> 'channel' is not null and jsonb_typeof(v_filters -> 'channel') <> 'null' and not (jsonb_typeof(v_filters -> 'channel') = 'array' and jsonb_array_length(v_filters -> 'channel') = 0)) or (x."channel")::text = any((case when jsonb_typeof(v_filters -> 'channel') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'channel')) else array[v_filters ->> 'channel'] end)::text[]))
and (not (v_filters -> 'source' is not null and jsonb_typeof(v_filters -> 'source') <> 'null' and not (jsonb_typeof(v_filters -> 'source') = 'array' and jsonb_array_length(v_filters -> 'source') = 0)) or (x."source")::text = any((case when jsonb_typeof(v_filters -> 'source') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'source')) else array[v_filters ->> 'source'] end)::text[]))
and (not (v_filters -> 'currency' is not null and jsonb_typeof(v_filters -> 'currency') <> 'null' and not (jsonb_typeof(v_filters -> 'currency') = 'array' and jsonb_array_length(v_filters -> 'currency') = 0)) or (x."currency")::text = any((case when jsonb_typeof(v_filters -> 'currency') = 'array' then array(select jsonb_array_elements_text(v_filters -> 'currency')) else array[v_filters ->> 'currency'] end)::text[]))
and (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), '') is null or x."placed_at" >= (nullif(coalesce(v_filters #>> '{from,0}', v_filters ->> 'from'), ''))::timestamptz)
and (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), '') is null or x."placed_at" < (nullif(coalesce(v_filters #>> '{to,0}', v_filters ->> 'to'), ''))::timestamptz)
and (v_search is null or x."number" ilike v_search or x."email" ilike v_search or x."external_id" ilike v_search)
limit v_total_cap + 1
) c;
end if;
return jsonb_build_object('items', v_items, 'hasMore', v_more, 'nextCursor', case when v_more then v_next end)
|| case when v_total is null then '{}'::jsonb
else jsonb_build_object('total', least(v_total, v_total_cap), 'totalCapped', v_total > v_total_cap) end;
end;
$$;
revoke execute on function "better_supabase"."page_orders"(uuid, jsonb) from public, anon;
grant execute on function "better_supabase"."page_orders"(uuid, jsonb) to authenticated, service_role;
-- Tenant isolation (ADR 0099): permissive policies combine with or, so one
-- the app adds would widen the block's. These restrictive policies keep each
-- table to the caller's organizations and what the block's policies grant.
create policy bsb_tenant_scope_select on "better_supabase"."carts" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("user_id" = (select auth.uid()) or "tenant_id" in (select better_supabase.tenant_ids_with('orders.read')))
);
create policy bsb_tenant_scope_insert on "better_supabase"."carts" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."carts" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."carts" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."cart_lines" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."carts" x where x."id" = "cart_id" and x."user_id" = (select auth.uid())))
);
create policy bsb_tenant_scope_insert on "better_supabase"."cart_lines" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."cart_lines" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."cart_lines" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."order_shipping_rates" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')))
);
create policy bsb_tenant_scope_insert on "better_supabase"."order_shipping_rates" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."order_shipping_rates" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."order_shipping_rates" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."orders" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("customer_id" = (select auth.uid()) or "tenant_id" in (select better_supabase.tenant_ids_with('orders.read')))
);
create policy bsb_tenant_scope_insert on "better_supabase"."orders" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."orders" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."orders" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."order_lines" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."orders" o where o."id" = "order_id" and o."customer_id" = (select auth.uid())))
);
create policy bsb_tenant_scope_insert on "better_supabase"."order_lines" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."order_lines" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."order_lines" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."order_payments" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."orders" o where o."id" = "order_id" and o."customer_id" = (select auth.uid())))
);
create policy bsb_tenant_scope_insert on "better_supabase"."order_payments" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."order_payments" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."order_payments" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."order_fulfillments" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."orders" o where o."id" = "order_id" and o."customer_id" = (select auth.uid())))
);
create policy bsb_tenant_scope_insert on "better_supabase"."order_fulfillments" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."order_fulfillments" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."order_fulfillments" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."order_fulfillment_lines" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')))
);
create policy bsb_tenant_scope_insert on "better_supabase"."order_fulfillment_lines" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."order_fulfillment_lines" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."order_fulfillment_lines" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."order_returns" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."orders" o where o."id" = "order_id" and o."customer_id" = (select auth.uid())))
);
create policy bsb_tenant_scope_insert on "better_supabase"."order_returns" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."order_returns" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."order_returns" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."order_return_lines" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')))
);
create policy bsb_tenant_scope_insert on "better_supabase"."order_return_lines" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."order_return_lines" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."order_return_lines" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_select on "better_supabase"."order_refunds" as restrictive
for select to public
using (
"tenant_id" in (select better_supabase.member_organization_ids())
or ("tenant_id" in (select better_supabase.tenant_ids_with('orders.read')) or exists (select 1 from "better_supabase"."orders" o where o."id" = "order_id" and o."customer_id" = (select auth.uid())))
);
create policy bsb_tenant_scope_insert on "better_supabase"."order_refunds" as restrictive
for insert to public
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_update on "better_supabase"."order_refunds" as restrictive
for update to public
using ("tenant_id" in (select better_supabase.member_organization_ids()))
with check ("tenant_id" in (select better_supabase.member_organization_ids()));
create policy bsb_tenant_scope_delete on "better_supabase"."order_refunds" as restrictive
for delete to public
using ("tenant_id" in (select better_supabase.member_organization_ids()));
Client
This block has no React entry yet. Its TypeScript side is in better-supabase/blocks/orders, which runs on the server.
Events
With the outbox installed, the module writes these in the same transaction as the change.
- fulfillment.delivered
- fulfillment.shipped
- order.cancelled
- order.completed
- order.drafted
- order.fulfilled
- order.hold_placed
- order.hold_released
- order.paid
- order.placed
- order.refunded
- return.received
- return.requested
Dependencies
- Required: the install command adds it too
- Works with: optional
Recipes
More in Billing & commerce
Stripe entitlementsentitlements Active Stripe entitlements per tenant from the Stripe Sync Engine, feature_claims() for the access token hook, and has_entitlement() and tenant_ids_with_entitlement() for RLS.React hooks Usage metering and quotasusage Usage counters per tenant, meter and day with idempotent increments, quotas per tenant or plan, within_quota() for policies and consume_quota(), which raises quota_exceeded. unreported_usage() feeds reportUsageToStripe.React hooks Stripe billingbilling Each tenant's Stripe customer, seat counts (options.seatRoles) and the active subscription item from the Stripe Sync Engine, for createBilling(): checkout, the customer portal, seat sync and Stripe webhook handling.React hooks Catalogcatalog Products with options, variants and prices in minor units, price lists, a category tree, collections, sales channels, translations per locale, product media and an optional public storefront. Inventoryinventory Stock per catalog variant and location in a unit with decimals: movements posted to the ledger block, lots, serial numbers, expiry, bins, weighted average cost, reservations with expiry, allocations with backorders, transfers, purchase requisitions with approval, and purchase orders with partial receiving into goods receipts. Promotionspromotions Percentage, fixed and free-shipping promotions, automatic or by code, with usage limits and stacking rules, priced into orders carts.