0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

refactor(core): move findUsersByRoleName() to library

This commit is contained in:
Gao Sun 2023-01-11 13:44:15 +08:00
parent e55ff3d185
commit a4e2484087
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
3 changed files with 22 additions and 24 deletions

View file

@ -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,
};
};

View file

@ -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);

View file

@ -30,12 +30,11 @@ export default function adminUserRoutes<T extends AuthedRouter>(
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) => {