From 95fa7718420732dc41c58ca626c7522af37374fb Mon Sep 17 00:00:00 2001 From: Victor Hugo Araujo Almeida Date: Thu, 29 Aug 2024 09:19:18 -0300 Subject: [PATCH 1/4] Add query getOrganizationsPaginated --- graphql/schema.graphql | 13 ++++ node/resolvers/Queries/Users.ts | 101 ++++++++++++++++++++++++++++++++ node/resolvers/index.ts | 2 + node/yarn.lock | 2 +- 4 files changed, 117 insertions(+), 1 deletion(-) diff --git a/graphql/schema.graphql b/graphql/schema.graphql index 1ca7d69..5129aa2 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -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 { @@ -215,6 +223,11 @@ type Organization { roleId: ID } +type OrganizationPagination { + data: [Organization] + pagination: Pagination +} + type Profile { id: ID name: String diff --git a/node/resolvers/Queries/Users.ts b/node/resolvers/Queries/Users.ts index b0385a9..38afcb2 100644 --- a/node/resolvers/Queries/Users.ts +++ b/node/resolvers/Queries/Users.ts @@ -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: page, + pageSize: 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) + } +} + export const getActiveUserByEmail = async ( _: any, params: any, @@ -815,6 +872,50 @@ export const getOrganizationsByEmail = async ( } } +export const getOrganizationsByEmailPaginated = async ( + _: 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 }, + 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, diff --git a/node/resolvers/index.ts b/node/resolvers/index.ts index 383002c..2e7e2d3 100644 --- a/node/resolvers/index.ts +++ b/node/resolvers/index.ts @@ -26,6 +26,7 @@ import { getAllUsersByEmail, getB2BUserById, getOrganizationsByEmail, + getOrganizationsByEmailPaginated, getUser, getUserByEmail, listAllUsers, @@ -59,6 +60,7 @@ export const resolvers = { getB2BUser: getB2BUserById, getFeaturesByModule, getOrganizationsByEmail, + getOrganizationsByEmailPaginated, getRole, getSessionWatcher, getUser, diff --git a/node/yarn.lock b/node/yarn.lock index 1a57dcf..cf3246e 100644 --- a/node/yarn.lock +++ b/node/yarn.lock @@ -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: From 1fa2273f793778a42cc16235950d8c3bad4d8129 Mon Sep 17 00:00:00 2001 From: Victor Hugo Araujo Almeida Date: Thu, 29 Aug 2024 09:23:07 -0300 Subject: [PATCH 2/4] Changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d8f3c1..8830201 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From d341af3dc2e3d58486a9bb0ebaf4385c1dffc40e Mon Sep 17 00:00:00 2001 From: Victor Hugo Araujo Almeida Date: Fri, 30 Aug 2024 11:26:12 -0300 Subject: [PATCH 3/4] lint --- node/resolvers/Queries/Users.ts | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/node/resolvers/Queries/Users.ts b/node/resolvers/Queries/Users.ts index 38afcb2..218a8d6 100644 --- a/node/resolvers/Queries/Users.ts +++ b/node/resolvers/Queries/Users.ts @@ -166,8 +166,8 @@ export const getUsersByEmailPaginated = async ({ 'active', ], pagination: { - page: page, - pageSize: pageSize, + page, + pageSize, }, schema: config.version, where: `email = "${email}"`, @@ -893,13 +893,7 @@ export const getOrganizationsByEmailPaginated = async ( try { const data = await masterdata.searchDocumentsWithPaginationInfo({ dataEntity: config.name, - fields: [ - 'clId', - 'costId', - 'id', - 'orgId', - 'roleId', - ], + fields: ['clId', 'costId', 'id', 'orgId', 'roleId'], pagination: { page, pageSize }, schema: config.version, where: `email = "${email}"`, From 0ac77f9286909417ac42b6f858cd6b9d5a660d59 Mon Sep 17 00:00:00 2001 From: Victor Hugo Araujo Almeida Date: Sun, 29 Sep 2024 16:20:28 -0400 Subject: [PATCH 4/4] Remove unused method --- node/resolvers/Queries/Users.ts | 57 --------------------------------- 1 file changed, 57 deletions(-) diff --git a/node/resolvers/Queries/Users.ts b/node/resolvers/Queries/Users.ts index 218a8d6..fbd6111 100644 --- a/node/resolvers/Queries/Users.ts +++ b/node/resolvers/Queries/Users.ts @@ -135,63 +135,6 @@ 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) - } -} - export const getActiveUserByEmail = async ( _: any, params: any,