-
Notifications
You must be signed in to change notification settings - Fork 15
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
Add new query getOrganizationsByEmailPaginated #172
base: master
Are you sure you want to change the base?
Changes from all commits
facb552
bac2cf1
c90de63
f0db88c
4818d99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import updateUser from '../mutations/updateUser' | ||
import getB2BUser from '../queries/getB2BUser' | ||
import getOrganizationsByEmail from '../queries/getOrganizationsByEmail' | ||
import getOrganizationsByEmailPaginated from '../queries/getOrganizationsByEmailPaginated' | ||
import getPermission from '../queries/getPermission' | ||
import getRole from '../queries/getRole' | ||
import getUser from '../queries/getUser' | ||
|
@@ -22,7 +23,7 @@ | |
super('[email protected]', ctx, options) | ||
} | ||
|
||
public checkUserPermission = async (app?: string): Promise<any> => { | ||
return this.query({ | ||
extensions: this.getPersistedQuery(app), | ||
query: getPermission, | ||
|
@@ -30,7 +31,7 @@ | |
}) | ||
} | ||
|
||
public getOrganizationsByEmail = async (email: string): Promise<any> => { | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: getOrganizationsByEmail, | ||
|
@@ -40,7 +41,23 @@ | |
}) | ||
} | ||
|
||
public getOrganizationsByEmailPaginated = async ( | ||
email: string, | ||
page: number, | ||
pageSize: number | ||
): Promise<any> => { | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: getOrganizationsByEmailPaginated, | ||
variables: { | ||
email, | ||
page, | ||
pageSize, | ||
}, | ||
}) | ||
} | ||
|
||
public listRoles = async (): Promise<any> => { | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: listRoles, | ||
|
@@ -48,7 +65,7 @@ | |
}) | ||
} | ||
|
||
public getRole = async (roleId: string): Promise<any> => { | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: getRole, | ||
|
@@ -56,7 +73,7 @@ | |
}) | ||
} | ||
|
||
public listAllUsers = async (): Promise<any> => { | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: listAllUsers, | ||
|
@@ -72,7 +89,7 @@ | |
roleId?: string | ||
organizationId?: string | ||
costCenterId?: string | ||
}): Promise<any> => { | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: listUsers, | ||
|
@@ -102,7 +119,7 @@ | |
pageSize?: number | ||
sortOrder?: string | ||
sortedBy?: string | ||
}): Promise<any> => { | ||
return this.query({ | ||
extensions: this.getPersistedQuery(), | ||
query: listUsersPaginated, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { print } from 'graphql' | ||
import gql from 'graphql-tag' | ||
|
||
export default print(gql` | ||
query organizationsPaginated($email: String!, $page: Int, $pageSize: Int) { | ||
getOrganizationsByEmailPaginated( | ||
email: $email | ||
page: $page | ||
pageSize: $pageSize | ||
) { | ||
data { | ||
costId | ||
orgId | ||
roleId | ||
id | ||
clId | ||
} | ||
pagination { | ||
page | ||
pageSize | ||
total | ||
} | ||
} | ||
} | ||
`) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,6 +241,111 @@ const Organizations = { | |
} | ||
}, | ||
|
||
getOrganizationsByEmailPaginated: async ( | ||
_: void, | ||
{ | ||
email, | ||
page = 1, | ||
pageSize = 25, | ||
}: { | ||
email: string | ||
page: number | ||
pageSize: number | ||
}, | ||
{ | ||
clients: { storefrontPermissions, session }, | ||
vtex: { logger, sessionToken, adminUserAuthToken }, | ||
}: any | ||
) => { | ||
const organizationFilters: string[] = [] | ||
let fromSession = false | ||
const { | ||
data: { checkUserPermission }, | ||
}: any = await storefrontPermissions | ||
.checkUserPermission('[email protected]') | ||
.catch((error: any) => { | ||
logger.error({ | ||
error, | ||
message: 'checkUserPermission-error', | ||
}) | ||
|
||
return { | ||
data: { | ||
checkUserPermission: null, | ||
}, | ||
} | ||
}) | ||
|
||
if ( | ||
(!adminUserAuthToken && | ||
!checkUserPermission?.permissions.includes('add-sales-users-all')) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we checking if the user has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @enzomerca |
||
!(email?.length > 0) | ||
) { | ||
const sessionData = await session | ||
.getSession(sessionToken as string, ['*']) | ||
.then((currentSession: any) => { | ||
return currentSession.sessionData | ||
}) | ||
.catch((error: any) => { | ||
logger.warn({ | ||
error, | ||
message: 'getOrganizationsByEmail-session-error', | ||
}) | ||
|
||
return null | ||
}) | ||
|
||
if (checkUserPermission?.permissions.includes('add-users-organization')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we checking if the user has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @enzomerca There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you remove it to test? It appears to be a bug or old mistake. |
||
const orgId = | ||
sessionData?.namespaces?.['storefront-permissions']?.organization | ||
?.value | ||
|
||
if (!orgId) { | ||
throw new Error('No permission for getting the organizations') | ||
} | ||
|
||
organizationFilters.push(orgId) | ||
Comment on lines
+299
to
+307
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. session will have just one org id, the current logged org, right? and below we are filtering the result to return only the orgs added to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @enzomerca There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vhaalmeida We would like to avoid old mistakes, and we have a lot of tasks to improve the source code and the B2B suite app implementations. So our advice is to adjust the rules to test. |
||
} | ||
|
||
if (!(email?.length > 0)) { | ||
email = sessionData?.namespaces?.profile?.email?.value | ||
fromSession = true | ||
} | ||
} | ||
Comment on lines
+310
to
+314
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the function is named I prefer to remove this, make the email a required field and simplify the code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @enzomerca |
||
|
||
const response = | ||
await storefrontPermissions.getOrganizationsByEmailPaginated( | ||
email, | ||
page, | ||
pageSize | ||
) | ||
|
||
const organizations = | ||
response?.data?.getOrganizationsByEmailPaginated?.data?.filter( | ||
({ orgId }: { orgId: string }) => { | ||
return ( | ||
fromSession || | ||
(organizationFilters.length > 0 | ||
? organizationFilters.find((id: string) => orgId === id) | ||
: true) | ||
) | ||
} | ||
) | ||
|
||
try { | ||
return { | ||
data: organizations, | ||
pagination: response.data?.getOrganizationsByEmailPaginated?.pagination, | ||
} | ||
} catch (error) { | ||
logger.error({ | ||
error, | ||
message: 'getOrganizationsByEmail-error', | ||
}) | ||
throw new GraphQLError(getErrorMessage(error)) | ||
} | ||
}, | ||
|
||
getOrganizationByIdStorefront: async ( | ||
_: void, | ||
{ id }: { id: string }, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this function here and on the storefront-permissions app? (PR)
Can't we query Master Data directly from this app and avoid additional call to another app?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@enzomerca I'm following the same approach of the getOrganizationsByEmail query
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but we are removing the cyclic dependencies, and this is one of them, and thinking about the domain and context, the storefront shouldn't know about the organization.