Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 9465bdb

Browse files
authored
Merge pull request #168 from DivanteLtd/develop
Release 1.7
2 parents 66a2a5b + dc1f781 commit 9465bdb

File tree

14 files changed

+7081
-6858
lines changed

14 files changed

+7081
-6858
lines changed

docker/vue-storefront-api/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:8-alpine
1+
FROM node:10-alpine
22

33
ENV VS_ENV prod
44

migrations/.common.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ let queue = kue.createQueue(Object.assign(config.kue, { redis: config.redis }))
55

66
let es = require('elasticsearch')
77
const esConfig = {
8-
host: {
9-
host: config.elasticsearch.host,
10-
port: config.elasticsearch.port
11-
},
8+
host: config.elasticsearch.host + ':' + config.elasticsearch.port,
129
log: 'debug',
1310
apiVersion: '5.5',
1411
requestTimeout: 1000 * 60 * 60,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"winston": "^2.4.2"
8888
},
8989
"devDependencies": {
90+
"@babel/polyfill": "^7.2.5",
9091
"apollo-server-express": "^1.3.6",
9192
"babel-cli": "^6.26.0",
9293
"babel-core": "^6.26.3",

src/api/catalog.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function _updateQueryStringParameter(uri, key, value) {
1616
hash = uri.replace(/.*#/, '#');
1717
uri = uri.replace(/#.*/, '');
1818
}
19-
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
19+
var separator = uri.indexOf('?') !== -1 ? "&" : "?";
2020
return uri + separator + key + "=" + value + hash;
2121
}
2222
}
@@ -62,7 +62,11 @@ export default ({config, db}) => function (req, res, body) {
6262
}
6363

6464
// pass the request to elasticsearch
65-
let url = 'http://' + config.elasticsearch.host + ':' + config.elasticsearch.port + (req.query.request ? _updateQueryStringParameter(req.url, 'request', null) : req.url)
65+
let url = config.elasticsearch.host + ':' + config.elasticsearch.port + (req.query.request ? _updateQueryStringParameter(req.url, 'request', null) : req.url)
66+
67+
if (!url.startsWith('http')) {
68+
url = 'http://' + url
69+
}
6670

6771
// Check price tiers
6872
if (config.usePriceTiers) {
@@ -77,24 +81,31 @@ export default ({config, db}) => function (req, res, body) {
7781

7882
delete requestBody.groupToken
7983
}
84+
85+
let auth = null;
86+
87+
// Only pass auth if configured
88+
if(config.elasticsearch.user || config.elasticsearch.password) {
89+
auth = {
90+
user: config.elasticsearch.user,
91+
pass: config.elasticsearch.password
92+
};
93+
}
8094

8195
request({ // do the elasticsearch request
8296
uri: url,
8397
method: req.method,
8498
body: requestBody,
8599
json: true,
86-
auth: {
87-
user: config.elasticsearch.user,
88-
pass: config.elasticsearch.password
89-
},
100+
auth: auth,
90101
}, function (_err, _res, _resBody) { // TODO: add caching layer to speed up SSR? How to invalidate products (checksum on the response BEFORE processing it)
91102
if (_resBody && _resBody.hits && _resBody.hits.hits) { // we're signing up all objects returned to the client to be able to validate them when (for example order)
92103

93104
const factory = new ProcessorFactory(config)
94-
let resultProcessor = factory.getAdapter(entityType, indexName)
105+
let resultProcessor = factory.getAdapter(entityType, indexName, req, res)
95106

96107
if (!resultProcessor)
97-
resultProcessor = factory.getAdapter('default', indexName) // get the default processor
108+
resultProcessor = factory.getAdapter('default', indexName, req, res) // get the default processor
98109

99110
if (entityType === 'product') {
100111
resultProcessor.process(_resBody.hits.hits, groupId).then((result) => {

src/api/product.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ const hmac = jwa('HS256');
88
export default ({ config, db }) => {
99

1010
let productApi = Router();
11-
11+
1212
const _getProxy = (req) => {
1313
const platform = config.platform
1414
const factory = new PlatformFactory(config, req)
1515
return factory.getAdapter(platform,'product')
1616
};
1717

18-
/**
18+
/**
1919
* GET get products info
2020
*/
2121
productApi.get('/list', (req, res) => {
2222

2323
const productProxy = _getProxy(req)
24-
24+
2525
if (!req.query.skus)
2626
return apiStatus(res, 'skus parameter is required', 500);
2727

@@ -32,33 +32,33 @@ export default ({ config, db }) => {
3232
})
3333
})
3434

35-
/**
35+
/**
3636
* GET get products info
3737
*/
3838
productApi.get('/render-list', (req, res) => {
3939

4040
const productProxy = _getProxy(req)
41-
41+
4242
if (!req.query.skus)
4343
return apiStatus(res, 'skus parameter is required', 500);
4444

45-
productProxy.renderList(req.query.skus.split(','), req.query.currencyCode, req.query.storeId && parseInt(req.query.storeId) > 0 || 1).then((result) => {
45+
productProxy.renderList(req.query.skus.split(','), req.query.currencyCode, (req.query.storeId && parseInt(req.query.storeId) > 0) ? req.query.storeId : 1).then((result) => {
4646
result.items = result.items.map((item) => {
4747
let sgnObj = item
4848
if (config.tax.calculateServerSide === true) {
4949
sgnObj = { priceInclTax: item.price_info.final_price }
5050
} else {
5151
sgnObj = { price: item.price_info.extension_attributes.tax_adjustments.final_price }
5252
}
53-
53+
5454
item.sgn = hmac.sign(sgnSrc(sgnObj, item), config.objHashSecret); // for products we sign off only price and id becase only such data is getting back with orders
5555
return item
5656
})
5757
apiStatus(res, result, 200);
5858
}).catch(err=> {
5959
apiStatus(res, err, 500);
6060
})
61-
})
61+
})
6262

6363
return productApi
6464
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '@babel/polyfill/noConflict';
12
import http from 'http';
23
import express from 'express';
34
import cors from 'cors';

src/platform/magento2/tax.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ class TaxProxy extends AbstractTaxProxy {
6262

6363
if (this._config.tax.calculateServerSide) {
6464
const esConfig = { // as we're runing tax calculation and other data, we need a ES indexer
65-
host: {
66-
host: this._config.elasticsearch.host,
67-
port: this._config.elasticsearch.port
68-
},
65+
host: this._config.elasticsearch.host + ':' + this._config.elasticsearch.port,
6966
log: 'debug',
7067
apiVersion: '5.5',
7168
requestTimeout: 5000

src/processor/default.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ const jwa = require('jwa');
22
const hmac = jwa('HS256');
33

44
class HmacProcessor {
5-
constructor(config, entityType, indexName){
5+
constructor(config, entityType, indexName, req, res){
66
this._config = config
77
this._entityType = entityType
88
this._indexName = indexName
9+
this._req = req
10+
this._res = res
911
}
1012

1113
process (items) {
@@ -15,7 +17,9 @@ class HmacProcessor {
1517

1618
return new Promise((resolve, reject) => {
1719
const rs = items.map((item) => {
18-
item._source.sgn = hmac.sign(item._source, this._config.objHashSecret); // for products we sign off only price and id becase only such data is getting back with orders
20+
if (this._req.query._source_exclude && this._req.query._source_exclude.indexOf('sgn') < 0) {
21+
item._source.sgn = hmac.sign(item._source, this._config.objHashSecret); // for products we sign off only price and id becase only such data is getting back with orders
22+
}
1923
return item
2024
})
2125

src/processor/factory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ProcessorFactory {
1515
this.config = app_config;
1616
}
1717

18-
getAdapter(entityType, indexName){
18+
getAdapter(entityType, indexName, req, res){
1919

2020
const moduleName = './' + entityType
2121

@@ -29,7 +29,7 @@ class ProcessorFactory {
2929
console.log('No additional data adapter for ' + entityType)
3030
return null
3131
} else {
32-
let adapter_instance = new adapter_class(this.config, entityType, indexName);
32+
let adapter_instance = new adapter_class(this.config, entityType, indexName, req, res);
3333

3434
if ((typeof adapter_instance.isValidFor == 'function') && !adapter_instance.isValidFor(entityType))
3535
throw new Error('Not valid adapter class or adapter is not valid for ' + entityType);

src/processor/product.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const hmac = jwa('HS256');
44
import { sgnSrc } from '../lib/util'
55

66
class ProductProcessor {
7-
constructor(config, entityType, indexName){
7+
constructor(config, entityType, indexName, req, res){
88
this._config = config
99
this._entityType = entityType
1010
this._indexName = indexName
11+
this._req = req
12+
this._res = res
1113
}
1214

1315
process (items, groupId = null) {
@@ -28,30 +30,34 @@ class ProductProcessor {
2830
throw Error('error with resultset for processor chaining')
2931
}
3032

31-
const rs = resultSet[0].map(((item) => {
32-
if (!item._source)
33+
if (this._req.query._source_exclude && this._req.query._source_exclude.indexOf('sgn') < 0) {
34+
const rs = resultSet[0].map(((item) => {
35+
if (!item._source)
36+
return item
37+
38+
const config = this._config
39+
let sgnObj = (config.tax.calculateServerSide === true) ? { priceInclTax: item._source.priceInclTax } : { price: item._source.price }
40+
item._source.sgn = hmac.sign(sgnSrc(sgnObj, item), config.objHashSecret); // for products we sign off only price and id becase only such data is getting back with orders
41+
42+
if (item._source.configurable_children) {
43+
item._source.configurable_children = item._source.configurable_children.map((subItem) => {
44+
if (subItem) {
45+
let sgnObj = (config.tax.calculateServerSide === true) ? { priceInclTax: subItem.priceInclTax } : { price: subItem.price }
46+
subItem.sgn = hmac.sign(sgnSrc(sgnObj, subItem), config.objHashSecret);
47+
}
48+
49+
return subItem
50+
})
51+
}
52+
3353
return item
54+
}).bind(this))
3455

35-
const config = this._config
36-
let sgnObj = (config.tax.calculateServerSide === true) ? { priceInclTax: item._source.priceInclTax } : { price: item._source.price }
37-
item._source.sgn = hmac.sign(sgnSrc(sgnObj, item), config.objHashSecret); // for products we sign off only price and id becase only such data is getting back with orders
38-
39-
if (item._source.configurable_children) {
40-
item._source.configurable_children = item._source.configurable_children.map((subItem) => {
41-
if (subItem) {
42-
let sgnObj = (config.tax.calculateServerSide === true) ? { priceInclTax: subItem.priceInclTax } : { price: subItem.price }
43-
subItem.sgn = hmac.sign(sgnSrc(sgnObj, subItem), config.objHashSecret);
44-
}
45-
46-
return subItem
47-
})
48-
}
49-
50-
return item
51-
}).bind(this))
52-
53-
// return first resultSet
54-
return rs
56+
// return first resultSet
57+
return rs
58+
} else {
59+
return resultSet[0]
60+
}
5561
}).bind(this))
5662
}
5763
}

0 commit comments

Comments
 (0)