0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-16 20:26:19 -05:00

feat(core): search organization roles (#5627)

This commit is contained in:
wangsijie 2024-04-07 11:09:27 +08:00 committed by GitHub
parent cb4ef9fd0e
commit 468558721a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 1 deletions

View file

@ -0,0 +1,6 @@
---
"@logto/integration-tests": minor
"@logto/core": minor
---
Get organization roles with search keyword.

View file

@ -27,6 +27,11 @@ import { conditionalSql, convertToIdentifiers } from '#src/utils/sql.js';
import { RoleUserRelationQueries, UserRelationQueries } from './relations.js';
/**
* The schema field keys that can be used for searching roles.
*/
export const organizationRoleSearchKeys = Object.freeze(['id', 'name', 'description'] as const);
class OrganizationRolesQueries extends SchemaQueries<
OrganizationRoleKeys,
CreateOrganizationRole,

View file

@ -9,7 +9,9 @@ import { z } from 'zod';
import koaGuard from '#src/middleware/koa-guard.js';
import koaPagination from '#src/middleware/koa-pagination.js';
import koaQuotaGuard from '#src/middleware/koa-quota-guard.js';
import { organizationRoleSearchKeys } from '#src/queries/organization/index.js';
import SchemaRouter from '#src/utils/SchemaRouter.js';
import { parseSearchOptions } from '#src/utils/search.js';
import { type AuthedRouter, type RouterInitArgs } from '../types.js';
@ -40,12 +42,16 @@ export default function organizationRoleRoutes<T extends AuthedRouter>(
'/',
koaPagination(),
koaGuard({
query: z.object({ q: z.string().optional() }),
response: organizationRoleWithScopesGuard.array(),
status: [200],
}),
async (ctx, next) => {
const { limit, offset } = ctx.pagination;
const [count, entities] = await roles.findAll(limit, offset);
const search = parseSearchOptions(organizationRoleSearchKeys, ctx.guard.query);
const [count, entities] = await roles.findAll(limit, offset, search);
ctx.pagination.totalCount = count;
ctx.body = entities;

View file

@ -64,6 +64,18 @@ describe('organization role APIs', () => {
expect(roles2[0]?.id).toBe(roles[10]?.id);
});
it('should be able to get organization roles with search keyword', async () => {
const [name1, name2] = ['test' + randomId(), 'test' + randomId()];
await Promise.all([
roleApi.create({ name: name1, description: 'A test organization role.' }),
roleApi.create({ name: name2 }),
]);
const roles = await roleApi.getList(new URLSearchParams({ q: name1 }));
expect(roles).toHaveLength(1);
expect(roles[0]).toHaveProperty('name', name1);
});
it('should be able to create and get organization roles by id', async () => {
const createdRole = await roleApi.create({ name: 'test' + randomId() });
const { scopes, ...role } = await roleApi.get(createdRole.id);