Skip to content

Commit

Permalink
fix(set-sort-order): ensure in stock sort check for common sort
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Jun 1, 2020
1 parent a1aaa60 commit fa1b8da
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/methods/set-sort-order.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import query from './../lib/dsl'

const inStockSort = {
_script: {
type: 'number',
script: {
lang: 'painless',
source: "doc['quantity'].value > 0 ? 1 : 0"
},
order: 'desc'
}
}

export default (self, enumOrder) => {
// get default sort array
const sort = query.sort.slice()
Expand All @@ -8,7 +19,7 @@ export default (self, enumOrder) => {
switch (enumOrder) {
case 'sales':
// sort by sales after relevance
sort.splice(2, 0, {
sort.splice(2, 0, inStockSort, {
sales: {
order: 'desc'
}
Expand All @@ -17,7 +28,7 @@ export default (self, enumOrder) => {

case 'news':
// sort by creation date after relevance
sort.splice(2, 0, {
sort.splice(2, 0, inStockSort, {
created_at: {
order: 'desc'
}
Expand All @@ -27,16 +38,7 @@ export default (self, enumOrder) => {
case 'lowest_price':
case 'highest_price':
// sort by price after stock and before relevance
sort.splice(1, 0, {
_script: {
type: 'number',
script: {
lang: 'painless',
source: "doc['quantity'].value > 0 ? 1 : 0"
},
order: 'desc'
}
}, {
sort.splice(1, 0, inStockSort, {
price: {
order: enumOrder === 'lowest_price' ? 'asc' : 'desc'
}
Expand Down

0 comments on commit fa1b8da

Please sign in to comment.