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
89 lines
3.9 KiB
TypeScript
89 lines
3.9 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import type { MemberCheckoutPreviewResult, PosQuotePayload, PosWalletLine } from "@/app/lib/types";
|
|
import type { PosLine } from "./shared";
|
|
|
|
interface QuoteResult {
|
|
key: string;
|
|
quote: MemberCheckoutPreviewResult | null;
|
|
error: string | null;
|
|
}
|
|
|
|
/**
|
|
* Prices the POS cart through the promotion-aware quote (add-promotions-campaigns PR 2) and owns the
|
|
* entered promo code. The cart, the split and the receipt total from `quote.orderTotal`. While a new
|
|
* quote loads, `quote` is null, so no sale can be placed on a stale total.
|
|
*/
|
|
export function usePosQuote(
|
|
fetchQuote: (payload: PosQuotePayload) => Promise<MemberCheckoutPreviewResult>,
|
|
memberCode: string | undefined,
|
|
lines: PosLine[],
|
|
eventId?: string,
|
|
) {
|
|
const [code, setCode] = useState("");
|
|
const [refreshCount, setRefreshCount] = useState(0);
|
|
const [result, setResult] = useState<QuoteResult | null>(null);
|
|
const [accepted, setAccepted] = useState<number[]>([]);
|
|
|
|
const cart: PosWalletLine[] = lines.map(({ pPricingId, qty }) => ({ pPricingId, qty }));
|
|
// An emptied cart ends that customer's sale: forget accepted gifts (a guarded state update during render).
|
|
if (cart.length === 0 && accepted.length > 0) setAccepted([]);
|
|
// A primitive key: a re-render with an equal cart must not re-quote.
|
|
const key = cart.length === 0 ? null : JSON.stringify([memberCode ?? "", cart, code, accepted, refreshCount, eventId ?? ""]);
|
|
|
|
useEffect(() => {
|
|
if (!key) return;
|
|
const [member, cartLines, promo, gifts, , event] = JSON.parse(key) as [string, PosWalletLine[], string, number[], number, string];
|
|
let cancelled = false;
|
|
fetchQuote({
|
|
...(event ? { eventId: event } : {}),
|
|
...(member ? { memberCode: member } : {}),
|
|
lines: cartLines,
|
|
...(promo ? { promoCode: promo } : {}),
|
|
...(gifts.length > 0 ? { acceptedRewardCampaignIds: gifts } : {}),
|
|
})
|
|
.then((quote) => {
|
|
if (!cancelled) setResult({ key, quote, error: null });
|
|
})
|
|
.catch((err: unknown) => {
|
|
if (!cancelled) {
|
|
setResult({ key, quote: null, error: err instanceof Error ? err.message : "PosPriceCartFailed" });
|
|
}
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [key, fetchQuote]);
|
|
|
|
const current = result?.key === key ? result : null;
|
|
const quote = current?.quote ?? null;
|
|
// The last answer for the code as entered — still valid while the same code is re-priced, so the
|
|
// applied code (and every order key built from it) does not flicker during a refresh.
|
|
const settled = result && code && (JSON.parse(result.key) as unknown[])[2] === code ? result.quote : null;
|
|
return {
|
|
quote,
|
|
loading: key !== null && current === null,
|
|
error: current?.error ?? null,
|
|
code,
|
|
/** The code the server accepted, as it normalised it; null when none is applied. */
|
|
appliedCode: settled && !settled.promoRejection ? (settled.promoCode ?? null) : null,
|
|
// Re-applying the same code re-checks it: the offer may have been resumed or the voucher re-issued.
|
|
applyCode: (next: string) => {
|
|
const trimmed = next.trim();
|
|
if (trimmed === code) setRefreshCount((n) => n + 1);
|
|
else setCode(trimmed);
|
|
},
|
|
removeCode: () => setCode(""),
|
|
/** Special-price gifts the operator accepted; every sale payload built from this quote carries them. */
|
|
acceptedRewardCampaignIds: accepted,
|
|
acceptReward: (campaignId: number) =>
|
|
setAccepted((prev) => (prev.includes(campaignId) ? prev : [...prev, campaignId])),
|
|
/** The operator changed their mind: drop one accepted gift and re-quote without it. */
|
|
removeReward: (campaignId: number) => setAccepted((prev) => prev.filter((id) => id !== campaignId)),
|
|
/** Forget accepted gifts (after a sale, or when the cart is emptied). */
|
|
clearRewards: () => setAccepted([]),
|
|
refresh: () => setRefreshCount((n) => n + 1),
|
|
};
|
|
}
|