From 6191dc984101c5dd3a740f07d7042d58ce5059b2 Mon Sep 17 00:00:00 2001 From: AnnaChiu95 <53097865+AnnaChiu95@users.noreply.github.com> Date: Thu, 14 Jul 2022 11:21:14 -0400 Subject: [PATCH 1/4] [QUICKORDER-27] Use sales channel from segment --- CHANGELOG.md | 4 ++++ node/clients/search.ts | 39 ++++++++++++++++++++++------------ node/resolvers/search/index.ts | 7 +++++- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15c9efb7..6caf486e 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 + +- Get sales channel from segment instead of orderForm + ## [3.9.3] - 2022-07-05 ### Fixed diff --git a/node/clients/search.ts b/node/clients/search.ts index 20fa82f0..d971e916 100644 --- a/node/clients/search.ts +++ b/node/clients/search.ts @@ -5,6 +5,7 @@ interface RefIdArgs { refids: any orderFormId: string refIdSellerMap: RefIdSellerMap + salesChannel: string } interface Items { id: string @@ -16,6 +17,13 @@ interface RefIdSellerMap { [key: string]: [string] } +interface SimulateArgs { + refids: [Items] + orderForm: any + refIdSellerMap: RefIdSellerMap + salesChannel: string +} + export class Search extends JanusClient { constructor(context: IOContext, options?: InstanceOptions) { super(context, { @@ -40,6 +48,7 @@ export class Search extends JanusClient { refids, orderFormId, refIdSellerMap, + salesChannel, }: RefIdArgs): Promise => { this.sellersList = await this.sellers() @@ -58,11 +67,11 @@ export class Search extends JanusClient { if (res.status === 200) { const orderForm = await this.getOrderForm(orderFormId) - const { salesChannel } = orderForm + const sc = salesChannel ?? orderForm.salesChannel // filter out sellers that aren't available in current sales channel this.sellersList = this.sellersList?.filter(seller => { - return seller.availableSalesChannels.includes(Number(salesChannel)) + return seller.availableSalesChannels.includes(Number(sc)) }) const refs = Object.getOwnPropertyNames(res.data) @@ -91,11 +100,12 @@ export class Search extends JanusClient { : null }) - const { items }: any = await this.simulate( - result, + const { items }: any = await this.simulate({ + refids: result, orderForm, - refIdSellerMap - ) + refIdSellerMap, + salesChannel: sc, + }) if (!items.length) return items @@ -156,13 +166,14 @@ export class Search extends JanusClient { }) } - private simulate = async ( - refids: [Items], - orderForm: any, - refIdSellerMap: RefIdSellerMap - ) => { + private simulate = async ({ + refids, + orderForm, + refIdSellerMap, + salesChannel, + }: SimulateArgs) => { const { - salesChannel, + salesChannel: orderFormSC, storePreferencesData: { countryCode }, shippingData, } = orderForm @@ -184,7 +195,9 @@ export class Search extends JanusClient { }) return this.http.post( - `/api/checkout/pub/orderForms/simulation?sc=${salesChannel}`, + `/api/checkout/pub/orderForms/simulation?sc=${ + salesChannel ?? orderFormSC + }`, { items: simulateItems, country: countryCode, diff --git a/node/resolvers/search/index.ts b/node/resolvers/search/index.ts index 3411c930..47466604 100644 --- a/node/resolvers/search/index.ts +++ b/node/resolvers/search/index.ts @@ -1,6 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { UserInputError } from '@vtex/api' +import atob from 'atob' import { resolvers as refidsResolvers } from './refids' @@ -15,7 +16,8 @@ export const queries = { ctx: Context ): Promise => { const { - clients: { search }, + clients: { search, segment }, + vtex: { segmentToken }, } = ctx if (!args.refids) { @@ -26,6 +28,9 @@ export const queries = { refids: args.refids, orderFormId: args.orderFormId, refIdSellerMap: args.refIdSellerMap, + salesChannel: segmentToken + ? JSON.parse(atob(segmentToken)).channel + : (await segment.getSegment()).channel, }) return { From 4283395be7f5c9c3a55370ae37b9631df12d7e17 Mon Sep 17 00:00:00 2001 From: AnnaChiu95 <53097865+AnnaChiu95@users.noreply.github.com> Date: Thu, 14 Jul 2022 12:53:52 -0400 Subject: [PATCH 2/4] Update unit tests --- node/tests/resolvers.test.ts | 3 ++- node/tests/vtexContext.ts | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/node/tests/resolvers.test.ts b/node/tests/resolvers.test.ts index 8a060d7b..521ba9a0 100644 --- a/node/tests/resolvers.test.ts +++ b/node/tests/resolvers.test.ts @@ -1,7 +1,7 @@ import vtexContext from './vtexContext' describe('Graphql resolvers', () => { - const { Query, clients } = vtexContext() + const { Query, clients, vtex } = vtexContext() it('[GraphQL] skuFromRefIds', async () => { const data = await Query.skuFromRefIds( @@ -13,6 +13,7 @@ describe('Graphql resolvers', () => { }, { clients, + vtex, } ) diff --git a/node/tests/vtexContext.ts b/node/tests/vtexContext.ts index af643359..df428d30 100644 --- a/node/tests/vtexContext.ts +++ b/node/tests/vtexContext.ts @@ -1,4 +1,5 @@ import type { IOContext } from '@vtex/api' +import { Segment } from '@vtex/api' import Service from '../index' import { Search } from '../clients/search' @@ -13,14 +14,18 @@ const vtexContext = () => { const context = {} as IOContext const clients = { search: new Search(context), + segment: new Segment(context), } + const vtex = {} as IOContext + return { context, clients, Query, resolvers, Service, + vtex, } } From 4e33197b2ce0b55e8e45003ccd8c24942fde5d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Artur=20Magalh=C3=A3es?= Date: Thu, 14 Jul 2022 14:39:34 -0300 Subject: [PATCH 3/4] test: fix on unit tests mock file --- node/tests/setupTests.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/node/tests/setupTests.ts b/node/tests/setupTests.ts index f5cac213..870be14d 100644 --- a/node/tests/setupTests.ts +++ b/node/tests/setupTests.ts @@ -253,6 +253,11 @@ jest.mock('@vtex/api', () => { IOClients: jest.fn(() => ({ getOrSet: jest.fn(), })), + Segment: jest.fn((): any => ({ + getSegment: jest.fn(() => ({ + channel: jest.fn(), + })), + })), LRUCache: jest.fn(), RecorderState: jest.fn(), UserInputError: jest.fn(), From 5c0e41a8513f151223d893a7a20b49fe2981e218 Mon Sep 17 00:00:00 2001 From: AnnaChiu95 <53097865+AnnaChiu95@users.noreply.github.com> Date: Thu, 14 Jul 2022 13:47:01 -0400 Subject: [PATCH 4/4] Update segment channel mock data --- node/tests/setupTests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node/tests/setupTests.ts b/node/tests/setupTests.ts index 870be14d..94506437 100644 --- a/node/tests/setupTests.ts +++ b/node/tests/setupTests.ts @@ -255,7 +255,7 @@ jest.mock('@vtex/api', () => { })), Segment: jest.fn((): any => ({ getSegment: jest.fn(() => ({ - channel: jest.fn(), + channel: '1', })), })), LRUCache: jest.fn(),