Skip to content

Commit

Permalink
Merge pull request #742 from tolkamps1/epicsystem-23-add-comment-peri…
Browse files Browse the repository at this point in the history
…od-aggregator-to-search

[EPICSYSTEM-23] Add Public Comment Period aggregator to search
  • Loading branch information
tolkamps1 committed Mar 8, 2024
2 parents ba5f985 + 824c2f0 commit 245a18c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
75 changes: 75 additions & 0 deletions api/aggregators/commentPeriodAggregator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const { setProjectDefault } = require('../helpers/aggregators');

/**
* Creates an aggregate for looking up recent activity.
*
* @param {boolean} populate Flag indicating if fields need a look up
* @returns {array} Aggregate for recent activity
*/
exports.createCommentPeriodAggr = (populate) => {
let aggregation = [];

if (populate) {
// Handle project.
aggregation.push(
{
'$lookup': {
'from': 'epic',
'localField': 'project',
'foreignField': '_id',
'as': 'project'
}
},
{
'$addFields': {
project: '$project',
}
},
{
'$unwind': {
'path': '$project',
'preserveNullAndEmptyArrays': true
}
}
);

// Here we have documents with a nested Project and a nested legislation key
const defaultAggr = setProjectDefault(false);
aggregation = [...aggregation, ...defaultAggr];

// We need to merge the legislation key with the Project while preserving the _id and the rest of the document info
// TODO: Abstract these types of stages, as we will need to do this a lot")
aggregation.push(
{
'$addFields': {
'project': { '$mergeObjects': ['$project', '$project.default'] },
}
},
{
'$project': { ['project.legislation_2002']: 0 }
},
{
'$project': { ['project.legislation_1996']: 0 }
},
{
'$project': { ['project.default']: 0 }
},
{
'$lookup': {
from: 'epic',
localField: 'project.currentPhaseName',
foreignField: '_id',
as: 'project.currentPhaseName'
}
},
{
'$unwind': {
path: '$project.currentPhaseName',
preserveNullAndEmptyArrays: true
}
}
);
}

return aggregation;
};
6 changes: 5 additions & 1 deletion api/controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const recentActivityAggregator = require('../aggregators/recentActivityAggregato
const inspectionAggregator = require('../aggregators/inspectionAggregator');
const notificationProjectAggregator = require('../aggregators/notificationProjectAggregator');
const itemAggregator = require('../aggregators/itemAggregator');
const commentPeriodAggregator = require('../aggregators/commentPeriodAggregator');
const searchAggregator = require('../aggregators/searchAggregator');
const aggregateHelper = require('../helpers/aggregators');

Expand Down Expand Up @@ -73,6 +74,10 @@ const searchCollection = async function (roles, keywords, schemaName, pageNum, p
case constants.LIST:
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);
schemaAggregation = commentPeriodAggregator.createCommentPeriodAggr(populate);
break;
case constants.ORGANIZATION:
matchAggregation = await searchAggregator.createMatchAggr(schemaName, project, decodedKeywords, caseSensitive, or, and, roles);
break;
Expand Down Expand Up @@ -209,7 +214,6 @@ const executeQuery = async function (args, res) {
} else if (dataset === constants.ITEM) {
const collectionObj = mongoose.model(args.swagger.params._schemaName.value);
const aggregation = itemAggregator.createItemAggr(args.swagger.params._id.value, args.swagger.params._schemaName.value, roles);

let data = await collectionObj.aggregate(aggregation).allowDiskUse(true);

if (args.swagger.params._schemaName.value === constants.COMMENT) {
Expand Down
4 changes: 4 additions & 0 deletions api/helpers/aggregators.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ const generateExpArray = async (field, roles, schemaName) => {
case 'datePostedEnd':
handleDateEndItem(orArray, ['datePosted'], decodeURIComponent(entry));
break;
case 'dateCompletedStart':
// PCP date range check if comment period has closed within last 30 days or will in future
handleDateStartItem(orArray, ['dateCompleted'], decodeURIComponent(entry));
break;
default:
if (schemaName === constants.PROJECT) {
for (let field of fields) {
Expand Down

0 comments on commit 245a18c

Please sign in to comment.