Skip to content

Commit

Permalink
Merge pull request #257 from smartprocure/GS-8094/consistent-api
Browse files Browse the repository at this point in the history
[GS-8094] feat: `step` type contract updated to use `min` and `max` instead of …
  • Loading branch information
stellarhoof authored Sep 20, 2024
2 parents 8e2a88b + 175e290 commit 87136a8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .changeset/seven-sheep-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'contexture-elasticsearch': major
'contexture-client': major
---

`step` type contract updated to use `min` and `max` instead of `range` so it can be consistent with `number` type
7 changes: 4 additions & 3 deletions packages/client/src/exampleTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,13 @@ export default F.stampKey('type', {
step: {
label: 'Number Slider',
reactors: {
range: 'all',
min: 'all',
max: 'all',
},
defaults: {
field: null,
range: [0, 1000],
steps: [0, 1000, 5000, 10000, 25000, 50000, 1000000],
min: 0,
max: null,
context: {
recordsCount: 0,
},
Expand Down
27 changes: 9 additions & 18 deletions packages/provider-elasticsearch/src/example-types/filters/step.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
import _ from 'lodash/fp.js'
import { pickSafeNumbers } from '../../utils/futil.js'

const getMinAndMax = (range) => {
const min = range[0]
const max = range[range.length - 1]
return { min, max }
}

let filter = ({ field, range }) => {
const { min, max } = getMinAndMax(range)
return {
range: { [field]: pickSafeNumbers({ gte: min, lte: max }) },
}
}
let filter = ({ field, min, max }) => ({
range: { [field]: pickSafeNumbers({ gte: min, lte: max }) },
})

let buildQuery = (field, range) => {
let buildQuery = (field, min, max) => {
return {
aggs: {
rangeFilter: {
filter: filter({ field, range }),
filter: filter({ field, min, max }),
},
},
}
}

let result = async (node, search) => {
let { field, range } = node
const result = await search(buildQuery(field, range))
let { field, min, max } = node
const result = await search(buildQuery(field, min, max))
const recordsCount = result.aggregations.rangeFilter.doc_count
return { recordsCount }
}

export default {
hasValue: ({ range }) => !_.isEmpty(range),
hasValue: ({ min }) => !_.isNil(min),
filter,
validContext: ({ range }) => !_.isEmpty(range),
validContext: ({ min }) => !_.isNil(min),
result,
buildQuery,
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ describe('step', () => {
const input = {
type: 'step',
field: 'test',
range: [0, 500],
min: 0,
max: 500,
steps: [0, 500, 1000],
}

Expand All @@ -25,7 +26,8 @@ describe('step', () => {
it('removes upper bound when upper bound is null', () => {
const inputWithNewRange = {
...input,
range: [0, null],
min: 0,
max: null,
}
let expectedValue = { range: { test: { gte: 0 } } }
expect(filter(inputWithNewRange)).toEqual(expectedValue)
Expand All @@ -39,7 +41,7 @@ describe('step', () => {
},
},
}
let output = buildQuery(input.field, input.range)
let output = buildQuery(input.field, input.min, input.max)
expect(output).toEqual(expectedDSL)
})
})

0 comments on commit 87136a8

Please sign in to comment.