2024-11-05 17:15:51 +03:00
|
|
|
import { auth } from '@/app/(auth)/auth';
|
2024-11-15 10:13:21 -05:00
|
|
|
import { getVotesByChatId, voteMessage } from '@/lib/db/queries';
|
2024-11-05 17:15:51 +03:00
|
|
|
|
|
|
|
|
export async function GET(request: Request) {
|
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
|
|
|
const chatId = searchParams.get('chatId');
|
|
|
|
|
|
|
|
|
|
if (!chatId) {
|
|
|
|
|
return new Response('chatId is required', { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const session = await auth();
|
|
|
|
|
|
|
|
|
|
if (!session || !session.user || !session.user.email) {
|
|
|
|
|
return new Response('Unauthorized', { status: 401 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const votes = await getVotesByChatId({ id: chatId });
|
|
|
|
|
|
|
|
|
|
return Response.json(votes, { status: 200 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function PATCH(request: Request) {
|
|
|
|
|
const {
|
|
|
|
|
chatId,
|
|
|
|
|
messageId,
|
|
|
|
|
type,
|
|
|
|
|
}: { chatId: string; messageId: string; type: 'up' | 'down' } =
|
|
|
|
|
await request.json();
|
|
|
|
|
|
|
|
|
|
if (!chatId || !messageId || !type) {
|
|
|
|
|
return new Response('messageId and type are required', { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const session = await auth();
|
|
|
|
|
|
|
|
|
|
if (!session || !session.user || !session.user.email) {
|
|
|
|
|
return new Response('Unauthorized', { status: 401 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await voteMessage({
|
|
|
|
|
chatId,
|
|
|
|
|
messageId,
|
|
|
|
|
type: type,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return new Response('Message voted', { status: 200 });
|
|
|
|
|
}
|