From be79441d8f1b34691312103fda0db07311a1af78 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Wed, 25 Nov 2026 20:43:55 +0330 Subject: [PATCH] Relabel false Didar contact sources as Excel and stop exporting CRM ids as Didar codes. Co-authored-by: Cursor --- .../migration.sql | 25 +++++++++++++++++++ .../src/contacts/contacts-excel.service.ts | 24 ++++++++++-------- apps/api/src/contacts/person-source.ts | 20 +++++++++++++-- apps/api/src/pipelines/deals-excel.service.ts | 22 ++++++++++++---- 4 files changed, 73 insertions(+), 18 deletions(-) create mode 100644 apps/api/prisma/migrations/20261125190000_fix_false_didar_import_source/migration.sql diff --git a/apps/api/prisma/migrations/20261125190000_fix_false_didar_import_source/migration.sql b/apps/api/prisma/migrations/20261125190000_fix_false_didar_import_source/migration.sql new file mode 100644 index 0000000..edc1fbb --- /dev/null +++ b/apps/api/prisma/migrations/20261125190000_fix_false_didar_import_source/migration.sql @@ -0,0 +1,25 @@ +-- برچسب اشتباه «دیدار» روی اشخاص/شرکت‌هایی که کد مبدأ واقعی دیدار نداشتند +-- (خروجی اکسل قبلاً id داخلی CRM را در ستون کد دیدار می‌گذاشت). + +UPDATE "Person" +SET + "customFields" = "customFields" - 'didarId', + "sourceRef" = CASE WHEN "sourceRef" = "id" THEN NULL ELSE "sourceRef" END +WHERE ("customFields"->>'didarId') = "id" + OR "sourceRef" = "id"; + +UPDATE "Person" +SET "importSource" = 'excel' +WHERE "importSource" = 'didar'; + +UPDATE "Person" +SET "previousImportSource" = 'excel' +WHERE "previousImportSource" = 'didar'; + +UPDATE "Company" +SET "sourceRef" = CASE WHEN "sourceRef" = "id" THEN NULL ELSE "sourceRef" END +WHERE "sourceRef" = "id"; + +UPDATE "Company" +SET "importSource" = 'excel' +WHERE "importSource" = 'didar'; diff --git a/apps/api/src/contacts/contacts-excel.service.ts b/apps/api/src/contacts/contacts-excel.service.ts index 1b85348..c8fb79c 100644 --- a/apps/api/src/contacts/contacts-excel.service.ts +++ b/apps/api/src/contacts/contacts-excel.service.ts @@ -25,7 +25,7 @@ import { import { CompaniesService } from './companies.service'; import { PeopleService } from './people.service'; import { normalizeEmail, normalizePhone } from './normalize'; -import { normalizePersonImportSource } from './person-source'; +import { resolveDidarSourceRef } from './person-source'; import { CustomerGroupsService, splitGroupNames, @@ -397,7 +397,10 @@ export class ContactsExcelService { const groupText = groupNamesText(p.groups, extra.group); return { _type: 'Contact', - didarId: extra.didarId ?? p.id, + didarId: + p.importSource === 'didar' + ? p.sourceRef || extra.didarId || '' + : '', customerCode: extra.customerCode ?? '', title: extra.title ?? '', firstName: p.firstName, @@ -586,14 +589,13 @@ export class ContactsExcelService { })(); try { - const didarId = customFields.didarId?.trim() || ''; + const didarId = resolveDidarSourceRef(customFields.didarId); const customerCode = customFields.customerCode?.trim() || ''; - const sourceCol = customFields.source?.trim() || ''; - const importSource = didarId - ? 'didar' - : sourceCol - ? normalizePersonImportSource(sourceCol) - : 'excel'; + // ایمپورت اکسل فقط با کد مبدأ واقعی دیدار برچسب «دیدار» می‌گیرد؛ + // cuid داخلی یا ستون «منبع اطلاعات» باعث دیدار شدن نمی‌شود. + if (didarId) customFields.didarId = didarId; + else delete customFields.didarId; + const importSource = didarId ? 'didar' : 'excel'; const sourceRef = didarId || customerCode || null; const groupIds = await this.groups.idsForNames(actor.organizationId, groupNames); @@ -691,7 +693,7 @@ export class ContactsExcelService { else ownerMissing = true; } - const didarId = row.didarId?.trim() || ''; + const didarId = resolveDidarSourceRef(row.didarId) || ''; const customFields: Record = {}; const visibility = row.visibility?.trim(); if (visibility) customFields.visibility = visibility; @@ -800,7 +802,7 @@ export class ContactsExcelService { didarId?: string, phone?: string, ): Promise { - const ref = didarId?.trim() || ''; + const ref = resolveDidarSourceRef(didarId) || ''; if (ref) { const byRef = await this.prisma.company.findFirst({ where: { diff --git a/apps/api/src/contacts/person-source.ts b/apps/api/src/contacts/person-source.ts index 1caddae..b5da88e 100644 --- a/apps/api/src/contacts/person-source.ts +++ b/apps/api/src/contacts/person-source.ts @@ -29,6 +29,20 @@ export function personImportSourceLabel(source: string): string { return source || PERSON_IMPORT_SOURCE_LABELS.manual; } +/** + * کد مبدأ واقعی دیدار برای برچسب‌زدن منبع. + * cuid داخلی مونتاکت و مقادیر توخالی را دیدار حساب نکن + * (باگ قبلی: خروجی اکسل id داخلی را در ستون کد دیدار می‌گذاشت). + */ +export function resolveDidarSourceRef(raw?: string | null): string | null { + const v = String(raw ?? '').trim(); + if (!v) return null; + if (/^(0+|-+|\.+|n\/?a|null|undefined|none)$/i.test(v)) return null; + // Prisma/cuid داخلی (~25 کاراکتر با پیشوند c) + if (/^c[a-z0-9]{20,32}$/i.test(v)) return null; + return v; +} + type SourceSnapshot = { importSource?: string | null; sourceRef?: string | null; @@ -40,8 +54,10 @@ type SourceSnapshot = { function didarIdFrom(customFields: unknown): string | null { if (!customFields || typeof customFields !== 'object') return null; const raw = (customFields as Record).didarId; - if (typeof raw === 'number' && Number.isFinite(raw)) return String(raw); - if (typeof raw === 'string' && raw.trim()) return raw.trim(); + if (typeof raw === 'number' && Number.isFinite(raw)) { + return resolveDidarSourceRef(String(raw)); + } + if (typeof raw === 'string') return resolveDidarSourceRef(raw); return null; } diff --git a/apps/api/src/pipelines/deals-excel.service.ts b/apps/api/src/pipelines/deals-excel.service.ts index be79d50..dc7da83 100644 --- a/apps/api/src/pipelines/deals-excel.service.ts +++ b/apps/api/src/pipelines/deals-excel.service.ts @@ -61,10 +61,11 @@ function parseTags(raw?: string) { .slice(0, 20); } -/** کدهای دیدار: «0» و خالی یعنی بدون کد */ +/** کدهای دیدار: «0»، خالی، و cuid داخلی یعنی بدون کد مبدأ واقعی */ function cleanCode(raw?: string): string { const v = toAsciiDigits(raw ?? '').trim(); if (!v || /^0+$/.test(v)) return ''; + if (/^c[a-z0-9]{20,32}$/i.test(v)) return ''; return v; } @@ -126,9 +127,18 @@ export class DealsExcelService { lastName: true, mobile: true, sourceRef: true, + importSource: true, + }, + }, + company: { + select: { + id: true, + name: true, + phone: true, + sourceRef: true, + importSource: true, }, }, - company: { select: { id: true, name: true, phone: true, sourceRef: true } }, owner: { select: { fullName: true } }, createdBy: { select: { fullName: true } }, pipeline: { select: { name: true } }, @@ -157,7 +167,7 @@ export class DealsExcelService { const extra = asCustomFields(row.customFields); const base: Record = { _type: 'Deal', - didarId: row.sourceRef ?? row.id, + didarId: row.importSource === 'didar' ? row.sourceRef || '' : '', code: row.code ?? '', title: row.title, amount: row.amount == null ? 0 : Number(row.amount), @@ -181,10 +191,12 @@ export class DealsExcelService { stageName: row.stage.name, personFirstName: row.person?.firstName ?? '', personName: row.person?.lastName ?? '', - personCode: row.person ? (row.person.sourceRef ?? row.person.id) : '', + personCode: + row.person?.importSource === 'didar' ? row.person.sourceRef || '' : '', personMobile: row.person?.mobile ?? '', companyName: row.company?.name ?? '', - companyCode: row.company ? (row.company.sourceRef ?? row.company.id) : '', + companyCode: + row.company?.importSource === 'didar' ? row.company.sourceRef || '' : '', companyPhone: row.company?.phone ?? '', }; for (const d of defs) {