feat: add support for new availability filter

This commit is contained in:
Wieland Schöbl
2021-05-03 17:34:36 +02:00
parent 334f5a7507
commit 47f3232f15
6 changed files with 136 additions and 171 deletions

View File

@@ -22,7 +22,7 @@ import {
SCThingType
} from '@openstapps/core';
import {expect} from 'chai';
import {ESDateRangeFilter} from '../../../src/storage/elasticsearch/common';
import {ESDateRangeFilter, ESRangeFilter} from '../../../src/storage/elasticsearch/common';
import {ESNumericRangeFilter} from '../../../src/storage/elasticsearch/common';
import {configFile} from '../../../src/common';
import {
@@ -197,14 +197,6 @@ describe('Query', function () {
value: SCThingType.Dish
}
},
availability: {
type: 'availability',
arguments: {
time: '2017-01-30T12:05:00.000Z',
fromField: 'offers.availabilityStarts',
toField: 'offers.availabilityEnds'
}
},
distance: {
type: 'distance',
arguments: {
@@ -228,8 +220,7 @@ describe('Query', function () {
{
type: 'availability',
arguments: {
fromField: 'offers.availabilityStarts',
toField: 'offers.availabilityEnds'
field: 'offers.availabilityRange'
}
}
]
@@ -254,10 +245,10 @@ describe('Query', function () {
const expectedFilter: ESNumericRangeFilter = {
range: {
price: {
relation: undefined,
}
}
}
};
const rawFilter: SCSearchNumericRangeFilter = {
type: 'numeric range',
@@ -274,11 +265,11 @@ describe('Query', function () {
rawFilter.arguments.bounds[location] = {
mode: bound as 'inclusive' | 'exclusive',
limit: out,
}
};
// @ts-ignore implicit any
expectedFilter.range.price[`${location === 'lowerBound' ? 'g' : 'l'}${bound === 'inclusive' ? 'te' : 't'}`] = out;
}
}
};
setBound('upperBound', upperMode);
setBound('lowerBound', lowerMode);
@@ -291,7 +282,7 @@ describe('Query', function () {
const exclusiveExists = typeof filter.range.price[`${bound}te`] !== 'undefined';
// only one should exist at the same time
expect(inclusiveExists && exclusiveExists).to.be.false
expect(inclusiveExists && exclusiveExists).to.be.false;
}
}
}
@@ -305,15 +296,17 @@ describe('Query', function () {
price: {
format: 'thisIsADummyFormat',
time_zone: 'thisIsADummyTimeZone',
relation: 'testRelation' as any,
}
}
}
};
const rawFilter: SCSearchDateRangeFilter = {
type: 'date range',
arguments: {
bounds: {},
field: 'price',
relation: 'testRelation' as any,
format: 'thisIsADummyFormat',
timeZone: 'thisIsADummyTimeZone',
}
@@ -326,11 +319,11 @@ describe('Query', function () {
rawFilter.arguments.bounds[location] = {
mode: bound as 'inclusive' | 'exclusive',
limit: out,
}
};
// @ts-ignore implicit any
expectedFilter.range.price[`${location === 'lowerBound' ? 'g' : 'l'}${bound === 'inclusive' ? 'te' : 't'}`] = out;
}
}
};
setBound('upperBound', upperMode);
setBound('lowerBound', lowerMode);
@@ -343,58 +336,96 @@ describe('Query', function () {
const exclusiveExists = typeof filter.range.price[`${bound}te`] !== 'undefined';
// only one should exist at the same time
expect(inclusiveExists && exclusiveExists).to.be.false
expect(inclusiveExists && exclusiveExists).to.be.false;
}
}
}
});
it('should build availability filter', function () {
const filter = buildFilter(searchFilters.availability);
const expectedFilter: ESBooleanFilter<any> = {
bool: {
should: [
{
bool: {
must: [
{
range: {
'offers.availabilityStarts': {
lte: '2017-01-30T12:05:00.000Z'
}
}
},
{
range: {
'offers.availabilityEnds': {
gte: '2017-01-30T12:05:00.000Z'
}
}
}
]
it('should build availability filters', function () {
it('should copy scope', function () {
for (const scope of ['a', 'b']) {
const filter = buildFilter({
type: 'availability',
arguments: {
time: 'test',
scope: scope as any,
field: 'offers.availabilityRange',
},
});
const expectedFilter: ESRangeFilter = {
range: {
'offers.availabilityRange': {
gte: `test||/${scope}`,
lt: `test||+1${scope}/${scope}`,
}
},
{
bool: {
must_not: [
{
exists: {
field: 'offers.availabilityStarts'
}
},
{
exists: {
field: 'offers.availabilityEnds'
}
}
]
}
}
]
};
expect(filter).to.be.eql(expectedFilter);
}
};
});
expect(filter).to.be.eql(expectedFilter);
it('should default to second scope', function() {
const filter = buildFilter({
type: 'availability',
arguments: {
time: 'test',
field: 'offers.availabilityRange',
},
});
const expectedFilter: ESRangeFilter = {
range: {
'offers.availabilityRange': {
gte: 'test||/s',
lt: 'test||+1s/s',
}
},
};
expect(filter).to.be.eql(expectedFilter);
})
it('should add || to dates', function () {
const filter = buildFilter({
type: 'availability',
arguments: {
time: 'test',
scope: 'd',
field: 'offers.availabilityRange',
},
});
const expectedFilter: ESRangeFilter = {
range: {
'offers.availabilityRange': {
gte: `test||/d`,
lt: `test||+1d/d`,
}
},
};
expect(filter).to.be.eql(expectedFilter);
});
it('should default to now and not add ||', function () {
const filter = buildFilter({
type: 'availability',
arguments: {
scope: 'd',
field: 'offers.availabilityRange',
},
});
const expectedFilter: ESRangeFilter = {
range: {
'offers.availabilityRange': {
gte: `now/d`,
lt: `now+1d/d`,
}
},
};
expect(filter).to.be.eql(expectedFilter);
});
});
it('should build distance filter', function () {
@@ -424,45 +455,12 @@ describe('Query', function () {
}
},
{
bool: {
should: [
{
bool: {
must: [
{
range: {
'offers.availabilityStarts': {
lte: 'now'
}
}
},
{
range: {
'offers.availabilityEnds': {
gte: 'now'
}
}
}
]
}
},
{
bool: {
must_not: [
{
exists: {
field: 'offers.availabilityStarts'
}
},
{
exists: {
field: 'offers.availabilityEnds'
}
}
]
}
}
]
range: {
'offers.availabilityRange': {
gte: 'now/s',
lt: 'now+1s/s',
relation: 'intersects',
}
}
}
],
@@ -545,7 +543,7 @@ describe('Query', function () {
it('should build generic sort', function () {
expect(sorts[1]).to.be.eql(expectedSorts.generic);
})
});
it('should build distance sort', function () {
expect(sorts[2]).to.be.eql(expectedSorts.distance);