Allow to filter user by display_name

This commit is contained in:
notmd
2023-01-18 15:19:00 +07:00
parent c912caaf76
commit 801ad553b8
9 changed files with 361 additions and 145 deletions
+10 -3
View File
@@ -1,11 +1,12 @@
import { withRole } from "src/lib/auth";
import { oasstApiClient } from "src/lib/oasst_api_client";
import prisma from "src/lib/prismadb";
import { BackendUser } from "src/types/Users";
/**
* The number of users to fetch in a single request. Could later be a query parameter.
*/
const PAGE_SIZE = 20;
const PAGE_SIZE = 1;
/**
* Returns a list of user results from the database when the requesting user is
@@ -17,10 +18,16 @@ const PAGE_SIZE = 20;
* direction.
*/
const handler = withRole("admin", async (req, res) => {
const { cursor, direction } = req.query;
const { cursor, direction, display_name = "" } = req.query;
// First, get all the users according to the backend.
const all_users = await oasstApiClient.fetch_users(PAGE_SIZE, cursor as string, direction === "forward");
let all_users: BackendUser[] = [];
if (typeof display_name === "string" && display_name) {
all_users = await oasstApiClient.fetch_user_by_display_name(display_name);
} else {
all_users = await oasstApiClient.fetch_users(PAGE_SIZE, cursor as string, direction === "forward");
}
// Next, get all the users stored in the web's auth database to fetch their role.
const local_user_ids = all_users.map(({ id }) => id);