Skip to content

Commit

Permalink
Merge branch 'master' into fix/login-error
Browse files Browse the repository at this point in the history
  • Loading branch information
enzomerca authored Oct 11, 2024
2 parents f6872f7 + 1e2a59d commit a2fbbae
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Force setProfile to use a valid cost center
- Increase timeout to 45 seconds

## [1.44.11] - 2024-10-10

### Fixed

- Error changing Cost Center after placing order

## [1.44.10] - 2024-10-07

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "storefront-permissions",
"vendor": "vtex",
"version": "1.44.10",
"version": "1.44.11",
"title": "Storefront Permissions",
"description": "Manage User's permissions on apps that relates to this app",
"mustUpdateAt": "2022-08-28",
Expand Down
2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vtex.checkout-ui-custom",
"version": "1.44.10",
"version": "1.44.11",
"dependencies": {
"@vtex/api": "6.47.0",
"atob": "^2.1.2",
Expand Down
75 changes: 65 additions & 10 deletions node/resolvers/Mutations/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,48 @@ import {

const config: any = currentSchema('b2b_users')

const MAX_RETRY = 5
const RETRY_BACKOFF_FACTOR_MS = 100
const MAX_BACKOFF_MS = 1000

const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
const setChangeSession = async (
sessionParameters: any,
countRetry = 0
): Promise<void> => {
const {
context: {
clients: { session },
vtex: { logger },
},
publicKey,
value,
sessionCookie,
} = sessionParameters

try {
await session.updateSession(publicKey, value, [], sessionCookie)
} catch (error) {
logger.error({
error,
message: 'setChangeSession.error',
attempt: countRetry,
})

if (countRetry < MAX_RETRY) {
countRetry++
const backoff = Math.min(
2 ** (countRetry - 1) * RETRY_BACKOFF_FACTOR_MS,
MAX_BACKOFF_MS
)

await delay(backoff)

return setChangeSession(sessionParameters, countRetry)
}
}
}

const addUserToMasterdata = async ({
masterdata,
params: { name, email },
Expand Down Expand Up @@ -333,14 +375,18 @@ export const deleteUser = async (_: any, params: any, ctx: Context) => {

export const impersonateUser = async (_: any, params: any, ctx: Context) => {
const {
clients: { session },
vtex: { logger, sessionToken },
} = ctx

const { userId } = params

try {
await session.updateSession('impersonate', userId, [], sessionToken)
await setChangeSession({
context: ctx,
publicKey: 'impersonate',
value: userId,
sessionCookie: sessionToken,
})

return { status: 'success', message: '' }
} catch (error) {
Expand Down Expand Up @@ -564,12 +610,14 @@ export const setCurrentOrganization = async (
ctx: Context
) => {
const {
vtex: { logger },
cookies,
request,
vtex: { logger },
clients: { session },
} = ctx

const sessionCookie =
cookies.get('vtex_session') ?? request.header?.sessiontoken

const { sessionData } = ctx.vtex as any

const { orgId, costId } = params
Expand Down Expand Up @@ -600,11 +648,7 @@ export const setCurrentOrganization = async (
return { status: 'error', message: error }
}

const sessionCookie =
cookies.get('vtex_session') ?? request.header?.sessiontoken

try {
await session.updateSession('', null, [], sessionCookie)
await setActiveUserByOrganization(
_,
{
Expand All @@ -626,6 +670,13 @@ export const setCurrentOrganization = async (

sendChangeTeamMetric(metricParams)

await setChangeSession({
context: ctx,
publicKey: 'b2bCurrentCostCenter',
value: costId,
sessionCookie,
})

return { status: 'success', message: '' }
} catch (error) {
logger.error({
Expand All @@ -646,14 +697,18 @@ export const ignoreB2BSessionData = async (
cookies,
request,
vtex: { logger },
clients: { session },
} = ctx

const sessionCookie =
cookies.get('vtex_session') ?? request.header?.sessiontoken

try {
await session.updateSession('removeB2B', enabled, [], sessionCookie)
await setChangeSession({
context: ctx,
publicKey: 'removeB2B',
value: enabled,
sessionCookie,
})

return { status: 'success', message: '' }
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion vtex.session/configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"authentication": ["storeUserEmail"],
"checkout": ["orderFormId"],
"impersonate": ["storeUserEmail", "storeUserId"],
"public": ["impersonate", "removeB2B"]
"public": ["impersonate", "removeB2B", "b2bCurrentCostCenter"]
},
"output": {
"public": ["facets", "sc", "regionId"],
Expand Down

0 comments on commit a2fbbae

Please sign in to comment.