refactor: rewrite useUsers with RQ
This commit is contained in:
@@ -1,19 +0,0 @@
|
|||||||
import paginate from '../../helpers/pagination.js';
|
|
||||||
import User from '../../models/user.js';
|
|
||||||
|
|
||||||
const getUsers = async (_parent, params, context) => {
|
|
||||||
context.currentUser.can('read', 'User');
|
|
||||||
|
|
||||||
const usersQuery = User.query()
|
|
||||||
.leftJoinRelated({
|
|
||||||
role: true,
|
|
||||||
})
|
|
||||||
.withGraphFetched({
|
|
||||||
role: true,
|
|
||||||
})
|
|
||||||
.orderBy('full_name', 'asc');
|
|
||||||
|
|
||||||
return paginate(usersQuery, params.limit, params.offset);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default getUsers;
|
|
@@ -1,148 +0,0 @@
|
|||||||
import { describe, it, expect, beforeEach } from 'vitest';
|
|
||||||
import request from 'supertest';
|
|
||||||
import app from '../../app';
|
|
||||||
import createAuthTokenByUserId from '../../helpers/create-auth-token-by-user-id';
|
|
||||||
import { createRole } from '../../../test/factories/role';
|
|
||||||
import { createPermission } from '../../../test/factories/permission';
|
|
||||||
import { createUser } from '../../../test/factories/user';
|
|
||||||
|
|
||||||
describe('graphQL getUsers query', () => {
|
|
||||||
const query = `
|
|
||||||
query {
|
|
||||||
getUsers(limit: 10, offset: 0) {
|
|
||||||
pageInfo {
|
|
||||||
currentPage
|
|
||||||
totalPages
|
|
||||||
}
|
|
||||||
totalCount
|
|
||||||
edges {
|
|
||||||
node {
|
|
||||||
id
|
|
||||||
fullName
|
|
||||||
email
|
|
||||||
role {
|
|
||||||
id
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
describe('and without permissions', () => {
|
|
||||||
it('should throw not authorized error', async () => {
|
|
||||||
const userWithoutPermissions = await createUser();
|
|
||||||
const token = createAuthTokenByUserId(userWithoutPermissions.id);
|
|
||||||
|
|
||||||
const response = await request(app)
|
|
||||||
.post('/graphql')
|
|
||||||
.set('Authorization', token)
|
|
||||||
.send({ query })
|
|
||||||
.expect(200);
|
|
||||||
|
|
||||||
expect(response.body.errors).toBeDefined();
|
|
||||||
expect(response.body.errors[0].message).toEqual('Not authorized!');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('and with correct permissions', () => {
|
|
||||||
let role, currentUser, anotherUser, token, requestObject;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
role = await createRole({
|
|
||||||
key: 'sample',
|
|
||||||
name: 'sample',
|
|
||||||
});
|
|
||||||
|
|
||||||
await createPermission({
|
|
||||||
action: 'read',
|
|
||||||
subject: 'User',
|
|
||||||
roleId: role.id,
|
|
||||||
});
|
|
||||||
|
|
||||||
currentUser = await createUser({
|
|
||||||
roleId: role.id,
|
|
||||||
fullName: 'Current User',
|
|
||||||
});
|
|
||||||
|
|
||||||
anotherUser = await createUser({
|
|
||||||
roleId: role.id,
|
|
||||||
fullName: 'Another User',
|
|
||||||
});
|
|
||||||
|
|
||||||
token = createAuthTokenByUserId(currentUser.id);
|
|
||||||
requestObject = request(app).post('/graphql').set('Authorization', token);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return users data', async () => {
|
|
||||||
const response = await requestObject.send({ query }).expect(200);
|
|
||||||
|
|
||||||
const expectedResponsePayload = {
|
|
||||||
data: {
|
|
||||||
getUsers: {
|
|
||||||
edges: [
|
|
||||||
{
|
|
||||||
node: {
|
|
||||||
email: anotherUser.email,
|
|
||||||
fullName: anotherUser.fullName,
|
|
||||||
id: anotherUser.id,
|
|
||||||
role: {
|
|
||||||
id: role.id,
|
|
||||||
name: role.name,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
node: {
|
|
||||||
email: currentUser.email,
|
|
||||||
fullName: currentUser.fullName,
|
|
||||||
id: currentUser.id,
|
|
||||||
role: {
|
|
||||||
id: role.id,
|
|
||||||
name: role.name,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
pageInfo: {
|
|
||||||
currentPage: 1,
|
|
||||||
totalPages: 1,
|
|
||||||
},
|
|
||||||
totalCount: 2,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
expect(response.body).toEqual(expectedResponsePayload);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not return users data with password', async () => {
|
|
||||||
const query = `
|
|
||||||
query {
|
|
||||||
getUsers(limit: 10, offset: 0) {
|
|
||||||
pageInfo {
|
|
||||||
currentPage
|
|
||||||
totalPages
|
|
||||||
}
|
|
||||||
totalCount
|
|
||||||
edges {
|
|
||||||
node {
|
|
||||||
id
|
|
||||||
fullName
|
|
||||||
password
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
const response = await requestObject.send({ query }).expect(400);
|
|
||||||
|
|
||||||
expect(response.body.errors).toBeDefined();
|
|
||||||
expect(response.body.errors[0].message).toEqual(
|
|
||||||
'Cannot query field "password" on type "User".'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
@@ -10,7 +10,6 @@ import getFlow from './queries/get-flow.js';
|
|||||||
import getFlows from './queries/get-flows.js';
|
import getFlows from './queries/get-flows.js';
|
||||||
import getNotifications from './queries/get-notifications.js';
|
import getNotifications from './queries/get-notifications.js';
|
||||||
import getStepWithTestExecutions from './queries/get-step-with-test-executions.js';
|
import getStepWithTestExecutions from './queries/get-step-with-test-executions.js';
|
||||||
import getUsers from './queries/get-users.js';
|
|
||||||
import testConnection from './queries/test-connection.js';
|
import testConnection from './queries/test-connection.js';
|
||||||
|
|
||||||
const queryResolvers = {
|
const queryResolvers = {
|
||||||
@@ -26,7 +25,6 @@ const queryResolvers = {
|
|||||||
getFlows,
|
getFlows,
|
||||||
getNotifications,
|
getNotifications,
|
||||||
getStepWithTestExecutions,
|
getStepWithTestExecutions,
|
||||||
getUsers,
|
|
||||||
testConnection,
|
testConnection,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -26,7 +26,6 @@ type Query {
|
|||||||
getBillingAndUsage: GetBillingAndUsage
|
getBillingAndUsage: GetBillingAndUsage
|
||||||
getConfig(keys: [String]): JSONObject
|
getConfig(keys: [String]): JSONObject
|
||||||
getNotifications: [Notification]
|
getNotifications: [Notification]
|
||||||
getUsers(limit: Int!, offset: Int!): UserConnection
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Mutation {
|
type Mutation {
|
||||||
@@ -304,16 +303,6 @@ type SamlAuthProvidersRoleMapping {
|
|||||||
remoteRoleName: String
|
remoteRoleName: String
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserConnection {
|
|
||||||
edges: [UserEdge]
|
|
||||||
pageInfo: PageInfo
|
|
||||||
totalCount: Int
|
|
||||||
}
|
|
||||||
|
|
||||||
type UserEdge {
|
|
||||||
node: User
|
|
||||||
}
|
|
||||||
|
|
||||||
input CreateConnectionInput {
|
input CreateConnectionInput {
|
||||||
key: String!
|
key: String!
|
||||||
appAuthClientId: String
|
appAuthClientId: String
|
||||||
|
@@ -19,18 +19,18 @@ import useFormatMessage from 'hooks/useFormatMessage';
|
|||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import TablePaginationActions from './TablePaginationActions';
|
import TablePaginationActions from './TablePaginationActions';
|
||||||
import { TablePagination } from './style';
|
import { TablePagination } from './style';
|
||||||
|
|
||||||
export default function UserList() {
|
export default function UserList() {
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const [page, setPage] = React.useState(0);
|
const [page, setPage] = React.useState(0);
|
||||||
const [rowsPerPage, setRowsPerPage] = React.useState(10);
|
const { data: usersData, isLoading } = useUsers(page + 1);
|
||||||
const { users, pageInfo, totalCount, loading } = useUsers(page, rowsPerPage);
|
const users = usersData?.data;
|
||||||
|
const { count } = usersData?.meta || {};
|
||||||
|
|
||||||
const handleChangePage = (event, newPage) => {
|
const handleChangePage = (event, newPage) => {
|
||||||
setPage(newPage);
|
setPage(newPage);
|
||||||
};
|
};
|
||||||
const handleChangeRowsPerPage = (event) => {
|
|
||||||
setRowsPerPage(+event.target.value);
|
|
||||||
setPage(0);
|
|
||||||
};
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<TableContainer component={Paper}>
|
<TableContainer component={Paper}>
|
||||||
@@ -68,14 +68,14 @@ export default function UserList() {
|
|||||||
</TableRow>
|
</TableRow>
|
||||||
</TableHead>
|
</TableHead>
|
||||||
<TableBody>
|
<TableBody>
|
||||||
{loading && (
|
{isLoading && (
|
||||||
<ListLoader
|
<ListLoader
|
||||||
data-test="users-list-loader"
|
data-test="users-list-loader"
|
||||||
rowsNumber={3}
|
rowsNumber={3}
|
||||||
columnsNumber={2}
|
columnsNumber={2}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
{!loading &&
|
{!isLoading &&
|
||||||
users.map((user) => (
|
users.map((user) => (
|
||||||
<TableRow
|
<TableRow
|
||||||
key={user.id}
|
key={user.id}
|
||||||
@@ -120,18 +120,16 @@ export default function UserList() {
|
|||||||
</TableRow>
|
</TableRow>
|
||||||
))}
|
))}
|
||||||
</TableBody>
|
</TableBody>
|
||||||
{totalCount && (
|
{!isLoading && typeof count === 'number' && (
|
||||||
<TableFooter>
|
<TableFooter>
|
||||||
<TableRow>
|
<TableRow>
|
||||||
<TablePagination
|
<TablePagination
|
||||||
data-total-count={totalCount}
|
data-total-count={count}
|
||||||
data-rows-per-page={rowsPerPage}
|
rowsPerPageOptions={[]}
|
||||||
rowsPerPageOptions={[10, 25, 50, 100]}
|
|
||||||
page={page}
|
page={page}
|
||||||
count={totalCount}
|
count={count}
|
||||||
onPageChange={handleChangePage}
|
onPageChange={handleChangePage}
|
||||||
rowsPerPage={rowsPerPage}
|
rowsPerPage={10}
|
||||||
onRowsPerPageChange={handleChangeRowsPerPage}
|
|
||||||
ActionsComponent={TablePaginationActions}
|
ActionsComponent={TablePaginationActions}
|
||||||
/>
|
/>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
|
@@ -1,23 +0,0 @@
|
|||||||
import { gql } from '@apollo/client';
|
|
||||||
export const GET_USERS = gql`
|
|
||||||
query GetUsers($limit: Int!, $offset: Int!) {
|
|
||||||
getUsers(limit: $limit, offset: $offset) {
|
|
||||||
pageInfo {
|
|
||||||
currentPage
|
|
||||||
totalPages
|
|
||||||
}
|
|
||||||
totalCount
|
|
||||||
edges {
|
|
||||||
node {
|
|
||||||
id
|
|
||||||
fullName
|
|
||||||
email
|
|
||||||
role {
|
|
||||||
id
|
|
||||||
name
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
`;
|
|
@@ -1,20 +1,18 @@
|
|||||||
import { useQuery } from '@apollo/client';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { GET_USERS } from 'graphql/queries/get-users';
|
import api from 'helpers/api';
|
||||||
const getLimitAndOffset = (page, rowsPerPage) => ({
|
|
||||||
limit: rowsPerPage,
|
export default function useUsers(page) {
|
||||||
offset: page * rowsPerPage,
|
|
||||||
});
|
const query = useQuery({
|
||||||
export default function useUsers(page, rowsPerPage) {
|
queryKey: ['users', page],
|
||||||
const { data, loading } = useQuery(GET_USERS, {
|
queryFn: async ({ signal }) => {
|
||||||
variables: getLimitAndOffset(page, rowsPerPage),
|
const { data } = await api.get(`/v1/admin/users`, {
|
||||||
|
signal,
|
||||||
|
params: { page },
|
||||||
|
});
|
||||||
|
return data;
|
||||||
|
},
|
||||||
});
|
});
|
||||||
const users = data?.getUsers.edges.map(({ node }) => node) || [];
|
|
||||||
const pageInfo = data?.getUsers.pageInfo;
|
return query;
|
||||||
const totalCount = data?.getUsers.totalCount;
|
|
||||||
return {
|
|
||||||
users,
|
|
||||||
pageInfo,
|
|
||||||
totalCount,
|
|
||||||
loading,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user