Skip to content

Commit

Permalink
Merge pull request #128 from vtex-apps/fix/QUICKORDER-27
Browse files Browse the repository at this point in the history
[QUICKORDER-27] Use sales channel from segment in checkout simulation
  • Loading branch information
AnnaChiu95 authored Jul 15, 2022
2 parents 7d048a4 + 5c0e41a commit 09b1e1b
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 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

- Get sales channel from segment instead of orderForm

## [3.9.3] - 2022-07-05

### Fixed
Expand Down
39 changes: 26 additions & 13 deletions node/clients/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface RefIdArgs {
refids: any
orderFormId: string
refIdSellerMap: RefIdSellerMap
salesChannel: string
}
interface Items {
id: string
Expand All @@ -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, {
Expand All @@ -40,6 +48,7 @@ export class Search extends JanusClient {
refids,
orderFormId,
refIdSellerMap,
salesChannel,
}: RefIdArgs): Promise<any> => {
this.sellersList = await this.sellers()

Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion node/resolvers/search/index.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -15,7 +16,8 @@ export const queries = {
ctx: Context
): Promise<any> => {
const {
clients: { search },
clients: { search, segment },
vtex: { segmentToken },
} = ctx

if (!args.refids) {
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion node/tests/resolvers.test.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -13,6 +13,7 @@ describe('Graphql resolvers', () => {
},
{
clients,
vtex,
}
)

Expand Down
5 changes: 5 additions & 0 deletions node/tests/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ jest.mock('@vtex/api', () => {
IOClients: jest.fn(() => ({
getOrSet: jest.fn(),
})),
Segment: jest.fn((): any => ({
getSegment: jest.fn(() => ({
channel: '1',
})),
})),
LRUCache: jest.fn(),
RecorderState: jest.fn(),
UserInputError: jest.fn(),
Expand Down
5 changes: 5 additions & 0 deletions node/tests/vtexContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { IOContext } from '@vtex/api'
import { Segment } from '@vtex/api'

import Service from '../index'
import { Search } from '../clients/search'
Expand All @@ -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,
}
}

Expand Down

0 comments on commit 09b1e1b

Please sign in to comment.