2016-09-22 13:46:27 +03:00
|
|
|
import createSearchFilters from '../../links/lib/createSearchFilters';
|
2016-10-23 18:39:11 +03:00
|
|
|
import cleanObjectForMetaFilters from './lib/cleanObjectForMetaFilters';
|
2016-09-22 12:01:47 +03:00
|
|
|
import sift from 'sift';
|
|
|
|
|
2016-10-23 18:39:11 +03:00
|
|
|
export default (childCollectionNode, {limit, skip, metaFilters}) => {
|
2016-09-22 12:01:47 +03:00
|
|
|
const parent = childCollectionNode.parent;
|
2016-09-22 13:46:27 +03:00
|
|
|
const linker = childCollectionNode.linker;
|
2016-09-22 12:01:47 +03:00
|
|
|
|
2016-09-22 13:46:27 +03:00
|
|
|
const strategy = linker.strategy;
|
|
|
|
const isSingle = linker.isSingle();
|
2016-10-07 10:46:28 +03:00
|
|
|
const isMeta = linker.isMeta();
|
2016-09-22 13:46:27 +03:00
|
|
|
const fieldStorage = linker.linkStorageField;
|
2016-09-22 12:01:47 +03:00
|
|
|
|
2016-10-23 18:39:11 +03:00
|
|
|
// cleaning the parent results from a child
|
|
|
|
// this may be the wrong approach but it works for now
|
|
|
|
if (isMeta && metaFilters) {
|
|
|
|
const metaFiltersTest = sift(metaFilters);
|
|
|
|
_.each(parent.results, parentResult => {
|
|
|
|
cleanObjectForMetaFilters(parentResult, fieldStorage, metaFiltersTest);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-22 12:01:47 +03:00
|
|
|
_.each(parent.results, result => {
|
2016-10-07 10:46:28 +03:00
|
|
|
let data = assembleData(childCollectionNode, result, {
|
2016-10-23 18:39:11 +03:00
|
|
|
fieldStorage, strategy, isSingle
|
2016-09-22 13:46:27 +03:00
|
|
|
});
|
2016-09-22 17:06:05 +03:00
|
|
|
|
2016-10-23 18:39:11 +03:00
|
|
|
result[childCollectionNode.linkName] = filterAssembledData(data, {limit, skip})
|
2016-09-22 13:46:27 +03:00
|
|
|
});
|
2016-09-22 17:06:05 +03:00
|
|
|
}
|
2016-09-22 13:46:27 +03:00
|
|
|
|
2016-10-23 18:39:11 +03:00
|
|
|
function filterAssembledData(data, {limit, skip}) {
|
2016-09-22 13:46:27 +03:00
|
|
|
if (limit) {
|
2016-09-23 13:31:33 +03:00
|
|
|
return data.slice(skip, limit);
|
2016-09-22 13:46:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2016-09-22 17:06:05 +03:00
|
|
|
}
|
|
|
|
|
2016-10-23 18:39:11 +03:00
|
|
|
function assembleData(childCollectionNode, result, {fieldStorage, strategy}) {
|
|
|
|
const filters = createSearchFilters(result, fieldStorage, strategy, false);
|
2016-09-22 17:06:05 +03:00
|
|
|
|
|
|
|
return sift(filters, childCollectionNode.results);
|
2016-10-07 10:46:28 +03:00
|
|
|
}
|