Index: src/app/api/customers/[id]/route.ts
===================================================================
--- src/app/api/customers/[id]/route.ts	(revision 057453cdc828f504e8bcd813422d99221b3b5732)
+++ src/app/api/customers/[id]/route.ts	(revision 299af01b89703f010e0519c717f8e1c8bb9bb299)
@@ -6,11 +6,15 @@
 export async function PATCH(request: NextRequest, { params }: { params: { id: string } }) {
   try {
-    const userId = await authenticateRequest(request);
-    if (!userId) {
+    const auth = await authenticateRequest(request);
+    if (!auth || auth instanceof NextResponse) {
       return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
     }
 
+    const { userId, tenantId } = auth;
+
     const body = await request.json();
     const validatedData = customerSchema.partial().parse(body);
+
+    console.log('validatedData', validatedData);
 
     const customer = await prisma.client.update({
@@ -18,5 +22,5 @@
       data: {
         ...validatedData,
-        bankAccounts: undefined,
+        tenantId,
       },
     });
Index: src/app/api/customers/route.ts
===================================================================
--- src/app/api/customers/route.ts	(revision 057453cdc828f504e8bcd813422d99221b3b5732)
+++ src/app/api/customers/route.ts	(revision 299af01b89703f010e0519c717f8e1c8bb9bb299)
@@ -4,4 +4,5 @@
 import { authenticateRequest } from 'src/lib/auth-middleware';
 import { CustomerStatus } from '@prisma/client';
+import { Prisma } from '@prisma/client';
 
 export async function GET(request: NextRequest) {
@@ -12,5 +13,5 @@
       return authResult;
     }
-    const { userId } = authResult;
+    const { userId, tenantId } = authResult;
 
     const searchParams = request.nextUrl.searchParams;
@@ -24,16 +25,19 @@
     const validatedFilters = customerTableFiltersSchema.parse(filters);
 
-    const customers = await prisma.client.findMany({
-      where: {
-        name: { contains: validatedFilters.name, mode: 'insensitive' },
-        status: validatedFilters.status
-          ? { equals: validatedFilters.status as CustomerStatus }
-          : undefined,
-      },
-    });
-    console.log('customers', customers);
+    // Replace Prisma query with raw SQL
+    const customers = await prisma.$queryRaw`
+      SELECT * FROM "Client"
+      WHERE "tenantId" = ${tenantId}
+        AND LOWER(name) LIKE LOWER(${`%${validatedFilters.name}%`})
+        ${
+          validatedFilters.status
+            ? Prisma.sql`AND status = ${validatedFilters.status}::"CustomerStatus"`
+            : Prisma.sql`AND TRUE`
+        }
+    `;
 
     return NextResponse.json(customers);
   } catch (error) {
+    console.error('Error fetching customers:', error);
     return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
   }
@@ -47,5 +51,5 @@
       return authResult;
     }
-    const { userId } = authResult;
+    const { userId, tenantId } = authResult;
 
     const body = await request.json();
@@ -57,4 +61,5 @@
         ...validatedData,
         // userId,
+        tenantId,
       },
     });
Index: src/app/api/employees/route.ts
===================================================================
--- src/app/api/employees/route.ts	(revision 057453cdc828f504e8bcd813422d99221b3b5732)
+++ src/app/api/employees/route.ts	(revision 299af01b89703f010e0519c717f8e1c8bb9bb299)
@@ -3,5 +3,5 @@
 import prisma from 'src/lib/prisma';
 import { authenticateRequest } from 'src/lib/auth-middleware';
-import { EmployeeStatus } from '@prisma/client';
+import { Prisma } from '@prisma/client';
 
 export async function GET(request: NextRequest) {
@@ -23,13 +23,15 @@
     const validatedFilters = employeeTableFiltersSchema.parse(filters);
 
-    const employees = await prisma.employee.findMany({
-      where: {
-        tenantId,
-        name: { contains: validatedFilters.name, mode: 'insensitive' },
-        status: validatedFilters.status
-          ? { equals: validatedFilters.status as EmployeeStatus }
-          : undefined,
-      },
-    });
+    // Replace Prisma query with raw SQL
+    const employees = await prisma.$queryRaw`
+      SELECT * FROM "Employee"
+      WHERE "tenantId" = ${tenantId}
+        AND LOWER(name) LIKE LOWER(${`%${validatedFilters.name}%`})
+        ${
+          validatedFilters.status
+            ? Prisma.sql`AND status = ${validatedFilters.status}:::"EmployeeStatus"`
+            : Prisma.sql`AND TRUE`
+        }
+    `;
 
     return NextResponse.json(employees);
Index: src/app/api/invoices/[id]/route.ts
===================================================================
--- src/app/api/invoices/[id]/route.ts	(revision 057453cdc828f504e8bcd813422d99221b3b5732)
+++ src/app/api/invoices/[id]/route.ts	(revision 299af01b89703f010e0519c717f8e1c8bb9bb299)
@@ -90,5 +90,5 @@
       // Conditionally delete and recreate items only if they are provided
       if (validation.data.items) {
-        await tx.invoiceItem.deleteMany({
+        await tx.lineItem.deleteMany({
           where: { invoiceId: params.id },
         });
@@ -174,5 +174,5 @@
     await prisma.$transaction(async (tx) => {
       // Delete related items first
-      await tx.invoiceItem.deleteMany({
+      await tx.lineItem.deleteMany({
         where: { invoiceId: params.id },
       });
Index: src/app/api/invoices/route.ts
===================================================================
--- src/app/api/invoices/route.ts	(revision 057453cdc828f504e8bcd813422d99221b3b5732)
+++ src/app/api/invoices/route.ts	(revision 299af01b89703f010e0519c717f8e1c8bb9bb299)
@@ -24,7 +24,7 @@
   try {
     const userId = await getUserId(request);
-    if (!userId) {
-      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
-    }
+    // if (!userId) {
+    //   return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
+    // }
 
     const searchParams = request.nextUrl.searchParams;
@@ -74,7 +74,7 @@
   try {
     const userId = await getUserId(request);
-    if (!userId) {
-      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
-    }
+    // if (!userId) {
+    //   return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
+    // }
 
     const body = await request.json();
Index: src/app/api/invoices/totals/route.ts
===================================================================
--- src/app/api/invoices/totals/route.ts	(revision 299af01b89703f010e0519c717f8e1c8bb9bb299)
+++ src/app/api/invoices/totals/route.ts	(revision 299af01b89703f010e0519c717f8e1c8bb9bb299)
@@ -0,0 +1,71 @@
+import { NextRequest, NextResponse } from 'next/server';
+import prisma from 'src/lib/prisma';
+import { authenticateRequest } from 'src/lib/auth-middleware';
+
+type InvoiceStatus = 'processing' | 'paid' | 'pending' | 'overdue' | 'draft';
+type CurrencyTotals = { EUR: number; USD: number };
+type Results = Record<InvoiceStatus | 'total', CurrencyTotals>;
+
+export async function GET(request: NextRequest) {
+  try {
+    // Authenticate the request
+    const authResult = await authenticateRequest(request);
+
+    if (!authResult || authResult instanceof NextResponse) {
+      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
+    }
+
+    const { userId, tenantId } = authResult;
+
+    const searchParams = request.nextUrl.searchParams;
+    const startDate = searchParams.get('startDate')
+      ? new Date(searchParams.get('startDate')!)
+      : null;
+
+    if (!startDate) {
+      return NextResponse.json({ error: 'Start date is required' }, { status: 400 });
+    }
+
+    // Calculate totals using raw SQL
+    const totals = await prisma.$queryRaw`
+      SELECT 
+        status,
+        currency,
+        SUM("totalAmount") as total
+      FROM "Invoice"
+      WHERE "createDate" >= ${startDate}
+        AND "invoiceFromId" = ${tenantId}
+      GROUP BY status, currency
+    `;
+
+    // Format the response
+    const results: Results = {
+      total: { EUR: 0, USD: 0 },
+      processing: { EUR: 0, USD: 0 },
+      paid: { EUR: 0, USD: 0 },
+      pending: { EUR: 0, USD: 0 },
+      overdue: { EUR: 0, USD: 0 },
+      draft: { EUR: 0, USD: 0 },
+    };
+
+    // Process the results - adjusted for raw SQL response format
+    for (const row of totals as {
+      status: string;
+      currency: 'EUR' | 'USD';
+      total: string | number;
+    }[]) {
+      const { status, currency, total } = row;
+      const statusKey = status.toLowerCase() as InvoiceStatus;
+      if (statusKey in results && (currency === 'EUR' || currency === 'USD')) {
+        const amount = Number(total) || 0;
+        results[statusKey][currency] = amount;
+        results.total[currency] += amount;
+      }
+    }
+
+    return NextResponse.json(results);
+  } catch (error) {
+    console.error('Error calculating totals:', error);
+    return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
+  }
+}
Index: src/app/api/tenant/route.ts
===================================================================
--- src/app/api/tenant/route.ts	(revision 057453cdc828f504e8bcd813422d99221b3b5732)
+++ src/app/api/tenant/route.ts	(revision 299af01b89703f010e0519c717f8e1c8bb9bb299)
@@ -1,12 +1,20 @@
-import { NextResponse } from 'next/server';
+import { NextRequest, NextResponse } from 'next/server';
 import prisma from 'src/lib/prisma';
 import { tenantSchema } from 'src/schemas';
 import { z } from 'zod';
+import { authenticateRequest } from 'src/lib/auth-middleware';
 
-export async function GET() {
+export async function GET(request: NextRequest) {
   try {
-    const tenants = await prisma.tenant.findMany();
-    const tenant = tenants[0]; // Get first tenant since we're dealing with single tenant
-    console.log('tenant', tenant);
+    const auth = await authenticateRequest(request);
+    if (auth instanceof NextResponse) {
+      return auth; // Return error response if authentication failed
+    }
+    const { tenantId } = auth;
+
+    const tenant = await prisma.tenant.findUnique({
+      where: { id: tenantId },
+    });
+
     if (!tenant) {
       return NextResponse.json({ error: 'No tenant found' }, { status: 404 });
@@ -20,6 +28,12 @@
 }
 
-export async function POST(request: Request) {
+export async function POST(request: NextRequest) {
   try {
+    const auth = await authenticateRequest(request);
+    if (auth instanceof NextResponse) {
+      return auth;
+    }
+    const { tenantId } = auth;
+
     const body = await request.json();
 
@@ -28,12 +42,20 @@
 
     // Check if tenant already exists
-    const existingTenants = await prisma.tenant.findMany();
-    if (existingTenants.length > 0) {
+    const existingTenant = await prisma.tenant.findUnique({
+      where: {
+        id: tenantId,
+      },
+    });
+
+    if (existingTenant) {
       return NextResponse.json({ error: 'Tenant already exists' }, { status: 400 });
     }
 
-    // Create new tenant
+    // Create new tenant with the authenticated tenantId
     const tenant = await prisma.tenant.create({
-      data: validatedData,
+      data: {
+        ...validatedData,
+        id: tenantId, // Ensure the tenant is created with the authenticated tenantId
+      },
     });
 
