35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { apiStaff, apiWrite } from '@/lib/server/guard';
|
|
import { createCustomer, customerSchema, listCustomers } from '@/lib/server/crm';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export async function GET(request: Request) {
|
|
const g = await apiStaff();
|
|
if (g.error) return g.error;
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
const page = Math.max(1, Number(searchParams.get('page') ?? '1') || 1);
|
|
const pageSize = Math.min(50, Math.max(1, Number(searchParams.get('pageSize') ?? '10') || 10));
|
|
const result = await listCustomers({
|
|
q: searchParams.get('q')?.trim() || undefined,
|
|
status: searchParams.get('status') || undefined,
|
|
sort: searchParams.get('sort') || undefined,
|
|
page,
|
|
pageSize,
|
|
});
|
|
return NextResponse.json(result);
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const g = await apiWrite();
|
|
if (g.error) return g.error;
|
|
|
|
const parsed = customerSchema.safeParse(await request.json().catch(() => ({})));
|
|
if (!parsed.success) return NextResponse.json({ error: 'Invalid customer', issues: parsed.error.flatten() }, { status: 400 });
|
|
|
|
const result = await createCustomer(parsed.data, g.user);
|
|
if (!result.ok) return NextResponse.json({ error: result.error }, { status: 409 });
|
|
return NextResponse.json({ customer: result.customer }, { status: 201 });
|
|
}
|