From 365f4c4b1f8c4c0c31e527ccb89fcaa45e4801a9 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Tue, 2 Apr 2024 06:00:12 -0600 Subject: [PATCH] Add `role` parameter to `users.list` endpoint (#6754) --- server/routes/api/users/schema.ts | 10 ++++++++++ server/routes/api/users/users.test.ts | 20 ++++++++++++++++++++ server/routes/api/users/users.ts | 10 +++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/server/routes/api/users/schema.ts b/server/routes/api/users/schema.ts index 05d10dabc..706b70f05 100644 --- a/server/routes/api/users/schema.ts +++ b/server/routes/api/users/schema.ts @@ -29,6 +29,16 @@ export const UsersListSchema = z.object({ query: z.string().optional(), + /** The user's role */ + role: z.nativeEnum(UserRole).optional(), + + /** + * Filter the users by their status – passing a user role is deprecated here, instead use the + * `role` parameter, which will allow filtering by role and status, eg invited members, or + * suspended admins. + * + * @deprecated + */ filter: z .enum([ "invited", diff --git a/server/routes/api/users/users.test.ts b/server/routes/api/users/users.test.ts index 81eabdfe2..07b438428 100644 --- a/server/routes/api/users/users.test.ts +++ b/server/routes/api/users/users.test.ts @@ -40,6 +40,26 @@ describe("#users.list", () => { expect(body.data[0].id).toEqual(user.id); }); + it("should allow filtering by role", async () => { + const user = await buildUser({ + name: "Tester", + }); + const admin = await buildAdmin({ + name: "Admin", + teamId: user.teamId, + }); + const res = await server.post("/api/users.list", { + body: { + role: UserRole.Admin, + token: user.getJwtToken(), + }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); + expect(body.data[0].id).toEqual(admin.id); + }); + it("should allow filtering to suspended users", async () => { const admin = await buildAdmin(); await buildUser({ diff --git a/server/routes/api/users/users.ts b/server/routes/api/users/users.ts index 740dd6caa..9bd932c77 100644 --- a/server/routes/api/users/users.ts +++ b/server/routes/api/users/users.ts @@ -35,7 +35,8 @@ router.post( pagination(), validate(T.UsersListSchema), async (ctx: APIContext) => { - const { sort, direction, query, filter, ids, emails } = ctx.input.body; + const { sort, direction, query, role, filter, ids, emails } = + ctx.input.body; const actor = ctx.state.auth.user; let where: WhereOptions = { @@ -113,6 +114,13 @@ router.post( } } + if (role) { + where = { + ...where, + role, + }; + } + if (query) { where = { ...where,