Skip to content

Commit

Permalink
feat(events): add covid fields events (#72)
Browse files Browse the repository at this point in the history
* feat(events): add covid fields events

* style: add newline
  • Loading branch information
WikiRik authored May 5, 2021
1 parent d263141 commit 3f1c5d7
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 13 deletions.
34 changes: 34 additions & 0 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,40 @@ module.exports = {
'optional_fee',
'optional_programme'
],
EVENT_COVID_FIELDS: [
'id',
'name',
'url',
'image',
'photos',
'video',
'description',
'email',
'website',
'social_media',
'starts',
'ends',
'fee',
'organizing_bodies',
'locations',
'type',
'theme_category',
'theme',
'trainers',
'pax_description',
'pax_confirmation',
'max_participants',
'activities_list',
'course_level',
'courses',
'special_equipment',
'university_support',
'optional_fee',
'optional_programme',
'covid_regulations',
'cancellation_rules',
'additional_regulation'
],
EVENT_TYPES: ['regular', 'pilot'],
CURRENT_USER_PREFIX: 'me'
};
26 changes: 21 additions & 5 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ exports.listEvents = async (req, res) => {
const whitelistEvents = [];

for (let event of events) {
if (event.published === 'full') {
if (event.published === 'covid') {
event = helpers.whitelistObject(event, constants.EVENT_COVID_FIELDS);
} else if (event.published === 'full') {
event = helpers.whitelistObject(event, constants.EVENT_FULL_FIELDS);
} else {
event = helpers.whitelistObject(event, constants.EVENT_MINIMAL_FIELDS);
Expand Down Expand Up @@ -150,7 +152,9 @@ exports.eventDetails = async (req, res) => {
if (!helpers.isOrganizer(event, req.user)
&& !req.permissions.manage_summeruniversity[event.type]
&& !req.permissions.approve_summeruniversity[event.type]) {
if (req.event.published === 'full') {
if (req.event.published === 'covid') {
event = helpers.whitelistObject(event, constants.EVENT_COVID_FIELDS);
} else if (req.event.published === 'full') {
event = helpers.whitelistObject(event, constants.EVENT_FULL_FIELDS);
} else {
event = helpers.whitelistObject(event, constants.EVENT_MINIMAL_FIELDS);
Expand Down Expand Up @@ -209,6 +213,14 @@ exports.editEvent = async (req, res) => {
}
}

if (['second approval', 'covid draft'].includes(oldStatus)) {
data.status = 'covid submission';

if (!req.permissions.change_status[data.status.replace(' ', '_')]) {
return errors.makeForbiddenError(res, 'You are not allowed to change status.');
}
}

await sequelize.transaction(async (t) => {
// Updating the event in a transaction, so if mail sending fails, the update would be reverted.
await event.update(data, { transaction: t });
Expand All @@ -226,7 +238,7 @@ exports.editEvent = async (req, res) => {
}
});

if (['first draft', 'first approval', 'second draft'].includes(oldStatus)) {
if (['first draft', 'first approval', 'second draft', 'second approval', 'covid draft'].includes(oldStatus)) {
await mailer.sendMail({
to: config.new_event_notifications,
subject: 'A event was submitted.',
Expand Down Expand Up @@ -283,7 +295,7 @@ exports.setApprovalStatus = async (req, res) => {
}
});

if (['first submission', 'second submission'].includes(req.event.status)) {
if (['first submission', 'second submission', 'covid submission'].includes(req.event.status)) {
await mailer.sendMail({
to: config.new_event_notifications,
subject: 'A new event was submitted.',
Expand All @@ -306,7 +318,7 @@ exports.setPublished = async (req, res) => {
return errors.makeForbiddenError(res, 'You are not allowed to set the publication.');
}

if (!['none', 'minimal', 'full'].includes(req.body.published)) {
if (!['none', 'minimal', 'full', 'covid'].includes(req.body.published)) {
return errors.makeBadRequestError(res, 'The wanted event publication status is not valid.');
}

Expand All @@ -318,6 +330,10 @@ exports.setPublished = async (req, res) => {
return errors.makeForbiddenError(res, 'This event status does not allow a full publication');
}

if (req.event.status !== 'covid approval' && req.body.published === 'covid') {
return errors.makeForbiddenError(res, 'This event status does not allow a covid publication');
}

await req.event.update({ published: req.body.published });

return res.json({
Expand Down
16 changes: 13 additions & 3 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,10 @@ exports.getEventPermissions = ({ permissions, event, user }) => {
permissions.see_summeruniversity = (event.published !== 'none' && !event.deleted)
|| canApproveOrIsOrganizer;

permissions.edit_summeruniversity = (event.status !== 'second approval' && exports.isOrganizer(event, user)) || permissions.manage_summeruniversity[event.type];
permissions.edit_summeruniversity = (event.status !== 'covid approval' && exports.isOrganizer(event, user)) || permissions.manage_summeruniversity[event.type];
permissions.delete_summeruniversity = permissions.manage_summeruniversity[event.type];

permissions.apply = event.application_status === 'open' && event.published === 'full';
permissions.apply = event.application_status === 'open' && event.published === 'covid';

permissions.approve_participants = exports.isOrganizer(event, user) || permissions.manage_summeruniversity[event.type];
permissions.set_participants_attended = exports.isOrganizer(event, user) || permissions.manage_summeruniversity[event.type];
Expand All @@ -214,6 +214,11 @@ exports.getEventPermissions = ({ permissions, event, user }) => {
// 7) second submission -> second draft - by those who can approve (reject approval)
// 8) second submission -> second approval - by those who can approve (approve)
// 9) second approval -> second submission - by those who can approve (unpublish)
// 10) second approval -> covid submission - by event creator / LOs (when saving second submission)
// 11) covid draft -> covid submission - by event creator / LOs (when saving covid submission)
// 12) covid submission -> covid draft - by those who can approve (reject approval)
// 13) covid submission -> covid approval - by those who can approve (approve)
// 14) covid approval -> covid submission - by those who can approve (unpublish)
permissions.change_status = {
first_draft: event.status === 'first submission' && canApprove, // 2
first_approval: event.status === 'first submission' && canApprove, // 3
Expand All @@ -223,7 +228,12 @@ exports.getEventPermissions = ({ permissions, event, user }) => {
second_approval: event.status === 'second submission' && canApprove, // 8
second_submission: (event.status === 'second approval' && canApprove) // 9
|| (event.status === 'second draft' && canApproveOrIsOrganizer) // 6
|| (event.status === 'first approval' && canApproveOrIsOrganizer) // 5
|| (event.status === 'first approval' && canApproveOrIsOrganizer), // 5
covid_draft: event.status === 'covid submission' && canApprove, // 12
covid_approval: event.status === 'covid submission' && canApprove, // 13
covid_submission: (event.status === 'covid approval' && canApprove) // 14
|| (event.status === 'covid draft' && canApproveOrIsOrganizer) // 11
|| (event.status === 'second approval' && canApproveOrIsOrganizer) // 10
};

return permissions;
Expand Down
37 changes: 37 additions & 0 deletions migrations/20210504212254-add-covid-to-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn(
'events',
'covid_regulations',
{ type: Sequelize.TEXT, allowNull: true }
);

await queryInterface.addColumn(
'events',
'fee_payment_date',
{ type: Sequelize.DATE, allowNull: true }
);

await queryInterface.addColumn(
'events',
'additional_regulation',
{ type: Sequelize.TEXT, allowNull: true }
);
},
down: async (queryInterface) => {
await queryInterface.removeColumn(
'events',
'covid_regulations'
);

await queryInterface.removeColumn(
'events',
'fee_payment_date'
);

await queryInterface.removeColumn(
'events',
'additional_regulation'
);
}
};
22 changes: 17 additions & 5 deletions models/Event.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,24 +297,24 @@ const Event = sequelize.define('event', {
}
},
status: {
type: Sequelize.ENUM('first draft', 'first submission', 'first approval', 'second draft', 'second submission', 'second approval'),
type: Sequelize.ENUM('first draft', 'first submission', 'first approval', 'second draft', 'second submission', 'second approval', 'covid draft', 'covid submission', 'covid approval'),
allowNull: false,
defaultValue: 'first draft',
validate: {
isIn: {
args: [['first draft', 'first submission', 'first approval', 'second draft', 'second submission', 'second approval']],
args: [['first draft', 'first submission', 'first approval', 'second draft', 'second submission', 'second approval', 'covid draft', 'covid submission', 'covid approval']],
msg: 'Event status is not valid.'
}
}
},
published: {
type: Sequelize.ENUM('none', 'minimal', 'full'),
type: Sequelize.ENUM('none', 'minimal', 'full', 'covid'),
allowNull: false,
defaultValue: 'none',
validate: {
isIn: {
args: [['none', 'minimal', 'full']],
msg: 'Event publication must be none, minimal or full.'
args: [['none', 'minimal', 'full', 'covid']],
msg: 'Event publication must be none, minimal, full or covid.'
}
}
},
Expand Down Expand Up @@ -446,6 +446,18 @@ const Event = sequelize.define('event', {
allowNull: true,
defaultValue: false
},
covid_regulations: {
type: Sequelize.TEXT,
allowNull: true
},
cancellation_rules: {
type: Sequelize.TEXT,
allowNull: true
},
additional_regulation: {
type: Sequelize.TEXT,
allowNull: true
},
agreed_to_su_terms: {
type: Sequelize.BOOLEAN,
allowNull: false,
Expand Down

0 comments on commit 3f1c5d7

Please sign in to comment.