/** * Recherche globale ⌘K — server function. * * Recherche transversale sur carbets / utilisateurs / réservations / * pages éditoriales / prestataires pirogue. Renvoie au max 5 résultats * par catégorie pour garder la palette lisible. */ import "server-only"; import { prisma } from "@/lib/prisma"; export type SearchHit = { type: "carbet" | "user" | "booking" | "page" | "provider"; id: string; title: string; subtitle?: string; href: string; }; export async function adminSearch(query: string): Promise { const q = query.trim(); if (q.length < 2) return []; const ci = { contains: q, mode: "insensitive" as const }; const [carbets, users, bookings, pages, providers] = await Promise.all([ prisma.carbet.findMany({ where: { OR: [{ slug: ci }, { title: ci }, { river: ci }], }, take: 5, select: { id: true, slug: true, title: true, river: true, status: true }, }), prisma.user.findMany({ where: { OR: [{ email: ci }, { firstName: ci }, { lastName: ci }], }, take: 5, select: { id: true, email: true, firstName: true, lastName: true, role: true }, }), prisma.booking.findMany({ where: { id: ci }, take: 5, select: { id: true, status: true, startDate: true, endDate: true }, }), prisma.contentPage.findMany({ where: { OR: [{ slug: ci }, { title: ci }], lang: "fr", }, take: 5, select: { slug: true, title: true, category: true, lang: true }, }), prisma.pirogueProvider.findMany({ where: { OR: [{ name: ci }] }, take: 5, select: { id: true, name: true, rivers: true }, }), ]); const hits: SearchHit[] = []; for (const c of carbets) { hits.push({ type: "carbet", id: c.id, title: c.title, subtitle: `${c.river} · ${c.status}`, href: `/admin/carbets/${c.id}`, }); } for (const u of users) { hits.push({ type: "user", id: u.id, title: `${u.firstName} ${u.lastName}`.trim() || u.email, subtitle: `${u.email} · ${u.role}`, href: `/admin/users/${u.id}`, }); } for (const b of bookings) { hits.push({ type: "booking", id: b.id, title: `Réservation ${b.id.slice(0, 8)}`, subtitle: `${b.status} · ${b.startDate.toISOString().slice(0, 10)} → ${b.endDate.toISOString().slice(0, 10)}`, href: `/admin/bookings/${b.id}`, }); } for (const p of pages) { hits.push({ type: "page", id: p.slug, title: p.title, subtitle: `/${p.slug} · ${p.category} · ${p.lang}`, href: `/admin/content-pages/${encodeURIComponent(p.slug)}`, }); } for (const p of providers) { hits.push({ type: "provider", id: p.id, title: p.name, subtitle: p.rivers.join(" · "), href: `/admin/pirogue-providers/${p.id}`, }); } return hits; }