Skip to content

Commit

Permalink
Merge pull request #63 from vtex-apps/feature/B2BTEAM-1265-add-token-…
Browse files Browse the repository at this point in the history
…on-b2b-organization-request

feat: add token in graphql clients
  • Loading branch information
Rudge authored Nov 30, 2023
2 parents a34b397 + 8f3e5d8 commit 1f8d580
Show file tree
Hide file tree
Showing 11 changed files with 396 additions and 364 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- Add a token to call graphql queries for the client of storefront-permission and b2b-organizations-graphql

## [1.10.1] - 2023-08-17
### Fixed
- Fix conditional chain causing issue when retrieving Cost Center data
Expand Down
5 changes: 4 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
}
},
{
"name": "vtex.graphql-server:resolve-graphql"
"name": "vtex.storefront-permissions:resolve-graphql"
},
{
"name": "vtex.b2b-organizations-graphql:resolve-graphql"
},
{
"name": "vbase-read-write"
Expand Down
58 changes: 58 additions & 0 deletions node/clients/Organizations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { InstanceOptions, IOContext } from '@vtex/api'
import { AppGraphQLClient } from '@vtex/api'

import { QUERIES } from '../resolvers/Routes/queries'
import { getTokenToHeader } from './index'

export class OrganizationsGraphQLClient extends AppGraphQLClient {
constructor(ctx: IOContext, options?: InstanceOptions) {
super('[email protected]', ctx, options)
}

public getAddresses = async (costCenterId: string): Promise<any> => {
return this.graphql.query(
{
extensions: {
persistedQuery: {
provider: '[email protected]',
sender: '[email protected]',
},
},
query: QUERIES.getAddresses,
variables: {
id: costCenterId,
},
},
{
params: {
headers: getTokenToHeader(this.context),
locale: this.context.locale,
},
}
)
}

public getOrganization = async (organizationId: string): Promise<any> => {
return this.graphql.query(
{
extensions: {
persistedQuery: {
provider: '[email protected]',
sender: '[email protected]',
},
},
query: QUERIES.getOrganizationDetails,
variables: {
id: organizationId,
},
},
{
params: {
headers: getTokenToHeader(this.context),
locale: this.context.locale,
},
}
)
}
}
33 changes: 33 additions & 0 deletions node/clients/StorefrontPermissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { InstanceOptions, IOContext } from '@vtex/api'
import { AppGraphQLClient } from '@vtex/api'

import { QUERIES } from '../resolvers/Routes/queries'
import { getTokenToHeader } from './index'

export default class StorefrontPermissions extends AppGraphQLClient {
constructor(ctx: IOContext, options?: InstanceOptions) {
super('[email protected]', ctx, options)
}

public checkUserPermission = async (): Promise<any> => {
return this.graphql.query(
{
extensions: {
persistedQuery: {
provider: '[email protected]',
sender: '[email protected]',
},
},
query: QUERIES.getPermission,
variables: {},
},
{
params: {
headers: getTokenToHeader(this.context),
locale: this.context.locale,
},
}
)
}
}
46 changes: 0 additions & 46 deletions node/clients/graphqlServer.ts

This file was deleted.

21 changes: 17 additions & 4 deletions node/clients/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import type { IOContext } from '@vtex/api'
import { IOClients } from '@vtex/api'

import { GraphQLServer } from './graphqlServer'
import { Checkout } from './Checkout'
import { AuthUser } from './AuthUser'
import { Checkout } from './Checkout'
import { OrganizationsGraphQLClient } from './Organizations'
import StorefrontPermissions from './StorefrontPermissions'

// Extend the default IOClients implementation with our own custom clients.
export class Clients extends IOClients {
public get graphQLServer() {
return this.getOrSet('graphQLServer', GraphQLServer)
public get storefrontPermissions() {
return this.getOrSet('storefrontPermissions', StorefrontPermissions)
}

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

public get checkout() {
Expand All @@ -18,3 +24,10 @@ export class Clients extends IOClients {
return this.getOrSet('authUser', AuthUser)
}
}

export const getTokenToHeader = (ctx: IOContext) => {
return {
VtexIdclientAutCookie:
ctx.storeUserAuthToken ?? ctx.adminUserAuthToken ?? ctx.authToken,
}
}
3 changes: 1 addition & 2 deletions node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vtex.b2b-organizations",
"version": "1.10.1",
"dependencies": {
"@vtex/api": "6.45.19",
"@vtex/api": "6.46.0",
"atob": "^2.1.2",
"co-body": "^6.0.0",
"graphql": "^14.5.0",
Expand All @@ -21,7 +21,6 @@
"@types/jsonwebtoken": "^8.5.0",
"@types/node": "^12.12.21",
"@types/ramda": "types/npm-ramda#dist",
"@vtex/api": "6.45.19",
"@vtex/prettier-config": "^0.3.1",
"@vtex/tsconfig": "^0.6.0",
"jest": "27.5.1",
Expand Down
76 changes: 35 additions & 41 deletions node/resolvers/Routes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,37 @@ import index from './index'
const costCenterPaymentTerms = 'costCenterPaymentTerms'
const organizationPaymentTerms = 'organizationPaymentTerms'

const graphQLQuery = jest.fn()

interface ResponseBody {
paymentTerms: PaymentTerm[]
}

const getAddressMocked = jest.fn().mockResolvedValueOnce({
data: {
getCostCenterById: {
addresses: {},
customFields: {},
paymentTerms: [{ id: costCenterPaymentTerms }],
},
},
})

const mockContext = () => {
return {
clients: {
graphQLServer: {
query: graphQLQuery
.mockResolvedValueOnce({ data: {} })
.mockResolvedValueOnce({
data: {
getCostCenterById: {
addresses: {},
customFields: {},
paymentTerms: [{ id: costCenterPaymentTerms }],
},
},
})
.mockResolvedValueOnce({
data: {
getOrganizationById: {
addresses: {},
customFields: {},
paymentTerms: [{ id: organizationPaymentTerms }],
},
storefrontPermissions: {
checkUserPermission: jest.fn().mockResolvedValueOnce({ data: {} }),
},
organizations: {
getAddresses: getAddressMocked,
getOrganization: jest.fn().mockResolvedValueOnce({
data: {
getOrganizationById: {
addresses: {},
customFields: {},
paymentTerms: [{ id: organizationPaymentTerms }],
},
}),
},
}),
},
session: {
getSession: jest.fn().mockResolvedValueOnce({
Expand Down Expand Up @@ -67,6 +68,10 @@ const mockContext = () => {
} as unknown as Context
}

afterEach(() => {
jest.clearAllMocks()
})

describe('given Routes to call b2b checkout settings', () => {
describe('when have the cost center and organization with payment terms', () => {
let context: Context
Expand Down Expand Up @@ -96,26 +101,15 @@ describe('given Routes to call b2b checkout settings', () => {
let context: Context

beforeEach(async () => {
graphQLQuery
.mockResolvedValueOnce({ data: {} })
.mockResolvedValueOnce({
data: {
getCostCenterById: {
addresses: {},
customFields: {},
paymentTerms: [],
},
},
})
.mockResolvedValueOnce({
data: {
getOrganizationById: {
addresses: {},
customFields: {},
paymentTerms: [{ id: organizationPaymentTerms }],
},
getAddressMocked.mockResolvedValueOnce({
data: {
getCostCenterById: {
addresses: {},
customFields: {},
paymentTerms: [],
},
})
},
})
context = mockContext()
await index.settings(context)
})
Expand Down
Loading

0 comments on commit 1f8d580

Please sign in to comment.