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
180 lines
7.5 KiB
TypeScript
180 lines
7.5 KiB
TypeScript
"use client";
|
|
|
|
import { useRef, useState } from "react";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from "@/app/components/ui/dialog";
|
|
import { Input } from "@/app/components/ui/input";
|
|
import { Label } from "@/app/components/ui/label";
|
|
import { RadioGroup, RadioGroupItem } from "@/app/components/ui/radio-group";
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/app/components/ui/select";
|
|
import { Textarea } from "@/app/components/ui/textarea";
|
|
import { formatPoints } from "@/app/lib/format";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
import { adjustWallet, getMemberWalletBalances, type WalletAdjustInput } from "@/app/lib/services/wallet";
|
|
import { WALLET_TYPE_FILTER_OPTIONS } from "@/app/lib/walletTrxnType";
|
|
import type { AdminWalletBalance } from "@/app/lib/types";
|
|
|
|
const MAX_REASON = 150;
|
|
const AMOUNT_RE = /^\d+(\.\d{1,2})?$/;
|
|
const ADJUSTABLE = WALLET_TYPE_FILTER_OPTIONS.filter(o => o.value !== "GIFT");
|
|
|
|
interface Props {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
onSaved: () => void;
|
|
}
|
|
|
|
/** Manual wallet adjustment (BABYBOO-54). The form is mounted only while open, so every opening
|
|
* starts with blank fields and a fresh refNo (the idempotency key a retry within it reuses). */
|
|
export default function WalletAdjustDialog({ open, onOpenChange, onSaved }: Props) {
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-lg">
|
|
<AdjustForm onOpenChange={onOpenChange} onSaved={onSaved} />
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|
|
function AdjustForm({ onOpenChange, onSaved }: Omit<Props, "open">) {
|
|
const t = useT();
|
|
const [refNo] = useState(() => crypto.randomUUID());
|
|
const [memberCode, setMemberCode] = useState("");
|
|
const [wallets, setWallets] = useState<AdminWalletBalance[] | null>(null);
|
|
const [notFound, setNotFound] = useState(false);
|
|
const [walletCode, setWalletCode] = useState("");
|
|
const [direction, setDirection] = useState<"IN" | "OUT" | "">("");
|
|
const [amount, setAmount] = useState("");
|
|
const [reason, setReason] = useState("");
|
|
const [confirming, setConfirming] = useState(false);
|
|
const [saving, setSaving] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const latestCode = useRef("");
|
|
|
|
const lookup = async () => {
|
|
const code = memberCode.trim();
|
|
setWalletCode("");
|
|
setWallets(null);
|
|
setNotFound(false);
|
|
latestCode.current = code;
|
|
if (!code) return;
|
|
try {
|
|
const all = await getMemberWalletBalances(code);
|
|
if (latestCode.current !== code) return; // a newer lookup superseded this one
|
|
const mine = all.filter(b => ADJUSTABLE.some(o => o.value === b.walletTypeCode));
|
|
setWallets(mine);
|
|
setNotFound(mine.length === 0);
|
|
} catch {
|
|
if (latestCode.current === code) setNotFound(true);
|
|
}
|
|
};
|
|
|
|
const walletLabel = (code: string) => t(ADJUSTABLE.find(o => o.value === code)?.labelKey ?? code);
|
|
|
|
const review = () => {
|
|
if (!wallets || !walletCode || !direction || !amount.trim() || !reason.trim()) return setError(t("WalletAdjRequired"));
|
|
if (!AMOUNT_RE.test(amount.trim()) || Number(amount) <= 0) return setError(t("WalletAdjAmountInvalid"));
|
|
setError(null);
|
|
setConfirming(true);
|
|
};
|
|
|
|
const submit = async () => {
|
|
setSaving(true);
|
|
setError(null);
|
|
try {
|
|
await adjustWallet({
|
|
memberCode: memberCode.trim(),
|
|
walletCode: walletCode as WalletAdjustInput["walletCode"],
|
|
direction: direction as "IN" | "OUT",
|
|
amount: Number(amount),
|
|
reason: reason.trim(),
|
|
refNo,
|
|
});
|
|
onSaved();
|
|
onOpenChange(false);
|
|
} catch (e) {
|
|
setError(t(e instanceof Error && e.message === "WalletAdjInsufficient" ? "WalletAdjInsufficient" : "WalletAdjFailed"));
|
|
setSaving(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<DialogHeader>
|
|
<DialogTitle>{t("WalletAdjTitle")}</DialogTitle>
|
|
<DialogDescription>{t("WalletAdjIntro")}</DialogDescription>
|
|
</DialogHeader>
|
|
{confirming ? (
|
|
<>
|
|
<p>
|
|
{t(direction === "IN" ? "WalletAdjConfirmAdd" : "WalletAdjConfirmDeduct", {
|
|
amount: formatPoints(Number(amount)),
|
|
member: memberCode.trim(),
|
|
wallet: walletLabel(walletCode),
|
|
})}
|
|
</p>
|
|
{error && <p role="alert" className="text-sm text-destructive">{error}</p>}
|
|
<div className="flex justify-end gap-2">
|
|
<Button variant="outline" disabled={saving} onClick={() => { setConfirming(false); setError(null); }}>
|
|
{t("Back")}
|
|
</Button>
|
|
<Button disabled={saving} onClick={submit}>{t("WalletAdjSubmit")}</Button>
|
|
</div>
|
|
</>
|
|
) : (
|
|
<>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="wadj-member">{t("WalletAdjMember")}</Label>
|
|
<Input id="wadj-member" value={memberCode} onChange={e => setMemberCode(e.target.value)} onBlur={lookup} />
|
|
{notFound && <p className="text-sm text-destructive">{t("WalletAdjMemberNotFound")}</p>}
|
|
</div>
|
|
{wallets && wallets.length > 0 && (
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="wadj-wallet">{t("WalletAdjWallet")}</Label>
|
|
<Select value={walletCode} onValueChange={setWalletCode}>
|
|
<SelectTrigger id="wadj-wallet" className="w-full">
|
|
<SelectValue />
|
|
</SelectTrigger>
|
|
<SelectContent>
|
|
{wallets.map(w => (
|
|
<SelectItem key={w.walletTypeCode} value={w.walletTypeCode}>
|
|
{walletLabel(w.walletTypeCode)} — {t("WalletAdjBalance", { balance: formatPoints(w.balance) })}
|
|
</SelectItem>
|
|
))}
|
|
</SelectContent>
|
|
</Select>
|
|
</div>
|
|
)}
|
|
<div className="space-y-1.5">
|
|
<Label id="wadj-direction">{t("WalletAdjDirection")}</Label>
|
|
<RadioGroup aria-labelledby="wadj-direction" className="flex gap-4" value={direction} onValueChange={v => setDirection(v as "IN" | "OUT")}>
|
|
<div className="flex items-center gap-2">
|
|
<RadioGroupItem id="wadj-add" value="IN" />
|
|
<Label htmlFor="wadj-add">{t("WalletAdjAdd")}</Label>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<RadioGroupItem id="wadj-deduct" value="OUT" />
|
|
<Label htmlFor="wadj-deduct">{t("WalletAdjDeduct")}</Label>
|
|
</div>
|
|
</RadioGroup>
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="wadj-amount">{t("WalletAdjAmount")}</Label>
|
|
<Input id="wadj-amount" inputMode="decimal" value={amount} onChange={e => setAmount(e.target.value)} />
|
|
</div>
|
|
<div className="space-y-1.5">
|
|
<Label htmlFor="wadj-reason">{t("WalletAdjReason")}</Label>
|
|
<Textarea id="wadj-reason" maxLength={MAX_REASON} value={reason} onChange={e => setReason(e.target.value)} />
|
|
<p className="text-xs text-muted-foreground">{t("WalletAdjReasonHint")}</p>
|
|
</div>
|
|
{error && <p role="alert" className="text-sm text-destructive">{error}</p>}
|
|
<div className="flex justify-end gap-2">
|
|
<Button variant="outline" onClick={() => onOpenChange(false)}>{t("Cancel")}</Button>
|
|
<Button onClick={review}>{t("WalletAdjReview")}</Button>
|
|
</div>
|
|
</>
|
|
)}
|
|
</>
|
|
);
|
|
}
|