Skip to content

Commit

Permalink
Merge pull request #45 from vtex-apps/feature/get-users-pagination
Browse files Browse the repository at this point in the history
[B2BORG-6] Organization storefront UI - Sales admin view
  • Loading branch information
arturmagalhaesjr authored May 9, 2022
2 parents 00dd7bc + 51e71a7 commit d6fca35
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Added the pagination to listUsers query

## [1.18.1] - 2022-04-28

### Fixed
Expand Down
25 changes: 25 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ type Query {
getUserByEmail(email: String!): User @cacheControl(scope: PRIVATE)
listUsers(organizationId: ID, costCenterId: ID, roleId: ID): [User]
@cacheControl(scope: PRIVATE, maxAge: SHORT)
@deprecated(
reason: "This query is deprecated, use listUsersPaginated query instead."
)

listUsersPaginated(
organizationId: ID
costCenterId: ID
roleId: ID
page: Int
pageSize: Int
search: String
sortOrder: String
sortedBy: String
): UserPagination @cacheControl(scope: PRIVATE, maxAge: SHORT)
checkImpersonation: UserImpersonation
@settings(settingsType: "workspace")
@withSession
Expand Down Expand Up @@ -103,6 +117,17 @@ type Mutation {
@cacheControl(scope: PRIVATE)
}

type Pagination {
page: Int
pageSize: Int
total: Int
}

type UserPagination {
data: [User]
pagination: Pagination
}

type User {
id: ID
roleId: ID
Expand Down
95 changes: 89 additions & 6 deletions node/resolvers/Queries/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const getUser = async (_: any, params: any, ctx: Context) => {
where: `clId=${id}`,
})

const ret = user.length
return user.length
? {
...user[0],
name: `${cl.firstName} ${cl.lastName}`,
Expand All @@ -88,8 +88,6 @@ export const getUser = async (_: any, params: any, ctx: Context) => {
name: `${cl.firstName} ${cl.lastName}`,
email: cl.email,
}

return ret
} catch (e) {
return { status: 'error', message: e }
}
Expand Down Expand Up @@ -138,7 +136,7 @@ export const getUserByEmail = async (_: any, params: any, ctx: Context) => {
return [cachedUser]
}

const ret = await masterdata
return await masterdata
.searchDocuments({
dataEntity: config.name,
fields: [
Expand All @@ -157,8 +155,6 @@ export const getUserByEmail = async (_: any, params: any, ctx: Context) => {
where: `email=${email}`,
})
.catch(() => [])

return ret
} catch (e) {
return { status: 'error', message: e }
}
Expand Down Expand Up @@ -220,6 +216,93 @@ export const listUsers = async (
}
}

export const listUsersPaginated = async (
_: any,
{
organizationId = '',
costCenterId = '',
roleId = '',
page = 1,
pageSize = 25,
search = '',
sortOrder = 'asc',
sortedBy = 'email',
}: {
organizationId: string
costCenterId: string
roleId: string
page: number
pageSize: number
search: string
sortOrder: string
sortedBy: string
},
ctx: Context
) => {
const {
clients: { masterdata },
} = ctx

let res: any = []

const whereArray: string[] = []

if (organizationId) {
whereArray.push(`orgId=${organizationId}`)
}

if (costCenterId) {
whereArray.push(`costId=${costCenterId}`)
}

if (roleId) {
whereArray.push(`roleId=${roleId}`)
}

let whereSearchFields: any[] = []

if (search && search.length > 0) {
const fields = ['email']

whereSearchFields = fields.map((field) => `${field}="*${search}*"`)
}

let where = `${whereArray.join(' AND ')}`

if (whereSearchFields.length > 0) {
if (where.length > 0) {
where += ' AND '
}

where += `(${whereSearchFields.join(' OR ')})`
}

try {
res = await masterdata.searchDocumentsWithPaginationInfo({
dataEntity: config.name,
fields: [
'id',
'roleId',
'userId',
'clId',
'orgId',
'costId',
'name',
'email',
'canImpersonate',
],
pagination: { page, pageSize },
schema: config.version,
sort: `${sortedBy} ${sortOrder}`,
...(where && { where }),
})

return res
} catch (e) {
return { status: 'error', message: e }
}
}

const getRoleAndPermissionsByEmail = async ({
email,
module,
Expand Down
2 changes: 2 additions & 0 deletions node/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getUser,
getUserByEmail,
listUsers,
listUsersPaginated,
} from './Queries/Users'
import { Routes } from './Routes'

Expand Down Expand Up @@ -46,6 +47,7 @@ export const resolvers = {
listFeatures,
listRoles,
listUsers,
listUsersPaginated,
},
Routes,
}

0 comments on commit d6fca35

Please sign in to comment.