Keep previous import source and code when a person is re-imported from another source

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-05 20:54:23 +03:30
co-authored by Cursor
parent c8753a4c4e
commit 46dcf296d3
8 changed files with 103 additions and 11 deletions
+5
View File
@@ -53,6 +53,8 @@ type PersonRow = {
previousLastName: string | null;
importSource: string;
sourceRef: string | null;
previousImportSource: string | null;
previousSourceRef: string | null;
email: string | null;
phone: string | null;
mobile: string | null;
@@ -119,6 +121,7 @@ export class PeopleService {
{ mobile: { contains: q.trim() } },
{ phone: { contains: q.trim() } },
{ sourceRef: { contains: q.trim() } },
{ previousSourceRef: { contains: q.trim() } },
{ company: { name: { contains: q.trim(), mode: 'insensitive' } } },
],
}
@@ -373,6 +376,8 @@ export class PeopleService {
previousFullName: previousFullName || null,
importSource,
sourceRef: row.sourceRef,
previousImportSource: row.previousImportSource,
previousSourceRef: row.previousSourceRef,
fullName: `${row.firstName} ${row.lastName}`.trim(),
email: row.email,
phone: row.phone,
+39
View File
@@ -29,6 +29,45 @@ export function personImportSourceLabel(source: string): string {
return source || PERSON_IMPORT_SOURCE_LABELS.manual;
}
type SourceSnapshot = {
importSource?: string | null;
sourceRef?: string | null;
previousImportSource?: string | null;
previousSourceRef?: string | null;
customFields?: unknown;
};
function didarIdFrom(customFields: unknown): string | null {
if (!customFields || typeof customFields !== 'object') return null;
const raw = (customFields as Record<string, unknown>).didarId;
if (typeof raw === 'number' && Number.isFinite(raw)) return String(raw);
if (typeof raw === 'string' && raw.trim()) return raw.trim();
return null;
}
/**
* وقتی شخصِ موجود از منبع دیگری دوباره وارد می‌شود، منبع و کد فعلی را به‌عنوان «قبلی» نگه می‌دارد.
* اگر منبع یکسان باشد (ورود مجدد از همان منبع)، مقادیر قبلی دست نمی‌خورند.
*/
export function carryPreviousSource(
existing: SourceSnapshot,
nextSource: PersonImportSource,
): { previousImportSource?: string | null; previousSourceRef?: string | null } {
const currentSource = normalizePersonImportSource(existing.importSource);
if (currentSource === nextSource) return {};
let ref = existing.sourceRef?.trim() || null;
let source: string = currentSource;
if (!ref) {
const didarId = didarIdFrom(existing.customFields);
if (didarId && nextSource !== 'didar') {
ref = didarId;
source = 'didar';
}
}
if (source === 'manual' && !ref) return {};
return { previousImportSource: source, previousSourceRef: ref };
}
export function namesDiffer(
aFirst: string,
aLast: string,
@@ -6,6 +6,7 @@ import {
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { PrismaService } from '../../prisma/prisma.service';
import { carryPreviousSource } from '../../contacts/person-source';
import { TahesabConnectionsService } from './connections.service';
import {
CreateInTahesabDto,
@@ -514,6 +515,7 @@ export class TahesabPersonLinkService {
previousLastName: prevLast || null,
}
: {}),
...carryPreviousSource(existing, 'tahesab'),
importSource: 'tahesab',
sourceRef,
...(mobile
@@ -549,6 +551,7 @@ export class TahesabPersonLinkService {
previousLastName: byMobile.lastName || null,
}
: {}),
...carryPreviousSource(byMobile, 'tahesab'),
importSource: 'tahesab',
sourceRef,
},