Skip to content

Commit

Permalink
fix: use listUsersPaginated instead of deprecated listUsers #50
Browse files Browse the repository at this point in the history
  • Loading branch information
enzomerca authored Aug 20, 2024
2 parents 5cebcb0 + c005049 commit 58cc092
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 31 deletions.
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](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Use listUsersPaginated internally instead of deprecated listUsers

## [2.5.3] - 2024-06-10

### Fixed
Expand Down
6 changes: 4 additions & 2 deletions node/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import StorefrontPermissions from './storefrontPermissions'
import VtexId from './vtexId'

export const getTokenToHeader = (ctx: IOContext) => {
const adminToken = ctx.adminUserAuthToken ?? ctx.authToken
const userToken = ctx.storeUserAuthToken
// provide authToken (app token) as an admin token as this is a call
// between b2b suite apps and no further token validation is needed
const adminToken = ctx.authToken
const userToken = ctx.storeUserAuthToken ?? null
const { sessionToken, account } = ctx

let allCookies = `VtexIdclientAutCookie=${adminToken}`
Expand Down
20 changes: 7 additions & 13 deletions node/clients/storefrontPermissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,11 @@ export const QUERIES = {
slug
}
}`,
listUsers: `query users($organizationId: ID, $costCenterId: ID, $roleId: ID) {
listUsers(organizationId: $organizationId, costCenterId: $costCenterId, roleId: $roleId) {
id
roleId
userId
clId
orgId
costId
name
email
canImpersonate
getOrgSalesAdminEmail: `query users($organizationId: ID, $roleId: ID) {
listUsersPaginated(organizationId: $organizationId, roleId: $roleId) {
data {
email
}
}
}`,
}
Expand All @@ -77,7 +71,7 @@ export default class StorefrontPermissions extends AppGraphQLClient {
})
}

public listUsers = async ({
public getOrgSalesAdminEmail = async ({
roleId,
organizationId,
}: {
Expand All @@ -86,7 +80,7 @@ export default class StorefrontPermissions extends AppGraphQLClient {
}): Promise<any> => {

Check warning on line 80 in node/clients/storefrontPermissions.ts

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
return this.query({
extensions: this.getPersistedQuery(),
query: QUERIES.listUsers,
query: QUERIES.getOrgSalesAdminEmail,
variables: {
roleId,
...(organizationId && { organizationId }),
Expand Down
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"ramda": "^0.25.0",
"atob": "^2.1.2",
"axios": "0.27.2",
"@vtex/api": "6.46.0"
"@vtex/api": "6.47.0"
},
"devDependencies": {
"@types/atob": "^2.1.2",
Expand Down
23 changes: 13 additions & 10 deletions node/utils/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ interface QuoteUpdate {
note: string
}

const getUsers = async (
// As this is currently used only to get the sales-admin users to
// send an email notification when a quote is created, we only get
// the first page of users (25) and return them.
const getOrgSalesAdminEmail = async (
storefrontPermissions: StorefrontPermissions,
roleSlug: string,
organizationId?: string
) => {
const {
data: { listRoles },
}: any = await storefrontPermissions.listRoles()

const role = listRoles.find((r: any) => r.slug === roleSlug)
const role = listRoles.find((r: any) => r.slug === 'sales-admin')

if (!role) {
return []
}

const {
data: { listUsers },
}: any = await storefrontPermissions.listUsers({
data: { getOrgSalesAdminEmailResult },
}: any = await storefrontPermissions.getOrgSalesAdminEmail({
roleId: role.id,
...(organizationId && { organizationId }),
})

return listUsers
// we only return the first page of sales-admin users (25)
return getOrgSalesAdminEmailResult.data
}

const getOrgAndCostCenterNames = async (
Expand Down Expand Up @@ -159,13 +162,13 @@ const message = (ctx: Context | EventBroadcastContext) => {
let users = []

try {
users = (await getUsers(storefrontPermissions, 'sales-admin')).map(
(user: any) => user.email
)
users = (
await getOrgSalesAdminEmail(storefrontPermissions, organization)
).map((user: any) => user.email)
} catch (error) {
logger.error({
error,
message: 'quoteCreatedMessage-getUsersError',
message: 'quoteCreatedMessage-getOrgSalesAdminEmailError',
})
}

Expand Down
10 changes: 5 additions & 5 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@
"@types/mime" "^1"
"@types/node" "*"

"@vtex/api@6.46.0":
version "6.46.0"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.46.0.tgz#208d14b96cbc8fd5eb6bd18fbd0c8424886e6154"
integrity sha512-XAvJlD1FG1GynhPXiMcayunahFCL2r3ilO5MHAWKxYvB/ljyxi4+U+rVpweeaQGpxHfhKHdfPe7qNEEh2oa2lw==
"@vtex/api@6.47.0":
version "6.47.0"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.47.0.tgz#6910455d593d8bb76f1f4f2b7660023853fda35e"
integrity sha512-t9gt7Q89EMbSj3rLhho+49Fv+/lQgiy8EPVRgtmmXFp1J4v8hIAZF7GPjCPie111KVs4eG0gfZFpmhA5dafKNA==
dependencies:
"@types/koa" "^2.11.0"
"@types/koa-compose" "^3.2.3"
Expand Down Expand Up @@ -1522,7 +1522,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@vtex/node-stats-lite#dist:
"stats-lite@github:vtex/node-stats-lite#dist":
version "2.2.0"
resolved "https://codeload.github.com/vtex/node-stats-lite/tar.gz/1b0d39cc41ef7aaecfd541191f877887a2044797"
dependencies:
Expand Down

0 comments on commit 58cc092

Please sign in to comment.