Added Chat Page
This commit is contained in:
163
app/api/conversations/[id]/route.ts
Normal file
163
app/api/conversations/[id]/route.ts
Normal file
@@ -0,0 +1,163 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
|
||||
// GET - Récupérer une conversation spécifique
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const conversation = await prisma.conversation.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
participants: {
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!conversation) {
|
||||
return NextResponse.json({ error: 'Conversation non trouvée' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Vérifier que l'utilisateur est participant
|
||||
const isParticipant = conversation.participants.some((p) => p.userId === user.id);
|
||||
if (!isParticipant) {
|
||||
return NextResponse.json({ error: 'Accès non autorisé' }, { status: 403 });
|
||||
}
|
||||
|
||||
return NextResponse.json(conversation);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération de la conversation:', error);
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// PUT - Mettre à jour une conversation (nom du groupe, etc.)
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { name } = body;
|
||||
|
||||
const conversation = await prisma.conversation.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
participants: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!conversation) {
|
||||
return NextResponse.json({ error: 'Conversation non trouvée' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Vérifier que l'utilisateur est participant
|
||||
const isParticipant = conversation.participants.some((p) => p.userId === user.id);
|
||||
if (!isParticipant) {
|
||||
return NextResponse.json({ error: 'Accès non autorisé' }, { status: 403 });
|
||||
}
|
||||
|
||||
// Seuls les groupes peuvent être renommés
|
||||
if (conversation.type !== 'group') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Seuls les groupes peuvent être renommés' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const updatedConversation = await prisma.conversation.update({
|
||||
where: { id: params.id },
|
||||
data: { name },
|
||||
include: {
|
||||
participants: {
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updatedConversation);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la mise à jour de la conversation:', error);
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Supprimer une conversation (ou quitter un groupe)
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const conversation = await prisma.conversation.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
participants: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!conversation) {
|
||||
return NextResponse.json({ error: 'Conversation non trouvée' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Vérifier que l'utilisateur est participant
|
||||
const isParticipant = conversation.participants.some((p) => p.userId === user.id);
|
||||
if (!isParticipant) {
|
||||
return NextResponse.json({ error: 'Accès non autorisé' }, { status: 403 });
|
||||
}
|
||||
|
||||
// Si c'est un groupe, retirer l'utilisateur
|
||||
// Si c'est une conversation directe, supprimer la conversation
|
||||
if (conversation.type === 'group') {
|
||||
await prisma.conversationParticipant.deleteMany({
|
||||
where: {
|
||||
conversationId: params.id,
|
||||
userId: user.id,
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// Pour les conversations directes, supprimer complètement
|
||||
await prisma.conversation.delete({
|
||||
where: { id: params.id },
|
||||
});
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la suppression de la conversation:', error);
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user