"use client"; import { useState } from "react"; import { z } from "zod"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; import { Button, Dialog, FormField, Input } from "@/components/ds"; import { loyaltyApi } from "@/modules/loyalty/services/loyalty-api"; import { createLoyaltyListPage } from "@/modules/loyalty/components/LoyaltyListCrudPage"; import { LoyaltyStatusChip } from "@/modules/loyalty/design-system"; import type { WalletAccount } from "@/modules/loyalty/types"; import { useTenantId } from "@/hooks/useTenantId"; const createSchema = z.object({ program_id: z.string().uuid(), member_id: z.string().uuid(), account_number: z.string().optional(), currency_code: z.string().default("IRR"), status: z.enum(["open", "frozen", "closed"]).default("open"), }); const editSchema = z.object({ status: z.enum(["open", "frozen", "closed"]).optional(), }); function WalletOps({ id }: { id: string }) { const { tenantId } = useTenantId(); const qc = useQueryClient(); const [op, setOp] = useState<"credit" | "debit" | "adjust" | null>(null); const [amount, setAmount] = useState("1000"); const [reason, setReason] = useState(""); const bal = useQuery({ queryKey: ["loyalty", tenantId, "wallet-bal", id], queryFn: () => loyaltyApi.wallets.balance(tenantId!, id), enabled: !!tenantId, }); const m = useMutation({ mutationFn: () => { const body = { amount: Number(amount), reason: reason || undefined }; if (op === "credit") return loyaltyApi.wallets.credit(tenantId!, id, body); if (op === "debit") return loyaltyApi.wallets.debit(tenantId!, id, body); return loyaltyApi.wallets.adjust(tenantId!, id, { amount: Number(amount), reason: reason || "adjust", }); }, onSuccess: () => { toast.success("تراکنش کیف پول ثبت شد"); setOp(null); qc.invalidateQueries({ queryKey: ["loyalty", tenantId, "wallet"] }); }, onError: (e: Error) => toast.error(e.message), }); return (
{bal.data?.balance ?? "…"} setOp(null)} title="تراکنش کیف پول">
setAmount(e.target.value)} /> setReason(e.target.value)} />
); } export default createLoyaltyListPage({ title: "کیف پول وفاداری", description: "حساب کیف پول اعضا — credit/debit/adjust روی ledger append-only.", breadcrumb: "کیف پول", resourceKey: "wallets", createSchema, editSchema, createDefaults: { program_id: "", member_id: "", account_number: "", currency_code: "IRR", status: "open", }, fields: [ { name: "program_id", label: "برنامه", createOnly: true, required: true }, { name: "member_id", label: "عضو", createOnly: true, required: true }, { name: "account_number", label: "شماره", createOnly: true }, { name: "currency_code", label: "ارز", createOnly: true }, { name: "status", label: "وضعیت", type: "select", options: [ { value: "open", label: "باز" }, { value: "frozen", label: "مسدود" }, { value: "closed", label: "بسته" }, ], }, ], columns: [ { key: "account_number", header: "شماره" }, { key: "member_id", header: "عضو" }, { key: "currency_code", header: "ارز" }, { key: "status", header: "وضعیت", render: (r) => , }, ], list: (tid) => loyaltyApi.wallets.list(tid, { page: 1, page_size: 500 }), create: (tid, d) => loyaltyApi.wallets.create(tid, d), update: (tid, id, d) => loyaltyApi.wallets.update(tid, id, d), remove: (tid, id) => loyaltyApi.wallets.delete(tid, id), filterDeleted: true, exportColumns: ["account_number", "member_id", "currency_code", "status"], extraActions: (row) => , });