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
323 lines
12 KiB
TypeScript
323 lines
12 KiB
TypeScript
"use client";
|
||
|
||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||
import Link from "next/link";
|
||
import type { ColumnDef } from "@tanstack/react-table";
|
||
import { Badge } from "@/app/components/ui/badge";
|
||
import { Button } from "@/app/components/ui/button";
|
||
import { DateRangePicker } from "@/app/components/ui/date-range-picker";
|
||
import { DataTable } from "@/app/components/ui/data-table";
|
||
import { FilterField, FilterPanel, FilterSearch, FilterSelect } from "@/app/components/ui/filter-panel";
|
||
import { PageHeader } from "@/app/components/ui/page-header";
|
||
import { EmptyState, ErrorState } from "@/app/components/ui/states";
|
||
import { formatDateTime, formatPoints } from "@/app/lib/format";
|
||
import { useAdminAuth } from "@/app/lib/AdminAuthContext";
|
||
import { RequireModule } from "@/app/lib/RequireModule";
|
||
import { useListingExport } from "@/app/lib/useListingExport";
|
||
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
||
import { useListFilters } from "@/app/lib/hooks/useListFilters";
|
||
import { useFitPageSize, MIN_PAGE_SIZE } from "@/app/lib/hooks/useFitPageSize";
|
||
import { useTableSort } from "@/app/lib/hooks/useTableSort";
|
||
import type { AdminWalletLedgerRow } from "@/app/lib/types";
|
||
import { getAllWalletLedger } from "@/app/lib/services/wallet";
|
||
import {
|
||
WALLET_TRXN_CATEGORIES,
|
||
WALLET_TYPE_FILTER_OPTIONS,
|
||
walletChipClass,
|
||
walletRefWords,
|
||
walletTrxnLabel,
|
||
walletTrxnTone,
|
||
} from "@/app/lib/walletTrxnType";
|
||
import WalletAdjustDialog from "./WalletAdjustDialog";
|
||
import WalletTabs from "./WalletTabs";
|
||
|
||
const DEFAULTS = { memberCode: "", type: "", walletType: "", from: "", to: "", refNo: "" };
|
||
|
||
/** The global wallet ledger (openspec/changes/add-admin-wallet-ledger): a read-only, paged
|
||
* listing of every dynamic-wallet transaction across all members, newest-first, with date / type
|
||
* / exact-member-code filters. Each member cell links to the existing per-member view. Replaces
|
||
* the former EmptyState landing on /wallet. Read-only: no write path is touched. */
|
||
export default function WalletLedgerPage() {
|
||
return (
|
||
<RequireModule pathname="/wallet">
|
||
<WalletLedger />
|
||
</RequireModule>
|
||
);
|
||
}
|
||
|
||
/** The gated global ledger. Only rendered once RequireModule confirms BO_WALLET — a user without
|
||
* the module never triggers the data fetch. */
|
||
function WalletLedger() {
|
||
const t = useT();
|
||
const { hasModule } = useAdminAuth();
|
||
const [adjustOpen, setAdjustOpen] = useState(false);
|
||
const [saved, setSaved] = useState(false);
|
||
const { tableRef, pageSize } = useFitPageSize();
|
||
const { filters, page, set, setPage, clear, activeCount } = useListFilters(DEFAULTS);
|
||
// BABYBOO-54: server-side header sort; column ids are the backend sort keys.
|
||
const { sorting, onSortingChange, sortBy, sortDir: dir } = useTableSort();
|
||
const sortDir = sortBy ? dir : undefined;
|
||
|
||
const [rows, setRows] = useState<AdminWalletLedgerRow[]>([]);
|
||
const [total, setTotal] = useState(0);
|
||
const [loading, setLoading] = useState(true);
|
||
const [error, setError] = useState<string | null>(null);
|
||
const [reloadKey, setReloadKey] = useState(0);
|
||
|
||
const retry = useCallback(() => setReloadKey(k => k + 1), []);
|
||
|
||
useEffect(() => {
|
||
if (pageSize == null) return;
|
||
let cancelled = false;
|
||
// eslint-disable-next-line react-hooks/set-state-in-effect -- resets loading/error ahead of the page/filter/reloadKey-triggered fetch below, which is the actual synchronization.
|
||
setLoading(true);
|
||
setError(null);
|
||
getAllWalletLedger(page, pageSize, {
|
||
category: filters.type || undefined,
|
||
from: filters.from || undefined,
|
||
to: filters.to || undefined,
|
||
memberCode: filters.memberCode || undefined,
|
||
walletType: filters.walletType || undefined,
|
||
refNo: filters.refNo || undefined,
|
||
sortBy,
|
||
sortDir,
|
||
})
|
||
.then(result => {
|
||
if (cancelled) return;
|
||
setRows(result.content ?? []);
|
||
setTotal(result.totalElements ?? 0);
|
||
})
|
||
.catch((err: unknown) => {
|
||
if (cancelled) return;
|
||
// Fail loud: a downstream failure is an error state, never an empty table (#71).
|
||
setError(err instanceof Error ? err.message : "Failed to load the wallet ledger");
|
||
})
|
||
.finally(() => {
|
||
if (!cancelled) setLoading(false);
|
||
});
|
||
return () => {
|
||
cancelled = true;
|
||
};
|
||
}, [page, filters, reloadKey, pageSize, sortBy, sortDir]);
|
||
|
||
const listingExport = useListingExport(
|
||
"wallet-ledger",
|
||
t("BO_WALLET"),
|
||
total,
|
||
async (page0, size) => {
|
||
// The service page is 1-based.
|
||
const r = await getAllWalletLedger(page0 + 1, size, {
|
||
category: filters.type || undefined,
|
||
from: filters.from || undefined,
|
||
to: filters.to || undefined,
|
||
memberCode: filters.memberCode || undefined,
|
||
walletType: filters.walletType || undefined,
|
||
refNo: filters.refNo || undefined,
|
||
sortBy,
|
||
sortDir,
|
||
});
|
||
return { rows: r.content ?? [], total: r.totalElements ?? 0 };
|
||
},
|
||
[
|
||
{ key: "date", title: t("WalletLedgerDate") },
|
||
{ key: "memberCode", title: t("MemberCode") },
|
||
{ key: "memberName", title: t("MemberName") },
|
||
{ key: "wallet", title: t("WalletLedgerWallet") },
|
||
{ key: "type", title: t("WalletLedgerType") },
|
||
{ key: "in", title: t("WalletLedgerIn") },
|
||
{ key: "out", title: t("WalletLedgerOut") },
|
||
{ key: "ref", title: t("WalletLedgerRef") },
|
||
{ key: "refNo", title: t("WalletLedgerRefNo") },
|
||
],
|
||
row => ({
|
||
date: formatDateTime(row.transactionDateTime),
|
||
memberCode: row.memberCode ?? "",
|
||
memberName: row.memberName ?? "",
|
||
wallet: row.walletName ?? "",
|
||
type: walletTrxnLabel(row.trxnType, t),
|
||
in: row.amountIn || "",
|
||
out: row.amountOut || "",
|
||
ref: walletRefWords(row, t),
|
||
refNo: row.refNo ?? "",
|
||
}),
|
||
);
|
||
|
||
const columns = useMemo<ColumnDef<AdminWalletLedgerRow, unknown>[]>(
|
||
() => [
|
||
{
|
||
id: "date",
|
||
accessorKey: "transactionDateTime",
|
||
header: t("WalletLedgerDate"),
|
||
cell: ({ row }) => <span className="tabular">{formatDateTime(row.original.transactionDateTime)}</span>,
|
||
},
|
||
{
|
||
id: "member",
|
||
accessorKey: "memberCode",
|
||
header: t("WalletLedgerMember"),
|
||
cell: ({ row }) => {
|
||
const { memberCode, memberName } = row.original;
|
||
if (!memberCode) return <span className="text-muted-foreground">—</span>;
|
||
return (
|
||
<div className="flex flex-col">
|
||
<Link
|
||
href={`/wallet/${memberCode}`}
|
||
className="font-medium tabular hover:underline focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none"
|
||
>
|
||
{memberCode}
|
||
</Link>
|
||
{memberName && <span className="text-xs text-muted-foreground">{memberName}</span>}
|
||
</div>
|
||
);
|
||
},
|
||
},
|
||
{
|
||
id: "wallet",
|
||
accessorKey: "walletName",
|
||
header: t("WalletLedgerWallet"),
|
||
// BABYBOO-50: each wallet type in its rank's badge colour, drawn like the Members rank badge.
|
||
cell: ({ row }) => {
|
||
if (!row.original.walletName) return "—";
|
||
const tint = walletChipClass(row.original.walletTypeCode);
|
||
return (
|
||
<Badge variant={tint ? "default" : "outline"} className={tint}>
|
||
{row.original.walletName}
|
||
</Badge>
|
||
);
|
||
},
|
||
},
|
||
{
|
||
id: "type",
|
||
accessorKey: "trxnType",
|
||
header: t("WalletLedgerType"),
|
||
cell: ({ row }) => (
|
||
<Badge variant={walletTrxnTone(row.original.trxnType)}>{walletTrxnLabel(row.original.trxnType, t)}</Badge>
|
||
),
|
||
},
|
||
{
|
||
id: "in",
|
||
accessorKey: "amountIn",
|
||
header: t("WalletLedgerIn"),
|
||
meta: { numeric: true },
|
||
cell: ({ row }) => (row.original.amountIn ? `+${formatPoints(row.original.amountIn)}` : "—"),
|
||
},
|
||
{
|
||
id: "out",
|
||
accessorKey: "amountOut",
|
||
header: t("WalletLedgerOut"),
|
||
meta: { numeric: true },
|
||
cell: ({ row }) => (row.original.amountOut ? `−${formatPoints(row.original.amountOut)}` : "—"),
|
||
},
|
||
{
|
||
id: "ref",
|
||
accessorKey: "refNo",
|
||
header: t("WalletLedgerRef"),
|
||
// BABYBOO-50: the reference in words, the raw reference underneath for support.
|
||
cell: ({ row }) => (
|
||
<div className="flex flex-col">
|
||
<span>{walletRefWords(row.original, t)}</span>
|
||
{row.original.refNo && (
|
||
<span className="text-xs text-muted-foreground tabular">{row.original.refNo}</span>
|
||
)}
|
||
</div>
|
||
),
|
||
},
|
||
],
|
||
[t],
|
||
);
|
||
|
||
return (
|
||
<div className="space-y-4">
|
||
<PageHeader title={t("BO_WALLET")}>
|
||
<WalletTabs />
|
||
</PageHeader>
|
||
|
||
{hasModule("BO_WALLET_ADJUST") && (
|
||
<div className="flex items-center justify-end gap-3">
|
||
{saved && <p role="status" className="text-sm text-positive">{t("WalletAdjSaved")}</p>}
|
||
<Button onClick={() => { setSaved(false); setAdjustOpen(true); }}>{t("WalletAdjTitle")}</Button>
|
||
<WalletAdjustDialog
|
||
open={adjustOpen}
|
||
onOpenChange={setAdjustOpen}
|
||
onSaved={() => { setSaved(true); retry(); }}
|
||
/>
|
||
</div>
|
||
)}
|
||
|
||
<FilterPanel
|
||
actions={listingExport.button}
|
||
activeCount={activeCount}
|
||
onClear={clear}
|
||
>
|
||
<FilterField label={t("Member")}>
|
||
<FilterSearch
|
||
className="w-full"
|
||
aria-label={t("Member")}
|
||
placeholder={t("MemberCodeOrName")}
|
||
value={filters.memberCode}
|
||
onChange={memberCode => set({ memberCode })}
|
||
/>
|
||
</FilterField>
|
||
<FilterField label={t("WalletLedgerType")}>
|
||
<FilterSelect
|
||
className="w-full"
|
||
aria-label={t("WalletLedgerType")}
|
||
allLabel={t("WalletLedgerAllTypes")}
|
||
options={WALLET_TRXN_CATEGORIES.map(c => ({ value: c.value, label: t(c.labelKey) }))}
|
||
value={filters.type}
|
||
onChange={type => set({ type })}
|
||
/>
|
||
</FilterField>
|
||
<FilterField label={t("WalletLedgerWallet")}>
|
||
<FilterSelect
|
||
className="w-full"
|
||
aria-label={t("WalletLedgerWallet")}
|
||
allLabel={t("WalletLedgerAllWallets")}
|
||
options={WALLET_TYPE_FILTER_OPTIONS.map(o => ({ value: o.value, label: t(o.labelKey) }))}
|
||
value={filters.walletType}
|
||
onChange={walletType => set({ walletType })}
|
||
/>
|
||
</FilterField>
|
||
<FilterField label={t("WalletLedgerDate")}>
|
||
<DateRangePicker
|
||
fromLabel={t("WalletLedgerFrom")}
|
||
toLabel={t("WalletLedgerTo")}
|
||
value={{ from: filters.from, to: filters.to }}
|
||
onChange={r => set({ from: r.from, to: r.to })}
|
||
/>
|
||
</FilterField>
|
||
<FilterField label={t("WalletLedgerRef")}>
|
||
<FilterSearch
|
||
className="w-full"
|
||
aria-label={t("WalletLedgerRef")}
|
||
placeholder={t("WalletLedgerRef")}
|
||
value={filters.refNo}
|
||
onChange={refNo => set({ refNo })}
|
||
/>
|
||
</FilterField>
|
||
</FilterPanel>
|
||
|
||
{listingExport.errorLine}
|
||
{error ? (
|
||
// Fail loud: a downstream failure is an error state, never an empty table (#71).
|
||
<ErrorState message={error} onRetry={retry} />
|
||
) : (
|
||
<DataTable
|
||
label={t("BO_WALLET")}
|
||
columns={columns}
|
||
data={rows}
|
||
total={total}
|
||
page={page}
|
||
pageSize={pageSize ?? MIN_PAGE_SIZE}
|
||
onPageChange={setPage}
|
||
isLoading={loading}
|
||
rowKey={row => row.refNo ?? `${row.memberCode}-${row.transactionDateTime}-${row.trxnType}`}
|
||
tableRef={tableRef}
|
||
empty={<EmptyState title={t("WalletLedgerEmpty")} />}
|
||
sorting={sorting}
|
||
onSortingChange={onSortingChange}
|
||
/>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|