Merge pull request #701 from LAION-AI/673-enhanced-admin-routing

673 enhanced admin management
This commit is contained in:
Keith Stevens
2023-01-15 08:34:09 +09:00
committed by GitHub
8 changed files with 191 additions and 60 deletions
+16 -10
View File
@@ -1,22 +1,28 @@
import { withRole } from "src/lib/auth";
import { oasstApiClient } from "src/lib/oasst_api_client";
import prisma from "src/lib/prismadb";
/**
* Update's the user's data in the database. Accessible only to admins.
*/
const handler = withRole("admin", async (req, res) => {
const { id, role } = req.body;
const { id, auth_method, user_id, notes, role } = req.body;
await prisma.user.update({
where: {
id,
},
data: {
role,
},
});
// If the user is authorized by the web, update their role.
if (auth_method === "local") {
await prisma.user.update({
where: {
id,
},
data: {
role,
},
});
}
// Tell the backend the user's enabled or not enabled status.
await oasstApiClient.set_user_status(user_id, role !== "banned", notes);
res.status(200).end();
res.status(200).json({});
});
export default handler;
+6 -8
View File
@@ -1,15 +1,13 @@
import { withRole } from "src/lib/auth";
import { oasstApiClient } from "src/lib/oasst_api_client";
import type { Message } from "src/types/Conversation";
/**
* Returns the messages recorded by the backend for a user.
*/
const handler = withRole("admin", async (req, res) => {
const { user } = req.query;
const messagesRes = await fetch(`${process.env.FASTAPI_URL}/api/v1/frontend_users/local/${user}/messages`, {
method: "GET",
headers: {
"X-API-Key": process.env.FASTAPI_KEY,
},
});
const messages = await messagesRes.json();
const messages: Message[] = await oasstApiClient.fetch_user_messages(user as string);
res.status(200).json(messages);
});
+28 -15
View File
@@ -1,31 +1,44 @@
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.
const PAGE_SIZE = 20;
/**
* Returns a list of user results from the database when the requesting user is
* a logged in admin.
*/
const handler = withRole("admin", async (req, res) => {
// Figure out the pagination index and skip that number of users.
//
// 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;
// TODO(#673): Update this to support pagination.
// 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);