- Remove newUser variable and directly return created user from Prisma transaction in register route - Add explicit casts for Prisma QueryMode to avoid TypeScript widening in listings search - Fix items search and getItemBySource with any cast and ts-expect-error to satisfy type checks Improves readability and type safety without changing behavior.
110 lines
2.8 KiB
TypeScript
110 lines
2.8 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { secureLogger } from "@/lib/secure-logger";
|
|
import { requireAuth } from "@/lib/auth/middleware";
|
|
import { getListingById } from "@/lib/prisma/queries/listings";
|
|
import { prisma } from "@/lib/prisma/app";
|
|
import { trackEvent } from "@/lib/prisma/queries/telemetry";
|
|
|
|
/**
|
|
* GET /api/listings/[id]/bids
|
|
* Lista lances de um anúncio
|
|
*/
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const authResult = await requireAuth(request);
|
|
if (authResult instanceof NextResponse) return authResult;
|
|
|
|
const { id } = await params;
|
|
const listingId = parseInt(id, 10);
|
|
if (isNaN(listingId)) {
|
|
return NextResponse.json(
|
|
{ success: false, error: "ID de anúncio inválido" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Buscar listing para verificar se é leilão
|
|
const listing = await getListingById(listingId);
|
|
if (!listing) {
|
|
return NextResponse.json(
|
|
{ success: false, error: "Anúncio não encontrado" },
|
|
{ status: 404 }
|
|
);
|
|
}
|
|
|
|
if (!listing.is_auction) {
|
|
return NextResponse.json(
|
|
{ success: false, error: "Este anúncio não é um leilão" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
// Buscar parâmetros de filtro
|
|
const { searchParams } = new URL(request.url);
|
|
const limit = parseInt(searchParams.get("limit") || "50", 10);
|
|
|
|
// Buscar lances
|
|
const bids = await prisma.bid.findMany({
|
|
where: {
|
|
listing_id: listingId,
|
|
is_active: true,
|
|
},
|
|
include: {
|
|
bidder: {
|
|
select: {
|
|
user_id: true,
|
|
username: true,
|
|
},
|
|
},
|
|
},
|
|
orderBy: {
|
|
created_at: "desc",
|
|
},
|
|
take: limit,
|
|
});
|
|
|
|
// Formatar resposta
|
|
const formattedBids = bids.map((bid) => ({
|
|
bid_id: bid.bid_id,
|
|
bidder_id: bid.bidder_id,
|
|
bidder_name: bid.bidder.username,
|
|
bid_amount: Number(bid.bid_amount),
|
|
bid_type: bid.bid_type,
|
|
created_at: bid.created_at,
|
|
}));
|
|
|
|
// Registrar telemetria
|
|
await trackEvent({
|
|
event_type: "bid_viewed",
|
|
ad_id: listingId,
|
|
user_id: authResult.userId,
|
|
metadata: {
|
|
bids_count: formattedBids.length,
|
|
},
|
|
});
|
|
|
|
return NextResponse.json(
|
|
{
|
|
success: true,
|
|
bids: formattedBids,
|
|
total: formattedBids.length,
|
|
},
|
|
{ status: 200 }
|
|
);
|
|
} catch (error) {
|
|
secureLogger.error("❌ Erro ao buscar histórico de lances", {
|
|
error: error instanceof Error ? error.message : "Erro desconhecido",
|
|
stack: error instanceof Error ? error.stack : undefined,
|
|
});
|
|
|
|
return NextResponse.json(
|
|
{ success: false, error: "Erro ao buscar histórico de lances" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|