Skip to content

Commit

Permalink
improvement(case-aggregations): add nationalities facet
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMaruchu committed Jun 5, 2020
1 parent a71f2d9 commit 56a814c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
31 changes: 30 additions & 1 deletion src/aggregations/case.aggregations.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,31 @@ export const CASE_FACET_OCCUPATIONS = {
],
};

/**
* @constant
* @name CASE_FACET_NATIONALITIES
* @description Cases victim nationalities facet
*
* @author Benson Maruchu<benmaruchu@gmail.com>
* @license MIT
* @since 0.8.0
* @version 0.1.0
*/
export const CASE_FACET_NATIONALITIES = {
nationalities: [
{
$group: {
_id: '$victim.nationality._id',
total: { $sum: 1 },
namespace: { $first: '$victim.nationality.namespace' },
name: { $first: '$victim.nationality.strings.name' },
weight: { $first: '$victim.nationality.numbers.weight' },
color: { $first: '$victim.nationality.strings.color' },
},
},
],
};

// start: aggregations
// order: base to specific

Expand Down Expand Up @@ -286,6 +311,7 @@ export const getEventCaseAnalysis = (criteria, done) => {
...CASE_FACET_GENDER,
...CASE_FACET_AGE_GROUPS,
...CASE_FACET_OCCUPATIONS,
...CASE_FACET_NATIONALITIES,
};

base.facet(facets);
Expand All @@ -294,7 +320,9 @@ export const getEventCaseAnalysis = (criteria, done) => {

// Normalize data
const normalize = (result, next) => {
const { gender, ageGroups, occupations } = safeMergeObjects(...result);
const { gender, ageGroups, occupations, nationalities } = safeMergeObjects(
...result
);

// add upper boundary for returned age groups
const normalizedResultsAgeGroups = map(ageGroups, (group) => ({
Expand All @@ -316,6 +344,7 @@ export const getEventCaseAnalysis = (criteria, done) => {
gender,
ageGroups: normalizedAgeGroups,
occupations,
nationalities,
},
});

Expand Down
1 change: 1 addition & 0 deletions test/integration/case.aggregations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('EventCase Aggregations', () => {
expect(report.data.overall.gender).to.exist.and.be.an('array');
expect(report.data.overall.ageGroups).to.exist.and.be.an('array');
expect(report.data.overall.occupations).to.exist.and.be.an('array');
expect(report.data.overall.nationalities).to.exist.and.be.an('array');
expect(report.data.overall.ageGroups).to.have.lengthOf(21);
done(error, report);
});
Expand Down

0 comments on commit 56a814c

Please sign in to comment.