Skip to content

Commit

Permalink
[EPICSYSTEM-14] Add searching Comment Periods by keyword for project.… (
Browse files Browse the repository at this point in the history
#744)

* [EPICSYSTEM-14] Add searching Comment Periods by keyword for project.name, add pcp status filter field.
  • Loading branch information
tolkamps1 authored Mar 26, 2024
1 parent 245a18c commit 82f17a4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 6 deletions.
35 changes: 34 additions & 1 deletion api/aggregators/searchAggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,40 @@ exports.createMatchAggr = async (schemaName, projectId, keywords, caseSensitive,
return aggregation;
};

exports.createKeywordRegexAggr = function(decodedKeywords, schemaName) {
/**
* Create a regex to perform the keyword search after the unwind.
* *
* @param {string} keywords List of keywords to search on
* @param {boolean} caseSensitive Case sensitive search?
*
* @returns {array} Aggregation for a match on project.name
*/
exports.createRegexForProjectLookupAggr = function (keywords, caseSensitive, fuzzy = false) {
console.log("keywords ", keywords);
const aggregation = [];
if (keywords) {
keywords = keywords.replace(/"/g, "").trim();
let keywordSearch = fuzzy && !keywords.startsWith("\"") && !keywords.endsWith("\"") ? fuzzySearch.createFuzzySearchString(keywords, 4, caseSensitive) : "\"" + keywords + "\"";
const regexKeyword = `.*${keywordSearch.replaceAll(/"/g, "").trim()}.*`,
keywordModifier = {
"project.name": {
"$regex": regexKeyword,
"$options": "i"
}

};

aggregation.push({
$match: {
...(keywordModifier ? keywordModifier : undefined),
}
});
}
return aggregation;
};


exports.createKeywordRegexAggr = function (decodedKeywords, schemaName) {
let keywordRegexFilter = [];
// if we have a keyword search, and it is not wrapped in quotes (ie, phrase searching)
// then do a regex match. To help keep regex matches closer to their values, also
Expand Down
7 changes: 5 additions & 2 deletions api/controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const searchCollection = async function (roles, keywords, schemaName, pageNum, p
// Create appropriate aggregations for the schema.
let schemaAggregation;
let matchAggregation;
let regexKeywordAggregation = [];
switch (schemaName) {
case constants.DOCUMENT:
matchAggregation = await documentAggregator.createMatchAggr(schemaName, project, decodedKeywords, caseSensitive, or, and, categorized, roles, fuzzy);
Expand Down Expand Up @@ -75,8 +76,10 @@ const searchCollection = async function (roles, keywords, schemaName, pageNum, p
matchAggregation = await searchAggregator.createMatchAggr(schemaName, project, decodedKeywords, caseSensitive, or, and, roles);
break;
case constants.COMMENT_PERIOD:
matchAggregation = await searchAggregator.createMatchAggr(schemaName, project, decodedKeywords, caseSensitive, or, and, roles);
// Comment Periods are searched via project name, need to add keyword after schemaAggregation to match on project.name
matchAggregation = await searchAggregator.createMatchAggr(schemaName, project, '', false, or, and, roles);
schemaAggregation = commentPeriodAggregator.createCommentPeriodAggr(populate);
regexKeywordAggregation = await searchAggregator.createRegexForProjectLookupAggr(decodedKeywords, caseSensitive);
break;
case constants.ORGANIZATION:
matchAggregation = await searchAggregator.createMatchAggr(schemaName, project, decodedKeywords, caseSensitive, or, and, roles);
Expand Down Expand Up @@ -105,7 +108,7 @@ const searchCollection = async function (roles, keywords, schemaName, pageNum, p
if (!schemaAggregation) {
aggregation = [...matchAggregation, ...keywordRegexFilter, ...resultAggr];
} else {
aggregation = [...matchAggregation, ...schemaAggregation, ...keywordRegexFilter, ...resultAggr];
aggregation = [...matchAggregation, ...schemaAggregation, ...keywordRegexFilter, ...regexKeywordAggregation, ...resultAggr];
}

return new Promise(function (resolve, reject) {
Expand Down
38 changes: 35 additions & 3 deletions api/helpers/aggregators.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ const generateExpArray = async (field, roles, schemaName) => {

if (item === 'pcp') {
await handlePCPItem(roles, expArray, decodeURIComponent(entry));
} else if (item === 'status') {
handlePCPStatus(expArray, decodeURIComponent(entry));
} else if (Array.isArray(entry)) {
// Arrays are a list of options so will always be ors
if (schemaName === constants.PROJECT) {
Expand Down Expand Up @@ -393,9 +395,8 @@ const isValidObjectId = (str) => {
return str.match(/^[a-f\d]{24}$/i);
};

const getPCPValue = async (roles, entry) => {
console.log('pcp: ', entry);

// Helper to get PCP query based on status
const getPCPQuery = (entry) => {
let query = null;
const now = new Date();
const in7days = new Date();
Expand All @@ -412,6 +413,14 @@ const getPCPValue = async (roles, entry) => {
]
};
break;
case 'upcoming':
query = {
_schemaName: constants.COMMENT_PERIOD,
$and: [
{ dateStarted: { $gt: now } }
]
};
break;

case 'open':
query = {
Expand All @@ -433,7 +442,30 @@ const getPCPValue = async (roles, entry) => {
default:
console.log('Unknown PCP entry');
}
return query;
};

const handlePCPStatus = (expArray, value) => {
if (!Array.isArray(value) && value.includes(',')) {
value = value.split(',');
}

if (Array.isArray(value)) {
// Arrays are a list of options so will always be ors
const orArray = [];
value.map(entry => {
orArray.push(getPCPQuery(entry));
});
expArray.push({ $or: orArray });
} else {
expArray.push(getPCPQuery(value));
}
};

const getPCPValue = async (roles, entry) => {
console.log('pcp: ', entry);

const query = getPCPQuery(entry);
var pcp = {};

if (query) {
Expand Down

0 comments on commit 82f17a4

Please sign in to comment.