modifs complete

This commit is contained in:
Biqoz
2025-11-27 13:58:47 +01:00
parent 5b8b3c84c9
commit 73f97919ac
12 changed files with 931 additions and 524 deletions

View File

@@ -55,29 +55,76 @@ export async function GET(
if (collection === "users") {
const email = searchParams.get("email");
const id = searchParams.get("id");
const search = searchParams.get("search"); // ✅ AJOUTER cette ligne
const search = searchParams.get("search");
const referent = searchParams.get("referent");
if (email) {
filter.email = email.toLowerCase();
} else if (id) {
// Vérifier si l'ID est un ObjectId valide
if (ObjectId.isValid(id)) {
filter._id = new ObjectId(id);
} else {
// Si l'ID n'est pas valide, retourner une erreur
return NextResponse.json(
{ error: "ID utilisateur invalide" },
{ status: 400 }
);
}
} else if (search) {
// ✅ AJOUTER ce bloc
// Recherche partielle sur nom et email
filter.$or = [
{ name: { $regex: search, $options: "i" } },
{ email: { $regex: search, $options: "i" } },
];
}
// Filtre par référent (peut être combiné avec search)
if (referent) {
filter.referent = referent;
}
}
// Gestion spéciale pour conversations - recherche par nom/email d'utilisateur
if (collection === "conversations") {
const search = searchParams.get("search");
const userId = searchParams.get("userId");
if (userId) {
// Recherche directe par userId (stocké comme string dans conversations)
filter.user = userId;
} else if (search && search.trim()) {
// Normaliser la recherche (enlever accents pour recherche insensible aux accents)
const normalizedSearch = search
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "");
const db = await getDatabase();
// Recherche users avec nom/email/username (insensible casse + accents)
const matchingUsers = await db
.collection("users")
.find({
$or: [
{ name: { $regex: normalizedSearch, $options: "i" } },
{ email: { $regex: normalizedSearch, $options: "i" } },
{ username: { $regex: normalizedSearch, $options: "i" } },
],
})
.project({ _id: 1 })
.toArray();
if (matchingUsers.length > 0) {
// IMPORTANT: Convertir en strings car user dans conversations est stocké comme string
const userIds = matchingUsers.map((u) => u._id.toString());
filter.user = { $in: userIds };
} else {
return NextResponse.json({
data: [],
total: 0,
page,
limit,
totalPages: 0,
});
}
}
}
const db = await getDatabase();