Skip to content

Commit

Permalink
feat(overview-analysis): expose parties overview
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Apr 29, 2020
1 parent 7fc796a commit f0d118b
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ npm install @codetanzania/ewea-reports --save
## Usage

```js
import { getPartyAnalysis } from '@codetanzania/ewea-reports';
import {
getOverviewAnalysis,
getPartyAnalysis
} from '@codetanzania/ewea-reports';

const criteria = { ... };
getOverviewAnalysis(criteria, (error, report) => { ... });
getPartyAnalysis(criteria, (error, report) => { ... });
```

Expand Down
30 changes: 30 additions & 0 deletions src/aggregations/overview.aggregations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { isFunction } from 'lodash';
import { parallel } from 'async';
import { safeMergeObjects } from '@lykmapipo/common';
import { getPartyOverview } from './party.aggregations';

export const DEFAULT_OVERVIEW_ANALYSIS = {
parties: { total: 0, agency: 0, focal: 0 },
};

export const getOverviewAnalysis = (criteria, done) => {
// normalize arguments
const filter = isFunction(criteria) ? {} : criteria;
const cb = isFunction(criteria) ? criteria : done;

// tasks
const parties = (next) => getPartyOverview(filter, next);
const tasks = { parties };

// return
return parallel(tasks, (error, overviews) => {
// back-off on error
if (error) {
return cb(error);
}

// normalize and return
const data = safeMergeObjects(DEFAULT_OVERVIEW_ANALYSIS, overviews);
return cb(null, { data });
});
};
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export * from './aggregations/changelog.aggregations';
export * from './aggregations/event.aggregations';
export * from './aggregations/party.aggregations';
export * from './aggregations/predefine.aggregations';
export * from './aggregations/overview.aggregations';

/**
* @name info
Expand Down
10 changes: 8 additions & 2 deletions src/report.http.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getString } from '@lykmapipo/env';
import { Router } from '@lykmapipo/express-rest-actions';

import { getPartyAnalysis } from './aggregations/party.aggregations';
import { getOverviewAnalysis } from './aggregations/overview.aggregations';

/* constants */
const API_VERSION = getString('API_VERSION', '1.0.0');
Expand Down Expand Up @@ -47,8 +48,13 @@ const router = new Router({
* @version 1.0.0
* @public
*/
router.get(PATH_OVERVIEW, (request, response) => {
response.ok({});
router.get(PATH_OVERVIEW, (request, response, next) => {
return getOverviewAnalysis({}, (error, report) => {
if (error) {
return next(error);
}
return response.ok(report);
});
});

/**
Expand Down
28 changes: 28 additions & 0 deletions test/integration/overview.aggregations.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
expect,
// enableDebug
} from '@lykmapipo/mongoose-test-helpers';

import { getOverviewAnalysis } from '../../src';

describe('Overview Aggregations', () => {
it('should provide overview analysis', (done) => {
getOverviewAnalysis((error, report) => {
expect(error).to.not.exist;
expect(report).to.exist.and.be.an('object');
expect(report.data).to.exist.and.be.an('object');
expect(report.data.parties).to.be.eql({
total: 4,
agency: 2,
focal: 2,
level: 4, // FIX
area: 4, // FIX
group: 4, // FIX
role: 4, // FIX
active: 4,
inactive: 0,
});
done(error, report);
});
});
});
14 changes: 13 additions & 1 deletion test/integration/report.http.router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ describe('Reports Rest API', () => {
.expect('Content-Type', /json/)
.end((error, { body }) => {
expect(error).to.not.exist;
expect(body).to.exist;
expect(body).to.exist.and.be.an('object');
expect(body.data).to.exist.and.be.an('object');
expect(body.data.parties).to.be.eql({
total: 4,
agency: 2,
focal: 2,
level: 4, // FIX
area: 4, // FIX
group: 4, // FIX
role: 4, // FIX
active: 4,
inactive: 0,
});
done(error, body);
});
});
Expand Down

0 comments on commit f0d118b

Please sign in to comment.