diff --git a/manifest.json b/manifest.json index 62dd076..d243a47 100644 --- a/manifest.json +++ b/manifest.json @@ -71,7 +71,7 @@ "name": "Save_User" }, { - "name": "vtex.graphql-server:resolve-graphql" + "name": "vtex.b2b-organizations-graphql:resolve-graphql" }, { "name": "outbound-access", diff --git a/node/clients/GraphQLServer.ts b/node/clients/GraphQLServer.ts deleted file mode 100644 index 8a955ed..0000000 --- a/node/clients/GraphQLServer.ts +++ /dev/null @@ -1,24 +0,0 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import type { InstanceOptions, IOContext } from '@vtex/api' -import { AppClient, GraphQLClient } from '@vtex/api' - -export class GraphQLServer extends AppClient { - protected graphql: GraphQLClient - - constructor(ctx: IOContext, options?: InstanceOptions) { - super('vtex.graphql-server@1.x', ctx, options) - this.graphql = new GraphQLClient(this.http) - } - - public query = async (query: string, variables: any, extensions: any) => { - return this.graphql.query( - { query, variables, extensions }, - { - params: { - locale: this.context.locale, - }, - url: '/graphql', - } - ) - } -} diff --git a/node/clients/Organizations.ts b/node/clients/Organizations.ts new file mode 100644 index 0000000..7de15fd --- /dev/null +++ b/node/clients/Organizations.ts @@ -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: 'vtex.b2b-organizations-graphql@0.x', + sender: 'vtex.storefront-permissions@1.x', + }, + } +} + +export class OrganizationsGraphQLClient extends AppGraphQLClient { + constructor(ctx: IOContext, options?: InstanceOptions) { + super('vtex.b2b-organizations-graphql@0.x', ctx, options) + } + + public getOrganizationById = async (orgId: string): Promise => { + 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 => { + 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 => { + return this.graphql.query( + { + extensions: getPersistedQuery(), + query: QUERIES.getCostCenterById, + variables: { + costId, + }, + }, + { + params: { + headers: getTokenToHeader(this.context), + locale: this.context.locale, + }, + } + ) + } + + public getB2BSettings = async (): Promise => { + 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 => { + return this.graphql.query( + { + extensions: getPersistedQuery(), + query: QUERIES.getOrganizationsByEmail, + variables: { email }, + }, + { + params: { + headers: getTokenToHeader(this.context), + locale: this.context.locale, + }, + } + ) + } +} diff --git a/node/clients/index.ts b/node/clients/index.ts index 907b4e9..5d9c595 100644 --- a/node/clients/index.ts +++ b/node/clients/index.ts @@ -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 { @@ -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() { diff --git a/node/resolvers/Routes/index.ts b/node/resolvers/Routes/index.ts index 3964768..5bbeac3 100644 --- a/node/resolvers/Routes/index.ts +++ b/node/resolvers/Routes/index.ts @@ -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 => { - return graphqlServer - .query( - QUERIES.getOrganizationById, - { id: orgId }, - { - persistedQuery: { - provider: 'vtex.b2b-organizations-graphql@0.x', - sender: 'vtex.storefront-permissions@1.x', - }, - } - ) - .catch((error) => { - logger.error({ - error, - message: 'setProfile.graphqlGetOrganizationById', - }) + return organizations.getOrganizationById(orgId).catch((error) => { + logger.error({ + error, + message: 'setProfile.graphqlGetOrganizationById', }) + }) } const [ @@ -263,39 +252,10 @@ export const Routes = { data: any }> = await Promise.all([ getOrganization(user.orgId), - graphqlServer.query( - QUERIES.getCostCenterById, - { id: user.costId }, - { - persistedQuery: { - provider: 'vtex.b2b-organizations-graphql@0.x', - sender: 'vtex.storefront-permissions@1.x', - }, - } - ), + organizations.getCostCenterById(user.costId), salesChannelClient.getSalesChannel(), - graphqlServer.query( - QUERIES.getMarketingTags, - { - costId: user.costId, - }, - { - persistedQuery: { - provider: 'vtex.b2b-organizations-graphql@0.x', - sender: 'vtex.storefront-permissions@1.x', - }, - } - ), - graphqlServer.query( - QUERIES.getB2BSettings, - {}, - { - persistedQuery: { - provider: 'vtex.b2b-organizations-graphql@0.x', - sender: 'vtex.storefront-permissions@1.x', - }, - } - ), + organizations.getMarketingTags(user.costId), + organizations.getB2BSettings(), ]) let organization = organizationResponse?.data?.getOrganizationById @@ -303,15 +263,8 @@ export const Routes = { // 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: 'vtex.b2b-organizations-graphql@0.x', - sender: 'vtex.storefront-permissions@1.x', - } - ) + const organizationsByUserResponse: any = await organizations + .getOrganizationsByEmail(email) .catch((error) => { logger.error({ error,