Skip to content

Commit

Permalink
Merge pull request #104 from vtex-apps/fix/impersonation-user
Browse files Browse the repository at this point in the history
fix: impersonation user many to many fixed
  • Loading branch information
arturmagalhaesjr authored Apr 20, 2023
2 parents bea97b2 + 6949a76 commit f9b2b91
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Fixed

- Fix on impersonation user


## [1.32.0] - 2023-04-19

### Added
Expand Down
5 changes: 4 additions & 1 deletion node/resolvers/Mutations/Users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ const addUserToMasterdata = async ({
return DocumentId
}

const getUser = async ({ masterdata, params: { email, id, userId } }: any) => {
export const getUser = async ({
masterdata,
params: { email, id, userId },
}: any) => {
const where = id || userId ? `id=${id || userId}` : `email=${email}`

return masterdata
Expand Down
53 changes: 32 additions & 21 deletions node/resolvers/Routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getRole } from '../Queries/Roles'
import { getAppSettings, getSessionWatcher } from '../Queries/Settings'
import { getActiveUserByEmail, getUserByEmail } from '../Queries/Users'
import { generateClUser, QUERIES } from './utils'
import { setActiveUserByOrganization } from '../Mutations/Users'
import { setActiveUserByOrganization, getUser } from '../Mutations/Users'

export const Routes = {
PROFILE_DOCUMENT_TYPE: 'cpf',
Expand Down Expand Up @@ -85,8 +85,8 @@ export const Routes = {
const {
clients: {
graphqlServer,
masterdata,
checkout,
profileSystem,
salesChannel: salesChannelClient,
},
req,
Expand Down Expand Up @@ -152,19 +152,28 @@ export const Routes = {
let phoneNumber = null
let tradeName = null
let stateRegistration = null
let user = null

if (b2bImpersonate) {
await profileSystem
.getProfileInfo(b2bImpersonate)
.then((profile: any) => {
response['storefront-permissions'].storeUserId.value = profile.userId
response['storefront-permissions'].storeUserEmail.value =
profile.email
email = profile.email
})
.catch((error) => {
logger.error({ message: 'setProfile.getProfileInfoError', error })
})
try {
user = (await getUser({
masterdata,
params: { userId: b2bImpersonate },
})) as {
orgId: string
costId: string
clId: string
id: string
email: string
userId: string
}

email = user.email
response['storefront-permissions'].storeUserId.value = user.userId
response['storefront-permissions'].storeUserEmail.value = user.email
} catch (error) {
logger.error({ message: 'setProfile.getUserError', error })
}
} else if (telemarketingImpersonate) {
const telemarketingEmail = body?.impersonate?.storeUserEmail?.value

Expand All @@ -182,15 +191,17 @@ export const Routes = {
return
}

const user = (await getActiveUserByEmail(null, { email }, ctx).catch(
(error) => {
logger.warn({ message: 'setProfile.getUserByEmailError', error })
if (user === null) {
user = (await getActiveUserByEmail(null, { email }, ctx).catch(
(error) => {
logger.warn({ message: 'setProfile.getUserByEmailError', error })
}
)) as {
orgId: string
costId: string
clId: string
id: string
}
)) as {
orgId: string
costId: string
clId: string
id: string
}

response['storefront-permissions'].userId.value = user?.id
Expand Down

0 comments on commit f9b2b91

Please sign in to comment.