Skip to content

Commit

Permalink
fix(event): add validation TODO and publication flow (#48)
Browse files Browse the repository at this point in the history
* fix(submission): add TODOs for validation

* fix(event): add publication flow

* chore(eslint): fix linter

* fix(event): list when published

* chore(event): add theme explanation to min
  • Loading branch information
WikiRik committed Mar 16, 2021
1 parent b73654f commit 2507cde
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 64 deletions.
44 changes: 38 additions & 6 deletions lib/constants.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,54 @@
module.exports = {
EVENT_PUBLIC_FIELDS: [
EVENT_MINIMAL_FIELDS: [
'id',
'name',
'url',
'image',
'description',
'application_starts',
'application_ends',
'email',
'starts',
'ends',
'fee',
'organizing_bodies',
'locations',
'type',
'questions',
'theme_category',
'theme',
'max_participants',
'application_status',
'status'
'accommodation_type',
'optional_fee',
'optional_programme'
],
EVENT_FULL_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'
],
EVENT_TYPES: ['regular', 'pilot'],
CURRENT_USER_PREFIX: 'me'
Expand Down
38 changes: 34 additions & 4 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ exports.listEvents = async (req, res) => {
// Get default query obj.
const defaultQueryObj = helpers.getDefaultQuery(req);

// Applying custom filter: deleted = false, status === published.
// Applying custom filter: deleted = false, published != none.
const queryObj = merge(defaultQueryObj, {
where: {
deleted: false,
status: 'published'
published: { [Sequelize.Op.not]: 'none' }
}
});

Expand Down Expand Up @@ -72,6 +72,7 @@ exports.addEvent = async (req, res) => {
const data = req.body;
delete data.id;
delete data.deleted;
delete data.published;

data.status = 'first submission';

Expand Down Expand Up @@ -128,7 +129,6 @@ exports.addEvent = async (req, res) => {
});
};

// TODO: Should be 3 seperate actions, show_min (after first submission), show (after second submission), show_approval (for SUCT)
exports.eventDetails = async (req, res) => {
if (!req.permissions.see_summeruniversity) {
return errors.makeForbiddenError(res, 'You cannot see this event.');
Expand All @@ -140,7 +140,11 @@ exports.eventDetails = async (req, res) => {
if (!helpers.isOrganizer(event, req.user)
&& !req.permissions.manage_summeruniversity[event.type]
&& !req.permissions.approve_summeruniversity[event.type]) {
event = helpers.whitelistObject(event, constants.EVENT_PUBLIC_FIELDS);
if (req.event.published === 'full') {
event = helpers.whitelistObject(event, constants.EVENT_FULL_FIELDS);
} else {
event = helpers.whitelistObject(event, constants.EVENT_MINIMAL_FIELDS);
}
}

return res.json({
Expand All @@ -163,6 +167,7 @@ exports.editEvent = async (req, res) => {
delete data.id;
delete data.status;
delete data.deleted;
delete data.published;

if (Object.keys(data).length === 0) {
return errors.makeValidationError(res, 'No valid field changes requested');
Expand Down Expand Up @@ -285,3 +290,28 @@ exports.setApprovalStatus = async (req, res) => {
message: 'Successfully changed approval status',
});
};

exports.setPublished = async (req, res) => {
if (!req.permissions.manage_summeruniversity[req.event.type]) {
return errors.makeForbiddenError(res, 'You are not allowed to set the publication.');
}

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

if (['first draft', 'first submission'].includes(req.event.status) && req.body.published === 'minimal') {
return errors.makeForbiddenError(res, 'This event status does not allow a minimal publication');
}

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

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

return res.json({
success: true,
message: 'Successfully changed publication status',
});
};
4 changes: 2 additions & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,13 @@ exports.getEventPermissions = ({ permissions, event, user }) => {
// The event can only be seen to public if it's published and not deleted.
// Otherwise (if it's deleted, submitted or draft) it should be accessible
// only to LOs and those who can approve it.
permissions.see_summeruniversity = (event.status === 'published' && !event.deleted)
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.delete_summeruniversity = permissions.manage_summeruniversity[event.type];

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

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 Down
1 change: 1 addition & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ EventsRouter.put('/', events.editEvent);
EventsRouter.delete('/', events.deleteEvent);
EventsRouter.put('/status', events.setApprovalStatus);
EventsRouter.post('/upload', imageserv.uploadImage);
EventsRouter.put('/published', events.setPublished);

server.use(endpointsMetrics.addEndpointMetrics);
server.use('/', GeneralRouter);
Expand Down
12 changes: 12 additions & 0 deletions migrations/20210314103546-add-published-to-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.addColumn(
'events',
'published',
{
type: Sequelize.STRING,
allowNull: false,
defaultValue: 'none'
},
),
down: (queryInterface) => queryInterface.removeColumn('events', 'published')
};

0 comments on commit 2507cde

Please sign in to comment.