288 lines
9.2 KiB
TypeScript
288 lines
9.2 KiB
TypeScript
import { useState } from "react";
|
||
import { z } from "zod";
|
||
import Alert from "@mui/material/Alert";
|
||
import { coupon, couponInput, pricing, pricingInput } from "@/lib/commerce";
|
||
import { usePlatform } from "@/lib/platform";
|
||
import { WorkspaceShell } from "@/components/WorkspaceShell";
|
||
import {
|
||
ActionForm,
|
||
Field,
|
||
Pager,
|
||
Panel,
|
||
QueryView,
|
||
Table,
|
||
usePrivateQuery,
|
||
readText,
|
||
readNumber,
|
||
} from "@/components/WorkspaceTools";
|
||
import { ConfirmAction } from "@/components/ConfirmAction";
|
||
const currencies = ["INR", "USD", "EUR", "GBP"].map((value) => ({
|
||
value,
|
||
label: value,
|
||
}));
|
||
function Coupons() {
|
||
const { request } = usePlatform();
|
||
const [offset, setOffset] = useState(0),
|
||
[kind, setKind] = useState("PERCENT");
|
||
const query = usePrivateQuery(
|
||
`/coupons?limit=25&offset=${offset}`,
|
||
z.array(coupon),
|
||
);
|
||
return (
|
||
<>
|
||
<Panel title="Coupon rules">
|
||
<QueryView query={query}>
|
||
{(items) => (
|
||
<>
|
||
<Table
|
||
head={["Code", "Rule", "Window", "Status", "Action"]}
|
||
rowKeys={items.map((item) => item.id)}
|
||
rows={items.map((c) => [
|
||
c.code,
|
||
c.kind === "FIXED"
|
||
? `${c.currency} ${c.amount}`
|
||
: `${(c.percentBps || 0) / 100}%`,
|
||
`${new Date(c.startsAt).toLocaleString()} – ${new Date(c.endsAt).toLocaleString()}`,
|
||
c.active ? "Active" : "Inactive",
|
||
<ConfirmAction
|
||
label={c.active ? "Deactivate" : "Activate"}
|
||
message="Change this coupon's availability? Order snapshots remain unchanged."
|
||
onConfirm={async () => {
|
||
await request(`/coupons/${c.id}/status`, coupon, {
|
||
method: "PATCH",
|
||
body: { active: !c.active },
|
||
});
|
||
await query.refetch();
|
||
}}
|
||
/>,
|
||
])}
|
||
/>
|
||
<Pager offset={offset} count={items.length} onPage={setOffset} />
|
||
</>
|
||
)}
|
||
</QueryView>
|
||
</Panel>
|
||
<Panel title="Create an immutable coupon">
|
||
<Alert severity="info">
|
||
Rules cannot be edited after creation. Dates must include an explicit
|
||
timezone. Campaign-linked coupons belong to the growth phase.
|
||
</Alert>
|
||
<div className="coupon-kind">
|
||
<button
|
||
type="button"
|
||
onClick={() => setKind("PERCENT")}
|
||
aria-pressed={kind === "PERCENT"}
|
||
>
|
||
Percentage
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => setKind("FIXED")}
|
||
aria-pressed={kind === "FIXED"}
|
||
>
|
||
Fixed amount
|
||
</button>
|
||
</div>
|
||
<ActionForm
|
||
key={kind}
|
||
label="Create coupon"
|
||
read={(data) => ({
|
||
code: readText(data, "code"),
|
||
currency: readText(data, "currency"),
|
||
minimumSubtotal: readText(data, "minimumSubtotal"),
|
||
maxUses: readNumber(data, "maxUses"),
|
||
perUserLimit: readNumber(data, "perUserLimit"),
|
||
startsAt: readText(data, "startsAt"),
|
||
endsAt: readText(data, "endsAt"),
|
||
kind,
|
||
...(kind === "FIXED"
|
||
? { amount: readText(data, "amount") }
|
||
: { percentBps: readNumber(data, "percentBps") }),
|
||
})}
|
||
submit={async (value) => {
|
||
await request("/coupons", coupon, {
|
||
method: "POST",
|
||
body: couponInput.parse(value),
|
||
});
|
||
await query.refetch();
|
||
}}
|
||
>
|
||
<Field name="code" label="Coupon code" maxLength={40} />
|
||
<Field
|
||
name="currency"
|
||
label="Currency"
|
||
options={currencies}
|
||
value="INR"
|
||
/>
|
||
<Field
|
||
name="minimumSubtotal"
|
||
label="Minimum merchandise subtotal"
|
||
value="0.00"
|
||
/>
|
||
{kind === "FIXED" ? (
|
||
<Field
|
||
name="amount"
|
||
label="Fixed discount amount"
|
||
helper="Two decimal places, e.g. 50.00"
|
||
/>
|
||
) : (
|
||
<Field
|
||
name="percentBps"
|
||
label="Discount in basis points"
|
||
type="number"
|
||
helper="1000 means 10%; maximum 10000."
|
||
/>
|
||
)}
|
||
<Field name="maxUses" label="Total usage limit" type="number" />
|
||
<Field
|
||
name="perUserLimit"
|
||
label="Per-account usage limit"
|
||
type="number"
|
||
value={1}
|
||
/>
|
||
<Field
|
||
name="startsAt"
|
||
label="Starts at (ISO with timezone)"
|
||
helper="Example: 2026-10-12T00:00:00+05:30"
|
||
/>
|
||
<Field name="endsAt" label="Ends at (ISO with timezone)" />
|
||
</ActionForm>
|
||
</Panel>
|
||
</>
|
||
);
|
||
}
|
||
function Pricing() {
|
||
const { request } = usePlatform();
|
||
const [offset, setOffset] = useState(0);
|
||
const query = usePrivateQuery(
|
||
`/admin/pricing-policies?limit=25&offset=${offset}`,
|
||
z.array(pricing),
|
||
);
|
||
return (
|
||
<>
|
||
<Panel title="Pricing policy versions">
|
||
<Alert severity="warning">
|
||
Use approved tax and shipping rules. New versions do not reprice
|
||
existing orders and do not enable payment.
|
||
</Alert>
|
||
<QueryView query={query}>
|
||
{(items) => (
|
||
<>
|
||
<Table
|
||
head={["Policy", "Scope", "Tax mode", "Status", "Action"]}
|
||
rowKeys={items.map((item) => item.id)}
|
||
rows={items.map((p) => [
|
||
p.name,
|
||
`${p.currency} · ${p.countryCode} · ${p.region || "Country fallback"}`,
|
||
p.taxMode,
|
||
p.active ? "Active" : "Inactive",
|
||
<ConfirmAction
|
||
label={p.active ? "Deactivate" : "Activate"}
|
||
message="Change this policy's status? Activation disables the previous version for this scope."
|
||
onConfirm={async () => {
|
||
await request(
|
||
`/admin/pricing-policies/${p.id}/status`,
|
||
pricing,
|
||
{ method: "PATCH", body: { active: !p.active } },
|
||
);
|
||
await query.refetch();
|
||
}}
|
||
/>,
|
||
])}
|
||
/>
|
||
<Pager offset={offset} count={items.length} onPage={setOffset} />
|
||
</>
|
||
)}
|
||
</QueryView>
|
||
</Panel>
|
||
<Panel title="New inactive pricing policy">
|
||
<ActionForm
|
||
label="Create pricing policy"
|
||
read={(data) => ({
|
||
name: readText(data, "name"),
|
||
currency: readText(data, "currency"),
|
||
countryCode: readText(data, "countryCode"),
|
||
region: readText(data, "region"),
|
||
taxMode: readText(data, "taxMode"),
|
||
merchandiseTaxBps: readNumber(data, "merchandiseTaxBps"),
|
||
shippingTaxBps: readNumber(data, "shippingTaxBps"),
|
||
shippingFee: readText(data, "shippingFee"),
|
||
freeShippingMinimum: readText(data, "freeShippingMinimum") || null,
|
||
})}
|
||
submit={async (value) => {
|
||
await request("/admin/pricing-policies", pricing, {
|
||
method: "POST",
|
||
body: pricingInput.parse(value),
|
||
});
|
||
await query.refetch();
|
||
}}
|
||
>
|
||
<Field name="name" label="Policy name" maxLength={100} />
|
||
<Field
|
||
name="currency"
|
||
label="Currency"
|
||
value="INR"
|
||
options={currencies}
|
||
/>
|
||
<Field
|
||
name="countryCode"
|
||
label="Country code"
|
||
maxLength={2}
|
||
value="IN"
|
||
/>
|
||
<Field
|
||
name="region"
|
||
label="Region (blank for country fallback)"
|
||
required={false}
|
||
maxLength={100}
|
||
/>
|
||
<Field
|
||
name="taxMode"
|
||
label="Merchandise tax mode"
|
||
value="EXCLUSIVE"
|
||
options={["INCLUSIVE", "EXCLUSIVE"].map((value) => ({
|
||
value,
|
||
label: value,
|
||
}))}
|
||
/>
|
||
<Field
|
||
name="merchandiseTaxBps"
|
||
label="Merchandise tax basis points"
|
||
type="number"
|
||
/>
|
||
<Field
|
||
name="shippingFee"
|
||
label="Net shipping fee"
|
||
helper="Two decimal places."
|
||
/>
|
||
<Field
|
||
name="shippingTaxBps"
|
||
label="Shipping tax basis points"
|
||
type="number"
|
||
/>
|
||
<Field
|
||
name="freeShippingMinimum"
|
||
label="Free-shipping minimum (optional)"
|
||
required={false}
|
||
helper="Leave blank if there is no threshold."
|
||
/>
|
||
</ActionForm>
|
||
</Panel>
|
||
</>
|
||
);
|
||
}
|
||
export default function OffersPage({
|
||
pricing: showPricing = false,
|
||
}: {
|
||
pricing?: boolean;
|
||
}) {
|
||
return (
|
||
<WorkspaceShell
|
||
title={showPricing ? "Pricing Policies" : "Coupon Administration"}
|
||
permission={showPricing ? "pricing.manage" : "coupons.manage"}
|
||
>
|
||
{showPricing ? <Pricing /> : <Coupons />}
|
||
</WorkspaceShell>
|
||
);
|
||
}
|