mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-09-13 12:11:30 +08:00
Combining the admin API routes with the backends user routes
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { withRole } from "src/lib/auth";
|
||||
import { oasstApiClient } from "src/lib/oasst_api_client";
|
||||
import prisma from "src/lib/prismadb";
|
||||
|
||||
// The number of users to fetch in any request.
|
||||
@@ -14,18 +15,37 @@ const handler = withRole("admin", async (req, res) => {
|
||||
// Note: with Prisma this isn't the most efficient but it's the only possible
|
||||
// option with cuid based User IDs.
|
||||
const { pageIndex } = req.query;
|
||||
const skip = parseInt(pageIndex as string) * PAGE_SIZE || 0;
|
||||
|
||||
// Fetch 20 users.
|
||||
const users = await prisma.user.findMany({
|
||||
// First, get all the users according to the backend.
|
||||
const all_users = await oasstApiClient.fetch_users(20);
|
||||
|
||||
// Next, get all the users stored in the web's auth datbase to fetch their role.
|
||||
const local_user_ids = all_users.map(({ id }) => id);
|
||||
const local_users = await prisma.user.findMany({
|
||||
where: {
|
||||
id: {
|
||||
in: local_user_ids,
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
role: true,
|
||||
name: true,
|
||||
email: true,
|
||||
},
|
||||
skip,
|
||||
take: PAGE_SIZE,
|
||||
});
|
||||
|
||||
// Combine the information by updating the set of full users with their role.
|
||||
// Default any users without a role set locally as "general".
|
||||
const local_user_map = local_users.reduce((result, user) => {
|
||||
result.set(user.id, user.role);
|
||||
return result;
|
||||
}, new Map());
|
||||
|
||||
const users = all_users.map((user) => {
|
||||
const role = local_user_map.get(user.id) || "general";
|
||||
return {
|
||||
...user,
|
||||
role,
|
||||
};
|
||||
});
|
||||
|
||||
res.status(200).json(users);
|
||||
|
||||
Reference in New Issue
Block a user