diff --git a/.gitignore b/.gitignore index 3c3629e..eb79dd5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.idea diff --git a/CHANGELOG.md b/CHANGELOG.md index 577ddbd..77fb2d8 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 + +- Added a graphql query to get all users by using the scroll MD function + ## [1.19.0] - 2022-05-09 ### Added diff --git a/graphql/schema.graphql b/graphql/schema.graphql index fe33846..2967baf 100644 --- a/graphql/schema.graphql +++ b/graphql/schema.graphql @@ -23,6 +23,8 @@ type Query { # User getUser(id: ID!): User @cacheControl(scope: PRIVATE) getUserByEmail(email: String!): User @cacheControl(scope: PRIVATE) + listAllUsers: [User] @cacheControl(scope: PRIVATE, maxAge: SHORT) + listUsers(organizationId: ID, costCenterId: ID, roleId: ID): [User] @cacheControl(scope: PRIVATE, maxAge: SHORT) @deprecated( diff --git a/node/package.json b/node/package.json index 5c9bcbe..b28f7bc 100644 --- a/node/package.json +++ b/node/package.json @@ -2,7 +2,7 @@ "name": "vtex.checkout-ui-custom", "version": "1.19.0", "dependencies": { - "@vtex/api": "6.45.11-beta", + "@vtex/api": "6.45.12", "co-body": "^6.0.0", "jsonwebtoken": "^8.5.0", "qs": "^6.9.4", @@ -22,7 +22,7 @@ "@types/jsonwebtoken": "^8.5.0", "@types/node": "^12.0.0", "@types/ramda": "types/npm-ramda#dist", - "@vtex/api": "6.45.11-beta", + "@vtex/api": "6.45.12", "@vtex/prettier-config": "^0.3.1", "tslint": "^5.12.0", "tslint-config-prettier": "^1.18.0", diff --git a/node/resolvers/Queries/Users.ts b/node/resolvers/Queries/Users.ts index 77acd6b..d02128f 100644 --- a/node/resolvers/Queries/Users.ts +++ b/node/resolvers/Queries/Users.ts @@ -7,6 +7,16 @@ import { getAppSettings } from './Settings' const config: any = currentSchema('b2b_users') +const SCROLL_AWAIT_TIME = 100 +const SLEEP_ADD_PERCENTAGE = 0.1 +const SCROLL_SIZE = 1000 + +const sleep = (ms: number) => { + const time = ms + SLEEP_ADD_PERCENTAGE * ms + + return new Promise((resolve) => setTimeout(resolve, time)) +} + export const getUserById = async (_: any, params: any, ctx: Context) => { const { clients: { masterdata }, @@ -160,6 +170,65 @@ export const getUserByEmail = async (_: any, params: any, ctx: Context) => { } } +export const listAllUsers = async (_: any, __: any, ctx: Context) => { + const { + clients: { masterdata }, + } = ctx + + try { + let token: string | undefined + let hasMore = true + const users = [] as any[] + + const scrollMasterData = async () => { + await sleep(SCROLL_AWAIT_TIME) + const { + mdToken, + data, + }: { + mdToken: string + data: any + } = await masterdata.scrollDocuments({ + dataEntity: config.name, + fields: [ + 'id', + 'roleId', + 'userId', + 'clId', + 'orgId', + 'costId', + 'name', + 'email', + 'canImpersonate', + ], + mdToken: token, + schema: config.version, + size: SCROLL_SIZE, + }) + + if (!data.length && token) { + hasMore = false + } + + if (!token && mdToken) { + token = mdToken + } + + users.push(...data) + + if (hasMore) { + await scrollMasterData() + } + } + + await scrollMasterData() + + return users + } catch (e) { + return { status: 'error', message: e } + } +} + export const listUsers = async ( _: any, { diff --git a/node/resolvers/index.ts b/node/resolvers/index.ts index 57ca439..aaed30a 100644 --- a/node/resolvers/index.ts +++ b/node/resolvers/index.ts @@ -18,6 +18,7 @@ import { checkUserPermission, getUser, getUserByEmail, + listAllUsers, listUsers, listUsersPaginated, } from './Queries/Users' @@ -44,6 +45,7 @@ export const resolvers = { getUser, getUserByEmail, hasUsers, + listAllUsers, listFeatures, listRoles, listUsers, diff --git a/node/yarn.lock b/node/yarn.lock index 48fdd2c..24a2bc9 100644 --- a/node/yarn.lock +++ b/node/yarn.lock @@ -190,10 +190,10 @@ "@types/mime" "^1" "@types/node" "*" -"@vtex/api@6.45.11-beta": - version "6.45.11-beta" - resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.11-beta.tgz#edc51a2fdd3903f33caf7d63f198005f0d5a0c98" - integrity sha512-BFHMWU4SEFKXpbW3TWj0CI944p9ysmoCtB8ewpQlyRpUZFhPx2On/qFgBrD+Gqwt1yvysuKIAOfzfpjqKy88Gg== +"@vtex/api@6.45.12": + version "6.45.12" + resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.12.tgz#b13c04398b12f576263ea823369f09c970d57479" + integrity sha512-SVLKo+Q/TxQy+1UKzH8GswTI3F2OCRCLfgaNQOrVAVdbM6Ci4wzTeX8j/S4Q1aEEnqBFlH/wVpHf8I6NBa+g9A== dependencies: "@types/koa" "^2.11.0" "@types/koa-compose" "^3.2.3" @@ -214,7 +214,7 @@ graphql "^14.5.8" graphql-tools "^4.0.6" graphql-upload "^8.1.0" - jaeger-client "^3.19.0" + jaeger-client "^3.18.0" js-base64 "^2.5.1" koa "^2.11.0" koa-compose "^4.1.0" @@ -224,7 +224,7 @@ mime-types "^2.1.12" opentracing "^0.14.4" p-limit "^2.2.0" - prom-client "^14.0.1" + prom-client "^12.0.0" qs "^6.5.1" querystring "^0.2.0" ramda "^0.26.0" @@ -971,7 +971,7 @@ iterall@^1.1.3, iterall@^1.2.2: resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea" integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg== -jaeger-client@^3.19.0: +jaeger-client@^3.18.0: version "3.19.0" resolved "https://registry.yarnpkg.com/jaeger-client/-/jaeger-client-3.19.0.tgz#9b5bd818ebd24e818616ee0f5cffe1722a53ae6e" integrity sha512-M0c7cKHmdyEUtjemnJyx/y9uX16XHocL46yQvyqDlPdvAcwPDbHrIbKjQdBqtiE4apQ/9dmr+ZLJYYPGnurgpw== @@ -1356,10 +1356,10 @@ process@^0.10.0: resolved "https://registry.yarnpkg.com/process/-/process-0.10.1.tgz#842457cc51cfed72dc775afeeafb8c6034372725" integrity sha1-hCRXzFHP7XLcd1r+6vuMYDQ3JyU= -prom-client@^14.0.1: - version "14.0.1" - resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-14.0.1.tgz#bdd9583e02ec95429677c0e013712d42ef1f86a8" - integrity sha512-HxTArb6fkOntQHoRGvv4qd/BkorjliiuO2uSWC2KC17MUTKYttWdDoXX/vxOhQdkoECEM9BBH0pj2l8G8kev6w== +prom-client@^12.0.0: + version "12.0.0" + resolved "https://registry.yarnpkg.com/prom-client/-/prom-client-12.0.0.tgz#9689379b19bd3f6ab88a9866124db9da3d76c6ed" + integrity sha512-JbzzHnw0VDwCvoqf8y1WDtq4wSBAbthMB1pcVI/0lzdqHGJI3KBJDXle70XK+c7Iv93Gihqo0a5LlOn+g8+DrQ== dependencies: tdigest "^0.1.1"