Skip to content

Commit

Permalink
refactor(client/recommend): refactoring the recommend and instantiator
Browse files Browse the repository at this point in the history
  • Loading branch information
korgon committed Sep 11, 2024
1 parent 7db133a commit 26eed8c
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 257 deletions.
1 change: 1 addition & 0 deletions docs/INTEGRATION_LEGACY_RECOMMENDATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Context variables may be applied to individual recommendation profiles similar t
| options.brands | array of brand strings | all | optional brand identifiers used in brand trending recommendation profiles |
| options.branch | template branch overwrite | all | optional branch overwrite for recommendations template (advanced usage) |
| options.filters | array of filters | all | optional recommendation filters |
| options.query | string | all | query to search |
| options.realtime | boolean | all | optional update recommendations if cart contents change (requires [cart attribute tracking](https://github.com/searchspring/snap/blob/main/docs/INTEGRATION_TRACKING.md)) |
| options.blockedItems | array of strings | all | SKU values to identify which products to exclude from the response |
| options.batched | boolean (default: `true`)| all | only applies to recommendation context, optional disable profile from being batched in a single request, can also be set globally [via config](https://github.com/searchspring/snap/tree/main/packages/snap-controller/src/Recommendation) |
Expand Down
4 changes: 2 additions & 2 deletions docs/INTEGRATION_RECOMMENDATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Context variables are applied to individual recommendation profiles similar to h
| Option | Value | Placement | Description | Required
|---|---|:---:|---|:---:|
| products | array of SKU strings | product detail page | SKU value(s) to identify the current product(s) being viewed | ✔️ |
| blockedItems | array of strings | all | SKU values to identify which products to exclude from the response | |
| cart | array (or function that returns an array) of current cart skus | all | optional method of setting cart contents | |
| shopper.id | logged in user unique identifier | all | required for personalization functionallity if not provided to the bundle (global) context | |

Expand All @@ -47,10 +48,9 @@ Context variables are applied to individual recommendation profiles similar to h
| options.brands | array of brand strings | all | optional brand identifiers used in brand trending recommendation profiles | |
| options.branch | template branch overwrite | all | optional branch overwrite for recommendations template (advanced usage) | |
| options.dedupe | boolean (default: `true`) | all | dedupe products across all profiles in the batch | |
| options.searchTerm | string | all | query to search | |
| options.query | string | dynamic custom | query to search | |
| options.filters | array of filters | all | optional recommendation filters | |
| options.realtime | boolean | all | optional update recommendations if cart contents change (requires [cart attribute tracking](https://github.com/searchspring/snap/blob/main/docs/INTEGRATION_TRACKING.md)) | |
| options.blockedItems | array of strings | all | SKU values to identify which products to exclude from the response | |
| options.limit | number (default: 20, max: 20) | all | optional maximum number of results to display, can also be set globally [via config globals](https://github.com/searchspring/snap/tree/main/packages/snap-controller/src/Recommendation) | |


Expand Down
62 changes: 31 additions & 31 deletions packages/snap-client/src/Client/apis/Recommend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ApiConfiguration } from './Abstract';
import { RecommendAPI } from './Recommend';
import { MockData } from '@searchspring/snap-shared';

import type { PostRecommendAPISpec } from '../../types';
import type { RecommendPostRequestModel } from '../../types';

const mockData = new MockData();

Expand Down Expand Up @@ -63,7 +63,7 @@ describe('Recommend Api', () => {
},
};

const requestParameters: PostRecommendAPISpec = {
const requestParameters: RecommendPostRequestModel = {
siteId: '8uyt2m',
profiles: [
{
Expand Down Expand Up @@ -147,14 +147,14 @@ describe('Recommend Api', () => {

api.batchRecommendations({
tag: 'similar',
limits: 14,
limit: 14,
batched: true,
...batchParams,
});

api.batchRecommendations({
tag: 'crossSell',
limits: 10,
limit: 10,
batched: true,
...batchParams,
});
Expand All @@ -167,7 +167,7 @@ describe('Recommend Api', () => {
headers: {
'Content-Type': 'text/plain',
},
body: '{"profiles":[{"tag":"similar","limit":14},{"tag":"crossSell","limit":10}],"siteId":"8uyt2m","product":"marnie-runner-2-7x10","lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
body: '{"profiles":[{"tag":"similar","limit":14},{"tag":"crossSell","limit":10}],"siteId":"8uyt2m","products":["marnie-runner-2-7x10"],"lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
};

expect(requestMock).toHaveBeenCalledWith(RequestUrl, POSTParams);
Expand All @@ -185,22 +185,22 @@ describe('Recommend Api', () => {
api.batchRecommendations({
tag: 'similar',
categories: ['shirts'],
limits: 14,
limit: 14,
batched: true,
...batchParams,
});
//no category
api.batchRecommendations({
tag: 'crossSell',
limits: 10,
limit: 10,
batched: true,
...batchParams,
});
//pants category
api.batchRecommendations({
tag: 'crossSell',
categories: ['pants'],
limits: 10,
limit: 10,
batched: true,
...batchParams,
});
Expand All @@ -213,7 +213,7 @@ describe('Recommend Api', () => {
headers: {
'Content-Type': 'text/plain',
},
body: '{"profiles":[{"tag":"similar","categories":["shirts"],"limit":14},{"tag":"crossSell","limit":10},{"tag":"crossSell","categories":["pants"],"limit":10}],"siteId":"8uyt2m","product":"marnie-runner-2-7x10","lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
body: '{"profiles":[{"tag":"similar","categories":["shirts"],"limit":14},{"tag":"crossSell","limit":10},{"tag":"crossSell","categories":["pants"],"limit":10}],"siteId":"8uyt2m","products":["marnie-runner-2-7x10"],"lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
};

expect(requestMock).toHaveBeenCalledWith(RequestUrl, POSTParams);
Expand All @@ -231,15 +231,15 @@ describe('Recommend Api', () => {
api.batchRecommendations({
tag: 'similar',
brands: ['shirts'],
limits: 14,
limit: 14,
batched: true,
...batchParams,
});
//pants category
api.batchRecommendations({
tag: 'crossSell',
brands: ['pants', 'pants2'],
limits: 10,
limit: 10,
batched: true,
...batchParams,
});
Expand All @@ -252,7 +252,7 @@ describe('Recommend Api', () => {
headers: {
'Content-Type': 'text/plain',
},
body: '{"profiles":[{"tag":"similar","brands":["shirts"],"limit":14},{"tag":"crossSell","brands":["pants","pants2"],"limit":10}],"siteId":"8uyt2m","product":"marnie-runner-2-7x10","lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
body: '{"profiles":[{"tag":"similar","brands":["shirts"],"limit":14},{"tag":"crossSell","brands":["pants","pants2"],"limit":10}],"siteId":"8uyt2m","products":["marnie-runner-2-7x10"],"lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
};

expect(requestMock).toHaveBeenCalledWith(RequestUrl, POSTParams);
Expand All @@ -270,22 +270,22 @@ describe('Recommend Api', () => {
api.batchRecommendations({
tag: 'similar',
categories: ['shirts'],
limits: 14,
limit: 14,
order: 3,
batched: true,
...batchParams,
});
//no order
api.batchRecommendations({
tag: 'crossSell',
limits: 10,
limit: 10,
batched: true,
...batchParams,
});
//no category
api.batchRecommendations({
tag: 'crossSell',
limits: 10,
limit: 10,
order: 2,
batched: true,
...batchParams,
Expand All @@ -294,7 +294,7 @@ describe('Recommend Api', () => {
api.batchRecommendations({
tag: 'crossSell',
categories: ['pants'],
limits: 10,
limit: 10,
order: 1,
batched: true,
...batchParams,
Expand All @@ -305,7 +305,7 @@ describe('Recommend Api', () => {
headers: {
'Content-Type': 'text/plain',
},
body: '{"profiles":[{"tag":"crossSell","categories":["pants"],"limit":10},{"tag":"crossSell","limit":10},{"tag":"similar","categories":["shirts"],"limit":14},{"tag":"crossSell","limit":10}],"siteId":"8uyt2m","product":"marnie-runner-2-7x10","lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
body: '{"profiles":[{"tag":"crossSell","categories":["pants"],"limit":10},{"tag":"crossSell","limit":10},{"tag":"similar","categories":["shirts"],"limit":14},{"tag":"crossSell","limit":10}],"siteId":"8uyt2m","products":["marnie-runner-2-7x10"],"lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
};

//add delay for paramBatch.timeout
Expand All @@ -326,7 +326,7 @@ describe('Recommend Api', () => {
const promise1 = api.batchRecommendations({
tag: 'similar',
categories: ['shirts'],
limits: 10,
limit: 10,
order: 2,
batched: true,
...batchParams,
Expand All @@ -335,7 +335,7 @@ describe('Recommend Api', () => {
const promise2 = api.batchRecommendations({
tag: 'crosssell',
categories: ['dress'],
limits: 20,
limit: 20,
order: 1,
batched: true,
...batchParams,
Expand All @@ -349,7 +349,7 @@ describe('Recommend Api', () => {
headers: {
'Content-Type': 'text/plain',
},
body: '{"profiles":[{"tag":"crosssell","categories":["dress"],"limit":20},{"tag":"similar","categories":["shirts"],"limit":10}],"siteId":"8uyt2m","product":"marnie-runner-2-7x10","lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
body: '{"profiles":[{"tag":"crosssell","categories":["dress"],"limit":20},{"tag":"similar","categories":["shirts"],"limit":10}],"siteId":"8uyt2m","products":["marnie-runner-2-7x10"],"lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
};

expect(requestMock).toHaveBeenCalledWith(RequestUrl, POSTParams);
Expand All @@ -371,7 +371,7 @@ describe('Recommend Api', () => {

api.batchRecommendations({
tag: 'crossSell',
limits: 10,
limit: 10,
filters: [
{
type: 'value',
Expand All @@ -389,7 +389,7 @@ describe('Recommend Api', () => {
headers: {
'Content-Type': 'text/plain',
},
body: '{"profiles":[{"tag":"crossSell","limit":10}],"siteId":"8uyt2m","product":"marnie-runner-2-7x10","lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"],"filters":[{"field":"color","type":"=","values":["red"]}]}',
body: '{"profiles":[{"tag":"crossSell","limit":10,"filters":[{"field":"color","type":"=","values":["red"]}]}],"siteId":"8uyt2m","products":["marnie-runner-2-7x10"],"lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
};

expect(requestMock).toHaveBeenCalledWith(RequestUrl, POSTParams);
Expand All @@ -407,7 +407,7 @@ describe('Recommend Api', () => {
api.batchRecommendations({
tag: 'crossSell',
...batchParams,
limits: undefined,
limit: undefined,
});

//add delay for paramBatch.timeout
Expand All @@ -417,7 +417,7 @@ describe('Recommend Api', () => {
headers: {
'Content-Type': 'text/plain',
},
body: '{"profiles":[{"tag":"crossSell","limit":20}],"siteId":"8uyt2m","product":"marnie-runner-2-7x10","lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
body: '{"profiles":[{"tag":"crossSell","limit":20}],"siteId":"8uyt2m","products":["marnie-runner-2-7x10"],"lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
};

expect(requestMock).toHaveBeenCalledWith(RequestUrl, POSTParams);
Expand Down Expand Up @@ -465,7 +465,7 @@ describe('Recommend Api', () => {
headers: {
'Content-Type': 'text/plain',
},
body: '{"profiles":[{"tag":"crossSell","limit":20}],"siteId":"8uyt2m","product":"marnie-runner-2-7x10","blockedItems":["blocked_sku1","blocked_sku2"],"lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
body: '{"profiles":[{"tag":"crossSell","limit":20}],"siteId":"8uyt2m","products":["marnie-runner-2-7x10"],"blockedItems":["blocked_sku1","blocked_sku2"],"lastViewed":["marnie-runner-2-7x10","ruby-runner-2-7x10","abbie-runner-2-7x10","riley-4x6","joely-5x8","helena-4x6","kwame-4x6","sadie-4x6","candice-runner-2-7x10","esmeray-4x6","camilla-230x160","candice-4x6","sahara-4x6","dayna-4x6","moema-4x6"]}',
};

expect(requestMock).toHaveBeenCalledWith(RequestUrl, POSTParams);
Expand All @@ -487,10 +487,15 @@ describe('Recommend Api', () => {
return {
tag: index.toString(),
limit: 20,
filters: [
{ field: 'color', type: '=', values: ['blue'] },
{ field: 'price', type: '>=', values: [0] },
{ field: 'price', type: '<=', values: [20] },
],
};
}),
siteId: '8uyt2m',
product: 'marnie-runner-2-7x10',
products: ['marnie-runner-2-7x10'],
lastViewed: [
'marnie-runner-2-7x10',
'ruby-runner-2-7x10',
Expand All @@ -508,11 +513,6 @@ describe('Recommend Api', () => {
'dayna-4x6',
'moema-4x6',
],
filters: [
{ field: 'color', type: '=', values: ['blue'] },
{ field: 'price', type: '>=', values: [0] },
{ field: 'price', type: '<=', values: [20] },
],
}),
};

Expand Down
Loading

0 comments on commit 26eed8c

Please sign in to comment.