Supporting user pagination on the web admin view

This commit is contained in:
Keith Stevens
2023-01-15 15:26:12 +09:00
parent 69952dbf80
commit 35e92dbd15
5 changed files with 78 additions and 16 deletions
+13 -3
View File
@@ -2,17 +2,27 @@ 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 a single request. Could later be a query parameter.
*/
const PAGE_SIZE = 20;
/**
* Returns a list of user results from the database when the requesting user is
* a logged in admin.
*
* This takes two query params:
* - `cursor`: A string representing a user's `display_name`.
* - `direction`: Either "forward" or "backward" representing the pagination
* direction.
*/
const handler = withRole("admin", async (req, res) => {
// TODO(#673): Update this to support pagination.
const { cursor, direction } = req.query;
// First, get all the users according to the backend.
const all_users = await oasstApiClient.fetch_users(20);
const all_users = await oasstApiClient.fetch_users(PAGE_SIZE, cursor as string, direction === "forward");
// Next, get all the users stored in the web's auth datbase to fetch their role.
// 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);
const local_users = await prisma.user.findMany({
where: {