Files
streaper2 889e70bfc0
Some checks failed
Build and Push App / build (push) Failing after 21s
fix build
2026-04-15 22:40:04 +02:00

62 lines
1.9 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import prisma from '../../../../lib/prisma'
// GET /api/conversations/[id] — Get messages for a conversation
export async function GET(
request: NextRequest,
context: { params: Promise<{ id: string }> }
): Promise<Response> {
try {
const userId = request.headers.get('x-user-id')
const { id } = await context.params
if (!userId) {
return NextResponse.json({ error: 'Utilisateur non identifié' }, { status: 401 })
}
// Verify user is a participant
const isParticipant = await prisma.conversationParticipant.findUnique({
where: {
userId_conversationId: {
userId: userId,
conversationId: id
}
}
})
if (!isParticipant) {
return NextResponse.json({ error: 'Accès non autorisé' }, { status: 403 })
}
const messages = await prisma.message.findMany({
where: { conversationId: id },
orderBy: { createdAt: 'asc' },
include: {
sender: {
select: {
id: true,
name: true,
email: true,
avatar: true
}
}
}
})
// Mark messages as read (where sender is not the current user)
await prisma.message.updateMany({
where: {
conversationId: id,
senderId: { not: userId },
read: false
},
data: { read: true }
})
return NextResponse.json(messages)
} catch (error) {
console.error('GET /api/conversations/[id] error:', error)
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 })
}
}