-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.js
133 lines (125 loc) · 4.11 KB
/
get.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
var _ = require('slapdash')
var qubitApi = require('@qubit/qubit-api')
var getLocale = require('./getLocale')
var query = `
query ($trackingId: String!, $contextId: String!, $experienceId: Int, $items: Int!, $strategy: [RecommendationStrategyInput!], $seed: [RecommendationSeedInput!], $rules: [RecommendationRuleInput!], $locale: String) {
property(trackingId: $trackingId, locale: $locale) {
visitor(contextId: $contextId) {
productRecommendations(experienceId: $experienceId, items: $items, strategy: $strategy, seed: $seed, customRules: $rules) {
strategy
weight
product {
product_id: productId
currency
sku_code: skuCode
name
description
url
categories {
name
}
images {
url
}
stock
language
locale
views
views_ip: viewsIp
unit_sale_price: unitSalePrice
unit_price: unitPrice
additionalFields
}
}
}
}
}
`
module.exports = function getRecommendations (config, options) {
return function (settings) {
settings = settings || {}
var strategy = settings.strategy || config.strategy
var limit = settings.limit || config.limit
var seed = settings.seed || config.seed
var timeout = settings.timeout || config.timeout
var rules = settings.rules || config.rules
var url = settings.url || config.url
var trackingId = settings.trackingId || config.trackingId
var visitorId = settings.visitorId || config.visitorId
var defaultCurrency = settings.defaultCurrency || config.defaultCurrency
var defaultLanguage = settings.defaultLanguage || config.defaultLanguage
var staticLocale = settings.locale || config.locale
if (seed !== 'all') {
if (!_.isArray(seed)) {
seed = [seed]
}
// convert { category: 'x' } -> { type: 'c', id: 'x' }
// hopefully, the API will add support for the former
// at which point we can remove this mapping
seed = _.map(seed, function (s) {
if (_.isObject(s) && s.category) {
return { type: 'CATEGORY', value: s.category }
} else {
return { type: 'PRODUCT', value: s }
}
})
} else {
seed = null
}
return getLocale({
uv: options.uv,
defaultCurrency: defaultCurrency,
defaultLanguage: defaultLanguage,
staticLocale: staticLocale
}).then(function (locale) {
var variables = {
trackingId: trackingId,
contextId: visitorId,
experienceId: config.experienceId,
items: limit,
strategy: [{ name: strategy }],
seed: seed,
rules: rules,
locale: locale
}
return qubitApi.query(query, variables, {
timeout: timeout,
url: url,
package: '@qubit/recommendations'
}).then(function (result) {
if (result) {
var items = _.get(
result,
'data.property.visitor.productRecommendations'
)
if (items && items.length) {
// Convert back to native Recs API format to support legacy experiences
items = _.map(items, function (origItem) {
var item = _.assign({}, origItem)
item.id = item.product.product_id
item.details = item.product
if (item.product.images) {
item.product.images = _.pluck(item.product.images, 'url')
}
if (item.product.categories) {
item.product.categories = _.pluck(item.product.categories, 'name')
}
if (item.product.additionalFields) {
_.objectEach(item.product.additionalFields, function (v, k) {
item.details[k] = v
})
}
delete item.details.additionalFields
delete item.product
return item
})
return items
}
}
var error = new Error('No recommendations')
error.code = 'NO_RECOMMENDATIONS'
throw error
})
})
}
}