Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(applications): fetch stats by date and dedupe members #686

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions lib/applications.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const xlsx = require('node-xlsx').default;
const moment = require('moment');

const core = require('./core');
const errors = require('./errors');
Expand Down Expand Up @@ -355,20 +356,59 @@ exports.getStats = async (req, res) => {
by_nationality: []
};

const applications = await Application.findAll({ attributes: ['event_id', 'body_name', 'nationality'] });
// Filtering by event start and end dates.
// The events are not inclusive, so when the event starts on 2018-01-02 and ends on 2018-01-17, querying
// from 2018-01-05 to 2018-01-10 won't return it.
const dateQuery = [];
if (req.query.starts) dateQuery.push({ starts: { [Sequelize.Op.gte]: moment(req.query.starts, 'YYYY-MM-DD').startOf('day').toDate() } });
if (req.query.ends) dateQuery.push({ ends: { [Sequelize.Op.lte]: moment(req.query.ends, 'YYYY-MM-DD').endOf('day').toDate() } });

const applications = await Application.findAll({
attributes: ['event_id'],
include: [
{
model: Event,
attributes: [],
where: dateQuery
}
]
});

const events = await Event.findAll({ attributes: ['id', 'name'] });

statsObject.by_event = helpers
.countByField(applications, 'event_id')
.map(({ type, value }) => ({ type: events.find((event) => event.id === type).name, value }));

// TODO: only use one application per user from here
const applicationsPerBody = await Application.findAll({
attributes: ['user_id', 'body_name'],
group: ['user_id', 'body_name'],
include: [
{
model: Event,
attributes: [],
where: dateQuery
}
]
});

statsObject.by_body = helpers
.countByField(applications, 'body_name');
.countByField(applicationsPerBody, 'body_name');

const applicationsPerNationality = await Application.findAll({
attributes: ['user_id', 'nationality'],
group: ['user_id', 'nationality'],
include: [
{
model: Event,
attributes: [],
where: dateQuery
}
]
});

statsObject.by_nationality = helpers
.countByField(applications, 'nationality');
.countByField(applicationsPerNationality, 'nationality');

return res.json({
success: true,
Expand Down