diff --git a/dashboard/15-final/app/dashboard/(overview)/page.tsx b/dashboard/15-final/app/dashboard/(overview)/page.tsx index 24349f6..1730bba 100644 --- a/dashboard/15-final/app/dashboard/(overview)/page.tsx +++ b/dashboard/15-final/app/dashboard/(overview)/page.tsx @@ -1,22 +1,21 @@ import Card from '@/app/ui/dashboard/card'; import RevenueChart from '@/app/ui/dashboard/revenue-chart'; import LatestInvoices from '@/app/ui/dashboard/latest-invoices'; -import { - fetchLatestInvoices, - fetchCounts, - fetchTotalAmountByStatus, -} from '@/app/lib/data'; -import { Suspense } from 'react'; import { lusitana } from '@/app/ui/fonts'; +import { fetchLatestInvoices, fetchCardData } from '@/app/lib/data'; +import { Suspense } from 'react'; import { RevenueChartSkeleton } from '@/app/ui/dashboard/skeletons'; export const dynamic = 'force-dynamic'; export default async function Page() { const latestInvoices = await fetchLatestInvoices(); - const { numberOfInvoices, numberOfCustomers } = await fetchCounts(); - const { totalPaidInvoices, totalPendingInvoices } = - await fetchTotalAmountByStatus(); + const { + numberOfInvoices, + numberOfCustomers, + totalPaidInvoices, + totalPendingInvoices, + } = await fetchCardData(); return (
diff --git a/dashboard/15-final/app/lib/data.ts b/dashboard/15-final/app/lib/data.ts index f6d291f..032f9d7 100644 --- a/dashboard/15-final/app/lib/data.ts +++ b/dashboard/15-final/app/lib/data.ts @@ -27,37 +27,6 @@ export async function fetchRevenue() { } } -export async function fetchCounts() { - try { - const invoiceCount = await sql`SELECT COUNT(*) FROM invoices`; - const numberOfInvoices = parseInt(invoiceCount.rows[0].count, 10); - - const customerCount = await sql`SELECT COUNT(*) FROM customers`; - const numberOfCustomers = parseInt(customerCount.rows[0].count, 10); - - return { numberOfCustomers, numberOfInvoices }; - } catch (error) { - console.error('Database Error:', error); - throw new Error('Failed to fetch counts.'); - } -} - -export async function fetchTotalAmountByStatus() { - try { - const data = await sql`SELECT - SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) AS "paid", - SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) AS "pending" - FROM invoices`; - const totalPaidInvoices = formatCurrency(data.rows[0].paid); - const totalPendingInvoices = formatCurrency(data.rows[0].pending); - - return { totalPaidInvoices, totalPendingInvoices }; - } catch (error) { - console.error('Database Error:', error); - throw new Error('Failed to fetch total amounts by status.'); - } -} - export async function fetchLatestInvoices() { try { const data = await sql` @@ -78,6 +47,38 @@ export async function fetchLatestInvoices() { } } +export async function fetchCardData() { + try { + const invoiceCountPromise = sql`SELECT COUNT(*) FROM invoices`; + const customerCountPromise = sql`SELECT COUNT(*) FROM customers`; + const invoiceStatusPromise = sql`SELECT + SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) AS "paid", + SUM(CASE WHEN status = 'pending' THEN amount ELSE 0 END) AS "pending" + FROM invoices`; + + const data = await Promise.all([ + invoiceCountPromise, + customerCountPromise, + invoiceStatusPromise, + ]); + + const numberOfInvoices = Number(data[0].rows[0].count ?? '0'); + const numberOfCustomers = Number(data[1].rows[0].count ?? '0'); + const totalPaidInvoices = formatCurrency(data[2].rows[0].paid ?? '0'); + const totalPendingInvoices = formatCurrency(data[2].rows[0].pending ?? '0'); + + return { + numberOfCustomers, + numberOfInvoices, + totalPaidInvoices, + totalPendingInvoices, + }; + } catch (error) { + console.error('Database Error:', error); + throw new Error('Failed to card data.'); + } +} + export async function fetchFilteredInvoices( query: string, currentPage: number,