98 lines
2.5 KiB
TypeScript
98 lines
2.5 KiB
TypeScript
|
|
import { NextRequest, NextResponse } from 'next/server';
|
||
|
|
import { prisma } from '@/lib/prisma';
|
||
|
|
import { getCurrentUser } from '@/lib/auth';
|
||
|
|
|
||
|
|
// GET - Obtenir un contact par ID
|
||
|
|
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 contact = await prisma.universPro.findUnique({
|
||
|
|
where: { id: params.id },
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!contact) {
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: 'Contact non trouvé' },
|
||
|
|
{ status: 404 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return NextResponse.json(contact);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Erreur lors de la récupération du contact:', error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: 'Erreur serveur' },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// PUT - Mettre à jour un contact
|
||
|
|
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 { nom, prenom, telephone, email, adresse, nomEntreprise } = body;
|
||
|
|
|
||
|
|
const updateData: any = {};
|
||
|
|
if (nom) updateData.nom = nom;
|
||
|
|
if (prenom) updateData.prenom = prenom;
|
||
|
|
if (telephone) updateData.telephone = telephone;
|
||
|
|
if (email) updateData.email = email;
|
||
|
|
if (adresse) updateData.adresse = adresse;
|
||
|
|
if (nomEntreprise) updateData.nomEntreprise = nomEntreprise;
|
||
|
|
|
||
|
|
const contact = await prisma.universPro.update({
|
||
|
|
where: { id: params.id },
|
||
|
|
data: updateData,
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json(contact);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Erreur lors de la mise à jour du contact:', error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: 'Erreur serveur' },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// DELETE - Supprimer un contact
|
||
|
|
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 });
|
||
|
|
}
|
||
|
|
|
||
|
|
await prisma.universPro.delete({
|
||
|
|
where: { id: params.id },
|
||
|
|
});
|
||
|
|
|
||
|
|
return NextResponse.json({ success: true });
|
||
|
|
} catch (error) {
|
||
|
|
console.error('Erreur lors de la suppression du contact:', error);
|
||
|
|
return NextResponse.json(
|
||
|
|
{ error: 'Erreur serveur' },
|
||
|
|
{ status: 500 }
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|