work on moving customers from tahesab 7

This commit is contained in:
2026-06-29 01:31:09 +03:30
parent ec535d4576
commit 31ac13d308
2 changed files with 55 additions and 26 deletions
+38 -19
View File
@@ -13,6 +13,11 @@ export type ImportStatus = 'idle' | 'running' | 'done' | 'canceled'
/** نتیجهٔ نهایی برای نمایش آیکن/متن مناسب */
export type ImportOutcome = 'running' | 'success' | 'error' | 'partial' | 'canceled'
/** تعداد مشتری در هر درخواست (برای جلوگیری از محدودیت نرخ سرور) */
const CHUNK_SIZE = 50
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms))
/**
* فروشگاه سراسری برای فرایند «وارد کردن همه» مشتریان ته‌حساب.
* چون به‌صورت singleton است، با جابه‌جایی بین صفحات از بین نمی‌رود و
@@ -100,6 +105,19 @@ export const useCustomerImportStore = defineStore('customerImport', () => {
await runLoop()
}
// ارسال یک دسته با تلاش مجدد در صورت خطا (مثلاً محدودیت نرخ موقتی)
async function importChunkWithRetry(chunk: TahesabOnlyCustomer[], attempt = 0): Promise<any> {
try {
return await importFromTahesab(chunk)
} catch (e) {
if (attempt < 2 && !cancelRequested) {
await sleep(1500 * (attempt + 1))
return importChunkWithRetry(chunk, attempt + 1)
}
throw e
}
}
async function runLoop() {
while (processed.value < queue.value.length) {
if (cancelRequested) {
@@ -107,29 +125,30 @@ export const useCustomerImportStore = defineStore('customerImport', () => {
return
}
const customer = queue.value[processed.value]
const chunk = queue.value.slice(processed.value, processed.value + CHUNK_SIZE)
try {
const res = await importFromTahesab([customer])
if (res.success) {
success.value++
doneCodes.value.push(customer.code)
} else {
failed.value++
errors.value.push({
code: customer.code,
name: customer.name,
error: res.errors?.[0]?.error || 'ثبت ناموفق بود',
})
const res = await importChunkWithRetry(chunk)
const errCodes = new Set((res.errors ?? []).map((e: any) => e.code))
for (const c of chunk) {
if (errCodes.has(c.code)) {
const err = (res.errors ?? []).find((e: any) => e.code === c.code)
failed.value++
errors.value.push({ code: c.code, name: c.name, error: err?.error || 'ثبت ناموفق بود' })
} else {
success.value++
doneCodes.value.push(c.code)
}
}
} catch (e) {
failed.value++
errors.value.push({
code: customer.code,
name: customer.name,
error: 'خطا در ارتباط با سرور',
})
// کل دسته ناموفق شد (مثلاً محدودیت نرخ یا قطع ارتباط)
for (const c of chunk) {
failed.value++
errors.value.push({ code: c.code, name: c.name, error: 'خطا در ارتباط با سرور' })
}
}
processed.value++
processed.value += chunk.length
// مکث کوتاه بین دسته‌ها برای جلوگیری از محدودیت نرخ سرور
if (processed.value < queue.value.length) await sleep(250)
}
status.value = 'done'