22 lines
894 B
TypeScript
22 lines
894 B
TypeScript
import { NextResponse } from 'next/server';
|
|
import { z } from 'zod';
|
|
import { apiImport } from '@/lib/server/guard';
|
|
import { importCustomers } from '@/lib/server/crm';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
const bodySchema = z.object({ csv: z.string().min(1), commit: z.boolean().optional() });
|
|
|
|
/** CSV import: `commit:false` returns a validated preview, `commit:true` writes it. */
|
|
export async function POST(request: Request) {
|
|
const g = await apiImport();
|
|
if (g.error) return g.error;
|
|
|
|
const parsed = bodySchema.safeParse(await request.json().catch(() => ({})));
|
|
if (!parsed.success) return NextResponse.json({ error: 'Provide CSV text' }, { status: 400 });
|
|
|
|
const result = await importCustomers(parsed.data.csv, parsed.data.commit === true, g.user);
|
|
if (!result.ok) return NextResponse.json({ error: result.error }, { status: 403 });
|
|
return NextResponse.json(result);
|
|
}
|