Skip to content

Commit

Permalink
feat: refactor organization graphql client and set auth token in que…
Browse files Browse the repository at this point in the history
…ries
  • Loading branch information
Rudge committed Nov 30, 2023
1 parent 93f01dd commit df08689
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 89 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"name": "Save_User"
},
{
"name": "vtex.graphql-server:resolve-graphql"
"name": "vtex.b2b-organizations-graphql:resolve-graphql"
},
{
"name": "outbound-access",
Expand Down
24 changes: 0 additions & 24 deletions node/clients/GraphQLServer.ts

This file was deleted.

112 changes: 112 additions & 0 deletions node/clients/Organizations.ts
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> => {

Check warning on line 27 in node/clients/Organizations.ts

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
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> => {

Check warning on line 45 in node/clients/Organizations.ts

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
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> => {

Check warning on line 63 in node/clients/Organizations.ts

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
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> => {

Check warning on line 81 in node/clients/Organizations.ts

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
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> => {

Check warning on line 97 in node/clients/Organizations.ts

View workflow job for this annotation

GitHub Actions / QE / Lint Node.js

Unexpected any. Specify a different type
return this.graphql.query(
{
extensions: getPersistedQuery(),
query: QUERIES.getOrganizationsByEmail,
variables: { email },
},
{
params: {
headers: getTokenToHeader(this.context),
locale: this.context.locale,
},
}
)
}
}
10 changes: 5 additions & 5 deletions node/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { IOClients } from '@vtex/api'
import { LMClient } from '../utils/LicenseManager'
import { ProfileSystemClient } from '../utils/ProfileSystem'
import { Checkout } from './checkout'
import { GraphQLServer } from './GraphQLServer'
import FullSessions from './FullSessions'
import IdentityClient from './IdentityClient'
import { OrganizationsGraphQLClient } from './Organizations'
import { SalesChannel } from './salesChannel'
import { Schema } from './schema'
import VtexId from './vtexId'
import { SalesChannel } from './salesChannel'
import FullSessions from './FullSessions'

// Extend the default IOClients implementation with our own custom clients.
export class Clients extends IOClients {
Expand All @@ -24,8 +24,8 @@ export class Clients extends IOClients {
return this.getOrSet('checkout', Checkout)
}

public get graphqlServer() {
return this.getOrSet('graphqlServer', GraphQLServer)
public get organizations() {
return this.getOrSet('organizations', OrganizationsGraphQLClient)
}

public get schema() {
Expand Down
71 changes: 12 additions & 59 deletions node/resolvers/Routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -90,7 +90,7 @@ export const Routes = {
setProfile: async (ctx: Context) => {
const {
clients: {
graphqlServer,
organizations,
masterdata,
checkout,
profileSystem,
Expand Down Expand Up @@ -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 [
Expand All @@ -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,
Expand Down

0 comments on commit df08689

Please sign in to comment.