Skip to content

Commit 5a5320f

Browse files
authored
[CDX-384]: added variationId param for getRecomendations function (#435)
* [CDX-384]: added variationId param for getRecomendations function * [CDX-384]: improved itemIds validation and test coverage * [CDX-384]: fixed max-len lint issue in recommendations JSDoc
1 parent 0b89768 commit 5a5320f

4 files changed

Lines changed: 69 additions & 1 deletion

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ test
77
# OS X specific files
88
.DS_Store
99

10+
# Editor specific files
11+
.idea
12+
1013
# Logs
1114
logs
1215
*.log

spec/src/modules/recommendations.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ describe(`ConstructorIO - Recommendations${bundledDescriptionSuffix}`, () => {
4949
const filteredItemsRecommendationsPodId = 'filtered_items';
5050
const itemId = 'power_drill';
5151
const itemIds = [itemId, 'drill'];
52+
const variationId = 'power_drill_variation';
5253

5354
it('Should return a response with valid itemIds (singular)', (done) => {
5455
const { recommendations } = new ConstructorIO({
@@ -99,6 +100,35 @@ describe(`ConstructorIO - Recommendations${bundledDescriptionSuffix}`, () => {
99100
});
100101
});
101102

103+
it('Should return a response with valid variationId', (done) => {
104+
const { recommendations } = new ConstructorIO({
105+
apiKey: testApiKey,
106+
fetch: fetchSpy,
107+
});
108+
109+
recommendations.getRecommendations(podId, { itemIds: itemId, variationId }).then((res) => {
110+
const requestedUrlParams = helpers.extractUrlParamsFromFetch(fetchSpy);
111+
112+
expect(res).to.have.property('request').to.be.an('object');
113+
expect(res).to.have.property('response').to.be.an('object');
114+
expect(res).to.have.property('result_id').to.be.an('string');
115+
expect(res.request.item_id).to.equal(itemId);
116+
expect(res.response).to.have.property('results').to.be.an('array');
117+
expect(res.response).to.have.property('pod');
118+
expect(res.response.pod).to.have.property('id').to.equal(podId);
119+
expect(res.response.pod).to.have.property('display_name');
120+
expect(fetchSpy).to.have.been.called;
121+
expect(requestedUrlParams).to.have.property('key');
122+
expect(requestedUrlParams).to.have.property('i');
123+
expect(requestedUrlParams).to.have.property('s');
124+
expect(requestedUrlParams).to.have.property('c').to.equal(clientVersion);
125+
expect(requestedUrlParams).to.have.property('item_id').to.equal(itemId);
126+
expect(requestedUrlParams).to.have.property('variation_id').to.equal(variationId);
127+
expect(res.request.variation_id).to.equal(variationId);
128+
done();
129+
});
130+
});
131+
102132
it('Should return a response with valid term for query recommendations strategy pod', (done) => {
103133
const term = 'apple';
104134
const { recommendations } = new ConstructorIO({
@@ -462,6 +492,23 @@ describe(`ConstructorIO - Recommendations${bundledDescriptionSuffix}`, () => {
462492
recommendations.getRecommendations(podId, { itemIds });
463493
});
464494

495+
it('Should be rejected when variationId is provided without itemId', () => {
496+
const { recommendations } = new ConstructorIO({ apiKey: testApiKey });
497+
498+
return expect(recommendations.getRecommendations(podId, {
499+
variationId,
500+
})).to.eventually.be.rejected;
501+
});
502+
503+
it('Should be rejected when variationId is provided with an empty itemIds array', () => {
504+
const { recommendations } = new ConstructorIO({ apiKey: testApiKey });
505+
506+
return expect(recommendations.getRecommendations(podId, {
507+
variationId,
508+
itemIds: [],
509+
})).to.eventually.be.rejected;
510+
});
511+
465512
it('Should be rejected when invalid pod id parameter is provided', () => {
466513
const { recommendations } = new ConstructorIO({ apiKey: testApiKey });
467514

src/modules/recommendations.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const EventDispatcher = require('../utils/event-dispatcher');
33
const helpers = require('../utils/helpers');
44

55
// Create URL from supplied parameters
6+
// eslint-disable-next-line complexity
67
function createRecommendationsUrl(podId, parameters, options) {
78
const { apiKey, version, serviceUrl, sessionId, userId, clientId, segments } = options;
89
let queryParams = { c: version };
@@ -30,6 +31,7 @@ function createRecommendationsUrl(podId, parameters, options) {
3031
const {
3132
numResults,
3233
itemIds,
34+
variationId,
3335
section,
3436
term,
3537
filters,
@@ -49,6 +51,18 @@ function createRecommendationsUrl(podId, parameters, options) {
4951
queryParams.item_id = itemIds;
5052
}
5153

54+
if (variationId) {
55+
if (!itemIds || (typeof itemIds === 'string' && !itemIds.trim())) {
56+
throw new Error('itemIds is a required parameter for variationId');
57+
}
58+
59+
if (Array.isArray(itemIds) && !itemIds.length) {
60+
throw new Error('At least one itemId is a required parameter for variationId');
61+
}
62+
63+
queryParams.variation_id = variationId;
64+
}
65+
5266
// Pull section from parameters
5367
if (section) {
5468
queryParams.section = section;
@@ -116,7 +130,9 @@ class Recommendations {
116130
* @description Retrieve recommendation results from Constructor.io API
117131
* @param {string} podId - Pod identifier
118132
* @param {object} [parameters] - Additional parameters to refine results
119-
* @param {string|array} [parameters.itemIds] - Item ID(s) to retrieve recommendations for (strategy specific)
133+
* @param {string|array} [parameters.itemIds] - Item ID(s) to retrieve recommendations for (strategy specific).
134+
* Required for variationId
135+
* @param {string} [parameters.variationId] - Variation ID to retrieve recommendations for (strategy specific)
120136
* @param {number} [parameters.numResults] - The number of results to return
121137
* @param {string} [parameters.section] - The section to return results from
122138
* @param {string} [parameters.term] - The term to use to refine results (strategy specific)

src/types/recommendations.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export default Recommendations;
55

66
export interface RecommendationsParameters {
77
itemIds?: string | string[];
8+
variationId?: string;
89
numResults?: number;
910
section?: string;
1011
term?: string;
@@ -41,6 +42,7 @@ export interface RecommendationsResponse extends Record<string, any> {
4142
export interface RecommendationsRequestType extends Record<string, any> {
4243
num_results: number;
4344
item_id: string | string[];
45+
variation_id: string;
4446
filters: {
4547
group_id: string;
4648
[key: string]: any;

0 commit comments

Comments
 (0)