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:
parent
e55ff3d185
commit
a4e2484087
3 changed files with 22 additions and 24 deletions
|
@ -50,8 +50,8 @@ export const createUserLibrary = (queries: Queries) => {
|
||||||
const {
|
const {
|
||||||
pool,
|
pool,
|
||||||
roles: { findRolesByRoleNames, insertRoles, findRoleByRoleName },
|
roles: { findRolesByRoleNames, insertRoles, findRoleByRoleName },
|
||||||
users: { hasUser, hasUserWithEmail, hasUserWithId, hasUserWithPhone },
|
users: { hasUser, hasUserWithEmail, hasUserWithId, hasUserWithPhone, findUsersByIds },
|
||||||
usersRoles: { insertUsersRoles },
|
usersRoles: { insertUsersRoles, findUsersRolesByRoleId },
|
||||||
} = queries;
|
} = queries;
|
||||||
|
|
||||||
const generateUserId = async (retries = 500) =>
|
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 {
|
return {
|
||||||
generateUserId,
|
generateUserId,
|
||||||
insertUser,
|
insertUser,
|
||||||
checkIdentifierCollision,
|
checkIdentifierCollision,
|
||||||
|
findUsersByRoleName,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -12,8 +12,8 @@ import type { Search } from '#src/utils/search.js';
|
||||||
import { buildConditionsFromSearch } from '#src/utils/search.js';
|
import { buildConditionsFromSearch } from '#src/utils/search.js';
|
||||||
|
|
||||||
// TODO: @sijie remove this
|
// TODO: @sijie remove this
|
||||||
import { findRoleByRoleName, findRolesByRoleIds } from './roles.js';
|
import { findRolesByRoleIds } from './roles.js';
|
||||||
import { findUsersRolesByRoleId, findUsersRolesByUserId } from './users-roles.js';
|
import { findUsersRolesByUserId } from './users-roles.js';
|
||||||
|
|
||||||
const { table, fields } = convertToIdentifiers(Users);
|
const { table, fields } = convertToIdentifiers(Users);
|
||||||
|
|
||||||
|
@ -216,22 +216,6 @@ export const createUserQueries = (pool: CommonQueryMethods) => {
|
||||||
group by date(${fields.createdAt})
|
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 {
|
return {
|
||||||
findUserByUsername,
|
findUserByUsername,
|
||||||
findUserByEmail,
|
findUserByEmail,
|
||||||
|
@ -251,7 +235,6 @@ export const createUserQueries = (pool: CommonQueryMethods) => {
|
||||||
deleteUserIdentity,
|
deleteUserIdentity,
|
||||||
hasActiveUsers,
|
hasActiveUsers,
|
||||||
getDailyNewUserCountsByTimeInterval,
|
getDailyNewUserCountsByTimeInterval,
|
||||||
findUsersByRoleName,
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -275,5 +258,4 @@ export const {
|
||||||
deleteUserIdentity,
|
deleteUserIdentity,
|
||||||
hasActiveUsers,
|
hasActiveUsers,
|
||||||
getDailyNewUserCountsByTimeInterval,
|
getDailyNewUserCountsByTimeInterval,
|
||||||
findUsersByRoleName,
|
|
||||||
} = createUserQueries(envSet.pool);
|
} = createUserQueries(envSet.pool);
|
||||||
|
|
|
@ -30,12 +30,11 @@ export default function adminUserRoutes<T extends AuthedRouter>(
|
||||||
updateUserById,
|
updateUserById,
|
||||||
hasUserWithEmail,
|
hasUserWithEmail,
|
||||||
hasUserWithPhone,
|
hasUserWithPhone,
|
||||||
findUsersByRoleName,
|
|
||||||
},
|
},
|
||||||
usersRoles: { deleteUsersRolesByUserIdAndRoleId, findUsersRolesByRoleId, insertUsersRoles },
|
usersRoles: { deleteUsersRolesByUserIdAndRoleId, findUsersRolesByRoleId, insertUsersRoles },
|
||||||
} = queries;
|
} = queries;
|
||||||
const {
|
const {
|
||||||
users: { checkIdentifierCollision, generateUserId, insertUser },
|
users: { checkIdentifierCollision, generateUserId, insertUser, findUsersByRoleName },
|
||||||
} = libraries;
|
} = libraries;
|
||||||
|
|
||||||
router.get('/users', koaPagination(), async (ctx, next) => {
|
router.get('/users', koaPagination(), async (ctx, next) => {
|
||||||
|
|
Loading…
Reference in a new issue