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

feat: creates paginated organizations query #189

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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added
- Introduced the getOrganizationsPaginatedByEmail function to retrieve organizations with pagination, preventing timeouts.

## [0.62.0] - 2024-12-03
### Added

Expand Down
13 changes: 12 additions & 1 deletion graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ type Query {
getOrganizationsByEmail(email: String): [B2BOrganization]
@checkUserAccess
@cacheControl(scope: PRIVATE)

getOrganizationsPaginatedByEmail(
email: String
page: Int = 1
pageSize: Int = 25
): B2BOrganizationPaginated
@checkUserAccess
@cacheControl(scope: PRIVATE)
getActiveOrganizationsByEmail(email: String): [B2BOrganization]
@cacheControl(scope: PRIVATE)
@validateStoreUserAccess
Expand Down Expand Up @@ -490,6 +496,11 @@ type B2BOrganization {
costCenterName: String
}

type B2BOrganizationPaginated {
data: [B2BOrganization]
pagination: Pagination
}

type SimpleRole {
id: ID
name: String!
Expand Down
17 changes: 17 additions & 0 deletions node/clients/storefrontPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import saveUser from '../mutations/saveUser'
import updateUser from '../mutations/updateUser'
import getB2BUser from '../queries/getB2BUser'
import getOrganizationsByEmail from '../queries/getOrganizationsByEmail'
import getOrganizationsPaginatedByEmail from '../queries/getOrganizationsPaginatedByEmail'
import getPermission from '../queries/getPermission'
import getRole from '../queries/getRole'
import getUser from '../queries/getUser'
Expand Down Expand Up @@ -40,6 +41,22 @@ export default class StorefrontPermissions extends AppGraphQLClient {
})
}

public getOrganizationsPaginatedByEmail = async (
email: string,
page: number,
pageSize: number
): Promise<any> => {
return this.query({
extensions: this.getPersistedQuery(),
query: getOrganizationsPaginatedByEmail,
variables: {
email,
page,
pageSize,
},
})
}

public listRoles = async (): Promise<any> => {
return this.query({
extensions: this.getPersistedQuery(),
Expand Down
25 changes: 25 additions & 0 deletions node/queries/getOrganizationsPaginatedByEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { print } from 'graphql'
import gql from 'graphql-tag'

export default print(gql`
query OrganizationsPaginated($email: String!, $page: Int, $pageSize: Int) {
getOrganizationsPaginatedByEmail(
email: $email
page: $page
pageSize: $pageSize
) {
data {
costId
orgId
roleId
id
clId
}
pagination {
page
pageSize
total
}
}
}
`)
52 changes: 52 additions & 0 deletions node/resolvers/Queries/Organizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,58 @@ const Organizations = {
}
},

getOrganizationsPaginatedByEmail: async (
_: void,
{
email,
page = 1,
pageSize = 25,
}: {
email?: string
page: number
pageSize: number
},
{
clients: { storefrontPermissions },
vtex: { logger },
}: any
) => {
try {
const organizationFilters: string[] = []
const fromSession = false

const response =
await storefrontPermissions.getOrganizationsPaginatedByEmail(
email,
page,
pageSize
)

const {
data: rawOrganizations = [],
pagination,
} = response?.data?.getOrganizationsPaginatedByEmail || {}

const organizations =
organizationFilters.length > 0
? rawOrganizations.filter(({ orgId }: { orgId: string }) =>
organizationFilters.includes(orgId) || fromSession
)
: rawOrganizations

return {
data: organizations,
pagination,
}
} catch (error) {
logger.error({
error,
message: 'getOrganizationsPaginatedByEmail-error',
})
throw new GraphQLError(getErrorMessage(error))
}
},

getActiveOrganizationsByEmail: async (
_: void,
{ email }: { email: string },
Expand Down