Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new query getOrganizationsByEmailPaginated #157

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

- Add new `getOrganizationsByEmailPaginated` query to allow pagination on organizations by email query

## [1.44.3] - 2024-08-22

### Fixed
Expand Down
13 changes: 13 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ type Query {
getOrganizationsByEmail(email: String!): [Organization]
@cacheControl(scope: PRIVATE)
@validateStoreUserAccess

getOrganizationsByEmailPaginated(
email: String!
page: Int
pageSize: Int
): OrganizationPagination
@cacheControl(scope: PRIVATE)
@validateStoreUserAccess
}

type Mutation {
Expand Down Expand Up @@ -215,6 +223,11 @@ type Organization {
roleId: ID
}

type OrganizationPagination {
data: [Organization]
pagination: Pagination
}

type Profile {
id: ID
name: String
Expand Down
95 changes: 95 additions & 0 deletions node/resolvers/Queries/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,63 @@ export const getAllUsersByEmail = async (_: any, params: any, ctx: Context) => {
return getAllUsers({ masterdata, logger, where })
}

export const getUsersByEmailPaginated = async ({
masterdata,
logger,
email,
page = 1,
pageSize = 25,
}: {
masterdata: any
logger: any
email: string
page: number
pageSize: number
}) => {
try {
const users = [] as any[]

const resp = await masterdata.searchDocumentsWithPaginationInfo({
dataEntity: config.name,
fields: [
'id',
'roleId',
'clId',
'email',
'name',
'orgId',
'costId',
'userId',
'canImpersonate',
'active',
],
pagination: {
page,
pageSize,
},
schema: config.version,
where: `email = "${email}"`,
})

const { data } = resp as unknown as {
pagination: {
total: number
}
data: any
}

users.push(...data)

return users
} catch (error) {
logger.error({
error,
message: 'Profiles.getUsersByEmailPaginated-error',
})
throw new Error(error)
}
}

vhaalmeida marked this conversation as resolved.
Show resolved Hide resolved
export const getActiveUserByEmail = async (
_: any,
params: any,
Expand Down Expand Up @@ -815,6 +872,44 @@ export const getOrganizationsByEmail = async (
}
}

export const getOrganizationsByEmailPaginated = async (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me, it doesn't make sense to implement the query here, it should be implemented on b2b-organization-graphql.

_: any,
{
email = '',
page = 1,
pageSize = 25,
}: {
email: string
page: number
pageSize: number
},
ctx: Context
) => {
const {
clients: { masterdata },
vtex: { logger },
} = ctx

try {
const data = await masterdata.searchDocumentsWithPaginationInfo({
dataEntity: config.name,
fields: ['clId', 'costId', 'id', 'orgId', 'roleId'],
pagination: { page, pageSize },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when querying paginated data from Master Data, we should add sort (e.g. sort: 'id asc', otherwise we might get inconsistent data result (e.g. duplicated entries) when querying a lot of data/pages.

schema: config.version,
where: `email = "${email}"`,
})

return data
} catch (error) {
logger.error({
error,
message: 'getOrganizationsByEmailPaginated-error',
})

return { status: 'error', message: error }
}
}

export const getUserByEmailOrgIdAndCostId = async (
_: any,
params: any,
Expand Down
2 changes: 2 additions & 0 deletions node/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
getAllUsersByEmail,
getB2BUserById,
getOrganizationsByEmail,
getOrganizationsByEmailPaginated,
getUser,
getUserByEmail,
listAllUsers,
Expand Down Expand Up @@ -59,6 +60,7 @@ export const resolvers = {
getB2BUser: getB2BUserById,
getFeaturesByModule,
getOrganizationsByEmail,
getOrganizationsByEmailPaginated,
getRole,
getSessionWatcher,
getUser,
Expand Down
2 changes: 1 addition & 1 deletion node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ sprintf-js@~1.0.2:
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=

"stats-lite@github:vtex/node-stats-lite#dist":
stats-lite@vtex/node-stats-lite#dist:
version "2.2.0"
resolved "https://codeload.github.com/vtex/node-stats-lite/tar.gz/1b0d39cc41ef7aaecfd541191f877887a2044797"
dependencies:
Expand Down
Loading