Import Tahesab customers in batches with progress overlay and prefetched rows

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-05 21:08:38 +03:30
co-authored by Cursor
parent 46dcf296d3
commit f88786ef61
2 changed files with 125 additions and 27 deletions
@@ -465,6 +465,7 @@ export class TahesabPersonLinkService {
async importFromTahesab( async importFromTahesab(
organizationId: string, organizationId: string,
dto: ImportFromTahesabDto, dto: ImportFromTahesabDto,
prefetched?: { api: TahesabApi; row?: Record<string, unknown> },
) { ) {
const clash = await this.prisma.personAccountingLink.findFirst({ const clash = await this.prisma.personAccountingLink.findFirst({
where: { where: {
@@ -479,16 +480,19 @@ export class TahesabPersonLinkService {
); );
} }
const { api } = await this.connections.getApi( let row: Record<string, unknown>;
organizationId, if (prefetched?.row) {
dto.connectionId, row = prefetched.row;
); } else {
const raw = await api.doListMoshtariByCode( const api =
dto.moshtariCode, prefetched?.api ??
dto.moshtariCode, (await this.connections.getApi(organizationId, dto.connectionId)).api;
); const raw = await api.doListMoshtariByCode(
const rows = asMoshtariList(raw); dto.moshtariCode,
const row = rows[0] ?? {}; dto.moshtariCode,
);
row = (asMoshtariList(raw)[0] ?? {}) as Record<string, unknown>;
}
const name = pickName(row) || `مشتری ته‌حساب ${dto.moshtariCode}`; const name = pickName(row) || `مشتری ته‌حساب ${dto.moshtariCode}`;
const tel = pickTel(row); const tel = pickTel(row);
const parts = name.trim().split(/\s+/); const parts = name.trim().split(/\s+/);
@@ -668,24 +672,49 @@ export class TahesabPersonLinkService {
error?: string; error?: string;
}> = []; }> = [];
const existingLinks = await this.prisma.personAccountingLink.findMany({
where: { connectionId: dto.connectionId, moshtariCode: { in: codes } },
select: { moshtariCode: true, personId: true },
});
const linkedByCode = new Map(existingLinks.map((l) => [l.moshtariCode, l.personId]));
const pending = codes.filter((c) => !linkedByCode.has(c));
let bulkApi: TahesabApi | undefined;
const rowsByCode = new Map<number, Record<string, unknown>>();
if (pending.length) {
bulkApi = (await this.connections.getApi(organizationId, dto.connectionId)).api;
const minCode = Math.min(...pending);
const maxCode = Math.max(...pending);
if (maxCode - minCode <= 2000) {
try {
const raw = await bulkApi.doListMoshtariByCode(minCode, maxCode);
for (const row of asMoshtariList(raw)) {
const code = pickCode(row);
if (code != null) rowsByCode.set(code, row);
}
} catch {
/* fallback: per-code fetch inside importFromTahesab */
}
}
}
for (const moshtariCode of codes) { for (const moshtariCode of codes) {
try { try {
const existing = await this.prisma.personAccountingLink.findFirst({ const linkedPersonId = linkedByCode.get(moshtariCode);
where: { connectionId: dto.connectionId, moshtariCode }, if (linkedPersonId) {
});
if (existing) {
items.push({ items.push({
moshtariCode, moshtariCode,
ok: true, ok: true,
skipped: true, skipped: true,
personId: existing.personId, personId: linkedPersonId,
}); });
continue; continue;
} }
const r = await this.importFromTahesab(organizationId, { const r = await this.importFromTahesab(
connectionId: dto.connectionId, organizationId,
moshtariCode, { connectionId: dto.connectionId, moshtariCode },
}); bulkApi ? { api: bulkApi, row: rowsByCode.get(moshtariCode) } : undefined,
);
items.push({ moshtariCode, ok: true, personId: r.personId }); items.push({ moshtariCode, ok: true, personId: r.personId });
} catch (e) { } catch (e) {
const message = const message =
+77 -8
View File
@@ -10,8 +10,10 @@ import type {
UpdateTahesabConnectionRequest, UpdateTahesabConnectionRequest,
} from '@mento/shared'; } from '@mento/shared';
import { brand } from '@mento/shared'; import { brand } from '@mento/shared';
import { toFaDigits } from '@/lib/jalali';
import { import {
UiBadge, UiBadge,
UiBusyOverlay,
UiButton, UiButton,
UiCard, UiCard,
UiField, UiField,
@@ -48,6 +50,13 @@ const sync = reactive({
toCode: '', toCode: '',
}); });
const syncBusy = ref(false); const syncBusy = ref(false);
/** تعداد مشتری در هر درخواست ورود دسته‌ای (برای جلوگیری از تایم‌اوت و نمایش درصد) */
const IMPORT_CHUNK = 25;
const importOverlay = reactive<{ open: boolean; message: string; progress: number | null }>({
open: false,
message: '',
progress: null,
});
type SyncResult = { type SyncResult = {
moshtariCode: number | null; moshtariCode: number | null;
name: string; name: string;
@@ -475,18 +484,72 @@ async function importBulk(mode: 'results' | 'range') {
body.toCode = toCode; body.toCode = toCode;
} }
} }
syncBusy.value = true;
importOverlay.open = true;
importOverlay.progress = 0;
importOverlay.message = 'در حال آماده‌سازی فهرست مشتریان…';
const totals = { imported: 0, skipped: 0, failed: 0 };
let firstError = '';
try { try {
syncBusy.value = true; let codes: number[];
const { data } = await api.post<{ if (Array.isArray(body.moshtariCodes)) {
imported: number; codes = body.moshtariCodes as number[];
skipped: number; } else {
failed: number; const { data } = await api.post<{ results: SyncResult[] }>(
}>('/integrations/tahesab/sync/import-bulk', body); '/integrations/tahesab/sync/search',
ok.value = `وارد شد: ${data.imported} · قبلاً بود: ${data.skipped} · ناموفق: ${data.failed}`; body,
await searchTahesab(mode === 'range' ? 'range' : 'filter'); { timeout: 90_000 },
);
const found = data.results ?? [];
totals.skipped += found.filter((r) => r.alreadyLinked).length;
codes = found
.filter((r) => r.moshtariCode != null && !r.alreadyLinked)
.map((r) => r.moshtariCode as number);
}
const total = codes.length;
for (let i = 0; i < total; i += IMPORT_CHUNK) {
const chunk = codes.slice(i, i + IMPORT_CHUNK);
importOverlay.message = `در حال وارد کردن مشتریان… ${toFaDigits(Math.min(i + chunk.length, total))} از ${toFaDigits(total)}`;
try {
const { data } = await api.post<{
imported: number;
skipped: number;
failed: number;
items?: Array<{ moshtariCode: number; ok: boolean; personId?: string; error?: string }>;
}>(
'/integrations/tahesab/sync/import-bulk',
{ connectionId: sync.connectionId, moshtariCodes: chunk },
{ timeout: 120_000 },
);
totals.imported += data.imported;
totals.skipped += data.skipped;
totals.failed += data.failed;
for (const item of data.items ?? []) {
if (!item.ok) {
if (!firstError && item.error) firstError = item.error;
continue;
}
const row = syncResults.value.find((r) => r.moshtariCode === item.moshtariCode);
if (row) {
row.alreadyLinked = true;
if (item.personId) row.personId = item.personId;
}
}
} catch (e) {
totals.failed += chunk.length;
if (!firstError) firstError = apiErr(e, 'خطا در ارسال');
}
importOverlay.progress = total ? ((i + chunk.length) / total) * 100 : 100;
}
ok.value = `وارد شد: ${toFaDigits(totals.imported)} · قبلاً بود: ${toFaDigits(totals.skipped)} · ناموفق: ${toFaDigits(totals.failed)}`;
if (firstError) error.value = `برخی موارد وارد نشدند: ${firstError}`;
} catch (e) { } catch (e) {
error.value = apiErr(e, 'وارد کردن دسته‌ای ناموفق'); error.value = apiErr(e, 'وارد کردن دسته‌ای ناموفق');
} finally { } finally {
importOverlay.open = false;
importOverlay.progress = null;
syncBusy.value = false; syncBusy.value = false;
} }
} }
@@ -980,6 +1043,12 @@ onMounted(load);
</div> </div>
</div> </div>
</UiCard> </UiCard>
<UiBusyOverlay
:open="importOverlay.open"
:message="importOverlay.message"
:progress="importOverlay.progress"
/>
</section> </section>
</template> </template>