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

Commit

Permalink
Basic applicationSummary endpoint to aggregate applications by PEPITE…
Browse files Browse the repository at this point in the history
… and status
  • Loading branch information
nbrohee committed Jun 19, 2017
1 parent ab03c38 commit d096a42
Show file tree
Hide file tree
Showing 6 changed files with 503 additions and 0 deletions.
2 changes: 2 additions & 0 deletions server/api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const pepite = require('./../pepite')
const committee = require('./../committee')
const region = require('./../region')
const establishment = require('./../establishment')
const stat = require('./../stat')
const user = require('./../user')
const auth = require('./../auth')

Expand All @@ -16,6 +17,7 @@ exports.configure = (app, options) => {
app.use('/api/pepite/:pepiteId(\\d+)/committee', committee(options))
app.use('/api/region', region(options))
app.use('/api/establishment', establishment(options))
app.use('/api/stat', stat(options))
app.use('/api/user', user(options))
app.use('/api/auth', auth(options))
}
11 changes: 11 additions & 0 deletions server/api/stat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express')
const Controller = require('./stat.controller')

var router = express.Router()

module.exports = (options) => {
var stat = new Controller(options)
router.get('/ping', stat.ping)
router.get('/applicationSummary', stat.applicationSummary)
return router
}
22 changes: 22 additions & 0 deletions server/api/stat/stat.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const statQuery = require('./stat.query')

class StatController {
ping(req, res) {
res.json('pong')
}

applicationSummary(req, res) {
return statQuery.applicationSummary()
.then((pepiteApplications) => {
return res.json(pepiteApplications)
})
.catch((err) => {
req.log.error(err)
return res.status(500).send(err)
})
}
}

module.exports = StatController
26 changes: 26 additions & 0 deletions server/api/stat/stat.query.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const Application = require('../application/application.model')
const Pepite = require('../pepite/pepite.model')

function applicationSummary() {
return Application.aggregate([
{ $match: { status: { $ne: 'saved' }, 'contact.schoolYear': 2017 } },
{ $group: { _id: { pepite: '$pepite.pepite', status: '$status' }, total: { $sum: 1 } } },
{ $project: { pepiteId: '$_id.pepite', total: 1, status: '$_id.status' } },
{ $sort: { pepite: 1 } }
])
.then((applicationStatusCount) => {
return Pepite.find({}, { email: 0 }).then((pepites) => {
const applicationSummary = pepites.map((p) => { return p.toObject() })
applicationStatusCount.forEach((statusCount) => {
applicationSummary[Number(statusCount.pepiteId) - 1][statusCount.status] = statusCount.total
})
return applicationSummary
})
})
}

module.exports = {
applicationSummary
}
Loading

0 comments on commit d096a42

Please sign in to comment.