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: fetch all notification_emails together #1186

Merged
merged 3 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
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
94 changes: 67 additions & 27 deletions lib/applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
const constants = require('./constants');
const helpers = require('./helpers');
const { sequelize } = require('./sequelize');
const logger = require('./logger');

exports.listAllApplications = async (req, res) => {
if (!req.permissions.see_applications) {
Expand All @@ -20,14 +21,21 @@
query: req.query
});

applications.rows = await Promise.all(applications.rows
.map(async (application) => {
const user = await core.fetchApplicationUser(application.user_id);
if (applications.rows.length > 0) {
const userIds = applications.rows.map((application) => application.user_id).toString();
const mails = await core.getMails(req, userIds);

application.dataValues.notification_email = user.notification_email;
for (const application of applications.rows) {
const user = mails.find((m) => application.user_id === m.id);

if (!user) {
logger.warn({ user_id: application.user_id }, 'Could not find user');
continue;
}

return application;
}));
application.dataValues.notification_email = user.notification_email;
}
}

return res.json({
success: true,
Expand All @@ -49,14 +57,21 @@
query: req.query
});

applications.rows = await Promise.all(applications.rows
.map(async (application) => {
const user = await core.fetchApplicationUser(application.user_id);
if (applications.rows.length > 0) {
const userIds = applications.rows.map((application) => application.user_id).toString();
const mails = await core.getMails(req, userIds);

application.dataValues.notification_email = user.notification_email;
for (const application of applications.rows) {
const user = mails.find((m) => application.user_id === m.id);

return application;
}));
if (!user) {
logger.warn({ user_id: application.user_id }, 'Could not find user');
continue;

Check warning on line 69 in lib/applications.js

View check run for this annotation

Codecov / codecov/patch

lib/applications.js#L68-L69

Added lines #L68 - L69 were not covered by tests
}

application.dataValues.notification_email = user.notification_email;
}
}

return res.json({
success: true,
Expand Down Expand Up @@ -208,18 +223,25 @@
}

// 'zzzzz' is used to be last, 'unset' won't do
let applications = await Application.findAll({
const applications = await Application.findAll({
where: { event_id: req.event.id, body_id: parseInt(req.params.body_id, 10) },
});

applications = await Promise.all(applications
.map(async (application) => {
const user = await core.fetchApplicationUser(application.user_id);
if (applications.length > 0) {
const userIds = applications.map((application) => application.user_id).toString();
const mails = await core.getMails(req, userIds);

application.dataValues.notification_email = user.notification_email;
for (const application of applications) {
const user = mails.find((m) => application.user_id === m.id);

if (!user) {
logger.warn({ user_id: application.user_id }, 'Could not find user');
continue;
}

return application;
}));
application.dataValues.notification_email = user.notification_email;
}
}

const sortedApplications = applications.sort((a, b) => {
// first, comparing by participant type
Expand Down Expand Up @@ -723,11 +745,22 @@
'Email'
];

let mails = [];

if (applications.length > 0) {
const userIds = applications.map((application) => application.user_id).toString();
mails = await core.getMails(req, userIds);
}

const applicationsString = await Promise.all(applications.map(async (application) => {
// Generating random pw for a user.
const password = crypto.randomBytes(5).toString('hex');

const user = await core.fetchApplicationUser(application.user_id);
const user = mails.find((m) => application.user_id === m.id);

if (!user) {
logger.warn({ user_id: application.user_id }, 'Could not find user');

Check warning on line 762 in lib/applications.js

View check run for this annotation

Codecov / codecov/patch

lib/applications.js#L762

Added line #L762 was not covered by tests
}

return [
'', // Title
Expand Down Expand Up @@ -788,16 +821,23 @@
const headersNames = helpers.getApplicationFields(req.event);
const headers = req.query.select.map((field) => headersNames[field]);

let applications = await Application.findAll({ where: { event_id: req.event.id, ...applicationsFilter } });
const applications = await Application.findAll({ where: { event_id: req.event.id, ...applicationsFilter } });

applications = await Promise.all(applications
.map(async (application) => {
const user = await core.fetchApplicationUser(application.user_id);
if (applications.length > 0) {
const userIds = applications.map((application) => application.user_id).toString();
const mails = await core.getMails(req, userIds);

application.dataValues.notification_email = user.notification_email;
for (const application of applications) {
const user = mails.find((m) => application.user_id === m.id);

if (!user) {
logger.warn({ user_id: application.user_id }, 'Could not find user');
continue;
}

return application;
}));
application.dataValues.notification_email = user.notification_email;
}
}

const resultArray = applications
.map((application) => application.toJSON())
Expand Down
8 changes: 5 additions & 3 deletions lib/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@
return errors.makeNotFoundError(res, userPrefix + ' haven\'t applied to this event yet.');
}

const user = await core.fetchApplicationUser(application.user_id);
const mail = await core.getMails(req, application.user_id);

application.dataValues.notification_email = user.notification_email;
if (mail) {
application.dataValues.notification_email = mail.notification_email;
}

req.application = application;

Expand All @@ -205,7 +207,7 @@
return errors.makeNotFoundError(res, userPrefix + ' haven\'t applied to this event yet.');
}

incomingApplication.dataValues.notification_email = user.notification_email;
incomingApplication.dataValues.notification_email = mail.notification_email;

Check warning on line 210 in lib/middlewares.js

View check run for this annotation

Codecov / codecov/patch

lib/middlewares.js#L210

Added line #L210 was not covered by tests

req.application = incomingApplication;
}
Expand Down
26 changes: 19 additions & 7 deletions lib/positions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
const { Position, Candidate, Image } = require('../models');
const helpers = require('./helpers');
const constants = require('./constants');
const logger = require('./logger');

exports.findPosition = async (req, res, next) => {
if (Number.isNaN(Number(req.params.position_id))) {
Expand Down Expand Up @@ -59,6 +60,7 @@
}]
});

// TODO: use core.getMails for this, fetching the notification_email of the candidates of all positions together
positions = await Promise.all(positions.map(async (position) => {
position.candidates = await Promise.all(position.candidates
.map(async (candidate) => {
Expand Down Expand Up @@ -90,6 +92,7 @@
}]
});

// TODO: use core.getMails for this, fetching the notification_email of the candidates of all positions together
positions = await Promise.all(positions.map(async (position) => {
position.candidates = await Promise.all(position.candidates
.map(async (candidate) => {
Expand Down Expand Up @@ -212,7 +215,7 @@
const headersNames = constants.CANDIDATE_FIELDS;
const headers = req.query.select.map((field) => headersNames[field]);

let applications = await Candidate.findAll({
const applications = await Candidate.findAll({
where: {
'$position.event_id$': req.event.id,
...applicationsFilter,
Expand All @@ -223,14 +226,23 @@
}]
});

applications = await Promise.all(applications
.map(async (application) => {
const user = await core.fetchApplicationUser(application.user_id);
let mails = [];

application.dataValues.notification_email = user.notification_email;
if (applications.length > 0) {
const userIds = applications.map((application) => application.user_id).toString();
mails = await core.getMails(req, userIds);
}

for (const application of applications) {
const user = mails.find((m) => application.user_id === m.id);

return application;
}));
if (!user) {
logger.warn({ user_id: application.user_id }, 'Could not find user');
continue;
}

application.dataValues.notification_email = user.notification_email;

Check warning on line 244 in lib/positions.js

View check run for this annotation

Codecov / codecov/patch

lib/positions.js#L244

Added line #L244 was not covered by tests
}

const resultArray = applications
.map((application) => application.toJSON())
Expand Down