-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor organization graphql client and set auth token in queries
- Loading branch information
Showing
5 changed files
with
130 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type { InstanceOptions, IOContext } from '@vtex/api' | ||
import { AppGraphQLClient } from '@vtex/api' | ||
|
||
import { QUERIES } from '../resolvers/Routes/utils' | ||
|
||
const getTokenToHeader = (ctx: IOContext) => { | ||
return { | ||
VtexIdclientAutCookie: | ||
ctx.storeUserAuthToken ?? ctx.adminUserAuthToken ?? ctx.authToken, | ||
} | ||
} | ||
|
||
const getPersistedQuery = () => { | ||
return { | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
} | ||
} | ||
|
||
export class OrganizationsGraphQLClient extends AppGraphQLClient { | ||
constructor(ctx: IOContext, options?: InstanceOptions) { | ||
super('[email protected]', ctx, options) | ||
} | ||
|
||
public getOrganizationById = async (orgId: string): Promise<any> => { | ||
return this.graphql.query( | ||
{ | ||
extensions: getPersistedQuery(), | ||
query: QUERIES.getOrganizationById, | ||
variables: { | ||
id: orgId, | ||
}, | ||
}, | ||
{ | ||
params: { | ||
headers: getTokenToHeader(this.context), | ||
locale: this.context.locale, | ||
}, | ||
} | ||
) | ||
} | ||
|
||
public getCostCenterById = async (costId: string): Promise<any> => { | ||
return this.graphql.query( | ||
{ | ||
extensions: getPersistedQuery(), | ||
query: QUERIES.getCostCenterById, | ||
variables: { | ||
id: costId, | ||
}, | ||
}, | ||
{ | ||
params: { | ||
headers: getTokenToHeader(this.context), | ||
locale: this.context.locale, | ||
}, | ||
} | ||
) | ||
} | ||
|
||
public getMarketingTags = async (costId: string): Promise<any> => { | ||
return this.graphql.query( | ||
{ | ||
extensions: getPersistedQuery(), | ||
query: QUERIES.getCostCenterById, | ||
variables: { | ||
costId, | ||
}, | ||
}, | ||
{ | ||
params: { | ||
headers: getTokenToHeader(this.context), | ||
locale: this.context.locale, | ||
}, | ||
} | ||
) | ||
} | ||
|
||
public getB2BSettings = async (): Promise<any> => { | ||
return this.graphql.query( | ||
{ | ||
extensions: getPersistedQuery(), | ||
query: QUERIES.getB2BSettings, | ||
variables: {}, | ||
}, | ||
{ | ||
params: { | ||
headers: getTokenToHeader(this.context), | ||
locale: this.context.locale, | ||
}, | ||
} | ||
) | ||
} | ||
|
||
public getOrganizationsByEmail = async (email: string): Promise<any> => { | ||
return this.graphql.query( | ||
{ | ||
extensions: getPersistedQuery(), | ||
query: QUERIES.getOrganizationsByEmail, | ||
variables: { email }, | ||
}, | ||
{ | ||
params: { | ||
headers: getTokenToHeader(this.context), | ||
locale: this.context.locale, | ||
}, | ||
} | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ import { json } from 'co-body' | |
import { getRole } from '../Queries/Roles' | ||
import { getSessionWatcher } from '../Queries/Settings' | ||
import { getActiveUserByEmail, getUserByEmail } from '../Queries/Users' | ||
import { generateClUser, QUERIES } from './utils' | ||
import { generateClUser } from './utils' | ||
import { getUser, setActiveUserByOrganization } from '../Mutations/Users' | ||
|
||
export const Routes = { | ||
|
@@ -90,7 +90,7 @@ export const Routes = { | |
setProfile: async (ctx: Context) => { | ||
const { | ||
clients: { | ||
graphqlServer, | ||
organizations, | ||
masterdata, | ||
checkout, | ||
profileSystem, | ||
|
@@ -234,23 +234,12 @@ export const Routes = { | |
response['storefront-permissions'].organization.value = user.orgId | ||
|
||
const getOrganization = async (orgId: any): Promise<any> => { | ||
return graphqlServer | ||
.query( | ||
QUERIES.getOrganizationById, | ||
{ id: orgId }, | ||
{ | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
} | ||
) | ||
.catch((error) => { | ||
logger.error({ | ||
error, | ||
message: 'setProfile.graphqlGetOrganizationById', | ||
}) | ||
return organizations.getOrganizationById(orgId).catch((error) => { | ||
logger.error({ | ||
error, | ||
message: 'setProfile.graphqlGetOrganizationById', | ||
}) | ||
}) | ||
} | ||
|
||
const [ | ||
|
@@ -263,55 +252,19 @@ export const Routes = { | |
data: any | ||
}> = await Promise.all([ | ||
getOrganization(user.orgId), | ||
graphqlServer.query( | ||
QUERIES.getCostCenterById, | ||
{ id: user.costId }, | ||
{ | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
} | ||
), | ||
organizations.getCostCenterById(user.costId), | ||
salesChannelClient.getSalesChannel(), | ||
graphqlServer.query( | ||
QUERIES.getMarketingTags, | ||
{ | ||
costId: user.costId, | ||
}, | ||
{ | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
} | ||
), | ||
graphqlServer.query( | ||
QUERIES.getB2BSettings, | ||
{}, | ||
{ | ||
persistedQuery: { | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
}, | ||
} | ||
), | ||
organizations.getMarketingTags(user.costId), | ||
organizations.getB2BSettings(), | ||
]) | ||
|
||
let organization = organizationResponse?.data?.getOrganizationById | ||
|
||
// prevent login if org is inactive | ||
if (organization.status === 'inactive') { | ||
// try to find a valid organization | ||
const organizationsByUserResponse: any = await graphqlServer | ||
.query( | ||
QUERIES.getOrganizationsByEmail, | ||
{ email }, | ||
{ | ||
provider: '[email protected]', | ||
sender: '[email protected]', | ||
} | ||
) | ||
const organizationsByUserResponse: any = await organizations | ||
.getOrganizationsByEmail(email) | ||
.catch((error) => { | ||
logger.error({ | ||
error, | ||
|