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
@@ -3,13 +3,16 @@
|
||||
* POST /api/chats/[chatId]/messages - Envia mensagem (fallback se Socket.io não estiver disponível)
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
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 { sendMessageSchema, getMessagesSchema } from "@/lib/validation/chat-schemas";
|
||||
import { secureLogger } from "@/lib/secure-logger";
|
||||
import {
|
||||
getMessagesSchema,
|
||||
sendMessageSchema,
|
||||
} from "@/lib/validation/chat-schemas";
|
||||
|
||||
/**
|
||||
* GET /api/chats/[chatId]/messages
|
||||
@@ -17,7 +20,7 @@ import { secureLogger } from "@/lib/secure-logger";
|
||||
*/
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ chatId: string }> }
|
||||
{ params }: { params: Promise<{ chatId: string }> },
|
||||
) {
|
||||
try {
|
||||
const authResult = await requireAuth(request);
|
||||
@@ -26,7 +29,7 @@ export async function GET(
|
||||
|
||||
const { chatId } = await params;
|
||||
const { searchParams } = new URL(request.url);
|
||||
|
||||
|
||||
const queryValidation = getMessagesSchema.safeParse({
|
||||
page: searchParams.get("page"),
|
||||
limit: searchParams.get("limit"),
|
||||
@@ -39,7 +42,7 @@ export async function GET(
|
||||
error: "Parâmetros inválidos",
|
||||
details: queryValidation.error.issues,
|
||||
},
|
||||
{ status: 400 }
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -53,14 +56,17 @@ export async function GET(
|
||||
if (!chat) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Chat não encontrado" },
|
||||
{ status: 404 }
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
if (chat.buyer_id !== auth.userId && chat.seller_id !== auth.userId) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Você não tem permissão para acessar este chat" },
|
||||
{ status: 403 }
|
||||
{
|
||||
success: false,
|
||||
error: "Você não tem permissão para acessar este chat",
|
||||
},
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -88,7 +94,7 @@ export async function GET(
|
||||
},
|
||||
{
|
||||
$set: { read_at: new Date() },
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// Atualizar contador de não lidas
|
||||
@@ -100,19 +106,17 @@ export async function GET(
|
||||
await chat.save();
|
||||
|
||||
// Serializar mensagens (ordem reversa para mostrar mais antigas primeiro)
|
||||
const serializedMessages = messages
|
||||
.reverse()
|
||||
.map((msg) => ({
|
||||
_id: msg._id.toString(),
|
||||
chat_id: msg.chat_id,
|
||||
sender_id: msg.sender_id,
|
||||
content: msg.content,
|
||||
created_at: msg.created_at.toISOString(),
|
||||
read_at: msg.read_at?.toISOString() || null,
|
||||
edited_at: msg.edited_at?.toISOString() || null,
|
||||
message_type: msg.message_type,
|
||||
metadata: msg.metadata || null,
|
||||
}));
|
||||
const serializedMessages = messages.reverse().map((msg) => ({
|
||||
_id: msg._id.toString(),
|
||||
chat_id: msg.chat_id,
|
||||
sender_id: msg.sender_id,
|
||||
content: msg.content,
|
||||
created_at: msg.created_at.toISOString(),
|
||||
read_at: msg.read_at?.toISOString() || null,
|
||||
edited_at: msg.edited_at?.toISOString() || null,
|
||||
message_type: msg.message_type,
|
||||
metadata: msg.metadata || null,
|
||||
}));
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
@@ -125,7 +129,7 @@ export async function GET(
|
||||
totalPages: Math.ceil(total / limit),
|
||||
},
|
||||
},
|
||||
{ status: 200 }
|
||||
{ status: 200 },
|
||||
);
|
||||
} catch (error) {
|
||||
secureLogger.error("❌ Erro ao buscar mensagens", {
|
||||
@@ -135,7 +139,7 @@ export async function GET(
|
||||
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Erro ao buscar mensagens" },
|
||||
{ status: 500 }
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -146,7 +150,7 @@ export async function GET(
|
||||
*/
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ chatId: string }> }
|
||||
{ params }: { params: Promise<{ chatId: string }> },
|
||||
) {
|
||||
try {
|
||||
const authResult = await requireAuth(request);
|
||||
@@ -164,7 +168,7 @@ export async function POST(
|
||||
error: "Dados inválidos",
|
||||
details: validationResult.error.issues,
|
||||
},
|
||||
{ status: 400 }
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -178,14 +182,17 @@ export async function POST(
|
||||
if (!chat) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Chat não encontrado" },
|
||||
{ status: 404 }
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
if (chat.buyer_id !== auth.userId && chat.seller_id !== auth.userId) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Você não tem permissão para enviar mensagens neste chat" },
|
||||
{ status: 403 }
|
||||
{
|
||||
success: false,
|
||||
error: "Você não tem permissão para enviar mensagens neste chat",
|
||||
},
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -202,7 +209,7 @@ export async function POST(
|
||||
// Atualizar chat
|
||||
chat.last_message_at = new Date();
|
||||
chat.last_message_preview = content.substring(0, 100);
|
||||
|
||||
|
||||
if (chat.buyer_id === auth.userId) {
|
||||
chat.seller_unread_count += 1;
|
||||
} else {
|
||||
@@ -228,7 +235,7 @@ export async function POST(
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (socketError) {
|
||||
} catch (_socketError) {
|
||||
// Socket.io não disponível, mensagem já foi salva
|
||||
secureLogger.warn("Socket.io não disponível, mensagem salva via REST", {
|
||||
chatId,
|
||||
@@ -248,7 +255,7 @@ export async function POST(
|
||||
message_type: message.message_type,
|
||||
},
|
||||
},
|
||||
{ status: 201 }
|
||||
{ status: 201 },
|
||||
);
|
||||
} catch (error) {
|
||||
secureLogger.error("❌ Erro ao enviar mensagem", {
|
||||
@@ -258,8 +265,7 @@ export async function POST(
|
||||
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Erro ao enviar mensagem" },
|
||||
{ status: 500 }
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
* GET /api/chats/[chatId] - Busca detalhes de um chat específico
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
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";
|
||||
@@ -10,7 +10,7 @@ import { secureLogger } from "@/lib/secure-logger";
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ chatId: string }> }
|
||||
{ params }: { params: Promise<{ chatId: string }> },
|
||||
) {
|
||||
try {
|
||||
const authResult = await requireAuth(request);
|
||||
@@ -27,15 +27,18 @@ export async function GET(
|
||||
if (!chat) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Chat não encontrado" },
|
||||
{ status: 404 }
|
||||
{ status: 404 },
|
||||
);
|
||||
}
|
||||
|
||||
// Verificar se o usuário pertence ao chat
|
||||
if (chat.buyer_id !== auth.userId && chat.seller_id !== auth.userId) {
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Você não tem permissão para acessar este chat" },
|
||||
{ status: 403 }
|
||||
{
|
||||
success: false,
|
||||
error: "Você não tem permissão para acessar este chat",
|
||||
},
|
||||
{ status: 403 },
|
||||
);
|
||||
}
|
||||
|
||||
@@ -59,7 +62,7 @@ export async function GET(
|
||||
seller_cache: chat.seller_cache,
|
||||
},
|
||||
},
|
||||
{ status: 200 }
|
||||
{ status: 200 },
|
||||
);
|
||||
} catch (error) {
|
||||
secureLogger.error("❌ Erro ao buscar chat", {
|
||||
@@ -69,8 +72,7 @@ export async function GET(
|
||||
|
||||
return NextResponse.json(
|
||||
{ success: false, error: "Erro ao buscar chat" },
|
||||
{ status: 500 }
|
||||
{ status: 500 },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user