From f9dcc78f6bf0fdd1d9050874a126071e823537e2 Mon Sep 17 00:00:00 2001 From: Enzo Mercanti Date: Wed, 13 Sep 2023 16:00:42 -0300 Subject: [PATCH 1/2] Add Orders client and use it to get orders info --- CHANGELOG.md | 4 +++ node/clients/Orders.ts | 35 +++++++++++++++++++++++++ node/clients/index.ts | 5 ++++ node/package.json | 4 +-- node/resolvers/Routes/index.ts | 48 ++++++++++++++++++++-------------- node/yarn.lock | 10 +++---- 6 files changed, 79 insertions(+), 27 deletions(-) create mode 100644 node/clients/Orders.ts 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: From 5d8298ef0626f4d49958395cb7a483e5e06168c7 Mon Sep 17 00:00:00 2001 From: enzomerca <131273915+enzomerca@users.noreply.github.com> Date: Tue, 19 Sep 2023 13:46:25 +0000 Subject: [PATCH 2/2] Release v0.36.1 --- CHANGELOG.md | 2 ++ manifest.json | 2 +- node/package.json | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a296777..5bae5940 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [0.36.1] - 2023-09-19 + ### Fixed - Use Orders endpoint instead of OMS to obtain order information diff --git a/manifest.json b/manifest.json index fb7cb28a..b272a528 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "name": "b2b-organizations-graphql", "vendor": "vtex", - "version": "0.36.0", + "version": "0.36.1", "title": "B2B Organizations", "description": "App to create and manage B2B Organizations and Cost Centers", "mustUpdateAt": "2022-08-28", diff --git a/node/package.json b/node/package.json index ec3afc85..b7efdf71 100644 --- a/node/package.json +++ b/node/package.json @@ -1,6 +1,6 @@ { "name": "vtex.b2b-organizations", - "version": "0.36.0", + "version": "0.36.1", "dependencies": { "@vtex/api": "6.45.20", "atob": "^2.1.2",