From 39bc1a82f18519dc146b6471a3716b5faaa9bd55 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Tue, 17 Jan 2023 13:05:07 +0800 Subject: [PATCH] feat(core): get all scopes for a role (#2967) --- packages/core/src/routes/role.scope.test.ts | 7 +------ packages/core/src/routes/role.scope.ts | 10 ++++++++-- .../integration-tests/src/tests/api/role.scope.test.ts | 1 - 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/core/src/routes/role.scope.test.ts b/packages/core/src/routes/role.scope.test.ts index d674573c4..50f7ef108 100644 --- a/packages/core/src/routes/role.scope.test.ts +++ b/packages/core/src/routes/role.scope.test.ts @@ -65,12 +65,7 @@ describe('role scope routes', () => { findScopesByIds.mockResolvedValueOnce([mockScope]); const response = await roleRequester.get(`/roles/${mockRole.id}/scopes`); expect(response.status).toEqual(200); - expect(response.body).toEqual([ - { - ...mockScope, - resource: mockResource, - }, - ]); + expect(response.body).toEqual([mockScope]); }); it('POST /roles/:id/scopes', async () => { diff --git a/packages/core/src/routes/role.scope.ts b/packages/core/src/routes/role.scope.ts index fc93b0763..dff3f986e 100644 --- a/packages/core/src/routes/role.scope.ts +++ b/packages/core/src/routes/role.scope.ts @@ -22,7 +22,7 @@ export default function roleScopeRoutes( router.get( '/roles/:id/scopes', - koaPagination(), + koaPagination({ isOptional: true }), koaGuard({ params: object({ id: string().min(1) }), }), @@ -30,7 +30,7 @@ export default function roleScopeRoutes( const { params: { id }, } = ctx.guard; - const { limit, offset } = ctx.pagination; + const { limit, offset, disabled } = ctx.pagination; const { searchParams } = ctx.request.URL; await findRoleById(id); @@ -41,6 +41,12 @@ export default function roleScopeRoutes( const rolesScopes = await findRolesScopesByRoleId(id); const scopeIds = rolesScopes.map(({ scopeId }) => scopeId); + if (disabled) { + ctx.body = await searchScopesByScopeIds(scopeIds, search); + + return next(); + } + const [{ count }, scopes] = await Promise.all([ countScopesByScopeIds(scopeIds, search), searchScopesByScopeIds(scopeIds, search, limit, offset), diff --git a/packages/integration-tests/src/tests/api/role.scope.test.ts b/packages/integration-tests/src/tests/api/role.scope.test.ts index 6669813b3..f94cb6945 100644 --- a/packages/integration-tests/src/tests/api/role.scope.test.ts +++ b/packages/integration-tests/src/tests/api/role.scope.test.ts @@ -17,7 +17,6 @@ describe('roles scopes', () => { expect(scopes.length).toBe(1); expect(scopes[0]).toHaveProperty('id', scope.id); - expect(scopes[0]).toHaveProperty('resource.id', resource.id); }); it('should assign scopes to role successfully', async () => {