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