import { ArchiveRestore, Eye, ShieldAlert, Trash2 } from "lucide-react"; import { useEffect, useMemo, useState, type FormEvent } from "react"; import { Button, Card, ErrorBanner, Field, Input, PageTitle, Select } from "../components/Ui"; import { api } from "../lib/api"; import type { ReferenceItem } from "../types"; import { ListToolbar, PaginationControls } from "../components/ListControls"; type Batch = { id: string; name: string; sourceSystem: string; status: string; recordCount: number; issueCount?: number; unresolvedIssueCount?: number; createdAtUtc?: string }; type Preview = { batchId: string; deletableCount: number; blockedCount: number; records: Array<{ entityType: string; entityId: string; canDelete: boolean; blockReason?: string }> }; type Issue = { id: string; processId?: number; sourceIdentity?: string; code: string; message: string; rawValue?: string; resolved: boolean; resolution?: string }; type Details = { batch: Batch; records: unknown[]; issues: Issue[] }; type Family = { processId: number; sourceTable: string; sourceCount: number; migratedSourceCount: number; quarantinedSourceCount: number; difference: number; isBalanced: boolean }; type Reconciliation = { trackedRecordCount: number; existingRecordCount: number; missingRecordCount: number; legacyMapCount: number; unresolvedIssueCount: number; families: Family[]; isConsistent: boolean }; export function TestBatchesPage() { const [themes, setThemes] = useState([]); const [values, setValues] = useState([]); const [themeId, setThemeId] = useState(""); const [valueId, setValueId] = useState(""); const [batches, setBatches] = useState([]); const [batch, setBatch] = useState(null); const [preview, setPreview] = useState(null); const [details, setDetails] = useState
(null); const [reconciliation, setReconciliation] = useState(null); const [error, setError] = useState(""); const [resolutions, setResolutions] = useState>({}); const [search,setSearch]=useState(""); const [statusFilter,setStatusFilter]=useState(""); const [page,setPage]=useState(1); const [pageSize,setPageSize]=useState(25); const filteredBatches=useMemo(()=>{const q=search.trim().toLocaleLowerCase("fa");return batches.filter(x=>(!q||x.name.toLocaleLowerCase("fa").includes(q)||x.sourceSystem.toLocaleLowerCase().includes(q))&&(!statusFilter||x.status===statusFilter));},[batches,search,statusFilter]); const visibleBatches=useMemo(()=>filteredBatches.slice((page-1)*pageSize,page*pageSize),[filteredBatches,page,pageSize]); const statuses=useMemo(()=>[...new Set(batches.map(x=>x.status))].sort(),[batches]); useEffect(()=>setPage(1),[search,statusFilter,pageSize]); async function refresh() { setBatches(await api("/api/v1/test-data-batches")); } useEffect(() => { void api("/api/v1/strategic-themes").then(setThemes).catch(e => setError(e.message)); }, []); useEffect(() => { void refresh().catch(e => setError(e.message)); }, []); useEffect(() => { if (themeId) void api(`/api/v1/innovation-values?strategicThemeId=${themeId}`).then(setValues); }, [themeId]); async function loadBatch(selected: Batch) { setBatch(selected); setPreview(null); const [loadedDetails, loadedReconciliation] = await Promise.all([ api
(`/api/v1/test-data-batches/${selected.id}`), api(`/api/v1/test-data-batches/${selected.id}/reconciliation`) ]); setDetails(loadedDetails); setReconciliation(loadedReconciliation); } async function create(event: FormEvent) { event.preventDefault(); const form = new FormData(event.currentTarget); const result = await api("/api/v1/test-data-batches/synthetic-project", { method: "POST", body: JSON.stringify({ batchName: form.get("batchName"), idempotencyKey: form.get("idempotencyKey"), strategicThemeId: themeId, innovationValueId: valueId, projectNumber: form.get("projectNumber"), projectTitle: form.get("projectTitle") }) }); await refresh(); await loadBatch(result); } async function createAcceptancePackage() { if (!themeId || !valueId) { setError("برای بسته پذیرش، مضمون و ارزش را انتخاب کنید."); return; } const result = await api("/api/v1/test-data-batches/synthetic-acceptance-package", { method: "POST", body: JSON.stringify({ batchName: "پذیرش مهاجرت چندخانواده‌ای", idempotencyKey: crypto.randomUUID(), strategicThemeId: themeId, innovationValueId: valueId }) }); await refresh(); await loadBatch(result); } async function inspect() { if (batch) { setPreview(await api(`/api/v1/test-data-batches/${batch.id}/deletion-preview`)); } } async function remove() { if (!batch || !preview || preview.blockedCount) return; await api(`/api/v1/test-data-batches/${batch.id}/delete`, { method: "POST" }); await refresh(); await loadBatch({ ...batch, status: "Deleted" }); } async function resolve(issue: Issue) { if (!batch || !resolutions[issue.id]?.trim()) return; await api(`/api/v1/test-data-batches/${batch.id}/issues/${issue.id}/resolve`, { method: "POST", body: JSON.stringify({ resolution: resolutions[issue.id] }) }); await loadBatch(batch); await refresh(); } return <>
داده مصنوعی هرگز داده منتقل‌شده واقعی معرفی نمی‌شود. اتصال APADANA فقط با حساب SELECT-only مجاز است.
{error && }

ایجاد بسته مصنوعی

مسیر ساده یا بسته پذیرش چندخانواده‌ای

void create(e).catch(x => setError(x.message))}>

وضعیت و حذف

وضعیت در لحظه حذف دوباره بررسی می‌شود

{!batch ?
بسته‌ای انتخاب نشده
:
شناسه
{batch.id}
وضعیت
{batch.status}
تعداد رکورد
{batch.recordCount}
تطبیق
{reconciliation?.isConsistent ? "سازگار" : "نیازمند بررسی"}
{preview &&

{preview.deletableCount} قابل حذف · {preview.blockedCount} مسدود

{preview.records.map(x =>
{x.entityType}{x.canDelete ? "قابل حذف" : x.blockReason}
)}
}
}
{reconciliation &&

تطبیق خانواده‌های منبع

source = migrated + quarantined

{reconciliation.families.length === 0 ?
آمار منبع برای این بسته ثبت نشده است
:
{reconciliation.families.map(x => )}
ProcessIDجدولمنبعمهاجرتقرنطینهاختلاف
{x.processId}{x.sourceTable}{x.sourceCount}{x.migratedSourceCount}{x.quarantinedSourceCount}{x.difference}
}
} {details && details.issues.length > 0 &&

قرنطینه و خطاهای داده

تعیین تکلیف هر مورد با نام راهبر و زمان ثبت می‌شود

{details.issues.map(x =>
{x.code} · Process {x.processId ?? "-"}

{x.message}

{x.sourceIdentity}{x.resolved ?

تعیین تکلیف شد: {x.resolution}

:
setResolutions(v => ({ ...v, [x.id]: e.target.value }))} placeholder="شرح تعیین تکلیف"/>
}
)}
}

بسته‌های ثبت‌شده

بسته‌های مصنوعی و انتقالی، همراه شمار خطاهای قرنطینه

{filteredBatches.length === 0 ?
بسته‌ای با این فیلتر یافت نشد
: <>
{visibleBatches.map(x => )}
ناممنشأوضعیترکوردخطای باز
{x.name}{x.sourceSystem}{batchStatusLabel(x.status)}{x.recordCount}{x.unresolvedIssueCount ?? 0}
}
; } function batchStatusLabel(value:string){return ({Previewed:"پیش‌نمایش‌شده",Importing:"در حال انتقال",Completed:"تکمیل‌شده",PartiallyBlocked:"دارای مورد مسدود",Deleted:"حذف‌شده",Failed:"ناموفق"} as Record)[value]??value;}