ehsan-dashboards/dashboards/crm/app/api/leads/[id]/activities/route.ts

59 lines
1.8 KiB
TypeScript

// app/api/leads/[id]/activities/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
export async function GET(req: NextRequest, { params }: { params: { id: string } }) {
try {
const { id } = params;
const activities = await prisma.activity.findMany({
where: { leadId: id },
orderBy: { createdAt: 'desc' },
include: {
user: { select: { name: true, email: true } },
},
});
return NextResponse.json({ success: true, activities });
} catch (error: any) {
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
}
}
export async function POST(req: NextRequest, { params }: { params: { id: string } }) {
try {
const { id } = params;
const body = await req.json();
const { type, content, userId, smsTemplate, smsMessageId, smsStatus } = body;
if (!type || !content) {
return NextResponse.json({ success: false, error: 'نوع فعالیت و متن آن الزامی است.' }, { status: 400 });
}
const activity = await prisma.activity.create({
data: {
leadId: id,
userId: userId || null,
type,
content,
smsTemplate,
smsMessageId,
smsStatus,
},
});
// به‌روزرسانی تاریخ آخرین تماس لید در صورت ثبت تماس یا پیامک
if (['call', 'sms', 'meeting'].includes(type)) {
await prisma.lead.update({
where: { id },
data: {
lastContactAt: new Date(),
lastContactType: type === 'call' ? 'تلفنی' : type === 'sms' ? 'پیامک' : 'حضوری',
},
});
}
return NextResponse.json({ success: true, activity });
} catch (error: any) {
return NextResponse.json({ success: false, error: error.message }, { status: 500 });
}
}