106 lines
3.3 KiB
TypeScript
106 lines
3.3 KiB
TypeScript
// app/api/import/route.ts
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/prisma';
|
|
import * as XLSX from 'xlsx';
|
|
import { calculateNextFollowup, calculateTemperature } from '@/lib/automation';
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const formData = await req.formData();
|
|
const file = formData.get('file') as Blob | null;
|
|
|
|
if (!file) {
|
|
return NextResponse.json({ success: false, error: 'فایل اکسل انتخاب نشده است.' }, { status: 400 });
|
|
}
|
|
|
|
const arrayBuffer = await file.arrayBuffer();
|
|
const buffer = Buffer.from(arrayBuffer);
|
|
const workbook = XLSX.read(buffer, { type: 'buffer' });
|
|
const sheetName = workbook.SheetNames[0];
|
|
const sheet = workbook.Sheets[sheetName];
|
|
const records: any[] = XLSX.utils.sheet_to_json(sheet);
|
|
|
|
let createdCount = 0;
|
|
let updatedCount = 0;
|
|
let errorCount = 0;
|
|
|
|
for (const row of records) {
|
|
const name = row['نام مجموعه'] || row['name'];
|
|
let phone = String(row['شماره مدیر'] || row['phone'] || row['ownerPhone'] || '').trim();
|
|
|
|
if (!name || !phone) {
|
|
errorCount++;
|
|
continue;
|
|
}
|
|
|
|
phone = phone.replace(/^(\+98|0098)/, '0');
|
|
if (!phone.startsWith('0')) phone = '0' + phone;
|
|
|
|
const city = row['شهر'] || row['city'] || 'تهران';
|
|
const district = row['منطقه'] || row['district'] || '';
|
|
const ownerName = row['نام مدیر'] || row['ownerName'] || 'مدیر مجموعه';
|
|
const stage = row['مرحله فروش'] || row['stage'] || 'ارتباط اولیه';
|
|
const roomsRaw = row['اتاقها'] || row['rooms'] || '';
|
|
|
|
const rooms = typeof roomsRaw === 'string'
|
|
? roomsRaw.split(/[,،-]/).map((r) => r.trim()).filter(Boolean)
|
|
: [];
|
|
|
|
const followup = calculateNextFollowup(stage);
|
|
const temp = calculateTemperature({ stage });
|
|
|
|
try {
|
|
const existing = await prisma.lead.findUnique({ where: { ownerPhone: phone } });
|
|
if (existing) {
|
|
await prisma.lead.update({
|
|
where: { id: existing.id },
|
|
data: {
|
|
name,
|
|
city,
|
|
district,
|
|
ownerName,
|
|
stage,
|
|
},
|
|
});
|
|
updatedCount++;
|
|
} else {
|
|
await prisma.lead.create({
|
|
data: {
|
|
name,
|
|
city,
|
|
district,
|
|
ownerName,
|
|
ownerPhone: phone,
|
|
stage,
|
|
nextAction: followup.nextAction,
|
|
nextFollowupAt: followup.nextFollowupAt,
|
|
nextChannel: followup.nextChannel,
|
|
temperature: temp.label,
|
|
temperatureScore: temp.score,
|
|
rooms: {
|
|
create: rooms.map((title) => ({ title })),
|
|
},
|
|
},
|
|
});
|
|
createdCount++;
|
|
}
|
|
} catch (err) {
|
|
console.error('Row import error:', err);
|
|
errorCount++;
|
|
}
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
summary: {
|
|
createdCount,
|
|
updatedCount,
|
|
errorCount,
|
|
total: records.length,
|
|
},
|
|
});
|
|
} catch (error: any) {
|
|
console.error('Import error:', error);
|
|
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
|
|
}
|
|
}
|