Spaces:
Runtime error
Runtime error
| import { NextRequest, NextResponse } from "next/server"; | |
| import { requireApiUser } from "@/server/auth/session"; | |
| import { | |
| createCustomer, | |
| listCustomers, | |
| normalizeCustomerListFilters, | |
| } from "@/server/customers/service"; | |
| import { createRouteErrorResponse } from "@/server/errors"; | |
| import { | |
| customerListQuerySchema, | |
| customerSchema, | |
| } from "@/server/validation/customer"; | |
| export async function GET(request: NextRequest) { | |
| try { | |
| const user = await requireApiUser(); | |
| const query = customerListQuerySchema.parse( | |
| Object.fromEntries(request.nextUrl.searchParams.entries()), | |
| ); | |
| const filters = normalizeCustomerListFilters(query); | |
| const result = await listCustomers(user.id, filters); | |
| return NextResponse.json({ | |
| ...result, | |
| filters, | |
| }); | |
| } catch (error) { | |
| return createRouteErrorResponse(error); | |
| } | |
| } | |
| export async function POST(request: Request) { | |
| try { | |
| const user = await requireApiUser(); | |
| const body = customerSchema.parse(await request.json()); | |
| const customer = await createCustomer(user.id, body); | |
| return NextResponse.json({ customer }, { status: 201 }); | |
| } catch (error) { | |
| return createRouteErrorResponse(error); | |
| } | |
| } | |