source: src/app/api/customers/route.ts@ 5d6f37a

main
Last change on this file since 5d6f37a was 5d6f37a, checked in by Naum Shapkarovski <naumshapkarovski@…>, 7 weeks ago

add customer

  • Property mode set to 100644
File size: 2.0 KB
Line 
1import { NextRequest, NextResponse } from 'next/server';
2import { customerTableFiltersSchema, newCustomerSchema } from 'mvpmasters-shared';
3import prisma from 'src/lib/prisma';
4import { authenticateRequest } from 'src/lib/auth-middleware';
5
6export async function GET(request: NextRequest) {
7 try {
8 // Authenticate the request
9 const authResult = await authenticateRequest(request);
10 if (authResult instanceof NextResponse) {
11 return authResult;
12 }
13 const { userId } = authResult;
14
15 const searchParams = request.nextUrl.searchParams;
16 const filters = {
17 name: searchParams.get('name') || '',
18 role: searchParams.getAll('role'),
19 status: searchParams.get('status') || '',
20 };
21
22 // Validate filters
23 const validatedFilters = customerTableFiltersSchema.parse(filters);
24
25 const customers = await prisma.customer.findMany({
26 where: {
27 name: { contains: validatedFilters.name, mode: 'insensitive' },
28 status: validatedFilters.status ? { equals: validatedFilters.status } : undefined,
29 },
30 });
31 console.log('customers', customers);
32
33 return NextResponse.json(customers);
34 } catch (error) {
35 return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
36 }
37}
38
39export async function POST(request: NextRequest) {
40 try {
41 // Authenticate the request
42 const authResult = await authenticateRequest(request);
43 if (authResult instanceof NextResponse) {
44 return authResult;
45 }
46 const { userId } = authResult;
47
48 const body = await request.json();
49 const validatedData = newCustomerSchema.parse(body);
50 console.log('validatedData', validatedData);
51
52 const customer = await prisma.customer.create({
53 data: {
54 ...validatedData,
55 // userId,
56 },
57 });
58
59 return NextResponse.json(customer, { status: 201 });
60 } catch (error) {
61 console.error('Error creating customer:', error);
62 return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
63 }
64}
Note: See TracBrowser for help on using the repository browser.