Added Pro Page
This commit is contained in:
103
app/api/chauffeurs/[id]/route.ts
Normal file
103
app/api/chauffeurs/[id]/route.ts
Normal file
@@ -0,0 +1,103 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
|
||||
// GET - Obtenir un chauffeur 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 chauffeur = await prisma.chauffeur.findUnique({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
if (!chauffeur) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Chauffeur non trouvé' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(chauffeur);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération du chauffeur:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// PUT - Mettre à jour un chauffeur
|
||||
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, dateNaissance, telephone, email, adresse, heuresContrat, dateDebutContrat, dateFinContrat, status } = body;
|
||||
|
||||
const updateData: any = {};
|
||||
if (nom) updateData.nom = nom;
|
||||
if (prenom) updateData.prenom = prenom;
|
||||
if (dateNaissance) updateData.dateNaissance = new Date(dateNaissance);
|
||||
if (telephone) updateData.telephone = telephone;
|
||||
if (email) updateData.email = email;
|
||||
if (adresse) updateData.adresse = adresse;
|
||||
if (heuresContrat !== undefined) updateData.heuresContrat = heuresContrat;
|
||||
if (dateDebutContrat) updateData.dateDebutContrat = new Date(dateDebutContrat);
|
||||
if (dateFinContrat !== undefined) {
|
||||
updateData.dateFinContrat = dateFinContrat ? new Date(dateFinContrat) : null;
|
||||
}
|
||||
if (status) updateData.status = status;
|
||||
|
||||
const chauffeur = await prisma.chauffeur.update({
|
||||
where: { id: params.id },
|
||||
data: updateData,
|
||||
});
|
||||
|
||||
return NextResponse.json(chauffeur);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la mise à jour du chauffeur:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE - Supprimer un chauffeur
|
||||
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.chauffeur.delete({
|
||||
where: { id: params.id },
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la suppression du chauffeur:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
89
app/api/chauffeurs/route.ts
Normal file
89
app/api/chauffeurs/route.ts
Normal file
@@ -0,0 +1,89 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
|
||||
// GET - Liste tous les chauffeurs avec recherche
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const search = searchParams.get('search') || '';
|
||||
|
||||
// Récupérer tous les chauffeurs (SQLite ne supporte pas bien les recherches complexes)
|
||||
const allChauffeurs = await prisma.chauffeur.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
|
||||
// Filtrer en JavaScript pour la recherche insensible à la casse
|
||||
const chauffeurs = search
|
||||
? allChauffeurs.filter(
|
||||
(ch) => {
|
||||
const searchLower = search.toLowerCase();
|
||||
return (
|
||||
ch.nom.toLowerCase().includes(searchLower) ||
|
||||
ch.prenom.toLowerCase().includes(searchLower) ||
|
||||
ch.email.toLowerCase().includes(searchLower) ||
|
||||
ch.telephone.includes(search) ||
|
||||
ch.adresse.toLowerCase().includes(searchLower)
|
||||
);
|
||||
}
|
||||
)
|
||||
: allChauffeurs;
|
||||
|
||||
return NextResponse.json(chauffeurs);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération des chauffeurs:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Créer un nouveau chauffeur
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const body = await request.json();
|
||||
const { nom, prenom, dateNaissance, telephone, email, adresse, heuresContrat, dateDebutContrat, dateFinContrat } = body;
|
||||
|
||||
if (!nom || !prenom || !dateNaissance || !telephone || !email || !adresse || !heuresContrat || !dateDebutContrat) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Tous les champs obligatoires sont requis' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const chauffeur = await prisma.chauffeur.create({
|
||||
data: {
|
||||
nom,
|
||||
prenom,
|
||||
dateNaissance: new Date(dateNaissance),
|
||||
telephone,
|
||||
email,
|
||||
adresse,
|
||||
heuresContrat: heuresContrat || 35,
|
||||
dateDebutContrat: new Date(dateDebutContrat),
|
||||
dateFinContrat: dateFinContrat ? new Date(dateFinContrat) : null,
|
||||
heuresRestantes: heuresContrat || 35, // Initialiser avec le nombre d'heures du contrat
|
||||
status: 'Disponible', // Par défaut
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(chauffeur, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la création du chauffeur:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
97
app/api/univers-pro/[id]/route.ts
Normal file
97
app/api/univers-pro/[id]/route.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
85
app/api/univers-pro/route.ts
Normal file
85
app/api/univers-pro/route.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
|
||||
// GET - Liste tous les contacts univers pro avec recherche
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const user = await getCurrentUser();
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'Non autorisé' }, { status: 401 });
|
||||
}
|
||||
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const search = searchParams.get('search') || '';
|
||||
|
||||
// Récupérer tous les contacts (SQLite ne supporte pas bien les recherches complexes)
|
||||
const allContacts = await prisma.universPro.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
|
||||
// Filtrer en JavaScript pour la recherche insensible à la casse
|
||||
const contacts = search
|
||||
? allContacts.filter(
|
||||
(contact) => {
|
||||
const searchLower = search.toLowerCase();
|
||||
return (
|
||||
contact.nom.toLowerCase().includes(searchLower) ||
|
||||
contact.prenom.toLowerCase().includes(searchLower) ||
|
||||
contact.email.toLowerCase().includes(searchLower) ||
|
||||
contact.telephone.includes(search) ||
|
||||
contact.adresse.toLowerCase().includes(searchLower) ||
|
||||
contact.nomEntreprise.toLowerCase().includes(searchLower)
|
||||
);
|
||||
}
|
||||
)
|
||||
: allContacts;
|
||||
|
||||
return NextResponse.json(contacts);
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la récupération des contacts:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// POST - Créer un nouveau contact
|
||||
export async function POST(request: NextRequest) {
|
||||
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;
|
||||
|
||||
if (!nom || !prenom || !telephone || !email || !adresse || !nomEntreprise) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Tous les champs sont requis' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const contact = await prisma.universPro.create({
|
||||
data: {
|
||||
nom,
|
||||
prenom,
|
||||
telephone,
|
||||
email,
|
||||
adresse,
|
||||
nomEntreprise,
|
||||
},
|
||||
});
|
||||
|
||||
return NextResponse.json(contact, { status: 201 });
|
||||
} catch (error) {
|
||||
console.error('Erreur lors de la création du contact:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
27
app/dashboard/chauffeurs/page.tsx
Normal file
27
app/dashboard/chauffeurs/page.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
import DashboardLayout from '@/components/DashboardLayout';
|
||||
import ChauffeursTable from '@/components/ChauffeursTable';
|
||||
|
||||
export default async function ChauffeursPage() {
|
||||
const user = await getCurrentUser();
|
||||
|
||||
if (!user) {
|
||||
redirect('/login');
|
||||
}
|
||||
|
||||
return (
|
||||
<DashboardLayout user={user}>
|
||||
<div className="p-6">
|
||||
<h1 className="text-3xl font-semibold text-cblack mb-1">
|
||||
Chauffeurs
|
||||
</h1>
|
||||
<p className="text-sm text-cgray mb-6">
|
||||
Base de données des chauffeurs
|
||||
</p>
|
||||
|
||||
<ChauffeursTable />
|
||||
</div>
|
||||
</DashboardLayout>
|
||||
);
|
||||
}
|
||||
27
app/dashboard/univers-pro/page.tsx
Normal file
27
app/dashboard/univers-pro/page.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
import { getCurrentUser } from '@/lib/auth';
|
||||
import DashboardLayout from '@/components/DashboardLayout';
|
||||
import UniversProTable from '@/components/UniversProTable';
|
||||
|
||||
export default async function UniversProPage() {
|
||||
const user = await getCurrentUser();
|
||||
|
||||
if (!user) {
|
||||
redirect('/login');
|
||||
}
|
||||
|
||||
return (
|
||||
<DashboardLayout user={user}>
|
||||
<div className="p-6">
|
||||
<h1 className="text-3xl font-semibold text-cblack mb-1">
|
||||
Univers Pro
|
||||
</h1>
|
||||
<p className="text-sm text-cgray mb-6">
|
||||
Base de données des contacts professionnels
|
||||
</p>
|
||||
|
||||
<UniversProTable />
|
||||
</div>
|
||||
</DashboardLayout>
|
||||
);
|
||||
}
|
||||
@@ -25,3 +25,31 @@ body {
|
||||
text-wrap: balance;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes slideUp {
|
||||
from {
|
||||
opacity: 0;
|
||||
transform: translateY(20px);
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
}
|
||||
|
||||
.animate-fadeIn {
|
||||
animation: fadeIn 0.2s ease-out;
|
||||
}
|
||||
|
||||
.animate-slideUp {
|
||||
animation: slideUp 0.3s ease-out;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user