import { apiGet, apiPost } from "@/app/lib/clientApi"; import type { PosSellableEvent, PosEventProduct, PosTender, PosPlaceSalePayload, PosPlaceSaleResult, PosWalletBalanceView, PosWalletPreviewPayload, PosWalletSalePayload, PosWalletSaleResult, MemberCheckoutPreviewResult, CounterSplitSalePayload, SplitSaleResult, SplitSettlementStatus, PosQuotePayload, PickupOutletRow, } from "@/app/lib/types"; // Client-side wrapper for the /pos page (add-event-pos-counter-sales, Task 10). Calls only the // portal's own /api/pos/* route handlers — never solvway-mobile-api directly. Ids stay as strings // end-to-end (repo convention snowflake-ids-json-string). All mutations go through apiPost so a // Next.js GET prefetch can never trigger a sale (CLAUDE.md GET-prefetch rule). export function listSellableEvents(): Promise { return apiGet("/api/pos/events"); } /** With an event, each row carries that event's warehouse availability (add-sale-channel-stock-badges). */ export function listEventProducts(eventId?: string): Promise { const qs = eventId ? `?eventId=${encodeURIComponent(eventId)}` : ""; return apiGet(`/api/pos/products${qs}`); } /** List the POS-enabled, active tenders for the branch (add-pos-tender-recording, 2b). Pure read. */ export function listPosTenders(): Promise { return apiGet("/api/pos/payment-methods"); } export function placeSale(payload: PosPlaceSalePayload): Promise { return apiPost("/api/pos/sale", payload); } // --- Wallet tender functions (Brick 3 Task 6) --- // The PIN (candidatePin) is held only for the duration of the placeWalletSale call // and is never stored, logged, or persisted client-side. /** Read member wallet balances + PIN status for the wallet tender UI. Pure read — GET. */ export function readMemberWallet(memberCode: string): Promise { return apiGet( `/api/pos/wallet/${encodeURIComponent(memberCode)}`, ); } /** Preview the settlement split before placing a wallet sale. Mutation path — POST. */ export function previewWallet( payload: PosWalletPreviewPayload, ): Promise { return apiPost("/api/pos/wallet/preview", payload); } /** Price the Event POS cart net of promotions. Nothing is paid or written — POST so a prefetch never fires it. */ export function quoteSale(payload: PosQuotePayload): Promise { return apiPost("/api/pos/quote", payload); } /** * Place a wallet event sale authorized by the member's PIN. * The candidatePin value must be discarded immediately after this call returns. */ export function placeWalletSale(payload: PosWalletSalePayload): Promise { return apiPost("/api/pos/wallet/sale", payload); } // --- Split-tender (wallet + cash + gateway) sale + poll/cancel (split-payment gateway) --- // The candidatePin (present only when a wallet amount is above 0) is held only for the duration of the // placeSplitSale call and is never stored, logged, or persisted client-side. /** Place an Event POS split-tender sale. Mutation path — POST. */ export function placeSplitSale(payload: CounterSplitSalePayload): Promise { return apiPost("/api/pos/split/sale", payload); } /** Poll a split gateway sale's settlement state. Pure read — GET, prefetch-safe. */ export function pollSplitStatus(uuid: string): Promise { return apiGet(`/api/pos/split/status/${encodeURIComponent(uuid)}`); } /** Cancel a still-pending split gateway sale (releases reserved stock). Mutation path — POST. */ export function cancelSplit(uuid: string): Promise { return apiPost(`/api/pos/split/cancel/${encodeURIComponent(uuid)}`); } export function listPosPickupOutlets(): Promise { return apiGet("/api/pos/pickup-outlets"); }