feat(02-02): add admin-queries, NavBar, and admin layout
- src/lib/admin-queries.ts: getAllClientsWithPayments() and getClientById() for admin DB reads - src/components/admin/NavBar.tsx: minimal nav with Clienti link and Esci (logout) button - src/app/admin/layout.tsx: wraps all /admin/* pages with NavBar + centered main content area
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { db } from "@/db";
|
||||
import { clients, payments } from "@/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
export type ClientWithPayments = {
|
||||
id: string;
|
||||
name: string;
|
||||
brand_name: string;
|
||||
token: string;
|
||||
accepted_total: string;
|
||||
created_at: Date;
|
||||
payments: Array<{
|
||||
id: string;
|
||||
label: string;
|
||||
status: string;
|
||||
amount: string;
|
||||
}>;
|
||||
};
|
||||
|
||||
export async function getAllClientsWithPayments(): Promise<ClientWithPayments[]> {
|
||||
const allClients = await db
|
||||
.select()
|
||||
.from(clients)
|
||||
.orderBy(clients.created_at);
|
||||
|
||||
if (allClients.length === 0) return [];
|
||||
|
||||
const allPayments = await db
|
||||
.select()
|
||||
.from(payments);
|
||||
|
||||
return allClients.map((c) => ({
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
brand_name: c.brand_name,
|
||||
token: c.token,
|
||||
accepted_total: c.accepted_total ?? "0",
|
||||
created_at: c.created_at,
|
||||
payments: allPayments
|
||||
.filter((p) => p.client_id === c.id)
|
||||
.map((p) => ({
|
||||
id: p.id,
|
||||
label: p.label,
|
||||
status: p.status,
|
||||
amount: p.amount,
|
||||
})),
|
||||
}));
|
||||
}
|
||||
|
||||
export async function getClientById(id: string) {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(clients)
|
||||
.where(eq(clients.id, id))
|
||||
.limit(1);
|
||||
return rows[0] ?? null;
|
||||
}
|
||||
Reference in New Issue
Block a user