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
295 lines
12 KiB
TypeScript
295 lines
12 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
import { AlertTriangle, Check, ChevronDown, ChevronUp } from "lucide-react";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { Label } from "@/app/components/ui/label";
|
|
import { Skeleton } from "@/app/components/ui/skeleton";
|
|
import { Textarea } from "@/app/components/ui/textarea";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
import { actOnKyc, getKycDetail, kycDocUrl } from "@/app/lib/services/adminKyc";
|
|
import type { AdminKycDetail, AdminKycPhotoType, AdminKycRow } from "@/app/lib/types";
|
|
import { KycDocViewer } from "./KycDocViewer";
|
|
import { DrawerSection, KycDrawer } from "./KycDrawer";
|
|
import { KycStatusBadge, formatIdentityNo } from "./KycStatusBadge";
|
|
import { formatDate, formatDateTime, formatPhone } from "@/app/lib/format";
|
|
|
|
const DOC_ORDER: AdminKycPhotoType[] = ["ID_FRONT", "ID_BACK"];
|
|
const DOC_LABEL: Record<AdminKycPhotoType, string> = {
|
|
ID_FRONT: "KycDocFront",
|
|
ID_BACK: "KycDocBack",
|
|
};
|
|
|
|
type Action = "approve" | "reject" | "revoke";
|
|
|
|
/**
|
|
* Review drawer for one KYC record (mockups "Admin · Pending review" / "Approved record + revoke").
|
|
* Loads the detail, shows documents / submitted-vs-profile / warnings / history, and offers the
|
|
* actions the status allows: P → approve or reject (remarks required), A → revoke (reason required),
|
|
* R/V → read-only. A 409 means another admin got there first: the drawer says so and reloads.
|
|
*/
|
|
export function KycReviewDrawer({
|
|
uuid,
|
|
onClose,
|
|
onChanged,
|
|
onNavigate,
|
|
}: {
|
|
uuid: string;
|
|
onClose: () => void;
|
|
onChanged: () => void;
|
|
onNavigate: (dir: -1 | 1) => void;
|
|
}) {
|
|
const t = useT();
|
|
const [detail, setDetail] = useState<AdminKycDetail | null>(null);
|
|
const [loadError, setLoadError] = useState<string | null>(null);
|
|
const [remarks, setRemarks] = useState("");
|
|
const [busy, setBusy] = useState<Action | null>(null);
|
|
const [actionError, setActionError] = useState<string | null>(null);
|
|
const [notice, setNotice] = useState<string | null>(null);
|
|
const [revokeOpen, setRevokeOpen] = useState(false);
|
|
const [viewing, setViewing] = useState<AdminKycPhotoType | null>(null);
|
|
|
|
// Depends on uuid only — useT() returns a new function every render, so it must not be a dependency.
|
|
const load = useCallback(() => {
|
|
setLoadError(null);
|
|
return getKycDetail(uuid)
|
|
.then(setDetail)
|
|
.catch((err: unknown) => setLoadError(err instanceof Error ? err.message : "Couldn't load this record."));
|
|
}, [uuid]);
|
|
|
|
useEffect(() => {
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect -- a new record starts from a clean slate before its fetch.
|
|
setDetail(null);
|
|
setRemarks("");
|
|
setRevokeOpen(false);
|
|
setActionError(null);
|
|
setNotice(null);
|
|
void load();
|
|
}, [load]);
|
|
|
|
const act = async (action: Action) => {
|
|
setActionError(null);
|
|
setNotice(null);
|
|
if (action !== "approve" && !remarks.trim()) {
|
|
setActionError(t("KycRequireRemarks"));
|
|
return;
|
|
}
|
|
setBusy(action);
|
|
try {
|
|
await actOnKyc(uuid, action, remarks.trim());
|
|
setRemarks("");
|
|
onChanged();
|
|
await load();
|
|
} catch (err) {
|
|
if (err instanceof ApiError && err.status === 409) {
|
|
setNotice(t("KycAlreadyActioned"));
|
|
onChanged();
|
|
await load();
|
|
} else {
|
|
setActionError(t("KycActionFailed"));
|
|
}
|
|
} finally {
|
|
setBusy(null);
|
|
}
|
|
};
|
|
|
|
const record = detail?.record;
|
|
const title = record ? record.submitName : loadError ? t("KycTitle") : "…";
|
|
|
|
return (
|
|
<>
|
|
<KycDrawer
|
|
title={
|
|
<span className="flex flex-wrap items-center gap-2.5">
|
|
{title}
|
|
{record && <KycStatusBadge status={record.status} />}
|
|
</span>
|
|
}
|
|
description={record ? subtitle(record, t) : undefined}
|
|
onClose={onClose}
|
|
header={
|
|
<div className="flex gap-2">
|
|
<Button variant="outline" size="icon" aria-label={t("KycPrev")} onClick={() => onNavigate(-1)}>
|
|
<ChevronUp aria-hidden="true" className="size-4" />
|
|
</Button>
|
|
<Button variant="outline" size="icon" aria-label={t("KycNext")} onClick={() => onNavigate(1)}>
|
|
<ChevronDown aria-hidden="true" className="size-4" />
|
|
</Button>
|
|
</div>
|
|
}
|
|
footer={record ? renderFooter() : undefined}
|
|
>
|
|
{loadError && <p role="alert" className="text-sm text-destructive">{loadError}</p>}
|
|
{!detail && !loadError && (
|
|
<div className="space-y-3" data-testid="kyc-detail-loading">
|
|
<Skeleton className="h-24 w-full" />
|
|
<Skeleton className="h-40 w-full" />
|
|
</div>
|
|
)}
|
|
{detail && record && (
|
|
<>
|
|
{notice && (
|
|
<p role="status" className="rounded-lg bg-muted px-3.5 py-2.5 text-sm">{notice}</p>
|
|
)}
|
|
{detail.warnings.length > 0 && (
|
|
<div role="alert" className="flex gap-2.5 rounded-lg border border-warning/40 bg-warning/10 px-3.5 py-3 text-sm text-foreground">
|
|
<AlertTriangle aria-hidden="true" className="mt-0.5 size-4 shrink-0" />
|
|
<ul className="space-y-1">
|
|
{detail.warnings.map(w => <li key={w}>{warningText(w, t)}</li>)}
|
|
</ul>
|
|
</div>
|
|
)}
|
|
|
|
<DrawerSection title={t("KycDocuments")}>
|
|
<div className="grid grid-cols-2 gap-2.5">
|
|
{DOC_ORDER.filter(d => detail.docTypes.includes(d)).map(doc => (
|
|
<button
|
|
key={doc}
|
|
type="button"
|
|
onClick={() => setViewing(doc)}
|
|
className="overflow-hidden rounded-lg border text-left transition-shadow hover:shadow-md focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none"
|
|
>
|
|
{/* eslint-disable-next-line @next/next/no-img-element -- auth-proxied private image */}
|
|
<img src={kycDocUrl(uuid, doc)} alt="" loading="lazy" className="h-24 w-full bg-muted object-cover" />
|
|
<span className="block px-2.5 py-2 text-sm font-medium">{t(DOC_LABEL[doc])}</span>
|
|
</button>
|
|
))}
|
|
</div>
|
|
<p className="text-xs text-muted-foreground">{t("KycEnlargeHint")}</p>
|
|
</DrawerSection>
|
|
|
|
<DrawerSection title={t("KycSubmittedDetails")}>
|
|
<div className="overflow-hidden rounded-lg border text-sm">
|
|
<DetailRow label={t("KycNameOnId")} value={record.submitName} />
|
|
<DetailRow
|
|
label={t("KycIdType")}
|
|
value={record.identityType === "MYKAD" ? "MyKad" : "Passport"}
|
|
/>
|
|
<DetailRow
|
|
label={t("KycIdNumber")}
|
|
value={formatIdentityNo(record.identityType, record.identityNo)}
|
|
/>
|
|
<DetailRow label={t("Email")} value={detail.member.email ? detail.member.email.toLowerCase() : "—"} />
|
|
<DetailRow label={t("Phone")} value={formatPhone(detail.member.phoneNoPrefix, detail.member.phoneNo) || "—"} />
|
|
</div>
|
|
{record.status === "P" && <p className="text-xs text-muted-foreground">{t("KycProfileLockNote")}</p>}
|
|
</DrawerSection>
|
|
|
|
<DrawerSection title={t("KycHistory")}>
|
|
<ol className="space-y-2 text-sm">
|
|
{detail.history.map(h => (
|
|
<li key={h.uuid} className="flex gap-2.5">
|
|
<span aria-hidden="true" className={`mt-1.5 size-2 shrink-0 rounded-full ${DOT[h.status]}`} />
|
|
<span>
|
|
<strong>{t(`KycStatus${h.status}`)}</strong>{" "}
|
|
{h.actionBy ? `· ${h.actionBy} · ${formatDateTime(h.actionAt)}` : `· ${formatDateTime(h.createDateTime)}`}
|
|
{h.actionRemarks ? ` — “${h.actionRemarks}”` : ""}
|
|
</span>
|
|
</li>
|
|
))}
|
|
</ol>
|
|
</DrawerSection>
|
|
</>
|
|
)}
|
|
</KycDrawer>
|
|
{viewing && (
|
|
<KycDocViewer src={kycDocUrl(uuid, viewing)} label={t(DOC_LABEL[viewing])} onClose={() => setViewing(null)} />
|
|
)}
|
|
</>
|
|
);
|
|
|
|
// A render function, not a nested component: a component defined in render remounts every keystroke.
|
|
function renderFooter() {
|
|
if (!record) return null;
|
|
if (record.status === "P") {
|
|
return (
|
|
<div className="space-y-2.5">
|
|
<Label htmlFor="kyc-remarks" className="text-sm font-semibold">
|
|
{t("KycRemarks")} <span className="font-normal text-muted-foreground">({t("KycRemarksRejectHint")})</span>
|
|
</Label>
|
|
<Textarea id="kyc-remarks" rows={2} value={remarks} onChange={e => setRemarks(e.target.value)} maxLength={500} />
|
|
{actionError && <p role="alert" className="text-sm text-destructive">{actionError}</p>}
|
|
<div className="flex justify-end gap-2">
|
|
<Button variant="outline" className="border-destructive text-destructive hover:bg-destructive/10"
|
|
disabled={busy !== null} onClick={() => act("reject")}>
|
|
{t("KycReject")}
|
|
</Button>
|
|
<Button disabled={busy !== null} onClick={() => act("approve")}>
|
|
<Check aria-hidden="true" className="size-4" />
|
|
{t("KycApprove")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
if (record.status === "A") {
|
|
return (
|
|
<div className="space-y-2.5">
|
|
<Button
|
|
variant="destructive-outline"
|
|
aria-expanded={revokeOpen}
|
|
aria-controls="kyc-revoke-form"
|
|
onClick={() => setRevokeOpen(o => !o)}
|
|
>
|
|
{t("KycRevokeOpen")}
|
|
</Button>
|
|
{revokeOpen && (
|
|
<div id="kyc-revoke-form" className="space-y-2.5">
|
|
<p className="text-sm font-semibold text-destructive">{t("KycRevokeTitle")}</p>
|
|
<p className="text-sm text-muted-foreground">{t("KycRevokeBody")}</p>
|
|
<Label htmlFor="kyc-revoke" className="text-sm font-semibold">{t("KycRevokeReason")}</Label>
|
|
<Textarea id="kyc-revoke" rows={2} value={remarks} onChange={e => setRemarks(e.target.value)} maxLength={500} />
|
|
{actionError && <p role="alert" className="text-sm text-destructive">{actionError}</p>}
|
|
<div className="flex justify-end">
|
|
<Button variant="destructive" disabled={busy !== null} onClick={() => act("revoke")}>
|
|
{t("KycRevoke")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const DOT: Record<AdminKycRow["status"], string> = {
|
|
P: "bg-warning",
|
|
A: "bg-positive",
|
|
R: "bg-negative",
|
|
V: "bg-muted-foreground",
|
|
};
|
|
|
|
function DetailRow({ label, value }: { label: string; value: string }) {
|
|
return (
|
|
<div className="grid grid-cols-[120px_1fr] gap-3 border-t px-3.5 py-2.5">
|
|
<span className="text-muted-foreground">{label}</span>
|
|
<span className="font-semibold break-words">{value}</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function subtitle(record: AdminKycRow, t: ReturnType<typeof useT>): string {
|
|
return t("KycSubmittedBy", {
|
|
code: record.memberCode,
|
|
by: record.submitChannel === "MEMBER" ? t("KycByMember") : record.submittedBy,
|
|
date: formatDateTime(record.createDateTime),
|
|
});
|
|
}
|
|
|
|
/** Warning codes from services-member: ID_IN_USE_BY:<code> and PREVIOUSLY_REJECTED:<date>:<remarks>. */
|
|
function warningText(code: string, t: ReturnType<typeof useT>): string {
|
|
if (code.startsWith("ID_IN_USE_BY:")) return t("KycWarnIdInUse", { code: code.slice("ID_IN_USE_BY:".length) });
|
|
if (code.startsWith("PREVIOUSLY_REJECTED:")) {
|
|
const rest = code.slice("PREVIOUSLY_REJECTED:".length);
|
|
const sep = rest.indexOf(":");
|
|
return t("KycWarnPreviouslyRejected", {
|
|
date: formatDate(sep < 0 ? rest : rest.slice(0, sep)),
|
|
remarks: sep < 0 ? "" : rest.slice(sep + 1),
|
|
});
|
|
}
|
|
return code;
|
|
}
|