17 lines
787 B
TypeScript
17 lines
787 B
TypeScript
import { z } from 'zod';
|
|
import { importCustomers } from '../../../utils/crm';
|
|
import { requireImport } from '../../../utils/guard';
|
|
|
|
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 default defineEventHandler(async (event) => {
|
|
const user = await requireImport(event);
|
|
const parsed = bodySchema.safeParse(await readBody(event).catch(() => ({})));
|
|
if (!parsed.success) throw createError({ statusCode: 400, statusMessage: 'Provide CSV text' });
|
|
|
|
const result = await importCustomers(parsed.data.csv, parsed.data.commit === true, user);
|
|
if (!result.ok) throw createError({ statusCode: 403, statusMessage: result.error });
|
|
return result;
|
|
});
|