Skip to content

Commit

Permalink
Merge pull request #16 from Billmike/feature/get-user-events
Browse files Browse the repository at this point in the history
feature/get-user-events
  • Loading branch information
Billmike committed Apr 20, 2018
2 parents a06f208 + 51b80d5 commit 4081350
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
* Users can create events
* Users can modify events they own
* Users can delete events they own
* Users can get all the events they own
* Users can get all events in the application
* Users can get all the centers in the application
* Users can search the application for centers using center name or location
* Users can fetch a single center to view all upcoming events
* Users get email notification whenever their events gets canceled.

### Contributing
Receiving no contributions at this time.
32 changes: 31 additions & 1 deletion server/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import validateSignup from '../validators/validateSignup';
import validateSignin from '../validators/validateSignin';
import serverError from '../errorHandler/serverError';

const { User } = db;
const { User, Center, Event } = db;

/**
* Controller class for handling user actions
Expand Down Expand Up @@ -164,6 +164,36 @@ class Users {
});
});
}

/**
* Get user events
*
* @param {object} request - The request object
* @param {object} response - The response object
*
* @returns {object} The user events
*/
static getUserEvents(request, response) {
Event.findAll({
where: {
organizer: request.userDetails.id
}
}).then((userEvents) => {
if (userEvents.length == 0) {
return response.status(200).json({
message: 'You currently have no events created.'
});
}
return response.status(200).json({
message: `You currently have ${userEvents.length} event(s).`,
eventDetails: userEvents
});
}).catch(() => {
return response.status(500).json({
message: serverError
});
});
}
}

export default Users;
5 changes: 5 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ module.exports = (app) => {
'/api/v1/user/profile', sessionControl.isLoggedIn,
sessionControl.isUser, userController.editPassword
);
app.get(
'/api/v1/user/events',
sessionControl.isLoggedIn, sessionControl.isUser,
userController.getUserEvents
);
};
26 changes: 25 additions & 1 deletion server/tests/user.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import supertest from 'supertest';
import { expect } from 'chai';
import app from '../app';
import dummyUser, { nonExistentUser } from './seed/userseed';
import dummyUser, { nonExistentUser, adminUser } from './seed/userseed';

const request = supertest(app);
const signupAPI = '/api/v1/users/signup';
Expand Down Expand Up @@ -244,6 +244,30 @@ describe('Integration tests for Authentication', () => {
});
});
});
describe('Get user events test', () => {
it('should return an empty array of a user has no events', (done) => {
request.get(`/api/v1/user/events?token=${dummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.end((error, response) => {
expect(response.status).to.equal(200);
done();
});
});
it('should return the events of a user', (done) => {
request.get(`/api/v1/user/events?token=${adminUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.end((error, response) => {
expect(response.status).to.equal(200);
expect(response.body.message).to
.equal('You currently have 3 event(s).');
done();
});
});
});
describe('User profile test', () => {
it('should return an error if the user is not found', (done) => {
request.put(`/api/v1/user/profile?token=${nonExistentUser.token}`)
Expand Down

0 comments on commit 4081350

Please sign in to comment.