Skip to content

Commit 9a3ce63

Browse files
psaizntarocco
authored andcommitted
InvenioSerializer: Concatenate nested filters with '::'
1 parent dcca02c commit 9a3ce63

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/lib/api/contrib/invenio/InvenioRequestSerializer.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ export class InvenioRequestSerializer {
2525
`Filter value "${filter}" in query state must be an array of 2 or 3 elements`
2626
);
2727
}
28-
const aggName = filter[0];
29-
const fieldValue = filter[1];
28+
const hasChild = filter.length === 3;
29+
var aggName = filter[0];
30+
var fieldValue = filter[1];
31+
if (hasChild) {
32+
if (!Array.isArray(filter[2])) {
33+
throw new Error(`Filter value "${filter[2]}" in query state must be an array.`);
34+
}
35+
fieldValue = fieldValue + "::" + filter[2][1];
36+
}
3037
if (aggName in filterUrlParams) {
3138
filterUrlParams[aggName].push(fieldValue);
3239
} else {
3340
filterUrlParams[aggName] = [fieldValue];
3441
}
35-
const hasChild = filter.length === 3;
36-
if (hasChild) {
37-
this._addFilter(filter[2], filterUrlParams);
38-
}
3942
};
4043

4144
_addFilters = (filters) => {
@@ -54,8 +57,7 @@ export class InvenioRequestSerializer {
5457
});
5558
/**
5659
* output: {
57-
* type_agg: 'value1'.
58-
* subtype_agg: 'a value'
60+
* type_agg: [ 'value1', 'value2::a value' ]
5961
* }
6062
*/
6163
return filterUrlParams;

src/lib/api/contrib/invenio/InvenioRequestSerializer.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,24 @@ describe("test InvenioRequestSerializer serializer", () => {
120120
],
121121
};
122122
const strState = serializer.serialize(queryState);
123-
expect(strState).toBe("q=test&type=report&subtype=pdf&category=video");
123+
expect(strState).toBe("q=test&type=report%3A%3Apdf&category=video");
124+
});
125+
126+
it("should serialize multiple nested filters", () => {
127+
const queryState = {
128+
queryString: "test",
129+
page: 0,
130+
size: 0,
131+
filters: [
132+
["type", "report", ["subtype", "pdf"]],
133+
["type", "report", ["subtype", "doc"]],
134+
["category", "video"],
135+
],
136+
};
137+
const strState = serializer.serialize(queryState);
138+
expect(strState).toBe(
139+
"q=test&type=report%3A%3Apdf&type=report%3A%3Adoc&category=video"
140+
);
124141
});
125142

126143
it("should fail when filters have wrong format", () => {

0 commit comments

Comments
 (0)