Ignore:
Timestamp:
02/26/25 14:27:26 (6 weeks ago)
Author:
Naum Shapkarovski <naumshapkarovski@…>
Branches:
main
Children:
3c5302a
Parents:
057453c
Message:

chore

Location:
src/app/api/customers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/app/api/customers/[id]/route.ts

    r057453c r299af01  
    66export async function PATCH(request: NextRequest, { params }: { params: { id: string } }) {
    77  try {
    8     const userId = await authenticateRequest(request);
    9     if (!userId) {
     8    const auth = await authenticateRequest(request);
     9    if (!auth || auth instanceof NextResponse) {
    1010      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    1111    }
    1212
     13    const { userId, tenantId } = auth;
     14
    1315    const body = await request.json();
    1416    const validatedData = customerSchema.partial().parse(body);
     17
     18    console.log('validatedData', validatedData);
    1519
    1620    const customer = await prisma.client.update({
     
    1822      data: {
    1923        ...validatedData,
    20         bankAccounts: undefined,
     24        tenantId,
    2125      },
    2226    });
  • src/app/api/customers/route.ts

    r057453c r299af01  
    44import { authenticateRequest } from 'src/lib/auth-middleware';
    55import { CustomerStatus } from '@prisma/client';
     6import { Prisma } from '@prisma/client';
    67
    78export async function GET(request: NextRequest) {
     
    1213      return authResult;
    1314    }
    14     const { userId } = authResult;
     15    const { userId, tenantId } = authResult;
    1516
    1617    const searchParams = request.nextUrl.searchParams;
     
    2425    const validatedFilters = customerTableFiltersSchema.parse(filters);
    2526
    26     const customers = await prisma.client.findMany({
    27       where: {
    28         name: { contains: validatedFilters.name, mode: 'insensitive' },
    29         status: validatedFilters.status
    30           ? { equals: validatedFilters.status as CustomerStatus }
    31           : undefined,
    32       },
    33     });
    34     console.log('customers', customers);
     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    `;
    3538
    3639    return NextResponse.json(customers);
    3740  } catch (error) {
     41    console.error('Error fetching customers:', error);
    3842    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
    3943  }
     
    4751      return authResult;
    4852    }
    49     const { userId } = authResult;
     53    const { userId, tenantId } = authResult;
    5054
    5155    const body = await request.json();
     
    5761        ...validatedData,
    5862        // userId,
     63        tenantId,
    5964      },
    6065    });
Note: See TracChangeset for help on using the changeset viewer.