From 57a0b379f69badbb60ebf633da74ffd72b2cf812 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Thu, 5 Nov 2026 11:11:40 +0330 Subject: [PATCH] fix(tahesab): store a matched mobile as 09xxxxxxxxx Co-authored-by: Cursor --- .../tahesab/person-link.service.ts | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/apps/api/src/integrations/tahesab/person-link.service.ts b/apps/api/src/integrations/tahesab/person-link.service.ts index 1f54e44..94b5003 100644 --- a/apps/api/src/integrations/tahesab/person-link.service.ts +++ b/apps/api/src/integrations/tahesab/person-link.service.ts @@ -9,7 +9,11 @@ import { import { Prisma } from '@prisma/client'; import { PrismaService } from '../../prisma/prisma.service'; import { carryPreviousSource } from '../../contacts/person-source'; -import { phoneIdentityKeys, storedPhoneNumber } from '../../contacts/normalize'; +import { + canonicalIranMobile, + phoneIdentityKeys, + storedPhoneNumber, +} from '../../contacts/normalize'; import { applyPersonGenderTitle } from '@mento/shared'; import { TahesabConnectionsService } from './connections.service'; import { @@ -26,6 +30,20 @@ import { formatJalaliDate, parseBirthDate } from '../../common/excel'; import { CustomerGroupsService } from '../../customer-groups/customer-groups.service'; import { normalizeIranMobile, SanadBuilders, type TahesabApi } from './tahesab-methods'; +/** موبایل ته‌حساب را به شکل 0912… ذخیره می‌کند، حتی اگر قبلاً 912… یا +98912… بوده باشد. */ +function canonicalMobileUpdate( + incoming: string | null, + current: { mobile: string | null; phone: string | null }, +): { mobile?: string; phone?: string } { + const mobile = + canonicalIranMobile(incoming) ?? + canonicalIranMobile(current.mobile) ?? + canonicalIranMobile(current.phone); + if (!mobile) return {}; + const phone = canonicalIranMobile(current.phone); + return phone ? { mobile, phone } : { mobile }; +} + /** اندازهٔ هر صفحهٔ «همه» (بازهٔ کد) — امن برای پروکسی/nginx */ const LIST_PAGE_SIZE = 2000; const LIST_TIMEOUT_MS = 55_000; @@ -605,12 +623,7 @@ export class TahesabPersonLinkService { importSource: 'tahesab', sourceRef, ...(!existing.birthDate && pickBirthDate(row) ? { birthDate: pickBirthDate(row) } : {}), - ...(mobile - ? { - mobile: storedPhoneNumber(existing.mobile) || mobile, - phone: storedPhoneNumber(existing.phone) || mobile, - } - : {}), + ...canonicalMobileUpdate(mobile, existing), }, }); } else { @@ -650,6 +663,7 @@ export class TahesabPersonLinkService { importSource: 'tahesab', sourceRef, ...(!byMobile.birthDate && pickBirthDate(row) ? { birthDate: pickBirthDate(row) } : {}), + ...canonicalMobileUpdate(mobile, byMobile), }, }); personId = byMobile.id;