Relabel false Didar contact sources as Excel and stop exporting CRM ids as Didar codes.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-25 20:43:55 +03:30
co-authored by Cursor
parent f3f043d4e8
commit be79441d8f
4 changed files with 73 additions and 18 deletions
@@ -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';
+13 -11
View File
@@ -25,7 +25,7 @@ import {
import { CompaniesService } from './companies.service'; import { CompaniesService } from './companies.service';
import { PeopleService } from './people.service'; import { PeopleService } from './people.service';
import { normalizeEmail, normalizePhone } from './normalize'; import { normalizeEmail, normalizePhone } from './normalize';
import { normalizePersonImportSource } from './person-source'; import { resolveDidarSourceRef } from './person-source';
import { import {
CustomerGroupsService, CustomerGroupsService,
splitGroupNames, splitGroupNames,
@@ -397,7 +397,10 @@ export class ContactsExcelService {
const groupText = groupNamesText(p.groups, extra.group); const groupText = groupNamesText(p.groups, extra.group);
return { return {
_type: 'Contact', _type: 'Contact',
didarId: extra.didarId ?? p.id, didarId:
p.importSource === 'didar'
? p.sourceRef || extra.didarId || ''
: '',
customerCode: extra.customerCode ?? '', customerCode: extra.customerCode ?? '',
title: extra.title ?? '', title: extra.title ?? '',
firstName: p.firstName, firstName: p.firstName,
@@ -586,14 +589,13 @@ export class ContactsExcelService {
})(); })();
try { try {
const didarId = customFields.didarId?.trim() || ''; const didarId = resolveDidarSourceRef(customFields.didarId);
const customerCode = customFields.customerCode?.trim() || ''; const customerCode = customFields.customerCode?.trim() || '';
const sourceCol = customFields.source?.trim() || ''; // ایمپورت اکسل فقط با کد مبدأ واقعی دیدار برچسب «دیدار» می‌گیرد؛
const importSource = didarId // cuid داخلی یا ستون «منبع اطلاعات» باعث دیدار شدن نمی‌شود.
? 'didar' if (didarId) customFields.didarId = didarId;
: sourceCol else delete customFields.didarId;
? normalizePersonImportSource(sourceCol) const importSource = didarId ? 'didar' : 'excel';
: 'excel';
const sourceRef = didarId || customerCode || null; const sourceRef = didarId || customerCode || null;
const groupIds = await this.groups.idsForNames(actor.organizationId, groupNames); const groupIds = await this.groups.idsForNames(actor.organizationId, groupNames);
@@ -691,7 +693,7 @@ export class ContactsExcelService {
else ownerMissing = true; else ownerMissing = true;
} }
const didarId = row.didarId?.trim() || ''; const didarId = resolveDidarSourceRef(row.didarId) || '';
const customFields: Record<string, string> = {}; const customFields: Record<string, string> = {};
const visibility = row.visibility?.trim(); const visibility = row.visibility?.trim();
if (visibility) customFields.visibility = visibility; if (visibility) customFields.visibility = visibility;
@@ -800,7 +802,7 @@ export class ContactsExcelService {
didarId?: string, didarId?: string,
phone?: string, phone?: string,
): Promise<string> { ): Promise<string> {
const ref = didarId?.trim() || ''; const ref = resolveDidarSourceRef(didarId) || '';
if (ref) { if (ref) {
const byRef = await this.prisma.company.findFirst({ const byRef = await this.prisma.company.findFirst({
where: { where: {
+18 -2
View File
@@ -29,6 +29,20 @@ export function personImportSourceLabel(source: string): string {
return source || PERSON_IMPORT_SOURCE_LABELS.manual; 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 = { type SourceSnapshot = {
importSource?: string | null; importSource?: string | null;
sourceRef?: string | null; sourceRef?: string | null;
@@ -40,8 +54,10 @@ type SourceSnapshot = {
function didarIdFrom(customFields: unknown): string | null { function didarIdFrom(customFields: unknown): string | null {
if (!customFields || typeof customFields !== 'object') return null; if (!customFields || typeof customFields !== 'object') return null;
const raw = (customFields as Record<string, unknown>).didarId; const raw = (customFields as Record<string, unknown>).didarId;
if (typeof raw === 'number' && Number.isFinite(raw)) return String(raw); if (typeof raw === 'number' && Number.isFinite(raw)) {
if (typeof raw === 'string' && raw.trim()) return raw.trim(); return resolveDidarSourceRef(String(raw));
}
if (typeof raw === 'string') return resolveDidarSourceRef(raw);
return null; return null;
} }
+17 -5
View File
@@ -61,10 +61,11 @@ function parseTags(raw?: string) {
.slice(0, 20); .slice(0, 20);
} }
/** کدهای دیدار: «0» و خالی یعنی بدون کد */ /** کدهای دیدار: «0»، خالی، و cuid داخلی یعنی بدون کد مبدأ واقعی */
function cleanCode(raw?: string): string { function cleanCode(raw?: string): string {
const v = toAsciiDigits(raw ?? '').trim(); const v = toAsciiDigits(raw ?? '').trim();
if (!v || /^0+$/.test(v)) return ''; if (!v || /^0+$/.test(v)) return '';
if (/^c[a-z0-9]{20,32}$/i.test(v)) return '';
return v; return v;
} }
@@ -126,9 +127,18 @@ export class DealsExcelService {
lastName: true, lastName: true,
mobile: true, mobile: true,
sourceRef: 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 } }, owner: { select: { fullName: true } },
createdBy: { select: { fullName: true } }, createdBy: { select: { fullName: true } },
pipeline: { select: { name: true } }, pipeline: { select: { name: true } },
@@ -157,7 +167,7 @@ export class DealsExcelService {
const extra = asCustomFields(row.customFields); const extra = asCustomFields(row.customFields);
const base: Record<string, string | number | null> = { const base: Record<string, string | number | null> = {
_type: 'Deal', _type: 'Deal',
didarId: row.sourceRef ?? row.id, didarId: row.importSource === 'didar' ? row.sourceRef || '' : '',
code: row.code ?? '', code: row.code ?? '',
title: row.title, title: row.title,
amount: row.amount == null ? 0 : Number(row.amount), amount: row.amount == null ? 0 : Number(row.amount),
@@ -181,10 +191,12 @@ export class DealsExcelService {
stageName: row.stage.name, stageName: row.stage.name,
personFirstName: row.person?.firstName ?? '', personFirstName: row.person?.firstName ?? '',
personName: row.person?.lastName ?? '', 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 ?? '', personMobile: row.person?.mobile ?? '',
companyName: row.company?.name ?? '', 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 ?? '', companyPhone: row.company?.phone ?? '',
}; };
for (const d of defs) { for (const d of defs) {