Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): search organization roles #5627

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/quick-kings-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@logto/integration-tests": minor
"@logto/core": minor
---

Get organization roles with search keyword.
5 changes: 5 additions & 0 deletions packages/core/src/queries/organization/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/routes/organization/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading