Skip to content

Commit

Permalink
Merge pull request #17 from Billmike/bug/modify-event
Browse files Browse the repository at this point in the history
bug/fix modify event
  • Loading branch information
Billmike committed Apr 20, 2018
2 parents 4081350 + 73354b9 commit b6e7c60
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
74 changes: 65 additions & 9 deletions server/controllers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,71 @@ class Events {
message: 'You do not have the privilege to modify this resource'
});
}
return event.update({
name: request.body.name || event.name,
image: request.body.image || event.image,
date: request.body.date || event.date,
duration: request.body.duration || event.duration
}).then((updatedEvent) => {
return response.status(201).json({
message: 'Event modified successfully.',
eventDetails: updatedEvent
const duration = timeDifference(
request.body.startTime,
request.body.endTime
);
const eventDuration =
`${duration.hours}hour(s), ${duration.minutes}minutes`;
const convertStartTime = new Date(request.body.startTime)
.toLocaleTimeString('en-US', { hour12: false });
const convertEndTime = new Date(request.body.endTime)
.toLocaleTimeString('en-US', { hour12: false });

Center.findOne({
where: {
name: request.body.venue
}
}).then((foundCenter) => {
if (!foundCenter) {
return response.status(400).json({
message: 'Center not found'
});
}

Event.findAll({
where: {
date: request.body.date,
venue: foundCenter.dataValues.id
}
}).then((foundEvent) => {
for (let index = 0; index < foundEvent.length; index += 1) {
const event = foundEvent[index];
if ((convertStartTime >= event.startTime
&& convertStartTime <= event.endTime) ||
(convertEndTime <= event.endTime
&& convertEndTime >= event.startTime)) {
return response.status(409).json({
message:
`Sorry, you cannot book an event at this
time because there will be an event
holding between ${event.dataValues.startTime} and ${event.dataValues.endTime}.`
});
}
}
return event.update({
name: request.body.name || event.name,
image: request.body.image || event.image,
date: request.body.date || event.date,
duration: eventDuration || event.duration,
startTime: request.body.startTime || event.startTime,
endTime: request.body.endTime || event.endTime,
venue: foundCenter.dataValues.id || event.venue
}).then((updatedEvent) => {
return response.status(201).json({
message: 'Event modified successfully.',
eventDetails: {
name: updatedEvent.name,
image: updatedEvent.image,
date: updatedEvent.date,
duration: updatedEvent.duration,
startTime: updatedEvent.startTime,
endTime: updatedEvent.endTime,
venue: foundCenter.dataValues.name,
organizer: updatedEvent.organizer
}
});
});
});
});
}).catch((err) => {
Expand Down
5 changes: 4 additions & 1 deletion server/tests/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,14 @@ describe('Tests for Events API', () => {
}
);
it('should edit an event successfully', (done) => {
const testEvent = { ...eventSeed };
testEvent.startTime = '2018-04-19 13:00:00 +01:00';
testEvent.endTime = '2018-04-19 15:00:00 +01:00';
request.put(`/api/v1/event/4?token=${secondDummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.send(eventSeed)
.send(testEvent)
.end((error, response) => {
expect(response.status).to.equal(201);
expect(response.body).to.be.an('object');
Expand Down

0 comments on commit b6e7c60

Please sign in to comment.