Added Chat Page
This commit is contained in:
171
app/api/conversations/[id]/participants/route.ts
Normal file
171
app/api/conversations/[id]/participants/route.ts
Normal file
@@ -0,0 +1,171 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
|
||||
// POST - Ajouter des participants à une conversation
|
||||
export async function POST(
|
||||
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 { userIds } = body;
|
||||
|
||||
if (!userIds || !Array.isArray(userIds) || userIds.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Au moins un utilisateur est requis' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
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 avoir des participants ajoutés
|
||||
if (conversation.type !== 'group') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Seuls les groupes peuvent avoir des participants ajoutés' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Vérifier que les utilisateurs existent
|
||||
const users = await prisma.user.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: userIds,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (users.length !== userIds.length) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Un ou plusieurs utilisateurs sont invalides' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Filtrer les utilisateurs déjà participants
|
||||
const existingParticipantIds = conversation.participants.map((p) => p.userId);
|
||||
const newUserIds = userIds.filter((id: string) => !existingParticipantIds.includes(id));
|
||||
|
||||
if (newUserIds.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Tous les utilisateurs sont déjà participants' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Ajouter les nouveaux participants
|
||||
await prisma.conversationParticipant.createMany({
|
||||
data: newUserIds.map((userId: string) => ({
|
||||
conversationId: params.id,
|
||||
userId,
|
||||
})),
|
||||
});
|
||||
|
||||
const updatedConversation = await prisma.conversation.findUnique({
|
||||
where: { id: params.id },
|
||||
include: {
|
||||
participants: {
|
||||
include: {
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
email: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(updatedConversation);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de l\'ajout des participants:', error);
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Retirer un participant d'une conversation
|
||||
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 searchParams = request.nextUrl.searchParams;
|
||||
const userId = searchParams.get('userId');
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json({ error: 'userId est requis' }, { status: 400 });
|
||||
}
|
||||
|
||||
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 avoir des participants retirés
|
||||
if (conversation.type !== 'group') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Seuls les groupes peuvent avoir des participants retirés' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Vérifier que le participant existe dans la conversation
|
||||
const participant = conversation.participants.find((p) => p.userId === userId);
|
||||
if (!participant) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Participant non trouvé dans la conversation' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
await prisma.conversationParticipant.delete({
|
||||
where: { id: participant.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors du retrait du participant:', error);
|
||||
return NextResponse.json({ error: 'Erreur serveur' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user