src/app/api/mobile/vitrine/commandes/route.ts

route·app·1.8 KB · 60 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";
import { captureError } from "@/lib/sentry";

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

  const { searchParams } = req.nextUrl;
  const statut = searchParams.get("statut");
  const page = Math.max(1, parseInt(searchParams.get("page") || "1"));
  const limit = Math.min(50, parseInt(searchParams.get("limit") || "20"));
  const skip = (page - 1) * limit;

  const where: any = { tenantId: session.tenantId };
  if (statut && ["EN_ATTENTE", "CONFIRME", "PRET_RETRAIT", "EN_LIVRAISON", "LIVRE", "ANNULE"].includes(statut)) {
    where.statut = statut;
  }

  try {
    const [commandes, total] = await Promise.all([
      prisma.commande.findMany({
        where,
        orderBy: { createdAt: "desc" },
        skip,
        take: limit,
        select: {
          id: true,
          statut: true,
          total: true,
          devise: true,
          createdAt: true,
          client: { select: { nom: true, email: true, telephone: true } },
          _count: { select: { lignes: true } },
        },
      }),
      prisma.commande.count({ where }),
    ]);

    return NextResponse.json({
      commandes,
      total,
      page,
      totalPages: Math.ceil(total / limit),
    });
  } catch (error) {
    captureError(error, {
      route: "/api/mobile/vitrine/commandes",
      tenantId: session.tenantId,
      userId: session.userId,
      role: session.role,
    });
    console.error("boutique/commandes error:", error);
    return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
  }
}