style: clean up code formatting and remove trailing whitespace
refactor: reorganize imports and fix type imports
This commit is contained in:
1 parent
d3704602fd
commit
47c04de6fe
232 files changed
+7140
-4798
No files matched your search
@@ -1,17 +1,17 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { secureLogger } from "@/lib/secure-logger";
|
||||
import { type NextRequest, NextResponse } from "next/server";
|
||||
import { requireAuth } from "@/lib/auth/middleware";
|
||||
import { connectMongoDB } from "@/lib/mongodb/connection";
|
||||
import { Chat } from "@/lib/mongodb/models/Chat";
|
||||
import { Message } from "@/lib/mongodb/models/Message";
|
||||
import { getMainCharacter } from "@/lib/prisma/queries/characters";
|
||||
import { getListingById } from "@/lib/prisma/queries/listings";
|
||||
import { createNotification } from "@/lib/prisma/queries/notifications";
|
||||
import {
|
||||
getPurchaseIntentionById,
|
||||
updatePurchaseIntentionStatus,
|
||||
} from "@/lib/prisma/queries/purchase-intentions";
|
||||
import { createNotification } from "@/lib/prisma/queries/notifications";
|
||||
import { connectMongoDB } from "@/lib/mongodb/connection";
|
||||
import { Chat } from "@/lib/mongodb/models/Chat";
|
||||
import { Message } from "@/lib/mongodb/models/Message";
|
||||
import { getUserById } from "@/lib/prisma/queries/users";
|
||||
import { getMainCharacter } from "@/lib/prisma/queries/characters";
|
||||
import { secureLogger } from "@/lib/secure-logger";
|
||||
|
||||
/**
|
||||
* GET /api/listings/[id]/intentions/[intentionId]
|
||||
@@ -19,7 +19,7 @@ import { getMainCharacter } from "@/lib/prisma/queries/characters";
|
||||
*/
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string; intentionId: string }> }
|
||||
{ params }: { params: Promise<{ id: string; intentionId: string }> },
|
||||
) {
|
||||
try {
|
||||
const authResult = await requireAuth(request);
|
||||
@@ -27,10 +27,10 @@ export async function GET(
|
||||
|
||||
const { intentionId: intentionIdParam } = await params;
|
||||
const intentionId = parseInt(intentionIdParam, 10);
|
||||
if (isNaN(intentionId)) {
|
||||
if (Number.isNaN(intentionId)) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "ID de intenção inválido" },
|
||||
{ status: 400 }
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -38,15 +38,21 @@ export async function GET(
|
||||
if (!intention) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Intenção não encontrada" },
|
||||
{ status: 404 }
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
// Verificar permissões: vendedor ou comprador
|
||||
if (intention.listing?.seller_id !== authResult.userId && intention.buyer_id !== authResult.userId) {
|
||||
if (
|
||||
intention.listing?.seller_id !== authResult.userId &&
|
||||
intention.buyer_id !== authResult.userId
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Você não tem permissão para ver esta intenção" },
|
||||
{ status: 403 }
|
||||
{
|
||||
success: false,
|
||||
error: "Você não tem permissão para ver esta intenção",
|
||||
},
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -55,7 +61,7 @@ export async function GET(
|
||||
success: true,
|
||||
intention,
|
||||
},
|
||||
{ status: 200 }
|
||||
{ status: 200 },
|
||||
);
|
||||
} catch (error) {
|
||||
secureLogger.error("❌ Erro ao buscar intenção", {
|
||||
@@ -65,7 +71,7 @@ export async function GET(
|
||||
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Erro ao buscar intenção" },
|
||||
{ status: 500 }
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -76,7 +82,7 @@ export async function GET(
|
||||
*/
|
||||
export async function PATCH(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string; intentionId: string }> }
|
||||
{ params }: { params: Promise<{ id: string; intentionId: string }> },
|
||||
) {
|
||||
try {
|
||||
const authResult = await requireAuth(request);
|
||||
@@ -84,10 +90,10 @@ export async function PATCH(
|
||||
|
||||
const { intentionId: intentionIdParam } = await params;
|
||||
const intentionId = parseInt(intentionIdParam, 10);
|
||||
if (isNaN(intentionId)) {
|
||||
if (Number.isNaN(intentionId)) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "ID de intenção inválido" },
|
||||
{ status: 400 }
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -100,8 +106,11 @@ export async function PATCH(
|
||||
|
||||
if (!status || !["accepted", "rejected"].includes(status)) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Status inválido. Use 'accepted' ou 'rejected'" },
|
||||
{ status: 400 }
|
||||
{
|
||||
success: false,
|
||||
error: "Status inválido. Use 'accepted' ou 'rejected'",
|
||||
},
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -110,15 +119,18 @@ export async function PATCH(
|
||||
if (!intention) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Intenção não encontrada" },
|
||||
{ status: 404 }
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
// Verificar se o usuário é o vendedor
|
||||
if (intention.listing?.seller_id !== authResult.userId) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Apenas o vendedor pode aceitar ou rejeitar intenções" },
|
||||
{ status: 403 }
|
||||
{
|
||||
success: false,
|
||||
error: "Apenas o vendedor pode aceitar ou rejeitar intenções",
|
||||
},
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -126,16 +138,20 @@ export async function PATCH(
|
||||
if (intention.status !== "pending") {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: `Esta intenção já foi ${intention.status}` },
|
||||
{ status: 400 }
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
// Verificar se o anúncio ainda está disponível (ativo ou em escrow)
|
||||
const listing = await getListingById(intention.listing_id);
|
||||
if (!listing || (listing.listing_status !== "active" && listing.listing_status !== "in_escrow")) {
|
||||
if (
|
||||
!listing ||
|
||||
(listing.listing_status !== "active" &&
|
||||
listing.listing_status !== "in_escrow")
|
||||
) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Anúncio não está mais disponível" },
|
||||
{ status: 400 }
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -143,7 +159,7 @@ export async function PATCH(
|
||||
const updatedIntention = await updatePurchaseIntentionStatus(
|
||||
intentionId,
|
||||
status,
|
||||
sellerResponse
|
||||
sellerResponse,
|
||||
);
|
||||
|
||||
await sendSellerResponseToChat({
|
||||
@@ -159,19 +175,23 @@ export async function PATCH(
|
||||
user_id: intention.buyer_id,
|
||||
notification_type: status === "accepted" ? "purchase" : "system",
|
||||
title: status === "accepted" ? "Intenção aceita!" : "Intenção rejeitada",
|
||||
message: status === "accepted"
|
||||
? `O vendedor aceitou sua intenção de compra do item "${intention.listing?.item_name}"`
|
||||
: `O vendedor rejeitou sua intenção de compra do item "${intention.listing?.item_name}"`,
|
||||
message:
|
||||
status === "accepted"
|
||||
? `O vendedor aceitou sua intenção de compra do item "${intention.listing?.item_name}"`
|
||||
: `O vendedor rejeitou sua intenção de compra do item "${intention.listing?.item_name}"`,
|
||||
related_id: intention.listing_id,
|
||||
send_whatsapp: false,
|
||||
});
|
||||
|
||||
secureLogger.info(`✅ Intenção ${status === "accepted" ? "aceita" : "rejeitada"}`, {
|
||||
userId: authResult.userId,
|
||||
intentionId,
|
||||
listingId: intention.listing_id,
|
||||
buyerId: intention.buyer_id,
|
||||
});
|
||||
secureLogger.info(
|
||||
`✅ Intenção ${status === "accepted" ? "aceita" : "rejeitada"}`,
|
||||
{
|
||||
userId: authResult.userId,
|
||||
intentionId,
|
||||
listingId: intention.listing_id,
|
||||
buyerId: intention.buyer_id,
|
||||
},
|
||||
);
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
@@ -179,7 +199,7 @@ export async function PATCH(
|
||||
intention: updatedIntention,
|
||||
message: `Intenção ${status === "accepted" ? "aceita" : "rejeitada"} com sucesso`,
|
||||
},
|
||||
{ status: 200 }
|
||||
{ status: 200 },
|
||||
);
|
||||
} catch (error) {
|
||||
secureLogger.error("❌ Erro ao atualizar intenção", {
|
||||
@@ -189,7 +209,7 @@ export async function PATCH(
|
||||
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Erro ao atualizar intenção" },
|
||||
{ status: 500 }
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -202,7 +222,10 @@ async function sendSellerResponseToChat({
|
||||
sellerId,
|
||||
}: {
|
||||
listing: Awaited<ReturnType<typeof getListingById>>;
|
||||
intention: Exclude<Awaited<ReturnType<typeof getPurchaseIntentionById>>, null>;
|
||||
intention: Exclude<
|
||||
Awaited<ReturnType<typeof getPurchaseIntentionById>>,
|
||||
null
|
||||
>;
|
||||
status: "accepted" | "rejected";
|
||||
sellerResponse?: string;
|
||||
sellerId: number;
|
||||
@@ -225,12 +248,13 @@ async function sendSellerResponseToChat({
|
||||
let chat = await Chat.findOne({ chat_id: chatId });
|
||||
|
||||
if (!chat) {
|
||||
const [buyer, seller, buyerCharacter, sellerCharacter] = await Promise.all([
|
||||
getUserById(intention.buyer_id),
|
||||
getUserById(listing.seller_id),
|
||||
getMainCharacter(intention.buyer_id),
|
||||
getMainCharacter(listing.seller_id),
|
||||
]);
|
||||
const [buyer, seller, buyerCharacter, sellerCharacter] =
|
||||
await Promise.all([
|
||||
getUserById(intention.buyer_id),
|
||||
getUserById(listing.seller_id),
|
||||
getMainCharacter(intention.buyer_id),
|
||||
getMainCharacter(listing.seller_id),
|
||||
]);
|
||||
|
||||
chat = new Chat({
|
||||
chat_id: chatId,
|
||||
@@ -284,4 +308,3 @@ async function sendSellerResponseToChat({
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user