Skip to content

Commit

Permalink
[FSEUS-521] Error changing Cost Center after placing order (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
wender authored Oct 10, 2024
1 parent abdf960 commit 124d26b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 11 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

- Error changing Cost Center after placing order

## [1.44.10] - 2024-10-07

### Fixed
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 124d26b

Please sign in to comment.