diff --git a/CHANGELOG.md b/CHANGELOG.md index 17345b66..3a296777 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/node/clients/Orders.ts b/node/clients/Orders.ts new file mode 100644 index 00000000..c5d120b3 --- /dev/null +++ b/node/clients/Orders.ts @@ -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 = (url: string, config: RequestConfig = {}) => { + config.headers = { + ...config.headers, + } + + return this.http.get(url, config).catch(statusToError) + } + + private get routes() { + const base = '/api/orders' + + return { + order: (id: string) => `${base}/pvt/document/${id}`, + } + } +} diff --git a/node/clients/index.ts b/node/clients/index.ts index 97f5a6ab..88619fd3 100644 --- a/node/clients/index.ts +++ b/node/clients/index.ts @@ -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' @@ -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) } diff --git a/node/package.json b/node/package.json index 51f3615c..ec3afc85 100644 --- a/node/package.json +++ b/node/package.json @@ -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", @@ -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", diff --git a/node/resolvers/Routes/index.ts b/node/resolvers/Routes/index.ts index e8c85855..bf5a97c8 100644 --- a/node/resolvers/Routes/index.ts +++ b/node/resolvers/Routes/index.ts @@ -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 { @@ -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, @@ -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, diff --git a/node/yarn.lock b/node/yarn.lock index 3b952b42..ced91527 100644 --- a/node/yarn.lock +++ b/node/yarn.lock @@ -831,10 +831,10 @@ dependencies: "@types/yargs-parser" "*" -"@vtex/api@6.45.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/api@6.45.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" @@ -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: