-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterbuilder.js
108 lines (92 loc) · 2.7 KB
/
filterbuilder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const axios = require("axios");
const appconstants = require("./constants");
/**
* Specifies the filter configuration
* The filter configuration is used to construct the XQuery.
* this is typically converted to the form:
* [ {xqueryElementXPath}[{xqueryAttr} eq '{incomingFilterValue}' {or...} ] ]
* for example if the incoming filter value is for "countries" :
*
* "countries":["bf","mr"]
*
* then the Query is constructed as:
*
* [ .//an:FRBRcountry[@value eq 'bf' or @value eq 'mr' ] ]
*
* if it is for "langs":
*
* "langs":["eng"],
*
* then the Query is constructed as:
*
* [ .//an:FRBRlanguage[ @language eq 'eng' ]]
*
*/
const filterConfig = {
"countries": {
xqueryElementXPath: ".//an:FRBRcountry",
xqueryAttr: "@value"
},
"langs": {
xqueryElementXPath: ".//an:FRBRlanguage",
xqueryAttr: "@language"
}
};
const convertEncodedStringToObject = (aString) =>
JSON.parse(decodeURIComponent(aString)) ;
/*
const convertObjectToEncodedString = (obj) =>
encodeURIComponent(JSON.stringify(obj)) ;
*/
const searchFilter = (req) => {
console.log(" searchFilter req.query ", req.query);
let filter = convertEncodedStringToObject(req.query.q);
let count = 1; // req.query.count || "10" ;
let from = 1 ; //req.query.from || "1" ;
let xQueryFilter = xQueryFilterBuilder(filter).join("");
let filterObj = {
count: count,
from: from,
q: xQueryFilter
};
return filterObj;
};
/**
* Queries the service for the filter cache api.
* This is called by the CRON service
*/
const searchFilterQuery = (filterObj) => {
console.log(" FILTER OBJ ", filterObj);
return axios.get(appconstants.API_SEARCH_FILTER, filterObj).then(
(response) => {
return response.data;
}
);
};
/**
* This returns an XQuery query which is sent to the data server
* as a server side query
* @param {object} filter incoming filter object from client
* This is typically sent in the format as below:
* {
* "langs":["eng"],
* "countries":["bf","mr"]
* }
*/
function xQueryFilterBuilder(filter) {
// the root document collection
let xQuery = [];
for (let filterName in filter) {
let cfg = filterConfig[filterName];
let attrQuery = filter[filterName].map(
value =>`${cfg.xqueryAttr} eq '${value}'`
).join(" or ");
xQuery.push(
`[${cfg.xqueryElementXPath}[ ${attrQuery} ]]`
);
}
return xQuery;
}
module.exports.xQueryFilterBuilder = xQueryFilterBuilder;
module.exports.searchFilterQuery = searchFilterQuery;
module.exports.searchFilter = searchFilter;