src/app/api/mobile/restaurant/mes-commandes/route.ts

route·app·1.2 KB · 39 lignes· Voir l'itinéraire
Annotation non disponible

Lance npm run annotate (nécessite ANTHROPIC_API_KEY dans .env.local) pour générer une annotation française par Claude Haiku 4.5.

1 export

GET

Code source· typescript

import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/prisma/client";
import { getSessionFromRequest } from "@/lib/auth/session";

export async function GET(req: NextRequest) {
  const session = await getSessionFromRequest(req);
  if (!session || session.role !== "CLIENT" || !session.userId) {
    return NextResponse.json({ error: "Non autorisé" }, { status: 401 });
  }

  try {
    const commandes = await prisma.commandeRepas.findMany({
      where: { clientAccountId: session.userId },
      orderBy: { createdAt: "desc" },
      include: {
        lignes: { select: { id: true } },
        tenant: { select: { id: true, subdomain: true, nom: true, logoUrl: true } },
      },
    });

    const result = commandes.map((c) => ({
      id: c.id,
      tenantId: c.tenantId,
      mode: c.mode,
      statut: c.statut,
      total: c.total,
      devise: c.devise,
      nbLignes: c.lignes.length,
      createdAt: c.createdAt.toISOString(),
      tenant: c.tenant,
    }));

    return NextResponse.json({ commandes: result });
  } catch (error) {
    console.error("restaurant/mes-commandes GET error:", error);
    return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
  }
}