Chat settings in Super Admin and changing the chat title and position.
This commit is contained in:
@@ -3,9 +3,66 @@ import { Cron } from '@nestjs/schedule';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import type { LeadImportJobDto } from '@mento/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import {
|
||||
parseExcelMapping,
|
||||
previewExcel,
|
||||
readMappedRows,
|
||||
type ExcelFieldOption,
|
||||
type ExcelPreviewResult,
|
||||
} from '../common/excel';
|
||||
import { LeadStagesService } from './lead-stages.service';
|
||||
import { LeadsService } from './leads.service';
|
||||
|
||||
const LEAD_ALIASES: Record<string, string> = {
|
||||
title: 'title',
|
||||
name: 'title',
|
||||
shop: 'title',
|
||||
نام: 'title',
|
||||
نامفروشگاه: 'title',
|
||||
فروشگاه: 'title',
|
||||
عنوان: 'title',
|
||||
contactname: 'contactName',
|
||||
owner: 'contactName',
|
||||
مالک: 'contactName',
|
||||
مسئول: 'contactName',
|
||||
ناممالک: 'contactName',
|
||||
ناممخاطب: 'contactName',
|
||||
mobile: 'mobile',
|
||||
phone: 'mobile',
|
||||
tel: 'mobile',
|
||||
موبایل: 'mobile',
|
||||
شماره: 'mobile',
|
||||
شمارهتماس: 'mobile',
|
||||
تلفن: 'phone',
|
||||
تلفنثابت: 'phone',
|
||||
city: 'city',
|
||||
شهر: 'city',
|
||||
region: 'region',
|
||||
منطقه: 'region',
|
||||
محله: 'region',
|
||||
trade: 'trade',
|
||||
صنف: 'trade',
|
||||
رسته: 'trade',
|
||||
source: 'source',
|
||||
منبع: 'source',
|
||||
منبعلید: 'source',
|
||||
notes: 'notes',
|
||||
توضیحات: 'notes',
|
||||
یادداشت: 'notes',
|
||||
};
|
||||
|
||||
const LEAD_FIELDS: ExcelFieldOption[] = [
|
||||
{ key: 'title', label: 'نام / فروشگاه', required: true },
|
||||
{ key: 'mobile', label: 'موبایل', required: true },
|
||||
{ key: 'contactName', label: 'نام مخاطب / مالک' },
|
||||
{ key: 'phone', label: 'تلفن' },
|
||||
{ key: 'city', label: 'شهر' },
|
||||
{ key: 'region', label: 'منطقه' },
|
||||
{ key: 'trade', label: 'صنف' },
|
||||
{ key: 'source', label: 'منبع' },
|
||||
{ key: 'notes', label: 'توضیحات' },
|
||||
];
|
||||
|
||||
@Injectable()
|
||||
export class LeadsImportService {
|
||||
private readonly logger = new Logger(LeadsImportService.name);
|
||||
@@ -17,6 +74,22 @@ export class LeadsImportService {
|
||||
private readonly leads: LeadsService,
|
||||
) {}
|
||||
|
||||
preview(file: Express.Multer.File): Promise<ExcelPreviewResult> {
|
||||
return previewExcel(file, LEAD_ALIASES, LEAD_FIELDS);
|
||||
}
|
||||
|
||||
async rowsFromFile(
|
||||
file: Express.Multer.File,
|
||||
mappingRaw?: string,
|
||||
): Promise<Array<Record<string, string>>> {
|
||||
const rows = await readMappedRows(
|
||||
file,
|
||||
LEAD_ALIASES,
|
||||
parseExcelMapping(mappingRaw),
|
||||
);
|
||||
return rows.map((r) => this.normalizeRow(r));
|
||||
}
|
||||
|
||||
async enqueue(
|
||||
organizationId: string,
|
||||
createdById: string,
|
||||
|
||||
@@ -25,7 +25,6 @@ import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import type { Actor } from '../common/record-scope';
|
||||
import { OrgModuleGuard } from '../billing/org-module.guard';
|
||||
import { RequireOrgModule } from '../billing/org-module.decorator';
|
||||
import { cellStr } from '../common/excel';
|
||||
import { brand } from '@mento/shared';
|
||||
import {
|
||||
AssignLeadsDto,
|
||||
@@ -139,36 +138,43 @@ export class LeadsController {
|
||||
);
|
||||
}
|
||||
|
||||
@Post('import/preview')
|
||||
@UseGuards(RolesGuard)
|
||||
@Roles('owner', 'admin')
|
||||
@UseInterceptors(
|
||||
FileInterceptor('file', {
|
||||
storage: memoryStorage(),
|
||||
limits: { fileSize: 15_000_000 },
|
||||
}),
|
||||
)
|
||||
importPreview(@UploadedFile() file?: Express.Multer.File) {
|
||||
if (!file?.buffer?.length) {
|
||||
throw new BadRequestException('فایل ارسال نشده');
|
||||
}
|
||||
return this.imports.preview(file);
|
||||
}
|
||||
|
||||
@Post('import/file')
|
||||
@UseGuards(RolesGuard)
|
||||
@Roles('owner', 'admin')
|
||||
@UseInterceptors(FileInterceptor('file'))
|
||||
@UseInterceptors(
|
||||
FileInterceptor('file', {
|
||||
storage: memoryStorage(),
|
||||
limits: { fileSize: 15_000_000 },
|
||||
}),
|
||||
)
|
||||
async importFile(
|
||||
@CurrentUser() user: Actor,
|
||||
@UploadedFile() file?: Express.Multer.File,
|
||||
@Body('mapping') mapping?: string,
|
||||
) {
|
||||
if (!file?.buffer?.length) {
|
||||
throw new BadRequestException('فایل ارسال نشده');
|
||||
}
|
||||
const wb = new ExcelJS.Workbook();
|
||||
await wb.xlsx.load(Uint8Array.from(file.buffer) as unknown as ExcelJS.Buffer);
|
||||
const sheet = wb.worksheets[0];
|
||||
if (!sheet) throw new BadRequestException('برگه اکسل خالی است');
|
||||
const headerRow = sheet.getRow(1);
|
||||
const headers: string[] = [];
|
||||
headerRow.eachCell({ includeEmpty: true }, (cell, col) => {
|
||||
headers[col - 1] = cellStr(cell.value);
|
||||
});
|
||||
const rows: Array<Record<string, string>> = [];
|
||||
sheet.eachRow({ includeEmpty: false }, (row, rowNumber) => {
|
||||
if (rowNumber === 1) return;
|
||||
const obj: Record<string, string> = {};
|
||||
headers.forEach((h, i) => {
|
||||
if (!h) return;
|
||||
obj[h] = cellStr(row.getCell(i + 1).value);
|
||||
});
|
||||
if (Object.values(obj).some((v) => v.trim())) rows.push(obj);
|
||||
});
|
||||
const rows = await this.imports.rowsFromFile(file, mapping);
|
||||
if (!rows.length) {
|
||||
throw new BadRequestException('ردیفی برای ایمپورت نیست');
|
||||
}
|
||||
return this.imports.enqueue(
|
||||
user.organizationId,
|
||||
user.userId,
|
||||
|
||||
Reference in New Issue
Block a user