1 | import { NextRequest, NextResponse } from 'next/server';
|
---|
2 | import { customerTableFiltersSchema, newCustomerSchema } from 'src/schemas';
|
---|
3 | import prisma from 'src/lib/prisma';
|
---|
4 | import { authenticateRequest } from 'src/lib/auth-middleware';
|
---|
5 | import { Prisma, ClientStatus } from '@prisma/client';
|
---|
6 |
|
---|
7 | export async function GET(request: NextRequest) {
|
---|
8 | try {
|
---|
9 | // Authenticate the request
|
---|
10 | const authResult = await authenticateRequest(request);
|
---|
11 | if (authResult instanceof NextResponse) {
|
---|
12 | return authResult;
|
---|
13 | }
|
---|
14 | const { userId, tenantId } = authResult;
|
---|
15 |
|
---|
16 | const searchParams = request.nextUrl.searchParams;
|
---|
17 | const filters = {
|
---|
18 | name: searchParams.get('name') || '',
|
---|
19 | role: searchParams.getAll('role'),
|
---|
20 | status: searchParams.get('status') || '',
|
---|
21 | };
|
---|
22 |
|
---|
23 | // Validate filters
|
---|
24 | const validatedFilters = customerTableFiltersSchema.parse(filters);
|
---|
25 |
|
---|
26 | // const customers = await prisma.$queryRaw`
|
---|
27 | // SELECT * FROM "Client"
|
---|
28 | // WHERE "tenant_id" = ${tenantId}
|
---|
29 | // AND LOWER(name) LIKE LOWER(${`%${validatedFilters.name}%`})
|
---|
30 | // ${
|
---|
31 | // validatedFilters.status
|
---|
32 | // ? Prisma.sql`AND status = ${validatedFilters.status}::"CustomerStatus"`
|
---|
33 | // : Prisma.sql`AND TRUE`
|
---|
34 | // }
|
---|
35 | // `;
|
---|
36 |
|
---|
37 | const customers = await prisma.client.findMany({
|
---|
38 | where: {
|
---|
39 | tenantId,
|
---|
40 | name: {
|
---|
41 | contains: validatedFilters.name,
|
---|
42 | mode: 'insensitive',
|
---|
43 | },
|
---|
44 | ...(validatedFilters.status && {
|
---|
45 | status: validatedFilters.status as ClientStatus,
|
---|
46 | }),
|
---|
47 | },
|
---|
48 | });
|
---|
49 |
|
---|
50 | return NextResponse.json(customers);
|
---|
51 | } catch (error) {
|
---|
52 | console.error('Error fetching customers:', error);
|
---|
53 | return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | export async function POST(request: NextRequest) {
|
---|
58 | try {
|
---|
59 | // Authenticate the request
|
---|
60 | const authResult = await authenticateRequest(request);
|
---|
61 | if (authResult instanceof NextResponse) {
|
---|
62 | return authResult;
|
---|
63 | }
|
---|
64 | const { userId, tenantId } = authResult;
|
---|
65 |
|
---|
66 | const body = await request.json();
|
---|
67 | const validatedData = newCustomerSchema.parse(body);
|
---|
68 | console.log('validatedData', validatedData);
|
---|
69 |
|
---|
70 | const customer = await prisma.client.create({
|
---|
71 | data: {
|
---|
72 | ...validatedData,
|
---|
73 | tenantId,
|
---|
74 | },
|
---|
75 | });
|
---|
76 |
|
---|
77 | return NextResponse.json(customer, { status: 201 });
|
---|
78 | } catch (error) {
|
---|
79 | console.error('Error creating customer:', error);
|
---|
80 | return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
---|
81 | }
|
---|
82 | }
|
---|