Track person import source and previous names; add source report and edit UX.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-10-04 12:01:21 +03:30
co-authored by Cursor
parent e144b2277a
commit 8b37525f2f
15 changed files with 632 additions and 25 deletions
@@ -493,22 +493,82 @@ export class TahesabPersonLinkService {
const parts = name.trim().split(/\s+/);
const firstName = parts[0] || name;
const lastName = parts.slice(1).join(' ') || '—';
const mobile = tel || null;
const sourceRef = String(dto.moshtariCode);
let personId = dto.personId;
if (personId) {
await this.getPerson(organizationId, personId);
} else {
const created = await this.prisma.person.create({
const existing = await this.getPerson(organizationId, personId);
const prevFirst = existing.firstName;
const prevLast = existing.lastName;
const nameChanged =
`${prevFirst} ${prevLast}`.trim() !== `${firstName} ${lastName}`.trim();
await this.prisma.person.update({
where: { id: personId },
data: {
organizationId,
firstName,
lastName,
mobile: tel || null,
phone: tel || null,
notes: `واردشده از ته‌حساب (کد ${dto.moshtariCode})`,
...(nameChanged
? {
previousFirstName: prevFirst || null,
previousLastName: prevLast || null,
}
: {}),
importSource: 'tahesab',
sourceRef,
...(mobile
? {
mobile: existing.mobile || mobile,
phone: existing.phone || mobile,
}
: {}),
},
});
personId = created.id;
} else {
// اگر موبایل تکراری است → همان شخص؛ نام قبلی حفظ و نام ته‌حساب جایگزین
const byMobile = mobile
? await this.prisma.person.findFirst({
where: {
organizationId,
OR: [{ mobile }, { phone: mobile }],
},
})
: null;
if (byMobile) {
const nameChanged =
`${byMobile.firstName} ${byMobile.lastName}`.trim() !==
`${firstName} ${lastName}`.trim();
await this.prisma.person.update({
where: { id: byMobile.id },
data: {
firstName,
lastName,
...(nameChanged
? {
previousFirstName: byMobile.firstName || null,
previousLastName: byMobile.lastName || null,
}
: {}),
importSource: 'tahesab',
sourceRef,
},
});
personId = byMobile.id;
} else {
const created = await this.prisma.person.create({
data: {
organizationId,
firstName,
lastName,
mobile,
phone: mobile,
notes: `واردشده از ته‌حساب (کد ${dto.moshtariCode})`,
importSource: 'tahesab',
sourceRef,
},
});
personId = created.id;
}
}
const link = await this.prisma.personAccountingLink.upsert({