Skip to content

Commit

Permalink
InvenioSerializer: Concatenate nested filters with '::'
Browse files Browse the repository at this point in the history
  • Loading branch information
psaiz authored and ntarocco committed Nov 7, 2023
1 parent dcca02c commit 9a3ce63
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/lib/api/contrib/invenio/InvenioRequestSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ export class InvenioRequestSerializer {
`Filter value "${filter}" in query state must be an array of 2 or 3 elements`
);
}
const aggName = filter[0];
const fieldValue = filter[1];
const hasChild = filter.length === 3;
var aggName = filter[0];
var fieldValue = filter[1];
if (hasChild) {
if (!Array.isArray(filter[2])) {
throw new Error(`Filter value "${filter[2]}" in query state must be an array.`);
}
fieldValue = fieldValue + "::" + filter[2][1];
}
if (aggName in filterUrlParams) {
filterUrlParams[aggName].push(fieldValue);
} else {
filterUrlParams[aggName] = [fieldValue];
}
const hasChild = filter.length === 3;
if (hasChild) {
this._addFilter(filter[2], filterUrlParams);
}
};

_addFilters = (filters) => {
Expand All @@ -54,8 +57,7 @@ export class InvenioRequestSerializer {
});
/**
* output: {
* type_agg: 'value1'.
* subtype_agg: 'a value'
* type_agg: [ 'value1', 'value2::a value' ]
* }
*/
return filterUrlParams;
Expand Down
19 changes: 18 additions & 1 deletion src/lib/api/contrib/invenio/InvenioRequestSerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,24 @@ describe("test InvenioRequestSerializer serializer", () => {
],
};
const strState = serializer.serialize(queryState);
expect(strState).toBe("q=test&type=report&subtype=pdf&category=video");
expect(strState).toBe("q=test&type=report%3A%3Apdf&category=video");
});

it("should serialize multiple nested filters", () => {
const queryState = {
queryString: "test",
page: 0,
size: 0,
filters: [
["type", "report", ["subtype", "pdf"]],
["type", "report", ["subtype", "doc"]],
["category", "video"],
],
};
const strState = serializer.serialize(queryState);
expect(strState).toBe(
"q=test&type=report%3A%3Apdf&type=report%3A%3Adoc&category=video"
);
});

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

0 comments on commit 9a3ce63

Please sign in to comment.