From 6949a76bfe941d5755401b1427c89a40ba26aab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Magalh=C3=A3es?= Date: Thu, 20 Apr 2023 15:54:03 -0300 Subject: [PATCH] fix: impersonation user many to many fixed --- CHANGELOG.md | 5 +++ node/resolvers/Mutations/Users.ts | 5 ++- node/resolvers/Routes/index.ts | 53 +++++++++++++++++++------------ 3 files changed, 41 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0e3ed2..c40c769 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/node/resolvers/Mutations/Users.ts b/node/resolvers/Mutations/Users.ts index a39bfc8..f179a6b 100644 --- a/node/resolvers/Mutations/Users.ts +++ b/node/resolvers/Mutations/Users.ts @@ -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 diff --git a/node/resolvers/Routes/index.ts b/node/resolvers/Routes/index.ts index c8dab36..f4b1df7 100644 --- a/node/resolvers/Routes/index.ts +++ b/node/resolvers/Routes/index.ts @@ -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', @@ -85,8 +85,8 @@ export const Routes = { const { clients: { graphqlServer, + masterdata, checkout, - profileSystem, salesChannel: salesChannelClient, }, req, @@ -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 @@ -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