Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use listUsersPaginated instead of deprecated listUsers #50

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@ -61,7 +55,7 @@
super('[email protected]', ctx, options)
}

public checkUserPermission = async (): Promise<any> => {

Check warning on line 58 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.getPermission,
Expand All @@ -69,7 +63,7 @@
})
}

public listRoles = async (): Promise<any> => {

Check warning on line 66 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.listRoles,
Expand All @@ -77,16 +71,16 @@
})
}

public listUsers = async ({
public getOrgSalesAdminEmail = async ({
roleId,
organizationId,
}: {
roleId: string
organizationId?: string
}): 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 All @@ -105,7 +99,7 @@

private query = async (param: {
query: string
variables: any

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

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
extensions: any
}): Promise<any> => {
const { query, variables, extensions } = param
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
Loading