2026-08-29 21:06:30 +03:30
|
|
|
import { BadRequestException, ConflictException, Injectable } from '@nestjs/common';
|
2026-09-03 14:20:55 +03:30
|
|
|
import type { ImportContactsResult, PersonMood } from '@mento/shared';
|
|
|
|
|
import { PERSON_MOOD_LABELS, PERSON_MOODS } from '@mento/shared';
|
2026-08-29 21:06:30 +03:30
|
|
|
import { PrismaService } from '../prisma/prisma.service';
|
|
|
|
|
import type { Actor } from '../common/record-scope';
|
|
|
|
|
import { ownerWhere } from '../common/record-scope';
|
2026-09-15 17:33:05 +03:30
|
|
|
import {
|
|
|
|
|
formatJalaliDate,
|
2026-09-19 15:30:31 +03:30
|
|
|
parseExcelMapping,
|
|
|
|
|
previewExcel,
|
2026-09-15 17:33:05 +03:30
|
|
|
readMappedRows,
|
|
|
|
|
writeXlsx,
|
2026-09-19 15:30:31 +03:30
|
|
|
type ExcelPreviewResult,
|
2026-09-15 17:33:05 +03:30
|
|
|
} from '../common/excel';
|
2026-08-29 21:06:30 +03:30
|
|
|
import { CompaniesService } from './companies.service';
|
|
|
|
|
import { PeopleService } from './people.service';
|
|
|
|
|
|
2026-09-03 14:20:55 +03:30
|
|
|
function parseMood(raw?: string): PersonMood | undefined {
|
|
|
|
|
const value = raw?.trim();
|
|
|
|
|
if (!value) return undefined;
|
|
|
|
|
if (PERSON_MOODS.includes(value as PersonMood)) return value as PersonMood;
|
|
|
|
|
return PERSON_MOODS.find((mood) => PERSON_MOOD_LABELS[mood] === value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-15 17:33:05 +03:30
|
|
|
function splitDidarName(firstName: string, lastName: string, title?: string) {
|
|
|
|
|
const first = firstName.trim();
|
|
|
|
|
const last = lastName.trim();
|
|
|
|
|
if (first && last) return { firstName: first.slice(0, 80), lastName: last.slice(0, 80) };
|
|
|
|
|
const full = (first || last || title || '').trim();
|
|
|
|
|
const parts = full.split(/\s+/).filter(Boolean);
|
|
|
|
|
if (parts.length >= 2) {
|
|
|
|
|
return {
|
|
|
|
|
firstName: parts[0]!.slice(0, 80),
|
|
|
|
|
lastName: parts.slice(1).join(' ').slice(0, 80),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (full) return { firstName: full.slice(0, 80), lastName: full.slice(0, 80) };
|
|
|
|
|
return null;
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PERSON_ALIASES: Record<string, string> = {
|
|
|
|
|
firstname: 'firstName',
|
|
|
|
|
نام: 'firstName',
|
2026-09-15 17:33:05 +03:30
|
|
|
ناممشتری: 'firstName',
|
2026-08-29 21:06:30 +03:30
|
|
|
lastname: 'lastName',
|
|
|
|
|
نامخانوادگی: 'lastName',
|
2026-09-15 17:33:05 +03:30
|
|
|
نامخانوادگیمشتری: 'lastName',
|
|
|
|
|
عنوانمشتری: 'title',
|
2026-08-29 21:06:30 +03:30
|
|
|
email: 'email',
|
|
|
|
|
ایمیل: 'email',
|
2026-09-15 17:33:05 +03:30
|
|
|
ایمیلمشتری: 'email',
|
2026-08-29 21:06:30 +03:30
|
|
|
phone: 'phone',
|
|
|
|
|
تلفن: 'phone',
|
2026-09-15 17:33:05 +03:30
|
|
|
تلفنثابت: 'phone',
|
|
|
|
|
تلفنثابتمشتری: 'phone',
|
2026-08-29 21:06:30 +03:30
|
|
|
mobile: 'mobile',
|
|
|
|
|
موبایل: 'mobile',
|
2026-09-15 17:33:05 +03:30
|
|
|
تلفنهمراهمشتری: 'mobile',
|
2026-08-29 21:06:30 +03:30
|
|
|
position: 'position',
|
|
|
|
|
سمت: 'position',
|
2026-09-15 17:33:05 +03:30
|
|
|
سمت1: 'position',
|
2026-08-29 21:06:30 +03:30
|
|
|
company: 'companyName',
|
|
|
|
|
companyname: 'companyName',
|
2026-09-15 17:33:05 +03:30
|
|
|
نامشرکت: 'companyName',
|
2026-08-29 21:06:30 +03:30
|
|
|
شرکت: 'companyName',
|
|
|
|
|
notes: 'notes',
|
|
|
|
|
یادداشت: 'notes',
|
2026-09-15 17:33:05 +03:30
|
|
|
توضیحاتشخص: 'notes',
|
2026-09-03 14:20:55 +03:30
|
|
|
mood: 'mood',
|
|
|
|
|
وضعیت: 'mood',
|
|
|
|
|
وضعیتمشتری: 'mood',
|
2026-09-15 17:33:05 +03:30
|
|
|
گروهبندیشخص: 'mood',
|
|
|
|
|
گروهمشتری: 'group',
|
|
|
|
|
آدرس: 'address',
|
|
|
|
|
آدرس1: 'address',
|
|
|
|
|
آدرسمنزل: 'homeAddress',
|
|
|
|
|
آدرمنزل: 'homeAddress',
|
|
|
|
|
کدپستی: 'postalCode',
|
|
|
|
|
کدپستی1: 'postalCode',
|
|
|
|
|
استانفیلدپیشفرض: 'province',
|
|
|
|
|
استان: 'province',
|
|
|
|
|
شهرستان: 'city',
|
|
|
|
|
شهر1: 'city',
|
|
|
|
|
کدمشتری: 'customerCode',
|
|
|
|
|
کددیدارمشتری: 'didarId',
|
|
|
|
|
کدملیمشتری: 'nationalId',
|
|
|
|
|
تاریخثبتمشتری: 'registeredAt',
|
|
|
|
|
تاریختولدمشتری: 'birthDate',
|
|
|
|
|
تاریختولد1: 'birthDate',
|
|
|
|
|
تاریختولد: 'birthDate',
|
|
|
|
|
منبعاطلاعات: 'source',
|
|
|
|
|
نوعمشتری: 'customerType',
|
|
|
|
|
type: '_type',
|
|
|
|
|
_type: '_type',
|
|
|
|
|
آیدیاینستاگرام: 'instagram',
|
|
|
|
|
معرف: 'referrer',
|
|
|
|
|
سایتهایمشتری: 'website',
|
|
|
|
|
شمارهفکس: 'fax',
|
|
|
|
|
تلفن2: 'phone2',
|
|
|
|
|
تلفنهایدیگرمشتری: 'otherPhones',
|
|
|
|
|
مسئولمشتری: 'ownerName',
|
|
|
|
|
تلفنثابتشرکت: 'companyPhone',
|
|
|
|
|
کددیدارشرکت: 'companyDidarId',
|
|
|
|
|
مجوزمشاهدهمشتری: 'visibility',
|
|
|
|
|
ایمیلهایدیگرمشتری: 'otherEmails',
|
|
|
|
|
کد1: 'customerCode',
|
|
|
|
|
گروه1: 'group',
|
|
|
|
|
تماسهمکار: 'colleagueContact',
|
|
|
|
|
شرکتقبلی: 'previousCompany',
|
|
|
|
|
ستاره: 'star',
|
2026-08-29 21:06:30 +03:30
|
|
|
};
|
|
|
|
|
|
2026-09-19 15:30:31 +03:30
|
|
|
const PERSON_FIELDS = [
|
|
|
|
|
{ key: 'firstName', label: 'نام' },
|
|
|
|
|
{ key: 'lastName', label: 'نام خانوادگی' },
|
|
|
|
|
{ key: 'title', label: 'نام کامل / عنوان' },
|
|
|
|
|
{ key: 'mobile', label: 'موبایل' },
|
|
|
|
|
{ key: 'phone', label: 'تلفن' },
|
|
|
|
|
{ key: 'email', label: 'ایمیل' },
|
|
|
|
|
{ key: 'position', label: 'سمت' },
|
|
|
|
|
{ key: 'companyName', label: 'شرکت' },
|
|
|
|
|
{ key: 'notes', label: 'یادداشت' },
|
|
|
|
|
{ key: 'mood', label: 'وضعیت مشتری' },
|
|
|
|
|
];
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
const COMPANY_ALIASES: Record<string, string> = {
|
|
|
|
|
name: 'name',
|
|
|
|
|
نام: 'name',
|
|
|
|
|
نامشرکت: 'name',
|
|
|
|
|
nationalid: 'nationalId',
|
|
|
|
|
شناسهملی: 'nationalId',
|
|
|
|
|
phone: 'phone',
|
|
|
|
|
تلفن: 'phone',
|
2026-09-15 17:33:05 +03:30
|
|
|
تلفنثابتشرکت: 'phone',
|
2026-08-29 21:06:30 +03:30
|
|
|
email: 'email',
|
|
|
|
|
ایمیل: 'email',
|
|
|
|
|
website: 'website',
|
|
|
|
|
وبسایت: 'website',
|
|
|
|
|
address: 'address',
|
|
|
|
|
آدرس: 'address',
|
|
|
|
|
notes: 'notes',
|
|
|
|
|
یادداشت: 'notes',
|
|
|
|
|
};
|
|
|
|
|
|
2026-09-19 15:30:31 +03:30
|
|
|
const COMPANY_FIELDS = [
|
|
|
|
|
{ key: 'name', label: 'نام شرکت', required: true },
|
|
|
|
|
{ key: 'nationalId', label: 'شناسه ملی' },
|
|
|
|
|
{ key: 'phone', label: 'تلفن' },
|
|
|
|
|
{ key: 'email', label: 'ایمیل' },
|
|
|
|
|
{ key: 'website', label: 'وبسایت' },
|
|
|
|
|
{ key: 'address', label: 'آدرس' },
|
|
|
|
|
{ key: 'notes', label: 'یادداشت' },
|
|
|
|
|
];
|
|
|
|
|
|
2026-09-15 17:33:05 +03:30
|
|
|
const CONTACT_COLUMNS = [
|
|
|
|
|
{ header: '_type', key: '_type', width: 12 },
|
|
|
|
|
{ header: 'کد دیدار مشتری', key: 'didarId', width: 16 },
|
|
|
|
|
{ header: 'کد مشتری', key: 'customerCode', width: 14 },
|
|
|
|
|
{ header: 'عنوان مشتری', key: 'title', width: 16 },
|
|
|
|
|
{ header: 'نام مشتری', key: 'firstName', width: 16 },
|
|
|
|
|
{ header: 'نام خانوادگی مشتری', key: 'lastName', width: 22 },
|
|
|
|
|
{ header: 'ایمیل مشتری', key: 'email', width: 24 },
|
|
|
|
|
{ header: 'شماره فکس', key: 'fax', width: 14 },
|
|
|
|
|
{ header: 'سمت', key: 'position', width: 16 },
|
|
|
|
|
{ header: 'کد پستی', key: 'postalCode', width: 12 },
|
|
|
|
|
{ header: 'توضیحات شخص', key: 'notes', width: 32 },
|
|
|
|
|
{ header: 'تلفن همراه مشتری', key: 'mobile', width: 16 },
|
|
|
|
|
{ header: 'استان (فیلد پیش فرض)', key: 'province', width: 16 },
|
|
|
|
|
{ header: 'شهرستان', key: 'city', width: 14 },
|
|
|
|
|
{ header: 'تلفن ثابت مشتری', key: 'phone', width: 16 },
|
|
|
|
|
{ header: 'تلفن ثابت شرکت', key: 'companyPhone', width: 16 },
|
|
|
|
|
{ header: 'نام شرکت', key: 'companyName', width: 24 },
|
|
|
|
|
{ header: 'کد دیدار شرکت', key: 'companyDidarId', width: 14 },
|
|
|
|
|
{ header: 'مسئول مشتری', key: 'ownerName', width: 16 },
|
|
|
|
|
{ header: 'مجوز مشاهده مشتری', key: 'visibility', width: 18 },
|
|
|
|
|
{ header: 'کد ملی مشتری', key: 'nationalId', width: 16 },
|
|
|
|
|
{ header: 'تاریخ ثبت مشتری', key: 'registeredAt', width: 16 },
|
|
|
|
|
{ header: 'تاریخ تولد مشتری', key: 'birthDate', width: 16 },
|
|
|
|
|
{ header: 'گروه بندی شخص', key: 'mood', width: 16 },
|
|
|
|
|
{ header: 'تلفن های دیگر مشتری', key: 'otherPhones', width: 18 },
|
|
|
|
|
{ header: 'آدرس', key: 'address', width: 28 },
|
|
|
|
|
{ header: 'سایت های مشتری', key: 'website', width: 20 },
|
|
|
|
|
{ header: 'ایمیل های دیگر مشتری', key: 'otherEmails', width: 20 },
|
|
|
|
|
{ header: 'سمت_1', key: 'position2', width: 14 },
|
|
|
|
|
{ header: 'شهر_1', key: 'city2', width: 12 },
|
|
|
|
|
{ header: 'کد_1', key: 'code2', width: 12 },
|
|
|
|
|
{ header: 'گروه_1', key: 'group2', width: 12 },
|
|
|
|
|
{ header: 'آدرس_1', key: 'address2', width: 20 },
|
|
|
|
|
{ header: 'تلفن2', key: 'phone2', width: 14 },
|
|
|
|
|
{ header: 'کدپستی', key: 'postalCode2', width: 12 },
|
|
|
|
|
{ header: 'معرف', key: 'referrer', width: 14 },
|
|
|
|
|
{ header: 'آدرس منزل', key: 'homeAddress', width: 20 },
|
|
|
|
|
{ header: 'تاریخ تولد_1', key: 'birthDate2', width: 14 },
|
|
|
|
|
{ header: 'آی دی اینستاگرام', key: 'instagram', width: 16 },
|
|
|
|
|
{ header: 'گروه مشتری', key: 'group', width: 16 },
|
|
|
|
|
{ header: 'تماس همکار', key: 'colleagueContact', width: 16 },
|
|
|
|
|
{ header: 'شرکت قبلی', key: 'previousCompany', width: 16 },
|
|
|
|
|
{ header: 'نوع مشتری', key: 'customerType', width: 14 },
|
|
|
|
|
{ header: 'ستاره', key: 'star', width: 10 },
|
|
|
|
|
{ header: 'منبع اطلاعات', key: 'source', width: 16 },
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const CUSTOM_KEYS = [
|
|
|
|
|
'didarId',
|
|
|
|
|
'customerCode',
|
|
|
|
|
'title',
|
|
|
|
|
'fax',
|
|
|
|
|
'postalCode',
|
|
|
|
|
'province',
|
|
|
|
|
'city',
|
|
|
|
|
'address',
|
|
|
|
|
'nationalId',
|
|
|
|
|
'birthDate',
|
|
|
|
|
'group',
|
|
|
|
|
'otherPhones',
|
|
|
|
|
'website',
|
|
|
|
|
'otherEmails',
|
|
|
|
|
'instagram',
|
|
|
|
|
'referrer',
|
|
|
|
|
'source',
|
|
|
|
|
'customerType',
|
|
|
|
|
'homeAddress',
|
|
|
|
|
'colleagueContact',
|
|
|
|
|
'previousCompany',
|
|
|
|
|
'star',
|
|
|
|
|
'phone2',
|
|
|
|
|
'companyDidarId',
|
|
|
|
|
'companyPhone',
|
|
|
|
|
'visibility',
|
|
|
|
|
] as const;
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
@Injectable()
|
|
|
|
|
export class ContactsExcelService {
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly prisma: PrismaService,
|
|
|
|
|
private readonly people: PeopleService,
|
|
|
|
|
private readonly companies: CompaniesService,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
async exportPeople(actor: Actor, q?: string): Promise<Buffer> {
|
|
|
|
|
const { items } = await this.people.list(actor, q, 1, 5000);
|
2026-09-15 17:33:05 +03:30
|
|
|
return writeXlsx(
|
|
|
|
|
'Contact',
|
|
|
|
|
CONTACT_COLUMNS,
|
|
|
|
|
items.map((p) => {
|
|
|
|
|
const extra = (p.customFields ?? {}) as Record<string, string>;
|
|
|
|
|
return {
|
|
|
|
|
_type: 'Contact',
|
|
|
|
|
didarId: extra.didarId ?? p.id,
|
|
|
|
|
customerCode: extra.customerCode ?? '',
|
|
|
|
|
title: extra.title ?? '',
|
|
|
|
|
firstName: p.firstName,
|
|
|
|
|
lastName: p.lastName,
|
|
|
|
|
email: p.email ?? '',
|
|
|
|
|
fax: extra.fax ?? '',
|
|
|
|
|
position: p.position ?? extra.position ?? '',
|
|
|
|
|
postalCode: extra.postalCode ?? '',
|
|
|
|
|
notes: p.notes ?? '',
|
|
|
|
|
mobile: p.mobile ?? '',
|
|
|
|
|
province: extra.province ?? '',
|
|
|
|
|
city: extra.city ?? '',
|
|
|
|
|
phone: p.phone ?? '',
|
|
|
|
|
companyPhone: extra.companyPhone ?? '',
|
|
|
|
|
companyName: p.company?.name ?? '',
|
|
|
|
|
companyDidarId: extra.companyDidarId ?? '',
|
|
|
|
|
ownerName: p.owner?.fullName ?? '',
|
|
|
|
|
visibility: 'همه افراد شرکت',
|
|
|
|
|
nationalId: extra.nationalId ?? '',
|
|
|
|
|
registeredAt: formatJalaliDate(p.createdAt),
|
|
|
|
|
birthDate: extra.birthDate ?? '',
|
|
|
|
|
mood: extra.group || PERSON_MOOD_LABELS[p.mood || 'newcomer'],
|
|
|
|
|
otherPhones: extra.otherPhones ?? '',
|
|
|
|
|
address: extra.address ?? '',
|
|
|
|
|
website: extra.website ?? '',
|
|
|
|
|
otherEmails: extra.otherEmails ?? '',
|
|
|
|
|
position2: p.position ?? extra.position ?? '',
|
|
|
|
|
city2: extra.city ?? '',
|
|
|
|
|
code2: extra.customerCode ?? '',
|
|
|
|
|
group2: extra.group ?? '',
|
|
|
|
|
address2: extra.address ?? '',
|
|
|
|
|
phone2: extra.phone2 ?? p.phone ?? '',
|
|
|
|
|
postalCode2: extra.postalCode ?? '',
|
|
|
|
|
referrer: extra.referrer ?? '',
|
|
|
|
|
homeAddress: extra.homeAddress ?? '',
|
|
|
|
|
birthDate2: extra.birthDate ?? '',
|
|
|
|
|
instagram: extra.instagram ?? '',
|
|
|
|
|
group: extra.group ?? '',
|
|
|
|
|
colleagueContact: extra.colleagueContact ?? '',
|
|
|
|
|
previousCompany: extra.previousCompany ?? '',
|
|
|
|
|
customerType: extra.customerType ?? '',
|
|
|
|
|
star: extra.star ?? '',
|
|
|
|
|
source: extra.source ?? '',
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
);
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async exportCompanies(actor: Actor, q?: string): Promise<Buffer> {
|
|
|
|
|
const { items } = await this.companies.list(actor, q, 1, 5000);
|
2026-09-15 17:33:05 +03:30
|
|
|
return writeXlsx(
|
|
|
|
|
'شرکتها',
|
|
|
|
|
[
|
|
|
|
|
{ header: 'نام شرکت', key: 'name', width: 28 },
|
|
|
|
|
{ header: 'شناسه ملی', key: 'nationalId', width: 16 },
|
|
|
|
|
{ header: 'تلفن', key: 'phone', width: 16 },
|
|
|
|
|
{ header: 'ایمیل', key: 'email', width: 28 },
|
|
|
|
|
{ header: 'وبسایت', key: 'website', width: 24 },
|
|
|
|
|
{ header: 'آدرس', key: 'address', width: 32 },
|
|
|
|
|
{ header: 'یادداشت', key: 'notes', width: 32 },
|
|
|
|
|
],
|
|
|
|
|
items.map((c) => ({
|
2026-08-29 21:06:30 +03:30
|
|
|
name: c.name,
|
|
|
|
|
nationalId: c.nationalId ?? '',
|
|
|
|
|
phone: c.phone ?? '',
|
|
|
|
|
email: c.email ?? '',
|
|
|
|
|
website: c.website ?? '',
|
|
|
|
|
address: c.address ?? '',
|
|
|
|
|
notes: c.notes ?? '',
|
2026-09-15 17:33:05 +03:30
|
|
|
})),
|
|
|
|
|
);
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|
|
|
|
|
|
2026-09-19 15:30:31 +03:30
|
|
|
previewPeople(file: Express.Multer.File): Promise<ExcelPreviewResult> {
|
|
|
|
|
return previewExcel(file, PERSON_ALIASES, PERSON_FIELDS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
previewCompanies(file: Express.Multer.File): Promise<ExcelPreviewResult> {
|
|
|
|
|
return previewExcel(file, COMPANY_ALIASES, COMPANY_FIELDS);
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
async importPeople(
|
|
|
|
|
actor: Actor,
|
|
|
|
|
file: Express.Multer.File,
|
2026-09-19 15:30:31 +03:30
|
|
|
mappingRaw?: string,
|
2026-08-29 21:06:30 +03:30
|
|
|
): Promise<ImportContactsResult> {
|
2026-09-19 15:30:31 +03:30
|
|
|
const rows = await readMappedRows(file, PERSON_ALIASES, parseExcelMapping(mappingRaw));
|
2026-09-15 17:33:05 +03:30
|
|
|
const result: ImportContactsResult = { created: 0, skipped: 0, errors: [] };
|
2026-08-29 21:06:30 +03:30
|
|
|
|
|
|
|
|
for (let i = 0; i < rows.length; i++) {
|
|
|
|
|
const row = rows[i]!;
|
|
|
|
|
const rowNum = i + 2;
|
2026-09-15 17:33:05 +03:30
|
|
|
if ((row._type || '').trim() && row._type.trim().toLowerCase() !== 'contact') {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-09-19 15:30:31 +03:30
|
|
|
const names = splitDidarName(row.firstName ?? '', row.lastName ?? '', row.title) ?? {
|
|
|
|
|
firstName: (row.firstName ?? '').trim().slice(0, 80),
|
|
|
|
|
lastName: (row.lastName ?? '').trim().slice(0, 80),
|
|
|
|
|
};
|
|
|
|
|
if (
|
|
|
|
|
!names.firstName &&
|
|
|
|
|
!names.lastName &&
|
|
|
|
|
!row.mobile?.trim() &&
|
|
|
|
|
!row.email?.trim() &&
|
|
|
|
|
!row.phone?.trim()
|
|
|
|
|
) {
|
|
|
|
|
result.errors.push({ row: rowNum, message: 'حداقل نام، موبایل، تلفن یا ایمیل لازم است' });
|
2026-08-29 21:06:30 +03:30
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let companyId: string | null = null;
|
|
|
|
|
const companyName = row.companyName?.trim();
|
|
|
|
|
if (companyName) {
|
|
|
|
|
companyId = await this.findOrCreateCompanyId(actor, companyName);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-15 17:33:05 +03:30
|
|
|
const customFields: Record<string, string> = {};
|
|
|
|
|
for (const key of CUSTOM_KEYS) {
|
|
|
|
|
const value = row[key]?.trim();
|
|
|
|
|
if (value) customFields[key] = value;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
try {
|
|
|
|
|
await this.people.create(actor, {
|
2026-09-15 17:33:05 +03:30
|
|
|
firstName: names.firstName,
|
|
|
|
|
lastName: names.lastName,
|
2026-08-29 21:06:30 +03:30
|
|
|
email: row.email || undefined,
|
2026-09-15 17:33:05 +03:30
|
|
|
phone: row.phone || row.phone2 || undefined,
|
2026-08-29 21:06:30 +03:30
|
|
|
mobile: row.mobile || undefined,
|
|
|
|
|
position: row.position || undefined,
|
|
|
|
|
notes: row.notes || undefined,
|
2026-09-15 17:33:05 +03:30
|
|
|
mood: parseMood(row.mood) ?? parseMood(row.group),
|
2026-08-29 21:06:30 +03:30
|
|
|
companyId,
|
2026-09-15 17:33:05 +03:30
|
|
|
customFields,
|
2026-08-29 21:06:30 +03:30
|
|
|
});
|
|
|
|
|
result.created += 1;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e instanceof ConflictException) {
|
|
|
|
|
result.skipped += 1;
|
|
|
|
|
} else {
|
|
|
|
|
result.errors.push({
|
|
|
|
|
row: rowNum,
|
|
|
|
|
message: e instanceof Error ? e.message : 'خطای ناشناخته',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async importCompanies(
|
|
|
|
|
actor: Actor,
|
|
|
|
|
file: Express.Multer.File,
|
2026-09-19 15:30:31 +03:30
|
|
|
mappingRaw?: string,
|
2026-08-29 21:06:30 +03:30
|
|
|
): Promise<ImportContactsResult> {
|
2026-09-19 15:30:31 +03:30
|
|
|
const rows = await readMappedRows(file, COMPANY_ALIASES, parseExcelMapping(mappingRaw));
|
2026-09-15 17:33:05 +03:30
|
|
|
const result: ImportContactsResult = { created: 0, skipped: 0, errors: [] };
|
2026-08-29 21:06:30 +03:30
|
|
|
|
|
|
|
|
for (let i = 0; i < rows.length; i++) {
|
|
|
|
|
const row = rows[i]!;
|
|
|
|
|
const rowNum = i + 2;
|
|
|
|
|
const name = row.name?.trim() ?? '';
|
|
|
|
|
if (!name) {
|
|
|
|
|
result.errors.push({ row: rowNum, message: 'نام شرکت الزامی است' });
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await this.companies.create(actor, {
|
|
|
|
|
name,
|
|
|
|
|
nationalId: row.nationalId || undefined,
|
|
|
|
|
phone: row.phone || undefined,
|
|
|
|
|
email: row.email || undefined,
|
|
|
|
|
website: row.website || undefined,
|
|
|
|
|
address: row.address || undefined,
|
|
|
|
|
notes: row.notes || undefined,
|
|
|
|
|
});
|
|
|
|
|
result.created += 1;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e instanceof ConflictException) {
|
|
|
|
|
result.skipped += 1;
|
|
|
|
|
} else {
|
|
|
|
|
result.errors.push({
|
|
|
|
|
row: rowNum,
|
|
|
|
|
message: e instanceof Error ? e.message : 'خطای ناشناخته',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-15 17:33:05 +03:30
|
|
|
private async findOrCreateCompanyId(actor: Actor, name: string): Promise<string> {
|
2026-08-29 21:06:30 +03:30
|
|
|
const existing = await this.prisma.company.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
organizationId: actor.organizationId,
|
|
|
|
|
name: { equals: name, mode: 'insensitive' },
|
|
|
|
|
...ownerWhere(actor),
|
|
|
|
|
},
|
|
|
|
|
select: { id: true },
|
|
|
|
|
});
|
|
|
|
|
if (existing) return existing.id;
|
|
|
|
|
const created = await this.companies.create(actor, { name });
|
|
|
|
|
return created.id;
|
|
|
|
|
}
|
|
|
|
|
}
|