From 9e08aec69c77ec1a10acd1cb096ad4a2ae174a05 Mon Sep 17 00:00:00 2001 From: Tiago de Andrade Freire Date: Tue, 26 Nov 2024 17:12:58 -0300 Subject: [PATCH 1/4] feat: creates paginated organizations query --- graphql/schema.graphql | 11 ++ node/clients/storefrontPermissions.ts | 17 +++ .../getOrganizationsPaginatedByEmail.ts | 25 +++++ node/resolvers/Queries/Organizations.ts | 105 ++++++++++++++++++ 4 files changed, 158 insertions(+) create mode 100644 node/queries/getOrganizationsPaginatedByEmail.ts diff --git a/graphql/schema.graphql b/graphql/schema.graphql index a6a49414..3303c678 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -93,6 +93,12 @@ type Query { @checkUserAccess @cacheControl(scope: PRIVATE) + getOrganizationsPaginatedByEmail( + email: String + page: Int = 1 + pageSize: Int = 25 + ): B2BOrganizationPaginated @checkUserAccess @cacheControl(scope: PRIVATE) + checkOrganizationIsActive(id: String): Boolean @cacheControl(scope: PRIVATE) @auditAccess @@ -486,6 +492,11 @@ type B2BOrganization { costCenterName: String } +type B2BOrganizationPaginated { + data: [B2BOrganization] + pagination: Pagination +} + type SimpleRole { id: ID name: String! diff --git a/node/clients/storefrontPermissions.ts b/node/clients/storefrontPermissions.ts index 08a480ce..2692b0f5 100644 --- a/node/clients/storefrontPermissions.ts +++ b/node/clients/storefrontPermissions.ts @@ -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' @@ -40,6 +41,22 @@ export default class StorefrontPermissions extends AppGraphQLClient { }) } + public getOrganizationsPaginatedByEmail = async ( + email: string, + page: number, + pageSize: number + ): Promise => { + return this.query({ + extensions: this.getPersistedQuery(), + query: getOrganizationsPaginatedByEmail, + variables: { + email, + page, + pageSize, + }, + }) + } + public listRoles = async (): Promise => { return this.query({ extensions: this.getPersistedQuery(), diff --git a/node/queries/getOrganizationsPaginatedByEmail.ts b/node/queries/getOrganizationsPaginatedByEmail.ts new file mode 100644 index 00000000..8c3d9fb1 --- /dev/null +++ b/node/queries/getOrganizationsPaginatedByEmail.ts @@ -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 + } + } + } +`) diff --git a/node/resolvers/Queries/Organizations.ts b/node/resolvers/Queries/Organizations.ts index f541e1fe..4d8a6e17 100644 --- a/node/resolvers/Queries/Organizations.ts +++ b/node/resolvers/Queries/Organizations.ts @@ -270,6 +270,111 @@ const Organizations = { } }, + getOrganizationsPaginatedByEmail: async ( + _: void, + { + email, + page = 1, + pageSize = 25, + }: { + email?: string + page: number + pageSize: number + }, + { + clients: { storefrontPermissions, session }, + vtex: { logger, sessionToken, adminUserAuthToken }, + }: any + ) => { + const organizationFilters: string[] = [] + let fromSession = false + const { + data: { checkUserPermission }, + }: any = await storefrontPermissions + .checkUserPermission('vtex.b2b-organizations@1.x') + .catch((error: any) => { + logger.error({ + error, + message: 'checkUserPermission-error', + }) + + return { + data: { + checkUserPermission: null, + }, + } + }) + + if ( + (!adminUserAuthToken && + !checkUserPermission?.permissions.includes('add-sales-users-all')) || + !email?.length + ) { + const sessionData = await session + .getSession(sessionToken as string, ['*']) + .then((currentSession: any) => { + return currentSession.sessionData + }) + .catch((error: any) => { + logger.warn({ + error, + message: 'getOrganizationsPaginatedByEmail-session-error', + }) + + return null + }) + + if (checkUserPermission?.permissions.includes('add-users-organization')) { + const orgId = + sessionData?.namespaces?.['storefront-permissions']?.organization + ?.value + + if (!orgId) { + throw new Error('No permission for getting the organizations') + } + + organizationFilters.push(orgId) + } + + if (!email?.length) { + email = sessionData?.namespaces?.profile?.email?.value + fromSession = true + } + } + + const response = + await storefrontPermissions.getOrganizationsPaginatedByEmail( + email, + page, + pageSize + ) + + const organizations = + response?.data?.getOrganizationsPaginatedByEmail?.data?.filter( + ({ orgId }: { orgId: string }) => { + return ( + fromSession || + (organizationFilters.length > 0 + ? organizationFilters.find((id: string) => orgId === id) + : true) + ) + } + ) + + try { + return { + data: organizations, + pagination: response.data?.getOrganizationsPaginatedByEmail?.pagination, + } + } catch (error) { + logger.error({ + error, + message: 'getOrganizationsPaginatedByEmail-error', + }) + throw new GraphQLError(getErrorMessage(error)) + } + }, + getOrganizationByIdStorefront: async ( _: void, { id }: { id: string }, From 862b5630bbfcec4ef8af1e68bfbe3d333bf6a722 Mon Sep 17 00:00:00 2001 From: Guido Bernal Date: Wed, 27 Nov 2024 14:51:38 -0300 Subject: [PATCH 2/4] chore: add changelog description --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64b869c9..ea21fd2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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.61.1] - 2024-10-29 ### Fixed From ace060b596b90ebc2fc4d9a2f0ccece501202bfc Mon Sep 17 00:00:00 2001 From: Guido Bernal Date: Fri, 6 Dec 2024 11:06:41 -0300 Subject: [PATCH 3/4] fix: remove unnecessary validations --- node/resolvers/Queries/Organizations.ts | 103 ++++++------------------ 1 file changed, 25 insertions(+), 78 deletions(-) diff --git a/node/resolvers/Queries/Organizations.ts b/node/resolvers/Queries/Organizations.ts index a018a908..1117d3a3 100644 --- a/node/resolvers/Queries/Organizations.ts +++ b/node/resolvers/Queries/Organizations.ts @@ -288,89 +288,36 @@ const Organizations = { pageSize: number }, { - clients: { storefrontPermissions, session }, - vtex: { logger, sessionToken, adminUserAuthToken }, + clients: { storefrontPermissions }, + vtex: { logger }, }: any ) => { - const organizationFilters: string[] = [] - let fromSession = false - const { - data: { checkUserPermission }, - }: any = await storefrontPermissions - .checkUserPermission('vtex.b2b-organizations@1.x') - .catch((error: any) => { - logger.error({ - error, - message: 'checkUserPermission-error', - }) - - return { - data: { - checkUserPermission: null, - }, - } - }) - - if ( - (!adminUserAuthToken && - !checkUserPermission?.permissions.includes('add-sales-users-all')) || - !email?.length - ) { - const sessionData = await session - .getSession(sessionToken as string, ['*']) - .then((currentSession: any) => { - return currentSession.sessionData - }) - .catch((error: any) => { - logger.warn({ - error, - message: 'getOrganizationsPaginatedByEmail-session-error', - }) - - return null - }) - - if (checkUserPermission?.permissions.includes('add-users-organization')) { - const orgId = - sessionData?.namespaces?.['storefront-permissions']?.organization - ?.value - - if (!orgId) { - throw new Error('No permission for getting the organizations') - } - - organizationFilters.push(orgId) - } - - if (!email?.length) { - email = sessionData?.namespaces?.profile?.email?.value - fromSession = true - } - } - - const response = - await storefrontPermissions.getOrganizationsPaginatedByEmail( - email, - page, - pageSize - ) - - const organizations = - response?.data?.getOrganizationsPaginatedByEmail?.data?.filter( - ({ orgId }: { orgId: string }) => { - return ( - fromSession || - (organizationFilters.length > 0 - ? organizationFilters.find((id: string) => orgId === id) - : true) - ) - } - ) - 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: response.data?.getOrganizationsPaginatedByEmail?.pagination, + pagination, } } catch (error) { logger.error({ From 21fcd51299dd0de904b9c64dd692d389378d1b6c Mon Sep 17 00:00:00 2001 From: Guido Bernal Date: Wed, 15 Jan 2025 15:40:56 -0300 Subject: [PATCH 4/4] fix: remove unnecessary code --- node/resolvers/Queries/Organizations.ts | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/node/resolvers/Queries/Organizations.ts b/node/resolvers/Queries/Organizations.ts index 1117d3a3..7112a987 100644 --- a/node/resolvers/Queries/Organizations.ts +++ b/node/resolvers/Queries/Organizations.ts @@ -293,32 +293,14 @@ const Organizations = { }: any ) => { try { - const organizationFilters: string[] = [] - const fromSession = false - - const response = + const { data: { getOrganizationsPaginatedByEmail } } = 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, - } + return getOrganizationsPaginatedByEmail; } catch (error) { logger.error({ error,