Files
co-lite/solvway-admin-portal/app/lib/services/wallet.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

111 lines
4.0 KiB
TypeScript

import { ApiError, apiGet, apiPost } from "@/app/lib/clientApi";
import { walletTrxnCategoryCodes } from "@/app/lib/walletTrxnType";
import type {
AdminWalletBalance,
AdminWalletLedgerFilters,
AdminWalletLedgerListPage,
AdminWalletLedgerPage,
AdminWalletLedgerRow,
} from "@/app/lib/types";
// Client over the BFF route handlers (app/api/admin/wallet/**), which forward
// the bb_admin JWT to mobile-api. Read-only: balances + paged ledger.
// A caller without the BO_WALLET module gets a 4xx from mobile-api and the
// view renders its not-authorised state (the RequireModule gate fires first).
export async function getMemberWalletBalances(memberCode: string): Promise<AdminWalletBalance[]> {
return apiGet<AdminWalletBalance[]>(
`/api/admin/wallet/${encodeURIComponent(memberCode)}/balances`,
);
}
/** Paged ledger (Spring page shape from the BFF). */
export async function getMemberWalletLedger(
memberCode: string,
page: number,
pageSize: number,
sortBy?: string,
sortDir?: "asc" | "desc",
): Promise<AdminWalletLedgerPage> {
const params = new URLSearchParams({
page: String(Math.max(0, page - 1)),
size: String(pageSize),
});
if (sortBy) params.set("sortBy", sortBy);
if (sortDir) params.set("sortDir", sortDir);
return apiGet<AdminWalletLedgerPage>(
`/api/admin/wallet/${encodeURIComponent(memberCode)}/ledger?${params.toString()}`,
);
}
/** The wire row carries the backend's displayWalletName; the portal row type exposes it as
* walletName (tasks 3.1). */
type LedgerWireRow = AdminWalletLedgerRow & { displayWalletName?: string };
type LedgerWirePage = Omit<AdminWalletLedgerListPage, "content"> & { content: LedgerWireRow[] };
/**
* Paged global ledger across ALL members (add-admin-wallet-ledger), newest-first, filtered by a
* date range, transaction type and exact member code. The memberCode filter is resolved to an
* owner uuid server-side — the client never supplies an owner id.
*/
export async function getAllWalletLedger(
page: number,
pageSize: number,
filters: AdminWalletLedgerFilters = {},
): Promise<AdminWalletLedgerListPage> {
const params = new URLSearchParams({
page: String(Math.max(0, page - 1)),
size: String(pageSize),
});
// A category filter (in + out) is expanded to its code set and sent as `types`; the backend
// matches ANY of them (e.g. Sales = SLI,SLO).
const types = walletTrxnCategoryCodes(filters.category);
if (types) params.set("types", types);
if (filters.from) params.set("from", filters.from);
if (filters.to) params.set("to", filters.to);
const memberCode = filters.memberCode?.trim();
if (memberCode) params.set("memberCode", memberCode);
if (filters.walletType) params.set("walletType", filters.walletType);
const refNo = filters.refNo?.trim();
if (refNo) params.set("refNo", refNo);
if (filters.sortBy) params.set("sortBy", filters.sortBy);
if (filters.sortDir) params.set("sortDir", filters.sortDir);
const data = await apiGet<LedgerWirePage>(`/api/admin/wallet/ledger?${params.toString()}`);
return {
...data,
content: (data.content ?? []).map(({ displayWalletName, ...row }) => ({
...row,
walletName: displayWalletName,
})),
};
}
export interface WalletAdjustInput {
memberCode: string;
walletCode: "PRODUCT" | "ACORN" | "APPLE";
direction: "IN" | "OUT";
amount: number;
reason: string;
refNo: string;
}
export interface WalletAdjustResult {
refNo: string;
trxnType: string;
amount: number;
balance: number;
}
/** Manual admin adjustment (BO_WALLET_ADJUST). A 409 INSUFFICIENT_BALANCE becomes the
* `WalletAdjInsufficient` error so the dialog can show its own copy; other errors pass through. */
export async function adjustWallet(input: WalletAdjustInput): Promise<WalletAdjustResult> {
try {
return await apiPost<WalletAdjustResult>("/api/admin/wallet/adjust", input);
} catch (e) {
if (e instanceof ApiError && e.status === 409 && (e.data as { code?: string } | undefined)?.code === "INSUFFICIENT_BALANCE") {
throw new Error("WalletAdjInsufficient");
}
throw e;
}
}