Skip to content

Commit

Permalink
fix(application): use notification_email (#1087)
Browse files Browse the repository at this point in the history
Co-authored-by: WikiRik <[email protected]>
  • Loading branch information
WikiRik and WikiRik authored Mar 9, 2024
1 parent 11ea30f commit eba2167
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 16 deletions.
15 changes: 10 additions & 5 deletions lib/applications.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,13 @@ exports.createApplication = async (req, res) => {
req.body.body_name = req.user.bodies.find((b) => b.id === req.body.body_id).name;
req.body.user_id = req.user.id;
req.body.event_id = req.event.id;
req.body.email = req.user.email;

let newApplication;

// Doing it inside of a transaction, so it'd fail and revert if mail was not sent.
await sequelize.transaction(async (t) => {
newApplication = await Application.create(req.body, { transaction: t });

// We don't need to recalculate the votes amount, as the pax type is not set here.

// Sending the mail to a user.
await mailer.sendMail({
to: req.user.notification_email,
Expand Down Expand Up @@ -102,7 +99,6 @@ exports.updateApplication = async (req, res) => {
}
req.body.user_id = req.user.id;
req.body.event_id = req.event.id;
req.body.email = req.user.email;

await sequelize.transaction(async (t) => {
// Updating application in a transaction, so if mail sending fails, the update would be reverted.
Expand Down Expand Up @@ -196,11 +192,20 @@ exports.exportAll = async (req, res) => {
return errors.makeForbiddenError(res, 'You are not allowed to see statistics.');
}

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

const headersNames = helpers.getApplicationFields(req.event);
const headers = Object.keys(headersNames).map((field) => headersNames[field]);

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

application.dataValues.notification_email = user.notification_email;

return application;
}));

const resultArray = applications
.map((application) => application.toJSON())
.map((application) => helpers.flattenObject(application))
Expand Down
2 changes: 1 addition & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
body_name: 'Body name',
first_name: 'First name',
last_name: 'Last name',
email: 'Email',
notification_email: 'Email',
status: 'Status',
confirmed: 'Confirmed',
attended: 'Attended',
Expand Down
41 changes: 41 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const makeRequest = (options) => {
resolveWithFullResponse: options.resolveWithFullResponse || false
};

if (options.body) {
requestOptions.body = options.body;
}

return request(requestOptions);
};

Expand Down Expand Up @@ -43,6 +47,42 @@ const fetchUser = async (user, token) => {
};
};

const fetchApplicationUser = async (user) => {
// Getting access and refresh token.
const authRequest = await makeRequest({
url: config.core.url + ':' + config.core.port + '/login',
method: 'POST',
body: {
username: config.core.user.login,
password: config.core.user.password
}
});

if (typeof authRequest !== 'object') {
throw new Error('Malformed response when fetching auth request: ' + authRequest);
}

if (!authRequest.success) {
throw new Error('Error fetching auth request: ' + JSON.stringify(authRequest));
}

// Fetching user
const userResponse = await makeRequest({
url: config.core.url + ':' + config.core.port + '/members/' + user,
token: authRequest.access_token,
});

if (typeof userResponse !== 'object') {
throw new Error('Malformed response when fetching user: ' + userResponse);
}

if (!userResponse.success) {
throw new Error('Error fetching user: ' + JSON.stringify(userResponse));
}

return userResponse.data;
};

const fetchBody = async (body, token) => {
// return invalid body as it is, will catch it in Event validation.
if (typeof body !== 'object' || typeof body.body_id !== 'number') {
Expand Down Expand Up @@ -70,5 +110,6 @@ const fetchBody = async (body, token) => {

module.exports = {
fetchUser,
fetchApplicationUser,
fetchBody
};
5 changes: 5 additions & 0 deletions lib/middlewares.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const request = require('request-promise-native');

const Bugsnag = require('./bugsnag');
const core = require('./core');
const errors = require('./errors');
const logger = require('./logger');
const { Event, Application } = require('../models');
Expand Down Expand Up @@ -125,6 +126,10 @@ exports.fetchSingleApplication = async (req, res, next) => {
return errors.makeNotFoundError(res, `Application with id ${req.params.application_id} not found`);
}

const user = await core.fetchApplicationUser(application.user_id);

application.dataValues.notification_email = user.notification_email;

req.application = application;
req.permissions = helpers.getApplicationPermissions({
permissions: req.permissions,
Expand Down
13 changes: 13 additions & 0 deletions migrations/20240118212121-change-application-email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.changeColumn(
'applications',
'email',
Sequelize.STRING
),
down: (queryInterface, Sequelize) => queryInterface.changeColumn(
'applications',
'email',
Sequelize.STRING,
{ allowNull: false }
),
};
8 changes: 2 additions & 6 deletions models/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ const Application = sequelize.define('application', {
}
},
email: {
allowNull: false,
type: Sequelize.STRING,
defaultValue: '',
validate: {
notEmpty: { msg: 'Email should be set.' }
}
allowNull: true,
type: Sequelize.STRING
},
body_name: {
allowNull: false,
Expand Down
4 changes: 0 additions & 4 deletions models/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,6 @@ const Event = sequelize.define(
if (typeof organizer.comment !== 'undefined' && typeof organizer.comment !== 'string') {
throw new Error('comment is malformed.');
}

if (typeof organizer.email !== 'undefined' && typeof organizer.email !== 'string') {
throw new Error('email is malformed.');
}
}
}
}
Expand Down

0 comments on commit eba2167

Please sign in to comment.