Skip to content

Commit

Permalink
Merge pull request #125 from vtex-apps/fix/B2BTEAM-1187-use-orders-en…
Browse files Browse the repository at this point in the history
…dpoint

Add Orders client and use it to get orders info
  • Loading branch information
enzomerca authored Sep 19, 2023
2 parents f41aa38 + f9dcc78 commit c2a4190
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 27 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]

### Fixed

- Use Orders endpoint instead of OMS to obtain order information

## [0.36.0] - 2023-08-11

### Added
Expand Down
35 changes: 35 additions & 0 deletions node/clients/Orders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { InstanceOptions, IOContext, RequestConfig } from '@vtex/api'
import { JanusClient } from '@vtex/api'

import { statusToError } from '../utils'

export default class OrdersClient extends JanusClient {
constructor(ctx: IOContext, options?: InstanceOptions) {
super(ctx, {
...options,
headers: {
...options?.headers,
VtexIdclientAutCookie: ctx.authToken,
},
})
}

public order = (id: string) =>
this.get(this.routes.order(id), { metric: 'orders' })

protected get = <T>(url: string, config: RequestConfig = {}) => {
config.headers = {
...config.headers,
}

return this.http.get<T>(url, config).catch(statusToError)
}

private get routes() {
const base = '/api/orders'

return {
order: (id: string) => `${base}/pvt/document/${id}`,
}
}
}
5 changes: 5 additions & 0 deletions node/clients/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import PaymentsClient from './payments'
import MailClient from './email'
import Checkout from './checkout'
import OMSClient from './Oms'
import OrdersClient from './Orders'
import StorefrontPermissions from './storefrontPermissions'
import IdentityClient from './IdentityClient'
import Catalog from './catalog'
Expand All @@ -28,6 +29,10 @@ export class Clients extends IOClients {
return this.getOrSet('oms', OMSClient)
}

public get orders() {
return this.getOrSet('orders', OrdersClient)
}

public get storefrontPermissions() {
return this.getOrSet('storefrontPermissions', StorefrontPermissions)
}
Expand Down
4 changes: 2 additions & 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": "0.36.0",
"dependencies": {
"@vtex/api": "6.45.19",
"@vtex/api": "6.45.20",
"atob": "^2.1.2",
"co-body": "^6.0.0",
"graphql": "^14.5.0",
Expand All @@ -19,7 +19,7 @@
"@types/jsonwebtoken": "^8.5.0",
"@types/node": "^12.12.21",
"@types/ramda": "types/npm-ramda#dist",
"@vtex/api": "6.45.19",
"@vtex/api": "6.45.20",
"@vtex/prettier-config": "^0.3.1",
"@vtex/tsconfig": "^0.6.0",
"jest": "27.5.1",
Expand Down
48 changes: 28 additions & 20 deletions node/resolvers/Routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,31 @@ const checkPermissionAgainstOrder = ({
return false
}

const getOrder = async (ctx: Context) => {
const {
vtex: {
route: {
params: { orderId },
},
},
clients: { orders },
} = ctx

if (!orderId) {
throw new UserInputError('Order ID is required')
}

const order: any = await orders.order(String(orderId))

// Some dependencies rely on this property not being null but an empty array instead.
// The new orders endpoint might return null for this property instead of an empty array,
// so we make sure to set it to an empty array to avoid breaking dependencies.
// For more info: https://vtex-dev.atlassian.net/browse/B2BTEAM-1376
order.marketplaceItems = order.marketplaceItems ?? []

return order
}

const Index = {
checkout: async (ctx: Context) => {
const {
Expand Down Expand Up @@ -167,20 +192,7 @@ const Index = {
ctx.response.body = response
},
order: async (ctx: Context) => {
const {
vtex: {
route: {
params: { orderId },
},
},
clients: { oms },
} = ctx

if (!orderId) {
throw new UserInputError('Order ID is required')
}

const order: any = await oms.order(String(orderId))
const order = await getOrder(ctx)

const {
permissions,
Expand Down Expand Up @@ -270,14 +282,10 @@ const Index = {
params: { orderId },
},
},
clients: { checkout, oms },
clients: { checkout },
} = ctx

if (!orderId) {
throw new UserInputError('Order ID is required')
}

const order: any = await oms.order(String(orderId))
const order = await getOrder(ctx)

const {
permissions,
Expand Down
10 changes: 5 additions & 5 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -831,10 +831,10 @@
dependencies:
"@types/yargs-parser" "*"

"@vtex/[email protected].19":
version "6.45.19"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.19.tgz#97b449850b517d6610e7123aa91caec2a29ae7b1"
integrity sha512-WUsAn1Oi+Z7Ih84DcRf4HQH85Mh+zO84L0ta7zuRyb13DoAEq2qMi0Mv6vZMd9BFWOCSD8AcTHVF/gZskwh9Yw==
"@vtex/[email protected].20":
version "6.45.20"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.20.tgz#c1090249a424fd700499de3fed0d80d99ff53332"
integrity sha512-O7RJWWr4PfvixNpc0GgQh85iKcv9j1IKkpApwJQSW/WzxoTgSrcjYHOVUmJ1GWWBmRV/qvB2VaV6Ow1QL3UOCQ==
dependencies:
"@types/koa" "^2.11.0"
"@types/koa-compose" "^3.2.3"
Expand Down Expand Up @@ -3470,7 +3470,7 @@ stack-utils@^2.0.3:
dependencies:
escape-string-regexp "^2.0.0"

stats-lite@vtex/node-stats-lite#dist:
"stats-lite@github:vtex/node-stats-lite#dist":
version "2.2.0"
resolved "https://codeload.github.com/vtex/node-stats-lite/tar.gz/1b0d39cc41ef7aaecfd541191f877887a2044797"
dependencies:
Expand Down

0 comments on commit c2a4190

Please sign in to comment.