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
123 lines
4.4 KiB
TypeScript
123 lines
4.4 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
// Split-tender gateway poll loop (split-payment gateway, Task 13). When a split sale has a pending
|
|
// gateway leg the backend returns a redirect URL (rendered as a QR the customer scans) and a sale
|
|
// `uuid`; this hook polls that uuid's settlement state until it terminates. `poll`/`cancel` are
|
|
// injected so the SAME hook drives BOTH the Branch Counter (BO_COUNTER) and Event POS (BO_POS)
|
|
// surfaces — each passes its own channel client fns.
|
|
//
|
|
// Terminal handling:
|
|
// - PAID → onPaid(); stop.
|
|
// - FAILED / EXPIRED → onFailed(state); stop.
|
|
// - timeoutMs elapsed → cancel(uuid) (release the reserved stock), then onFailed("EXPIRED"); stop.
|
|
// A transient poll error (network blip) is swallowed — the next tick retries. The interval and the
|
|
// timeout are cleared on unmount and whenever `uuid` clears, so a closed panel never keeps polling.
|
|
|
|
export type SplitPollFailure = "FAILED" | "EXPIRED";
|
|
|
|
interface UseSplitPollArgs {
|
|
/** The pending sale's uuid, or null/"" when there is nothing to poll. */
|
|
uuid: string | null;
|
|
poll: (uuid: string) => Promise<{ status: "PENDING" | "PAID" | "FAILED" | "EXPIRED" }>;
|
|
cancel: (uuid: string) => Promise<unknown>;
|
|
onPaid: () => void;
|
|
onFailed: (state: SplitPollFailure) => void;
|
|
/** Total wait before auto-cancel. Defaults to 5 minutes. */
|
|
timeoutMs?: number;
|
|
/** Poll cadence. Defaults to 2.5s. */
|
|
intervalMs?: number;
|
|
}
|
|
|
|
export function useSplitPoll({
|
|
uuid,
|
|
poll,
|
|
cancel,
|
|
onPaid,
|
|
onFailed,
|
|
timeoutMs = 5 * 60_000,
|
|
intervalMs = 2500,
|
|
}: UseSplitPollArgs): { polling: boolean; elapsed: number } {
|
|
const [polling, setPolling] = useState(false);
|
|
const [elapsed, setElapsed] = useState(0);
|
|
|
|
// Latest callbacks/fns held in refs so the effect depends only on `uuid` (and the timings) and
|
|
// never re-subscribes when the parent re-renders with new closures.
|
|
const pollRef = useRef(poll);
|
|
const cancelRef = useRef(cancel);
|
|
const onPaidRef = useRef(onPaid);
|
|
const onFailedRef = useRef(onFailed);
|
|
// Keep the injected callbacks fresh without re-subscribing the poll loop. The refs are read only
|
|
// inside the effect below (the interval/timeout callbacks), never during render.
|
|
/* eslint-disable react-hooks/refs -- latest-callback refs; read only in the effect, not render. */
|
|
pollRef.current = poll;
|
|
cancelRef.current = cancel;
|
|
onPaidRef.current = onPaid;
|
|
onFailedRef.current = onFailed;
|
|
/* eslint-enable react-hooks/refs */
|
|
|
|
// This effect owns the poll lifecycle: it subscribes to an external timer system and drives the
|
|
// pending sale to a terminal state. The setState calls synchronise React with that timer, which is
|
|
// the intended use of an effect (subscribe to an external system), not a cascading-render smell.
|
|
/* eslint-disable react-hooks/set-state-in-effect -- poll lifecycle synced to an external timer. */
|
|
useEffect(() => {
|
|
if (!uuid) {
|
|
setPolling(false);
|
|
setElapsed(0);
|
|
return;
|
|
}
|
|
setPolling(true);
|
|
setElapsed(0);
|
|
const start = Date.now();
|
|
let done = false;
|
|
let intervalId: ReturnType<typeof setInterval>;
|
|
let timeoutId: ReturnType<typeof setTimeout>;
|
|
|
|
const stop = () => {
|
|
done = true;
|
|
clearInterval(intervalId);
|
|
clearTimeout(timeoutId);
|
|
setPolling(false);
|
|
};
|
|
|
|
intervalId = setInterval(() => {
|
|
if (done) return;
|
|
setElapsed(Date.now() - start);
|
|
void pollRef
|
|
.current(uuid)
|
|
.then((res) => {
|
|
if (done) return;
|
|
if (res.status === "PAID") {
|
|
stop();
|
|
onPaidRef.current();
|
|
} else if (res.status === "FAILED" || res.status === "EXPIRED") {
|
|
stop();
|
|
onFailedRef.current(res.status);
|
|
}
|
|
// PENDING → keep polling.
|
|
})
|
|
.catch(() => {
|
|
// Transient poll error — swallow and let the next tick retry.
|
|
});
|
|
}, intervalMs);
|
|
|
|
timeoutId = setTimeout(() => {
|
|
if (done) return;
|
|
done = true;
|
|
clearInterval(intervalId);
|
|
setPolling(false);
|
|
void Promise.resolve(cancelRef.current(uuid)).finally(() => onFailedRef.current("EXPIRED"));
|
|
}, timeoutMs);
|
|
|
|
return () => {
|
|
done = true;
|
|
clearInterval(intervalId);
|
|
clearTimeout(timeoutId);
|
|
};
|
|
}, [uuid, timeoutMs, intervalMs]);
|
|
/* eslint-enable react-hooks/set-state-in-effect */
|
|
|
|
return { polling, elapsed };
|
|
}
|