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

Commit

Permalink
Merge pull request #420 from DivanteLtd/hotfix/v1.11.1
Browse files Browse the repository at this point in the history
Hotfix/v1.11.1
  • Loading branch information
patzick committed Mar 17, 2020
2 parents 875ed27 + 24f95a8 commit 093d393
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 56 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.11.1] - 2020.03.17

### Added
- Add save address on place order - @lucasqm (#394)
- Add ElasticSearch client support for HTTP authentication - @cewald (#397)
- Add error handling for catalog and add header 'X-VS-Cache-Tags' to response - @gibkigonzo

### Fixed
- Add fallback for `sourcePriceInclTax` and `finalPriceInclTax` in `magento1` platform - @cewald (#398)
- Add default ES index to config and update `getStockList` - @gibkigonzo (#405)
- Makes elastic-stock extension compatible with both ES5 and ES7. Allows for stock fetch of configurable children that is set as "Not Visible Individually" - @didkan (#410)


## [1.11.0] - 2019.12.20

### Fixed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Then, please do change the `config/local.json` to start using the new Elastic AP
```json
"elasticsearch": {
"host": "localhost",
"index": "vue_storefront_catalog",
"port": 9200,
"protocol": "http",
"min_score": 0.01,
Expand Down
2 changes: 2 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
},
"elasticsearch": {
"host": "localhost",
"index": "vue_storefront_catalog",
"port": 9200,
"protocol": "http",
"requestTimeout": 5000,
"min_score": 0.01,
"indices": [
"vue_storefront_catalog",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-storefront-api",
"version": "1.11.0",
"version": "1.11.1",
"private": true,
"description": "vue-storefront API and data services",
"main": "dist",
Expand Down
71 changes: 35 additions & 36 deletions src/api/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ProcessorFactory from '../processor/factory';
import { adjustBackendProxyUrl } from '../lib/elastic'
import cache from '../lib/cache-instance'
import { sha3_224 } from 'js-sha3'
import { apiError } from '../lib/util';

function _cacheStorageHandler (config, result, hash, tags) {
if (config.server.useOutputCache && cache) {
Expand Down Expand Up @@ -86,43 +87,41 @@ export default ({config, db}) => function (req, res, body) {
body: requestBody,
json: true,
auth: auth
}, (_err, _res, _resBody) => { // TODO: add caching layer to speed up SSR? How to invalidate products (checksum on the response BEFORE processing it)
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)
const factory = new ProcessorFactory(config)
const tagsArray = []
if (config.server.useOutputCache && cache) {
const tagPrefix = entityType[0].toUpperCase() // first letter of entity name: P, T, A ...
tagsArray.push(entityType)
_resBody.hits.hits.map(item => {
if (item._source.id) { // has common identifier
tagsArray.push(`${tagPrefix}${item._source.id}`)
}
})
}

let resultProcessor = factory.getAdapter(entityType, indexName, req, res)

if (!resultProcessor) { resultProcessor = factory.getAdapter('default', indexName, req, res) } // get the default processor

if (entityType === 'product') {
resultProcessor.process(_resBody.hits.hits, groupId).then((result) => {
_resBody.hits.hits = result
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
res.json(_resBody);
}).catch((err) => {
console.error(err)
})
} else {
resultProcessor.process(_resBody.hits.hits).then((result) => {
_resBody.hits.hits = result
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
res.json(_resBody);
}).catch((err) => {
console.error(err)
})
}, async (_err, _res, _resBody) => { // TODO: add caching layer to speed up SSR? How to invalidate products (checksum on the response BEFORE processing it)
if (_err || _resBody.error) {
apiError(res, _err || _resBody.error);
return
}
try {
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)
const factory = new ProcessorFactory(config)
const tagsArray = []
if (config.server.useOutputCache && cache) {
const tagPrefix = entityType[0].toUpperCase() // first letter of entity name: P, T, A ...
tagsArray.push(entityType)
_resBody.hits.hits.map(item => {
if (item._source.id) { // has common identifier
tagsArray.push(`${tagPrefix}${item._source.id}`)
}
})
const cacheTags = tagsArray.join(' ')
res.setHeader('X-VS-Cache-Tags', cacheTags)
}

let resultProcessor = factory.getAdapter(entityType, indexName, req, res)

if (!resultProcessor) { resultProcessor = factory.getAdapter('default', indexName, req, res) } // get the default processor

const productGroupId = entityType === 'product' ? groupId : undefined
const result = await resultProcessor.process(_resBody.hits.hits, productGroupId)
_resBody.hits.hits = result
_cacheStorageHandler(config, _resBody, reqHash, tagsArray)
res.json(_resBody);
} else { // no cache storage if no results from Elastic
res.json(_resBody);
}
} else { // no cache storage if no results from Elastic
res.json(_resBody);
} catch (err) {
apiError(res, err);
}
});
}
Expand Down
9 changes: 4 additions & 5 deletions src/api/extensions/elastic-stock/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { apiStatus, getCurrentStoreView, getCurrentStoreCode } from '../../../lib/util';
import { getClient as getElasticClient, adjustQuery } from '../../../lib/elastic'
import { getClient as getElasticClient, adjustQuery, getHits } from '../../../lib/elastic'
import { Router } from 'express';
const bodybuilder = require('bodybuilder')

Expand All @@ -12,14 +12,13 @@ module.exports = ({
const getStockList = (storeCode, skus) => {
let storeView = getCurrentStoreView(storeCode)
const esQuery = adjustQuery({
index: storeView.elasticsearch.indexName, // current index name
index: storeView.elasticsearch.index, // current index name
type: 'product',
_source_includes: ['stock'],
body: bodybuilder().filter('terms', 'visibility', [2, 3, 4]).andFilter('term', 'status', 1).andFilter('terms', 'sku', skus).build()
body: bodybuilder().filter('term', 'status', 1).andFilter('terms', 'sku', skus).build()
}, 'product', config)
return getElasticClient(config).search(esQuery).then((products) => { // we're always trying to populate cache - when online
console.log(products)
return products.hits.hits.map(el => { return el._source.stock })
return getHits(products).map(el => { return el._source.stock })
}).catch(err => {
console.error(err)
})
Expand Down
15 changes: 8 additions & 7 deletions src/lib/elastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ function getHits (result) {
}

function getClient (config) {
const esConfig = { // as we're runing tax calculation and other data, we need a ES indexer
node: `${config.elasticsearch.protocol}://${config.elasticsearch.host}:${config.elasticsearch.port}`,
apiVersion: config.elasticsearch.apiVersion,
requestTimeout: 5000
}
let { host, port, protocol, apiVersion, requestTimeout } = config.elasticsearch
const node = `${protocol}://${host}:${port}`

let auth
if (config.elasticsearch.user) {
esConfig.auth = config.elasticsearch.user + ':' + config.elasticsearch.password
const { user, password } = config.elasticsearch
auth = { username: user, password }
}
return new es.Client(esConfig)

return new es.Client({ node, auth, apiVersion, requestTimeout })
}

function putAlias (db, originalName, aliasName, next) {
Expand Down
6 changes: 4 additions & 2 deletions src/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ export function apiStatus (res, result = 'OK', code = 200, meta = null) {
* @param {number} [code=200] Status code to send on success
* @param {json} [result='OK'] Text message or result information object
*/
export function apiError (res, errorObj, code = 500) {
return apiStatus(res, errorObj.errorMessage ? errorObj.errorMessage : errorObj, errorObj.code ? errorObj.code : 500)
export function apiError (res, errorObj) {
const errorCode = errorObj.code || errorObj.status || 500
const errorMessage = errorObj.errorMessage || errorObj
return apiStatus(res, errorMessage, errorCode)
}

export function encryptToken (textToken, secret) {
Expand Down
6 changes: 6 additions & 0 deletions src/models/order.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ exports.default = {
},
sameAsBilling: {
type: 'number'
},
save_address: {
type: 'number'
}
}
},
Expand Down Expand Up @@ -203,6 +206,9 @@ exports.default = {
},
sameAsBilling: {
type: 'number'
},
save_address: {
type: 'number'
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/magento1/tax.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class TaxProxy extends AbstractTaxProxy {
if (store.elasticsearch.index === indexName) {
taxRegion = store.tax.defaultRegion
taxCountry = store.tax.defaultCountry
sourcePriceInclTax = store.tax.sourcePriceIncludesTax
finalPriceInclTax = store.tax.finalPriceIncludesTax
sourcePriceInclTax = store.tax.sourcePriceIncludesTax || null
finalPriceInclTax = store.tax.finalPriceIncludesTax || null
this._storeConfigTax = store.tax
break;
}
Expand Down
9 changes: 6 additions & 3 deletions src/platform/magento2/o2m.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ function processSingleOrder (orderData, config, job, done, logger = console) {
'regionCode': mappedBillingRegion.regionCode,
'regionId': mappedBillingRegion.regionId,
'company': billingAddr.company,
'vatId': billingAddr.vat_id
'vatId': billingAddr.vat_id,
'save_in_address_book': billingAddr.save_address
}
}

Expand All @@ -177,7 +178,8 @@ function processSingleOrder (orderData, config, job, done, logger = console) {
'regionCode': mappedBillingRegion.regionCode,
'region': billingAddr.region,
'company': billingAddr.company,
'vatId': billingAddr.vat_id
'vatId': billingAddr.vat_id,
'save_in_address_book': billingAddr.save_address
},
'shippingMethodCode': orderData.addressInformation.shipping_method_code,
'shippingCarrierCode': orderData.addressInformation.shipping_carrier_code,
Expand All @@ -198,7 +200,8 @@ function processSingleOrder (orderData, config, job, done, logger = console) {
'regionId': mappedShippingRegion.regionId,
'regionCode': mappedShippingRegion.regionCode,
'region': shippingAddr.region,
'company': shippingAddr.company
'company': shippingAddr.company,
'save_in_address_book': shippingAddr.save_address
}
} else {
shippingAddressInfo['addressInformation']['shippingAddress'] = shippingAddressInfo['addressInformation']['billingAddress']
Expand Down

0 comments on commit 093d393

Please sign in to comment.