From a4e2484087373c1473527e99a45cfabec1f00437 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Wed, 11 Jan 2023 13:44:15 +0800 Subject: [PATCH] refactor(core): move `findUsersByRoleName()` to library --- packages/core/src/libraries/user.ts | 21 +++++++++++++++++++-- packages/core/src/queries/user.ts | 22 ++-------------------- packages/core/src/routes/admin-user.ts | 3 +-- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/packages/core/src/libraries/user.ts b/packages/core/src/libraries/user.ts index a50129b1f..5ff96453b 100644 --- a/packages/core/src/libraries/user.ts +++ b/packages/core/src/libraries/user.ts @@ -50,8 +50,8 @@ export const createUserLibrary = (queries: Queries) => { const { pool, roles: { findRolesByRoleNames, insertRoles, findRoleByRoleName }, - users: { hasUser, hasUserWithEmail, hasUserWithId, hasUserWithPhone }, - usersRoles: { insertUsersRoles }, + users: { hasUser, hasUserWithEmail, hasUserWithId, hasUserWithPhone, findUsersByIds }, + usersRoles: { insertUsersRoles, findUsersRolesByRoleId }, } = queries; const generateUserId = async (retries = 500) => @@ -140,9 +140,26 @@ export const createUserLibrary = (queries: Queries) => { } }; + const findUsersByRoleName = async (roleName: string) => { + const role = await findRoleByRoleName(roleName); + + if (!role) { + return []; + } + + const usersRoles = await findUsersRolesByRoleId(role.id); + + if (usersRoles.length === 0) { + return []; + } + + return findUsersByIds(usersRoles.map(({ userId }) => userId)); + }; + return { generateUserId, insertUser, checkIdentifierCollision, + findUsersByRoleName, }; }; diff --git a/packages/core/src/queries/user.ts b/packages/core/src/queries/user.ts index 26fae23eb..82312c365 100644 --- a/packages/core/src/queries/user.ts +++ b/packages/core/src/queries/user.ts @@ -12,8 +12,8 @@ import type { Search } from '#src/utils/search.js'; import { buildConditionsFromSearch } from '#src/utils/search.js'; // TODO: @sijie remove this -import { findRoleByRoleName, findRolesByRoleIds } from './roles.js'; -import { findUsersRolesByRoleId, findUsersRolesByUserId } from './users-roles.js'; +import { findRolesByRoleIds } from './roles.js'; +import { findUsersRolesByUserId } from './users-roles.js'; const { table, fields } = convertToIdentifiers(Users); @@ -216,22 +216,6 @@ export const createUserQueries = (pool: CommonQueryMethods) => { group by date(${fields.createdAt}) `); - const findUsersByRoleName = async (roleName: string) => { - const role = await findRoleByRoleName(roleName); - - if (!role) { - return []; - } - - const usersRoles = await findUsersRolesByRoleId(role.id); - - if (usersRoles.length === 0) { - return []; - } - - return findUsersByIds(usersRoles.map(({ userId }) => userId)); - }; - return { findUserByUsername, findUserByEmail, @@ -251,7 +235,6 @@ export const createUserQueries = (pool: CommonQueryMethods) => { deleteUserIdentity, hasActiveUsers, getDailyNewUserCountsByTimeInterval, - findUsersByRoleName, }; }; @@ -275,5 +258,4 @@ export const { deleteUserIdentity, hasActiveUsers, getDailyNewUserCountsByTimeInterval, - findUsersByRoleName, } = createUserQueries(envSet.pool); diff --git a/packages/core/src/routes/admin-user.ts b/packages/core/src/routes/admin-user.ts index 2b421366f..0275c8868 100644 --- a/packages/core/src/routes/admin-user.ts +++ b/packages/core/src/routes/admin-user.ts @@ -30,12 +30,11 @@ export default function adminUserRoutes( updateUserById, hasUserWithEmail, hasUserWithPhone, - findUsersByRoleName, }, usersRoles: { deleteUsersRolesByUserIdAndRoleId, findUsersRolesByRoleId, insertUsersRoles }, } = queries; const { - users: { checkIdentifierCollision, generateUserId, insertUser }, + users: { checkIdentifierCollision, generateUserId, insertUser, findUsersByRoleName }, } = libraries; router.get('/users', koaPagination(), async (ctx, next) => {