Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions api/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { apiStatus } from '../../../lib/util';
import { Router } from 'express';
import { getClient } from '../../../lib/elastic'

const createQuery = (index, term) => ({
index,
Expand All @@ -15,18 +16,28 @@ const createQuery = (index, term) => ({
}
})

module.exports = ({ config, db }) => {
const getHits = function (result) {
if (result.body) { // differences between ES5 andd ES7
return result.body.hits
} else {
return result.hits
}
}

module.exports = ({ config }) => {
const api = Router();
const db = getClient(config)
const index = config.elasticsearch.indices.join(',')

api.get('/categoryByPath', (req, res) => {
const { path: url_path } = req.query;
db.search(createQuery(index, { url_path })).then(result => {
if (result.hits.total === 0) {
const hits = getHits(result)
if (hits.total === 0) {
apiStatus(res, 'No result')
return
}
const _id = result.hits.hits[0]._id
const _id = hits.hits[0]._id
db.search(createQuery(index, { _id })).then(result => {
const storeViews = Object.entries(config.storeViews)
.filter(([key, indice]) =>
Expand All @@ -37,7 +48,8 @@ module.exports = ({ config, db }) => {
key
}))
const url_paths = {}
result.hits.hits.forEach(hit => {
const hits = getHits(result)
hits.hits.forEach(hit => {
const storeView = storeViews.find(storeView => hit._index === storeView.elasticsearch.index || hit._index.startsWith(storeView.elasticsearch.index + '_'))
if (storeView) {
url_paths[storeView.key] = hit._source.url_path
Expand Down
1 change: 0 additions & 1 deletion components/SharedMixin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import store from '@vue-storefront/core/store'
import { isServer } from '@vue-storefront/core/helpers'
import { mapGetters } from 'vuex'
import { localizedRoute, removeStoreCodeFromRoute } from '@vue-storefront/core/lib/multistore'
Expand Down
34 changes: 14 additions & 20 deletions forceStorecode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,36 @@ export const forceStorecode = async (context, { url }) => {
if (storeViews.multistore && storeViews.forcePrefix && !storeCode) {
const redirect = url => {
const statusCode = storeViews.redirectStatusCode || 302
if (queryString) {
url = url + '?' + queryString
}
res.set('location', url)
res.status(statusCode).send()
}
const createUrl = (_storeCode: string) => `/${_storeCode}/${url}`
const createUrl = (_storeCode: string) => `/${_storeCode}${url}`

const cfCountry = req.headers.http_cf_ipcountry ? req.headers.http_cf_ipcountry.toLowerCase() : undefined
// cfCountry matches existing storeView code...
if (cfCountry && storeViews[cfCountry] && storeViews[cfCountry].disabled !== true) {
let newUrl = createUrl(cfCountry)
if (queryString) {
newUrl = newUrl + '?' + queryString
if (cfCountry) {
// cfCountry matches existing storeView code...
if (storeViews[cfCountry] && storeViews[cfCountry].disabled !== true) {
let newUrl = createUrl(cfCountry)
return redirect(newUrl)
}
return redirect(newUrl)
}
// ...otherwise check if cfCountry is mapped to a specific storeView...
if (cfCountry && storeViews.countryStoreViewMapping && storeViews.countryStoreViewMapping[cfCountry]){
let newUrl = createUrl(storeViews.countryStoreViewMapping[cfCountry])
if (queryString) {
newUrl = newUrl + '?' + queryString
// ...otherwise check if cfCountry is mapped to a specific storeView...
if (storeViews.countryStoreViewMapping && storeViews.countryStoreViewMapping[cfCountry]){
let newUrl = createUrl(storeViews.countryStoreViewMapping[cfCountry])
return redirect(newUrl)
}
return redirect(newUrl)
}
// ...otherwise check if a single fallback storeview is configured (ie our default storeView)
if (storeViews.fallbackStoreCode) {
let newUrl = createUrl(storeViews.fallbackStoreCode)
if (queryString) {
newUrl = newUrl + '?' + queryString
}
return redirect(newUrl)
}
// ...we have nothing to go on. Just pick any available storeview.
const lastResort: any = Object.values(storeViews)
.find((view: any) => view && view.storeCode && !view.disabled)
let newUrl = createUrl(lastResort.storeCode)
if (queryString) {
newUrl = newUrl + '?' + queryString
}
return redirect(newUrl)
}
}
Expand Down