Skip to content
This repository has been archived by the owner on Oct 16, 2019. It is now read-only.

Commit

Permalink
fix(Reco): Send empty tracking string instead of undefined
Browse files Browse the repository at this point in the history
Send tracking_string: '' with the first call getRecomendation()

Fix #95
  • Loading branch information
Gleb Galkin committed Jun 21, 2017
1 parent e5d9661 commit 7a233da
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/index.node.js

Large diffs are not rendered by default.

72 changes: 72 additions & 0 deletions src/api/__tests__/utils_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import test from 'ava';
import { mergePlainOptsWithDefault } from '../utils';


test('Should throw an error when no options or no default opts', t => {
t.is(t.throws(() => {
mergePlainOptsWithDefault(null, null);
}, Error).message, 'options and default options values are mandatory');

t.is(t.throws(() => {
mergePlainOptsWithDefault({}, null);
}, Error).message, 'options and default options values are mandatory');

t.is(t.throws(() => {
mergePlainOptsWithDefault(null, {});
}, Error).message, 'options and default options values are mandatory');
});

test('Should merge options without path', t => {
const { reco_id, tracking_string } = mergePlainOptsWithDefault(
{ reco_id: '123', tracking_string: 'TRACK' },
{ reco_id: '', tracking_string: '' });

t.is(reco_id, '123');
t.is(tracking_string, 'TRACK');
});

test('Should merge options without path and get one value from defaults', t => {
const { reco_id, tracking_string } = mergePlainOptsWithDefault(
{ reco_id: '123' },
{ reco_id: '', tracking_string: '' });

t.is(reco_id, '123');
t.is(tracking_string, '');
});

test('Should merge options with path', t => {
const { cdn, resolutions } = mergePlainOptsWithDefault(
{
media: {
cdn: 'cdn2',
resolutions: ['large', 'small']
}
},
{
cdn: 'cdn1',
resolutions: ['small']
},
'media');

t.is(cdn, 'cdn2');
t.deepEqual(resolutions, ['large', 'small']);
});

test('Should merge options with path and get one value from defaults', t => {
const { cdn, resolutions } = mergePlainOptsWithDefault(
{
media: {
cdn: 'cdn2'
}
},
{
cdn: 'cdn1',
resolutions: ['small']
},
'media');

t.is(cdn, 'cdn2');
t.deepEqual(resolutions, ['small']);
});


17 changes: 10 additions & 7 deletions src/api/atlas_sdk_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../models/guest_checkout_models';
import { RecommendedArticles } from '../models/recommendation_models';
import mediaTransform from './transforms/media_trasform';

import { mergePlainOptsWithDefault } from './utils';
const successCode = 200;
const badRequestCode = 399;

Expand Down Expand Up @@ -324,14 +324,17 @@ class AtlasSDKClient {
* }
* });
*/
getRecommendations(sku, options = {
reco_id: '',
tracking_string: ''
}) {
getRecommendations(sku, options = {}) {
const { reco_id: recoId, tracking_string: trackingString } = mergePlainOptsWithDefault(options, {
reco_id: '',
tracking_string: ''
});

const config = this.config;
const catalogUrl = config.catalogApi.url;
const type = config.recommendations[0].type;
const url = `${catalogUrl}/articles/${sku}/recommendations/?client_id=${config.clientId}&anon_id=${options.reco_id}`; /* eslint max-len: 0 */
const url = `${catalogUrl}/articles/${sku}/recommendations/?client_id=${config.clientId}&anon_id=${recoId}`; /* eslint max-len: 0 */

const GetRecommendationsEndpoint = {
url: url,
method: 'GET',
Expand All @@ -343,7 +346,7 @@ class AtlasSDKClient {
'X-Reco-Location': config.recommendations[0].location,
'X-Reco-Type': type,
'X-Channel': config.recommendations[0].channel,
'X-Tracking-String': options.tracking_string
'X-Tracking-String': trackingString
},
transform: (response) => {
const result = [];
Expand Down
22 changes: 9 additions & 13 deletions src/api/transforms/media_trasform.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { mergePlainOptsWithDefault } from '../utils';

const CDNS = ['mosaic01', 'mosaic02'];
const AVAILABLE_RESOLUTIONS = {
thumbnail: 'pdp-thumb',
Expand All @@ -11,6 +13,10 @@ const AVAILABLE_RESOLUTIONS = {
large_hd: 'large_hd'
};
const DEFAULT_RESOLUTIONS = ['thumbnail', 'medium', 'large'];
const DEFAULT_OPTS = {
cdn: CDNS[0],
image_resolutions: DEFAULT_RESOLUTIONS
};

const isImage = (type) => ['IMAGE', 'IMAGE_360', 'IMAGE_PARTNER'].indexOf(type) !== -1;
const isVideo = (type) => ['VIDEO_THUMBNAIL', 'VIDEO_HD', 'VIDEO_LOW'].indexOf(type) !== -1;
Expand All @@ -37,9 +43,7 @@ const createVideoItem = (item, cdn) => {
};
};

const getImageResolutions = (optsImageResolutions) => {
const imageResolutions = optsImageResolutions || DEFAULT_RESOLUTIONS;

const filterImageResolutions = (imageResolutions) => {
return Object.keys(AVAILABLE_RESOLUTIONS)
.filter(key => imageResolutions.indexOf(key) !== -1) /* eslint no-magic-numbers: [0] */
.reduce((finalResolutions, key) => {
Expand All @@ -49,16 +53,8 @@ const getImageResolutions = (optsImageResolutions) => {
};

const getCdnAndResolutions = (options) => {
const defaultOptions = {
cdn: CDNS[0],
optsImageResolutions: DEFAULT_RESOLUTIONS
};

let { cdn, image_resolutions: imageResolutions } = (options && options.media) || defaultOptions;

cdn = cdn || defaultOptions.cdn;

imageResolutions = getImageResolutions(imageResolutions);
const { cdn, image_resolutions } = mergePlainOptsWithDefault(options, DEFAULT_OPTS, 'media');
const imageResolutions = filterImageResolutions(image_resolutions);

return { cdn, imageResolutions };
};
Expand Down
17 changes: 17 additions & 0 deletions src/api/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mergePlainOptsWithDefault = (opts = {}, defaultPlainOpts = {}, pathToPlain = null) => {
if (!opts || !defaultPlainOpts) {
throw new Error('options and default options values are mandatory');
}

const plainOpts = pathToPlain ? opts[pathToPlain] : opts;

if (!plainOpts) {
return defaultPlainOpts;
}

return Object.assign({}, defaultPlainOpts, plainOpts);
};

export {
mergePlainOptsWithDefault
};

0 comments on commit 7a233da

Please sign in to comment.