Skip to content

Commit

Permalink
adds filtering to profilesearch
Browse files Browse the repository at this point in the history
  • Loading branch information
jhmullen authored and davelandry committed Sep 3, 2020
1 parent 911b547 commit cd21607
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/cms/src/api/searchRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,22 @@ module.exports = function(app) {
where.locale = locale;
const contentRows = await db.search_content.findAll({where}).catch(catcher);
searchWhere.contentId = Array.from(new Set(contentRows.map(r => r.id)));
// If the user has specified a profile by slug or id, restrict the search results to that profile's members
if (req.query.profile) {
// using "slug" here is not 100% correct, as profiles can be bilateral, and therefore have two slugs.
// However, for the more common unilateral case, allow "single-slug" lookup for convenience.
const metaWhere = !isNaN(req.query.profile) ? {profile_id: req.query.profile} : {slug: req.query.profile};
const thisMeta = await db.profile_meta.findOne({where: metaWhere});
if (thisMeta) {
searchWhere.cubeName = thisMeta.cubeName;
searchWhere.dimension = thisMeta.dimension;
searchWhere.hierarchy = thisMeta.levels;
}
}
// Also allow the user to directly limit searches by dimension and comma separated hierarchy (levels)
// Note that this can happen in conjunction with the req.query.profile limitation above, as overrides.
if (req.query.dimension) searchWhere.dimension = req.query.dimension.split(",");
if (req.query.hierarchy) searchWhere.hierarchy = req.query.hierarchy.split(",");
let rows = await db.search.findAll({
include: [{model: db.image, include: [{association: "content"}]}, {association: "content"}],
// when a limit is provided, it is for EACH dimension, but this initial rowsearch is for a flat member list.
Expand Down Expand Up @@ -400,7 +416,7 @@ module.exports = function(app) {
const relevantResults = groupedMeta.reduce((acc, group, i) => {
acc[i] = [];
group.forEach(m => {
const theseResults = results.results[m.dimension] ? results.results[m.dimension].filter(d => d.metadata.cube_name === m.cubeName) : false;
const theseResults = results.results[m.dimension] ? results.results[m.dimension].filter(d => d.metadata.cube_name === m.cubeName && m.levels.includes(d.metadata.hierarchy)) : false;
if (theseResults) {
acc[i] = acc[i].concat(theseResults
.map(r => ({
Expand Down

0 comments on commit cd21607

Please sign in to comment.