Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
233 lines
8.1 KiB
TypeScript
233 lines
8.1 KiB
TypeScript
"use client";
|
||
|
||
import { useState, type KeyboardEvent } from "react";
|
||
import { Button } from "@/app/components/ui/button";
|
||
import { Input } from "@/app/components/ui/input";
|
||
import type { MemberCheckoutDiscountLine, MemberCheckoutPreviewResult } from "@/app/lib/types";
|
||
import { useMoney } from "@/app/pos/currency";
|
||
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
||
import type { Translate } from "@/app/lib/i18n/t";
|
||
|
||
interface PosPromoPanelProps {
|
||
quote: MemberCheckoutPreviewResult | null;
|
||
loading: boolean;
|
||
error: string | null;
|
||
code: string;
|
||
appliedCode: string | null;
|
||
onApply: (code: string) => void;
|
||
onRemove: () => void;
|
||
/** Re-prices the same cart after a failed quote. */
|
||
onRetry: () => void;
|
||
/** Accept a special-price gift the quote offers (add-promotion-reward-lines). */
|
||
onAcceptReward?: (campaignId: number) => void;
|
||
/** Gifts the operator accepted; each gets a Remove button. */
|
||
acceptedRewardCampaignIds?: number[];
|
||
/** Drop an accepted gift. */
|
||
onRemoveReward?: (campaignId: number) => void;
|
||
/** Catalogue name for a product code, when the server sent none. */
|
||
nameFor?: (productCode: string) => string | undefined;
|
||
}
|
||
|
||
/**
|
||
* Map a promo rejection or notice code to an operator-readable message.
|
||
* The caller passes `quote.promoRejection ?? quote.promoNotice ?? null` as the first argument.
|
||
* Returns null when there is nothing to display.
|
||
*/
|
||
function promoMessage(
|
||
code: string | null,
|
||
shortfall: number | null | undefined,
|
||
money: (amount: number) => string,
|
||
t: Translate,
|
||
): string | null {
|
||
if (!code) return null;
|
||
switch (code) {
|
||
case "NOT_FOUND":
|
||
return t("PosPromoNotFound");
|
||
case "NOT_STARTED":
|
||
return t("PosPromoNotStarted");
|
||
case "EXPIRED":
|
||
return t("PosPromoExpired");
|
||
case "NOT_VALID_FOR_CHANNEL":
|
||
return t("PosPromoNotForChannel");
|
||
case "LOGIN_REQUIRED":
|
||
return t("PosPromoLoginRequired");
|
||
case "NOT_FOR_YOUR_RANK":
|
||
return t("PosPromoNotForRank");
|
||
case "NOT_VALID_FOR_ACCOUNT":
|
||
return t("PosPromoOtherAccount");
|
||
case "USAGE_LIMIT_REACHED":
|
||
return t("PosPromoFullyRedeemed");
|
||
case "MIN_SPEND_NOT_MET":
|
||
return t("PosPromoMinSpend", { amount: money(shortfall ?? 0) });
|
||
case "MIN_QUANTITY_NOT_MET": {
|
||
const n = shortfall ?? 0;
|
||
return t(n === 1 ? "PosPromoMinQtyOne" : "PosPromoMinQtyMany", { count: n });
|
||
}
|
||
case "NOT_VALID_FOR_ITEMS":
|
||
return t("PosPromoNotForItems");
|
||
case "CODE_USED":
|
||
return t("PosPromoCodeUsed");
|
||
case "NOT_ACTIVE":
|
||
return t("PosPromoNotActive");
|
||
case "BETTER_OFFER_APPLIED":
|
||
return t("PosPromoBetterOffer");
|
||
default:
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/** One row per campaign label (amounts summed, first-seen order) — the cart summary and the receipt. */
|
||
export function discountRows(
|
||
lines: MemberCheckoutDiscountLine[] | undefined,
|
||
): Array<{ label: string; amount: number }> {
|
||
const byLabel = new Map<string, number>();
|
||
for (const d of lines ?? []) {
|
||
byLabel.set(d.campaignLabel, (byLabel.get(d.campaignLabel) ?? 0) + d.amount);
|
||
}
|
||
return [...byLabel].map(([label, amount]) => ({ label, amount }));
|
||
}
|
||
|
||
/**
|
||
* The operator's promo code entry and the cart's discount summary. It sits inside the POS sale
|
||
* <form>, so it has no form of its own and Enter applies the code instead of placing the sale.
|
||
*/
|
||
export function PosPromoPanel({
|
||
quote,
|
||
loading,
|
||
error,
|
||
code,
|
||
appliedCode,
|
||
onApply,
|
||
onRemove,
|
||
onRetry,
|
||
onAcceptReward,
|
||
acceptedRewardCampaignIds = [],
|
||
onRemoveReward,
|
||
nameFor,
|
||
}: PosPromoPanelProps) {
|
||
const money = useMoney();
|
||
const t = useT();
|
||
const [draft, setDraft] = useState(code);
|
||
const message = quote && code
|
||
? promoMessage(quote.promoRejection ?? quote.promoNotice ?? null, quote.promoShortfall, money, t)
|
||
: null;
|
||
const rows = discountRows(quote?.discountLines);
|
||
const rewards = quote?.rewardLines ?? [];
|
||
const nameOf = (r: { productCode: string; productName?: string | null }) =>
|
||
r.productName || nameFor?.(r.productCode) || r.productCode;
|
||
|
||
function handleKeyDown(e: KeyboardEvent<HTMLInputElement>) {
|
||
if (e.key === "Enter") {
|
||
e.preventDefault();
|
||
if (draft.trim()) onApply(draft);
|
||
}
|
||
}
|
||
|
||
return (
|
||
<div className="space-y-2">
|
||
{appliedCode ? (
|
||
<div className="flex items-center gap-2">
|
||
<span className="text-sm font-medium">{t("PosPromoApplied", { code: appliedCode })}</span>
|
||
<Button
|
||
type="button"
|
||
variant="outline"
|
||
size="sm"
|
||
onClick={() => {
|
||
setDraft("");
|
||
onRemove();
|
||
}}
|
||
>
|
||
{t("PosRemove")}
|
||
</Button>
|
||
</div>
|
||
) : (
|
||
<div className="flex gap-2">
|
||
<Input
|
||
aria-label={t("PosPromoCode")}
|
||
placeholder={t("PosPromoCode")}
|
||
value={draft}
|
||
onChange={(e) => setDraft(e.target.value)}
|
||
onKeyDown={handleKeyDown}
|
||
/>
|
||
<Button
|
||
type="button"
|
||
variant="outline"
|
||
size="sm"
|
||
disabled={!draft.trim()}
|
||
onClick={() => onApply(draft)}
|
||
>
|
||
{t("PosApply")}
|
||
</Button>
|
||
</div>
|
||
)}
|
||
|
||
{message && (
|
||
<p role="status" className="text-sm text-muted-foreground">
|
||
{message}
|
||
</p>
|
||
)}
|
||
|
||
{loading && <p className="text-sm text-muted-foreground">{t("PosPricing")}</p>}
|
||
{error && (
|
||
<div className="flex items-center gap-2">
|
||
<p role="alert" className="text-sm text-destructive">
|
||
{t("PosPriceFailed", { error: t(error) })}
|
||
</p>
|
||
<Button type="button" variant="outline" size="sm" onClick={onRetry}>
|
||
{t("UiRetry")}
|
||
</Button>
|
||
</div>
|
||
)}
|
||
|
||
{rewards.length > 0 && (
|
||
<ul className="text-sm space-y-1" aria-label={t("PosPromoRewards")}>
|
||
{rewards.map((r) => (
|
||
<li key={`${r.campaignId}-${r.productCode}-${r.variationId ?? ""}`} data-testid={`reward-line-${r.campaignId}-${r.productCode}-${r.variationId ?? ""}`}
|
||
className="flex items-center justify-between gap-2">
|
||
<span>
|
||
{nameOf(r)} × {r.qty}
|
||
<span className="block text-xs text-muted-foreground">{r.campaignLabel}</span>
|
||
</span>
|
||
{r.status === "NEEDS_ACCEPTANCE" ? (
|
||
<Button type="button" variant="outline" size="sm" onClick={() => onAcceptReward?.(r.campaignId)}>
|
||
{t("PosAddRewardFor", { name: nameOf(r), price: money(r.netUnitPrice) })}
|
||
</Button>
|
||
) : (
|
||
<span className="flex items-center gap-2">
|
||
<span className="font-mono">{r.netUnitPrice === 0 ? t("PosFree") : money(r.netUnitPrice)}</span>
|
||
{r.status === "APPLIED" && onRemoveReward && acceptedRewardCampaignIds.includes(r.campaignId) && (
|
||
<Button type="button" variant="ghost" size="sm" aria-label={t("PosRemoveAria", { name: nameOf(r) })}
|
||
onClick={() => onRemoveReward(r.campaignId)}>
|
||
{t("PosRemove")}
|
||
</Button>
|
||
)}
|
||
</span>
|
||
)}
|
||
</li>
|
||
))}
|
||
</ul>
|
||
)}
|
||
{(quote?.rewardOutOfStock ?? []).map((code) => (
|
||
<p key={`oos-${code}`} className="text-sm text-muted-foreground">
|
||
{t("PosRewardOutOfStock", { name: nameFor?.(code) || code })}
|
||
</p>
|
||
))}
|
||
|
||
{quote && rows.length > 0 && (
|
||
<dl className="text-sm space-y-1">
|
||
<div className="flex justify-between">
|
||
<dt className="text-muted-foreground">{t("PosSubtotal")}</dt>
|
||
<dd className="font-mono">{money(quote.grossTotal ?? quote.orderTotal)}</dd>
|
||
</div>
|
||
{rows.map(({ label, amount }) => (
|
||
<div className="flex justify-between" key={label}>
|
||
<dt className="text-muted-foreground">{t("PosDiscountRow", { label })}</dt>
|
||
<dd className="font-mono text-positive">-{money(amount)}</dd>
|
||
</div>
|
||
))}
|
||
</dl>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|