diff --git a/lib/constants.js b/lib/constants.js index ec8b5dad..d2fe6ec0 100644 --- a/lib/constants.js +++ b/lib/constants.js @@ -31,6 +31,7 @@ module.exports = { 'max_participants', 'application_status', 'status', + 'publication_date', 'meals_per_day', 'accommodation_type', 'optional_programme', diff --git a/lib/events.js b/lib/events.js index 5e0fda07..1c88e973 100644 --- a/lib/events.js +++ b/lib/events.js @@ -276,6 +276,10 @@ exports.setApprovalStatus = async (req, res) => { } }); + if (req.event.status === 'published') { + await req.event.update({ publication_date: new Date() }, { transaction: t }); + } + // If the new status is submitted and the old status is draft, send a mail // to those who have permissions (EQAC/CD) if (oldStatus !== 'draft' || req.event.status !== 'submitted') { diff --git a/migrations/20211021185321-add-publication-date-to-event.js b/migrations/20211021185321-add-publication-date-to-event.js new file mode 100644 index 00000000..532ce889 --- /dev/null +++ b/migrations/20211021185321-add-publication-date-to-event.js @@ -0,0 +1,12 @@ +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.addColumn( + 'events', + 'publication_date', + { type: Sequelize.DATE, allowNull: true } + ); + }, + down: (queryInterface) => { + queryInterface.removeColumn('events', 'publication_date'); + } +}; diff --git a/models/Event.js b/models/Event.js index 1b358aa7..b94a51d2 100644 --- a/models/Event.js +++ b/models/Event.js @@ -208,6 +208,13 @@ const Event = sequelize.define('event', { } } }, + publication_date: { + type: Sequelize.DATE, + allowNull: true, + validate: { + isDate: { msg: 'Event publication date should be valid.' } + } + }, deleted: { type: Sequelize.BOOLEAN, allowNull: false, diff --git a/test/api/events-approval.test.js b/test/api/events-approval.test.js index b6d64e2b..56b2fc08 100644 --- a/test/api/events-approval.test.js +++ b/test/api/events-approval.test.js @@ -117,6 +117,27 @@ describe('Events status change', () => { const eventFromDb = await Event.findByPk(event.id); expect(eventFromDb.status).not.toEqual('submitted'); }); + + it('should not set the publication date', async () => { + const event = await generator.createEvent({ + status: 'draft', + organizers: [{ user_id: 1337, first_name: 'test', last_name: 'test' }] + }); + + const res = await request({ + uri: '/single/' + event.id + '/status', + headers: { 'X-Auth-Token': 'foobar' }, + method: 'PUT', + body: { status: 'submitted' } + }); + + expect(res.statusCode).toEqual(200); + expect(res.body.success).toEqual(true); + expect(res.body).toHaveProperty('message'); + + const eventFromDb = await Event.findByPk(event.id); + expect(eventFromDb.publication_date).toBeNull(); + }); }); describe('draft -> published', () => { @@ -207,6 +228,27 @@ describe('Events status change', () => { const eventFromDb = await Event.findByPk(event.id); expect(eventFromDb.status).not.toEqual('published'); }); + + it('should set the publication date', async () => { + const event = await generator.createEvent({ + status: 'submitted', + organizers: [{ user_id: 1337, first_name: 'test', last_name: 'test' }] + }); + + const res = await request({ + uri: '/single/' + event.id + '/status', + headers: { 'X-Auth-Token': 'foobar' }, + method: 'PUT', + body: { status: 'published' } + }); + + expect(res.statusCode).toEqual(200); + expect(res.body.success).toEqual(true); + expect(res.body).toHaveProperty('message'); + + const eventFromDb = await Event.findByPk(event.id); + expect(eventFromDb.publication_date).not.toBeNull(); + }); }); describe('submitted -> draft', () => {