-
Notifications
You must be signed in to change notification settings - Fork 3
/
query-params.js
83 lines (75 loc) · 2.44 KB
/
query-params.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
/**
* Create a query params Object
* Joi is already converting the query and params for the short name to the JSONAPI format
* ex: occupation to filter[occupation]
*/
const queryParamsAll = require('./query-params-all');
const queryParamsObjects = require('./query-params-objects');
const queryParamsPeople = require('./query-params-people');
const queryParamsDocuments = require('./query-params-documents');
const queryParamsGroup = require('./query-params-group');
module.exports = (typeFormat, queryParams) => {
const result = {};
// Main values
result.query = queryParams.query;
result.q = queryParams.query.q ? queryParams.query.q.trim() : undefined;
result.pageNumber = parseInt(queryParams.query['page[number]']) || 0;
result.pageSize = parseInt(queryParams.query['page[size]']) || 50;
result.pageType = queryParams.query['page[type]'] || 'search';
result.pageSort = queryParams.query['page[sort]'] || 'default';
result.type =
queryParams.params.type || queryParams.query['fields[type]'] || 'all';
result.random = queryParams.query.random;
result.filter = {
all: {},
objects: {},
people: {},
documents: {},
group: {}
};
// All
Object.assign(result.filter.all, queryParamsAll(typeFormat, queryParams));
// Objects
Object.assign(
result.filter.objects,
queryParamsObjects(typeFormat, queryParams)
);
if (result.filter.objects.categories) {
Object.assign(
result.filter.objects.categories,
formatCategoryNames(result.filter.objects.categories)
);
}
// People
Object.assign(
result.filter.people,
queryParamsPeople(typeFormat, queryParams)
);
// Documents
Object.assign(
result.filter.documents,
queryParamsDocuments(typeFormat, queryParams)
);
// Groups
Object.assign(result.filter.group, queryParamsGroup(typeFormat, queryParams));
return result;
};
/*
Some cateogires contains a dash in their name.
By default the application convert space to dash on the front end
and dash to space on the backend. This function add dash for the
specific categories.
see issue #1008
*/
function formatCategoryNames (categories) {
const categoryNames = {
'x rays': 'x-rays',
'medical ceramic ware': 'medical ceramic-ware',
'medical glass ware': 'medical glass-ware',
'pharmacy ware': 'pharmacy ware',
'penn gaskell collection': 'penn-gaskell collection'
};
return categories.map((c) => {
return categoryNames[c.toLowerCase()] || c;
});
}