Skip to content
This repository has been archived by the owner on May 28, 2018. It is now read-only.

Commit

Permalink
Add gender summary ressource
Browse files Browse the repository at this point in the history
  • Loading branch information
nbrohee committed Jun 22, 2017
1 parent aac7058 commit 9251c2f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions server/api/stat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ module.exports = (options) => {
var stat = new Controller(options)
router.get('/ping', stat.ping)
router.get('/applicationSummary', stat.applicationSummary)
router.get('/applicationGenderSummary', stat.applicationGenderSummary)
return router
}
16 changes: 16 additions & 0 deletions server/api/stat/stat.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ class StatController {
return res.status(500).send(err)
})
}

applicationGenderSummary(req, res) {
return statQuery.applicationGenderSummary()
.then((applicationGenderSummary) => {
if (applicationGenderSummary.length) {
return res.json(applicationGenderSummary[0])
}
else {
return res.json({})
}
})
.catch((err) => {
req.log.error(err)
return res.status(500).send(err)
})
}
}

module.exports = StatController
31 changes: 30 additions & 1 deletion server/api/stat/stat.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,35 @@ function applicationSummary() {
})
}

function applicationGenderSummary() {
return Application.aggregate([
{ $match: { status: { $ne: 'saved' }, 'contact.schoolYear': 2017 } },
{
$project: {
male: { $cond: [{ $eq: ['$profile.gender', 'male'] }, 1, 0] },
female: { $cond: [{ $eq: ['$profile.gender', 'female'] }, 1, 0] },
}
},
{
$group: {
_id: null,
total: { $sum: 1 },
male: { $sum: '$male' },
female: { $sum: '$female' },
}
},
{
$project: {
_id: 0,
total: 1,
female: 1,
male: 1
}
}
])
}

module.exports = {
applicationSummary
applicationSummary,
applicationGenderSummary
}
2 changes: 1 addition & 1 deletion server/api/stat/test/application.seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ const applications = [
'teamType': ''
},
'profile': {
'gender': 'male',
'gender': 'female',
'situation': 'graduate',
'nationality': 'AW',
'motivation': 'azeaz',
Expand Down
43 changes: 43 additions & 0 deletions server/api/stat/test/stat.api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,47 @@ describe('api: stat', () => {
})
})
})

describe('When requesting /api/stat/applicationGenderSummary', () => {
describe('When there is no application registered', () => {
it('should return empty object', (done) => {
supertest(app)
.get('/api/stat/applicationGenderSummary')
.end((err, res) => {
if (err) {
return done(err)
}
expect(res.body).toEqual({})
return done()
})
})
})

describe('When there is both a male and a female', () => {
before((done) => {
ApplicationModel.insertMany(applicationData, done)
})

after((done) => {
ApplicationModel.remove(done)
})

it('should return a summary with both male and female', (done) => {
const expectedGenderSummary = {
total: 2,
female: 1,
male: 1
}
supertest(app)
.get('/api/stat/applicationGenderSummary')
.end((err, res) => {
if (err) {
return done(err)
}
expect(res.body).toEqual(expectedGenderSummary)
return done()
})
})
})
})
})

0 comments on commit 9251c2f

Please sign in to comment.