Skip to content

Commit

Permalink
fix(client/recommend): adjusting request batching to ensure undefined…
Browse files Browse the repository at this point in the history
… entries do not overwrite
  • Loading branch information
korgon committed Sep 11, 2024
1 parent bb0212a commit 5b2e460
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
35 changes: 35 additions & 0 deletions packages/snap-client/src/Client/apis/Recommend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,41 @@ describe('Recommend Api', () => {
requestMock.mockReset();
});

it('batchRecommendations uses parameters regardless of order specified in requests', async () => {
const api = new RecommendAPI(new ApiConfiguration({}));

const requestMock = jest
.spyOn(global.window, 'fetch')
.mockImplementation(() => Promise.resolve({ status: 200, json: () => Promise.resolve(mockData.recommend()) } as Response));

api.batchRecommendations({
tag: 'similar',
products: ['sku1'],
batched: true,
siteId: '8uyt2m',
});

api.batchRecommendations({
tag: 'crossSell',
batched: true,
siteId: '8uyt2m',
});

//add delay for paramBatch.timeout
await wait(250);

const POSTParams = {
method: 'POST',
headers: {
'Content-Type': 'text/plain',
},
body: '{"profiles":[{"tag":"similar","limit":20},{"tag":"crossSell","limit":20}],"siteId":"8uyt2m","products":["sku1"]}',
};

expect(requestMock).toHaveBeenCalledWith(RequestUrl, POSTParams);
requestMock.mockReset();
});

it('batchRecommendations handles order prop as expected', async () => {
const api = new RecommendAPI(new ApiConfiguration({}));

Expand Down
52 changes: 23 additions & 29 deletions packages/snap-client/src/Client/apis/Recommend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,29 @@ export class RecommendAPI extends API {
// delete the batch so a new one can take its place
delete this.batches[key];

//resort batch entries based on order
// resort batch entries based on order
batch.entries.sort(sortBatchEntries);

// now that the requests are in proper order, map through them
// and build out the batches
// now that the requests are in proper order, map through them and build out the batches
batch.entries.map((entry) => {
// use products request only and combine when needed
if (entry.request.product) {
if (Array.isArray(entry.request.products) && entry.request.products.indexOf(entry.request.product) == -1) {
entry.request.products = entry.request.products.concat(entry.request.product);
} else {
entry.request.products = [entry.request.product];
}
}

// parameters used for profile specific
const { tag, categories, brands, query, filters, dedupe } = entry.request;

let transformedFilters;
if (filters) {
transformedFilters = transformRecommendationFiltersPost(filters) as RecommendPostRequestFiltersModel[];
}

// build profile specific parameters
const profile: RecommendPostRequestProfileModel = {
tag,
categories,
Expand All @@ -97,39 +107,23 @@ export class RecommendAPI extends API {

batch.request.profiles?.push(profile);

batch.request = {
...batch.request,
siteId: parameters.siteId,
product: parameters.product,
products: parameters.products,
blockedItems: parameters.blockedItems,
test: parameters.test,
cart: parameters.cart,
lastViewed: parameters.lastViewed,
shopper: parameters.shopper,
} as RecommendPostRequestModel;

// use products request only and combine when needed
if (batch.request.product) {
if (Array.isArray(batch.request.products) && batch.request.products.indexOf(batch.request.product) == -1) {
batch.request.products = batch.request.products.concat(batch.request.product);
} else {
batch.request.products = [batch.request.product];
}

delete batch.request.product;
}
// parameters used globally
const { siteId, products, blockedItems, test, cart, lastViewed, shopper } = entry.request;
// only when these parameters are defined should they be added to the list
if (siteId) batch.request.siteId = siteId;
if (products) batch.request.products = products;
if (blockedItems) batch.request.blockedItems = blockedItems;
if (test) batch.request.test = test;
if (cart) batch.request.cart = cart;
if (lastViewed) batch.request.lastViewed = lastViewed;
if (shopper) batch.request.shopper = shopper;
});

try {
if (this.configuration.mode == AppMode.development) {
batch.request.test = true;
}

if (batch.request['product']) {
batch.request['product'] = batch.request['product'].toString();
}

const response = await this.postRecommendations(batch.request as RecommendPostRequestModel);

batch.entries?.forEach((entry, index) => {
Expand Down

0 comments on commit 5b2e460

Please sign in to comment.