Skip to content

Commit

Permalink
fix(stats): only use 1 application per user - attempt 2 (#795)
Browse files Browse the repository at this point in the history
Co-authored-by: Rik Smale <git@riksmale.info>
  • Loading branch information
WikiRik and Rik Smale committed Sep 24, 2024
1 parent 4546663 commit 52ff5af
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions lib/applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ exports.getStats = async (req, res) => {
by_nationality: []
};

let applicationQuery = { attributes: ['event_id', 'body_name', 'nationality'] };
let applicationQuery = { attributes: ['user_id', 'event_id', 'body_name', 'nationality'] };
let eventQuery = { attributes: ['id', 'name'] };

if (req.query.season) {
Expand All @@ -370,23 +370,12 @@ exports.getStats = async (req, res) => {
.countByField(applications, 'event_id')
.map(({ type, value }) => ({ type: events.find((event) => event.id === type).name, value }));

let uniqueApplicationUsersQuery = {
...applicationQuery,
attributes: ['user_id', 'body_name', 'nationality', 'event_id'],
group: ['user_id', 'body_name', 'nationality', 'event_id'],
};

if (req.query.season) {
uniqueApplicationUsersQuery = { ...uniqueApplicationUsersQuery, group: ['user_id', 'body_name', 'nationality', 'event_id', 'event.id'] };
}

const uniqueApplicationUsers = await Application.findAll(uniqueApplicationUsersQuery);

statsObject.by_body = helpers
.countByField(uniqueApplicationUsers, 'body_name');
const uniqueApplicationUsers = Array.from(
new Map(applications.map((app) => [app.user_id, app])).values()
);

statsObject.by_nationality = helpers
.countByField(uniqueApplicationUsers, 'nationality');
statsObject.by_body = helpers.countByField(uniqueApplicationUsers, 'body_name');
statsObject.by_nationality = helpers.countByField(uniqueApplicationUsers, 'nationality');

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

0 comments on commit 52ff5af

Please sign in to comment.