-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add eshop active/inactive facets builder
- Loading branch information
Niki
committed
Aug 24, 2021
1 parent
00acb51
commit 61f59a0
Showing
4 changed files
with
91 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import {AggregationSchema} from 'Builders/Aggregation/types'; | ||
import {Bool} from 'Builders/Bool'; | ||
|
||
interface IFacet { | ||
active: object; | ||
inactive: InactiveFacet; | ||
} | ||
|
||
type FacetType = keyof IFacet; | ||
|
||
type InactiveFacet = Record<'filter' | 'aggs', object>; | ||
|
||
export class EshopFacets { | ||
private _facets: IFacet = { | ||
active: {}, | ||
inactive: { | ||
aggs: {}, | ||
filter: {}, | ||
}, | ||
}; | ||
|
||
public setupInactiveFacets(filter: Bool): void { | ||
this._facets.inactive!.filter = filter; | ||
} | ||
|
||
public addInactiveFacet<Type extends keyof AggregationSchema>( | ||
facetType: FacetType, | ||
aggType: Type, | ||
name: string, | ||
d: AggregationSchema[Type], | ||
) { | ||
const {filter, subAgg, ...all} = d.params as { filter?: any; subAgg?: any }; | ||
let sub = {}; | ||
if (subAgg) { | ||
sub = { | ||
aggs: { | ||
...subAgg, | ||
}, | ||
}; | ||
} | ||
if (filter) { | ||
this._facets[facetType][name] = { | ||
filter, | ||
aggs: { | ||
[`${name}_filtered`]: { | ||
[aggType]: {...all, ...((d.opts as object) || {})}, | ||
...sub, | ||
}, | ||
}, | ||
}; | ||
return this; | ||
} | ||
|
||
this._facets[facetType][name] = { | ||
[aggType]: {...all, ...((d.opts as object) || {})}, | ||
...sub, | ||
}; | ||
return this; | ||
} | ||
|
||
public addCustom(facetType: FacetType, name: string, cutsomAgg: object) { | ||
this._facets[facetType][name] = cutsomAgg; | ||
} | ||
|
||
public build(): object { | ||
if (Object.keys(this._facets.inactive.aggs).length > 0 && Object.keys(this._facets.inactive.filter).length < 1) { | ||
throw new Error('Please setup inactive facets'); | ||
} | ||
return this._facets; | ||
} | ||
|
||
public isNotEmty(): boolean { | ||
return Object.keys({...this._facets.active, ...this._facets.inactive.aggs}).length > 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters