66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getAuthorizedProjectDb, getWritableProjectDb } from '@/lib/project-auth';
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ projectCode: string }> }
|
|
) {
|
|
try {
|
|
const { projectCode } = await params;
|
|
const projectDbResult = await getAuthorizedProjectDb(projectCode);
|
|
|
|
if (!projectDbResult.success) {
|
|
return projectDbResult.response;
|
|
}
|
|
|
|
const customers = await projectDbResult.prisma.customer.findMany({
|
|
orderBy: { createdAt: 'desc' },
|
|
include: {
|
|
_count: { select: { licenses: true } },
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ customers });
|
|
} catch (error) {
|
|
console.error('获取项目客户列表失败:', error);
|
|
return NextResponse.json({ error: '服务器错误' }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ projectCode: string }> }
|
|
) {
|
|
try {
|
|
const { projectCode } = await params;
|
|
const projectDbResult = await getWritableProjectDb(projectCode);
|
|
|
|
if (!projectDbResult.success) {
|
|
return projectDbResult.response;
|
|
}
|
|
|
|
const { companyName, contactName, contactEmail, contactPhone } = await request.json();
|
|
|
|
if (!companyName) {
|
|
return NextResponse.json(
|
|
{ success: false, error: '公司名称不能为空' },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const customer = await projectDbResult.prisma.customer.create({
|
|
data: {
|
|
companyName,
|
|
contactName: contactName || null,
|
|
contactEmail: contactEmail || null,
|
|
contactPhone: contactPhone || null,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json({ success: true, customer });
|
|
} catch (error) {
|
|
console.error('创建项目客户失败:', error);
|
|
return NextResponse.json({ success: false, error: '服务器错误' }, { status: 500 });
|
|
}
|
|
}
|