user reaearch and fix function delete
This commit is contained in:
@@ -1,12 +1,35 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getDatabase } from '@/lib/db/mongodb';
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getDatabase } from "@/lib/db/mongodb";
|
||||
import { ObjectId } from "mongodb";
|
||||
|
||||
const ALLOWED_COLLECTIONS = [
|
||||
'accessroles', 'aclentries', 'actions', 'agentcategories', 'agents',
|
||||
'assistants', 'balances', 'banners', 'conversations', 'conversationtags',
|
||||
'files', 'groups', 'keys', 'memoryentries', 'messages', 'pluginauths',
|
||||
'presets', 'projects', 'promptgroups', 'prompts', 'roles', 'sessions',
|
||||
'sharedlinks', 'tokens', 'toolcalls', 'transactions', 'users'
|
||||
"accessroles",
|
||||
"aclentries",
|
||||
"actions",
|
||||
"agentcategories",
|
||||
"agents",
|
||||
"assistants",
|
||||
"balances",
|
||||
"banners",
|
||||
"conversations",
|
||||
"conversationtags",
|
||||
"files",
|
||||
"groups",
|
||||
"keys",
|
||||
"memoryentries",
|
||||
"messages",
|
||||
"pluginauths",
|
||||
"presets",
|
||||
"projects",
|
||||
"promptgroups",
|
||||
"prompts",
|
||||
"roles",
|
||||
"sessions",
|
||||
"sharedlinks",
|
||||
"tokens",
|
||||
"toolcalls",
|
||||
"transactions",
|
||||
"users",
|
||||
];
|
||||
|
||||
export async function GET(
|
||||
@@ -14,32 +37,53 @@ export async function GET(
|
||||
{ params }: { params: Promise<{ collection: string }> }
|
||||
) {
|
||||
const { collection } = await params;
|
||||
|
||||
|
||||
try {
|
||||
|
||||
if (!ALLOWED_COLLECTIONS.includes(collection)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Collection non autorisée' },
|
||||
{ error: "Collection non autorisée" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const page = parseInt(searchParams.get('page') || '1');
|
||||
const limit = parseInt(searchParams.get('limit') || '20');
|
||||
const filter = JSON.parse(searchParams.get('filter') || '{}');
|
||||
const page = parseInt(searchParams.get("page") || "1");
|
||||
const limit = parseInt(searchParams.get("limit") || "20");
|
||||
const filter = JSON.parse(searchParams.get("filter") || "{}");
|
||||
|
||||
// Gestion spéciale pour la collection users avec recherche par email ou id
|
||||
if (collection === "users") {
|
||||
const email = searchParams.get("email");
|
||||
const id = searchParams.get("id");
|
||||
|
||||
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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const db = await getDatabase();
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const [data, total] = await Promise.all([
|
||||
db.collection(collection)
|
||||
db
|
||||
.collection(collection)
|
||||
.find(filter)
|
||||
.skip(skip)
|
||||
.limit(limit)
|
||||
.sort({ createdAt: -1 })
|
||||
.toArray(),
|
||||
db.collection(collection).countDocuments(filter)
|
||||
db.collection(collection).countDocuments(filter),
|
||||
]);
|
||||
|
||||
return NextResponse.json({
|
||||
@@ -47,13 +91,10 @@ export async function GET(
|
||||
total,
|
||||
page,
|
||||
limit,
|
||||
totalPages: Math.ceil(total / limit)
|
||||
totalPages: Math.ceil(total / limit),
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Erreur lors de la récupération de ${collection}:`, error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Erreur serveur' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return NextResponse.json({ error: "Erreur serveur" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user