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

Commit e1eaaf7

Browse files
author
tkostuch
committed
Merge branch 'develop' of https://github.com/DivanteLtd/vue-storefront-api into task/3948
2 parents 09b0d14 + a729c70 commit e1eaaf7

File tree

8 files changed

+71
-61
lines changed

8 files changed

+71
-61
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- The support for `SearchQuery` instead of the ElasticSearch DSL as for the input to `/api/catalog` - using `storefront-query-builder` package - @pkarw - https://github.com/DivanteLtd/vue-storefront/issues/2167
1414
- Create attribute service that allows to fetch attributes with specific options - used for products aggregates - @gibkigonzo (https://github.com/DivanteLtd/vue-storefront/pull/4001, https://github.com/DivanteLtd/mage2vuestorefront/pull/99)
1515

16+
### Fixed
17+
- add es7 support for map url module and fixed default index for es config - @gibkigonzo
18+
1619
## [1.11.0] - 2019.12.20
1720

1821
### Fixed
1922

2023
- Fixed some smaller issues with graphql so that it is now working again with the fronted - #350
2124
- Replaced the old `crop` function call which has been removed from Sharp image processor - @grimasod (#381)
25+
- Add fallback for `sourcePriceInclTax` and `finalPriceInclTax` in `magento1` platform - @cewald (#398)
2226

2327

2428
## [1.11.0-rc.1] - 2019.10.03
@@ -35,6 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3539
- Added support for tax calculation where the values from customer_tax_class_ids is used - @resubaka (#307)
3640
- The `db` context object - passed to every api endpoint now has two usefull methods: `getElasticClient` and `getRedisClient` for accesing the data stores - @pkarw (#328)
3741
- The `lib/utils` got two new methods `getStoreCode(req: Express.Request)` and `getStoreView(code: string)` for getting the current multistore context from `vue-storefront` frontend requests - @pkarw
42+
- Check message property instead of errorMessage in apiError function - @cdshotels-liborpansky (#378)
3843

3944
### Removed
4045
- The `scripts/seo.js` tool has been removed, the legacy `migrations` scripts have been removed, the unused legacy extensions (`gls-parcelshop-dk`, `postnord-parcelshop-dk`) - @pkarw (#342)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Then, please do change the `config/local.json` to start using the new Elastic AP
8585
```json
8686
"elasticsearch": {
8787
"host": "localhost",
88+
"index": "vue_storefront_catalog",
8889
"port": 9200,
8990
"protocol": "http",
9091
"min_score": 0.01,

config/default.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"elasticsearch": {
2121
"host": "localhost",
22+
"index": "vue_storefront_catalog",
2223
"port": 9200,
2324
"protocol": "http",
2425
"min_score": 0.01,

src/api/extensions/elastic-stock/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = ({
1212
const getStockList = (storeCode, skus) => {
1313
let storeView = getCurrentStoreView(storeCode)
1414
const esQuery = adjustQuery({
15-
index: storeView.elasticsearch.indexName, // current index name
15+
index: storeView.elasticsearch.index, // current index name
1616
type: 'product',
1717
_source_includes: ['stock'],
1818
body: bodybuilder().filter('terms', 'visibility', [2, 3, 4]).andFilter('term', 'status', 1).andFilter('terms', 'sku', skus).build()

src/api/url/map.ts

Lines changed: 57 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
import { Router } from 'express';
2-
import { apiStatus } from '../../lib/util';
3-
import { buildMultiEntityUrl } from '../../lib/elastic';
4-
import request from 'request';
2+
import { apiStatus, getCurrentStoreView, getCurrentStoreCode } from '../../lib/util';
3+
import { getClient as getElasticClient } from '../../lib/elastic'
54
import get from 'lodash/get';
65

6+
const adjustQueryForOldES = ({ config }) => {
7+
const searchedEntities = get(config, 'urlModule.map.searchedEntities', [])
8+
.map((entity) => ({ type: { value: entity } }))
9+
if (parseInt(config.elasticsearch.apiVersion) < 6) {
10+
return {
11+
filter: {
12+
bool: {
13+
should: searchedEntities
14+
}
15+
}
16+
}
17+
} else {
18+
return {}
19+
}
20+
}
21+
722
/**
823
* Builds ES query based on config
924
*/
1025
const buildQuery = ({ value, config }) => {
1126
const searchedFields = get(config, 'urlModule.map.searchedFields', [])
1227
.map((field) => ({ match_phrase: { [field]: { query: value } } }))
13-
const searchedEntities = get(config, 'urlModule.map.searchedEntities', [])
14-
.map((entity) => ({ type: { value: entity } }))
1528

1629
return {
1730
query: {
1831
bool: {
1932
filter: {
2033
bool: {
2134
should: searchedFields,
22-
filter: {
23-
bool: {
24-
should: searchedEntities
25-
}
26-
}
35+
...adjustQueryForOldES({ config })
2736
}
2837
}
2938
}
@@ -32,58 +41,62 @@ const buildQuery = ({ value, config }) => {
3241
}
3342
}
3443

44+
const buildIndex = ({ indexName, config }) => {
45+
return parseInt(config.elasticsearch.apiVersion) < 6
46+
? indexName
47+
: get(config, 'urlModule.map.searchedEntities', [])
48+
.map(entity => `${indexName}_${entity}`)
49+
}
50+
51+
const adjustResultType = ({ result, config, indexName }) => {
52+
if (parseInt(config.elasticsearch.apiVersion) < 6) return result
53+
54+
// extract type from index for es 7
55+
const type = result._index.replace(new RegExp(`^(${indexName}_)|(_[^_]*)$`, 'g'), '')
56+
result._type = type
57+
58+
return result
59+
}
60+
3561
/**
3662
* checks result equality because ES can return record even if searched value is not EXACLY what we want (check `match_phrase` in ES docs)
3763
*/
38-
const checkFieldValueEquality = ({ config, response, value }) => {
64+
const checkFieldValueEquality = ({ config, result, value }) => {
3965
const isEqualValue = get(config, 'urlModule.map.searchedFields', [])
40-
.find((field) => response._source[field] === value)
66+
.find((field) => result._source[field] === value)
4167

4268
return Boolean(isEqualValue)
4369
}
4470

4571
const map = ({ config }) => {
4672
const router = Router()
47-
router.post('/:index', (req, res) => {
73+
router.post('/', async (req, res) => {
4874
const { url, excludeFields, includeFields } = req.body
4975
if (!url) {
5076
return apiStatus(res, 'Missing url', 500);
5177
}
5278

53-
const esUrl = buildMultiEntityUrl({
54-
config,
55-
includeFields: includeFields ? includeFields.concat(get(config, 'urlModule.map.includeFields', [])) : [],
56-
excludeFields
57-
})
58-
const query = buildQuery({ value: url, config })
59-
60-
// Only pass auth if configured
61-
let auth = null;
62-
if (config.elasticsearch.user || config.elasticsearch.password) {
63-
auth = {
64-
user: config.elasticsearch.user,
65-
pass: config.elasticsearch.password
66-
}
79+
const indexName = getCurrentStoreView(getCurrentStoreCode(req)).elasticsearch.index
80+
const esQuery = {
81+
index: buildIndex({ indexName, config }), // current index name
82+
_source_includes: includeFields ? includeFields.concat(get(config, 'urlModule.map.includeFields', [])) : [],
83+
_source_excludes: excludeFields,
84+
body: buildQuery({ value: url, config })
6785
}
6886

69-
// make simple ES search
70-
request({
71-
uri: esUrl,
72-
method: 'POST',
73-
body: query,
74-
json: true,
75-
auth: auth
76-
}, (_err, _res, _resBody) => {
77-
if (_err) {
78-
console.log(_err)
79-
return apiStatus(res, new Error('ES search error'), 500);
80-
}
81-
const responseRecord = _resBody.hits.hits[0]
82-
if (responseRecord && checkFieldValueEquality({ config, response: responseRecord, value: req.body.url })) {
83-
return res.json(responseRecord)
87+
try {
88+
const esResponse = await getElasticClient(config).search(esQuery)
89+
const result = get(esResponse, 'body.hits.hits[0]', null)
90+
91+
if (result && checkFieldValueEquality({ config, result, value: req.body.url })) {
92+
return res.json(adjustResultType({ result, config, indexName }))
8493
}
85-
res.json()
86-
})
94+
95+
res.json(null)
96+
} catch (err) {
97+
console.error(err)
98+
return apiStatus(res, new Error('ES search error'), 500);
99+
}
87100
})
88101

89102
return router

src/lib/elastic.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,6 @@ function adjustBackendProxyUrl (req, indexName, entityType, config) {
5757
return url
5858
}
5959

60-
/**
61-
* similar to `adjustBackendProxyUrl`, builds multi-entity query url
62-
*/
63-
function buildMultiEntityUrl ({ config, includeFields = [], excludeFields = [] }) {
64-
let url = `${config.elasticsearch.host}:${config.elasticsearch.port}/_search?_source_include=${includeFields.join(',')}&_source_exclude=${excludeFields.join(',')}`
65-
if (!url.startsWith('http')) {
66-
url = config.elasticsearch.protocol + '://' + url
67-
}
68-
return url
69-
}
70-
7160
function adjustQuery (esQuery, entityType, config) {
7261
if (parseInt(config.elasticsearch.apiVersion) < 6) {
7362
esQuery.type = entityType
@@ -278,6 +267,5 @@ module.exports = {
278267
getClient,
279268
getHits,
280269
adjustIndexName,
281-
putMappings,
282-
buildMultiEntityUrl
270+
putMappings
283271
}

src/lib/util.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ export function apiStatus (res, result = 'OK', code = 200, meta = null) {
8484
* @param {json} [result='OK'] Text message or result information object
8585
*/
8686
export function apiError (res, errorObj, code = 500) {
87-
return apiStatus(res, errorObj.errorMessage ? errorObj.errorMessage : errorObj, errorObj.code ? errorObj.code : 500)
87+
const result = errorObj.message ? errorObj.message : (errorObj.errorMessage ? errorObj.errorMessage : errorObj);
88+
const resultCode = errorObj.code ? errorObj.code : code;
89+
return apiStatus(res, result, resultCode)
8890
}
8991

9092
export function encryptToken (textToken, secret) {

src/platform/magento1/tax.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class TaxProxy extends AbstractTaxProxy {
2323
if (store.elasticsearch.index === indexName) {
2424
taxRegion = store.tax.defaultRegion
2525
taxCountry = store.tax.defaultCountry
26-
sourcePriceInclTax = store.tax.sourcePriceIncludesTax
27-
finalPriceInclTax = store.tax.finalPriceIncludesTax
26+
sourcePriceInclTax = store.tax.sourcePriceIncludesTax || null
27+
finalPriceInclTax = store.tax.finalPriceIncludesTax || null
2828
this._storeConfigTax = store.tax
2929
break;
3030
}

0 commit comments

Comments
 (0)