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

chore

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.