Skip to content

Commit

Permalink
Merge pull request #94 from vtex-apps/fix/scroll-search
Browse files Browse the repository at this point in the history
fix: moved scroll to search strategy
  • Loading branch information
arturmagalhaesjr authored Mar 1, 2023
2 parents b3e0f3e + d34c2d5 commit 588eafc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 89 deletions.
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]

### Fixed

- Changed the scroll to search with pagination

## [1.31.2] - 2023-02-27

### Fixed
Expand Down
130 changes: 41 additions & 89 deletions node/resolvers/Queries/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,27 @@ 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))
const PAGINATION = {
page: 1,
pageSize: 50,
}

export const getAllUsersByEmail = async (_: any, params: any, ctx: Context) => {
const {
clients: { masterdata },
vtex: { logger },
} = ctx

const { email } = params

export const getAllUsers = async ({
masterdata,
logger,
where,
}: {
masterdata: any
logger: any
where?: string
}) => {
try {
let token: string | undefined
let currentPage = PAGINATION.page
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({
const resp = await masterdata.searchDocumentsWithPaginationInfo({
dataEntity: config.name,
fields: [
'id',
Expand All @@ -56,23 +45,31 @@ export const getAllUsersByEmail = async (_: any, params: any, ctx: Context) => {
'canImpersonate',
'active',
],
mdToken: token,
pagination: {
page: currentPage,
pageSize: PAGINATION.pageSize,
},
schema: config.version,
size: SCROLL_SIZE,
where: `email = "${email}"`,
...(where ? { where } : {}),
})

if (!data.length && token) {
hasMore = false
const { data, pagination } = resp as unknown as {
pagination: {
total: number
}
data: any
}

if (!token && mdToken) {
token = mdToken
const totalPages = Math.ceil(pagination.total / PAGINATION.pageSize)

if (currentPage >= totalPages) {
hasMore = false
}

users.push(...data)

if (hasMore) {
currentPage += 1
await scrollMasterData()
}
}
Expand All @@ -89,6 +86,17 @@ export const getAllUsersByEmail = async (_: any, params: any, ctx: Context) => {
}
}

export const getAllUsersByEmail = async (_: any, params: any, ctx: Context) => {
const {
clients: { masterdata },
vtex: { logger },
} = ctx

const { email } = params

return getAllUsers({ masterdata, logger, where: `email=${email}` })
}

export const getActiveUserByEmail = async (
_: any,
params: any,
Expand Down Expand Up @@ -299,63 +307,7 @@ export const listAllUsers = async (_: any, __: any, ctx: Context) => {
vtex: { logger },
} = 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) {
logger.error({
error: e,
message: 'Profiles.listAllUsers-error',
})

return { status: 'error', message: e }
}
return getAllUsers({ masterdata, logger })
}

export const listUsers = async (
Expand Down

0 comments on commit 588eafc

Please sign in to comment.