Skip to content

Commit

Permalink
Merge 2aa68e4 into 2b57644
Browse files Browse the repository at this point in the history
  • Loading branch information
alainmateso committed Nov 11, 2019
2 parents 2b57644 + 2aa68e4 commit da35efe
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"test": "npm run rollback && npm run migrate && npm run seed && npm run test-suite",
"test:destinations": "npm run rollback && npm run migrate && npm run seed && npm run destination-suite",
"destination-suite": "NODE_ENV=test nyc --reporter=html --reporter=text mocha src/tests/destinationTests.spec.js --require @babel/register --timeout 20000 --exit",
"test-suite": "NODE_ENV=test nyc --reporter=html --reporter=text mocha src/tests/index.spec.js --require @babel/register --timeout 20000 --exit",
"test-suite": "NODE_ENV=test nyc --reporter=html --reporter=text mocha src/tests/bookmarkTests.spec.js --require @babel/register --timeout 20000 --exit",
"coveralls": "nyc report --reporter=text-lcov| coveralls",
"localbuild": "babel src --out-dir lib --copy-files --source-maps --watch"
},
Expand Down
17 changes: 17 additions & 0 deletions src/controllers/bookmarksController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import responseUtil from '../utils/responseUtil';
import strings from '../utils/stringsUtil';
import findBookmarks from '../services/bookmarksServices';

const { YOUR_BOOKMARKS, NO_BOOKMARKS } = strings.bookmarks;

class BoookmarksController {
static async viewBookmarks(req, res) {
const { id } = req.user.payload;
const bookmarks = await findBookmarks({ userId: id });

return responseUtil(res, 200, (!bookmarks.length)
? NO_BOOKMARKS : YOUR_BOOKMARKS, bookmarks);
}
}

export default BoookmarksController;
30 changes: 30 additions & 0 deletions src/database/migrations/20191111154330-create-bookmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('bookmarks', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER
},
accommodationId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATEONLY
},
updatedAt: {
allowNull: false,
type: Sequelize.DATEONLY
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('bookmarks');
}
};
16 changes: 16 additions & 0 deletions src/database/models/bookmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const bookmarks = sequelize.define('bookmarks', {
userId: DataTypes.INTEGER,
accommodationId: DataTypes.INTEGER
}, {});
bookmarks.associate = function(models) {
// associations can be defined here
bookmarks.belongsTo(models.accommodations, {
as: 'accommodation',
sourceKey: 'accommodationId',
targetKey: 'id',
});
};
return bookmarks;
};
17 changes: 17 additions & 0 deletions src/database/seeders/20191111164134-defaultBookmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => Promise.all([
queryInterface.bulkInsert('bookmarks', [
{
userId: 3,
accommodationId: 1,
createdAt: new Date(),
updatedAt: new Date()
}
])
]),
down: (queryInterface, Sequelize) => Promise.all([
queryInterface.bulkDelete('bookmarks', null, {})
])
};
3 changes: 2 additions & 1 deletion src/middlewares/requests/relationVerification.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import bookingsChecker from './bookingsVerification';
import destinationChecker from './destinationVerification';
import validationErrorFormatter from '../../utils/validationErrorFormatter';
import Utilities from '../../utils/index';
import strings from '../../utils/stringsUtil';

export default async (req, res, next) => {

Expand Down Expand Up @@ -34,7 +35,7 @@ export default async (req, res, next) => {
if (!result) {
return Utilities.responseHelper(
res,
Utilities.stringsHelper.validation.requests.locations.DOES_NOT_EXIST,
strings.request.error.NO_LOCATION,
null,
400
);
Expand Down
11 changes: 11 additions & 0 deletions src/routes/api/bookmarks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import express from 'express';
import validateToken from '../../middlewares/auth/validateToken';
import BookmarksController from '../../controllers/bookmarksController';

const router = express.Router();

const { viewBookmarks } = BookmarksController;

router.get('/', validateToken, viewBookmarks);

export default router;
2 changes: 2 additions & 0 deletions src/routes/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import swaggerRoute from '../swagger-doc';
import ratingRoutes from './ratings';
import destinationRoutes from './destinations';
import chatRoutes from './chats';
import bookmarksRoutes from './bookmarks';

const router = new Router();
router.use('/auth', authRoutes);
Expand All @@ -23,5 +24,6 @@ router.use('/comments', comments);
router.use('/api-docs', swaggerRoute);
router.use('/destinations', destinationRoutes);
router.use('/chats', chatRoutes);
router.use('/bookmarks', bookmarksRoutes);

export default router;
26 changes: 26 additions & 0 deletions src/services/bookmarksServices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import db from '../database/models';
import responseUtil from '../utils/responseUtil';

const findBookmarks = data => {
try {
const myBookmarks = db.bookmarks.findAll({
where: data,
attributes: { exclude: ['userId', 'accommodationId'] },
include: [{
model: db.accommodations,
as: 'accommodation',
attributes: { exclude: ['owner', 'locationId'] },
include: [
{ model: db.users, as: 'ownerUser', attributes: ['id', 'username', 'email'] },
{ model: db.locations, as: 'accommodationLocation', attributes: ['id', 'name'] },
],
}],
});
return myBookmarks;
} catch (error) {
return responseUtil(500, 'Ooops Something unexpected happened!');
}

};

export default findBookmarks;
59 changes: 59 additions & 0 deletions src/tests/bookmarkTests.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import chai from 'chai';
import chaiHttp from 'chai-http';
import { describe, it } from 'mocha';
import app from '../index';
import generateToken from '../utils/generateToken';
import mockData from './mockData/mockData';

chai.should();
chai.use(chaiHttp);

const userToken = generateToken(mockData.verifiedUser1);
console.log(userToken);

const userToken2 = generateToken(mockData.verifiedUser3);
const wrongToken = 'wrongToken'

describe("Bookmarks tests", ()=> {

it('it should retrieve all bookmarks for the logged in user', done => {
chai.request(app)
.get('/api/v1/bookmarks')
.set('Authorization', `Bearer ${userToken}`)
.end((err, res) => {
console.log(userToken);
res.should.have.status(200);
done();
});
});

it('it should reject an invalid token', done => {
chai.request(app)
.get('/api/v1/bookmarks')
.set('Authorization', `Bearer ${wrongToken}`)
.end((err, res) => {
res.should.have.status(400);
done();
});
});
it('it should reject a missing token', done => {
chai.request(app)
.get('/api/v1/bookmarks')
.end((err, res) => {
res.should.have.status(401);
done();
});
});

it('it should tell the user that no bookmarks were found', done => {
chai.request(app)
.get('/api/v1/bookmarks')
.set('Authorization', `Bearer ${userToken2}`)
.end((err, res) => {
console.log(res.body);

res.should.have.status(200);
done();
});
});
});
3 changes: 2 additions & 1 deletion src/tests/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import destinationTests from './destinationTests.spec';
import chatTests from './chatTests.spec';
import RequestStats from './RequestStats.spec'
import bookingTests from './bookingsTests.spec';
import bookmarkTests from './bookmarkTests.spec';

describe('Default Tests', defaultTests);
describe('Edit Request Tests', editRequest);
Expand All @@ -41,4 +42,4 @@ describe('Destination Tests', destinationTests);
describe('Chat Tests', chatTests);
describe('RequestStats Tests', RequestStats);
describe('Booking Tests', bookingTests);

describe('Bookmarks Tests', bookmarkTests);
7 changes: 6 additions & 1 deletion src/utils/stringsUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const strings = {
NOT_ALLOWED: 'Access denied! a supplier can not access this part of the system!',
TRAVEL_ADMINS_ONLY: 'Access denied! Only travel administrators can access this part of the system!',
NOT_YOUR_REQUEST: 'You are not the owner of the request or the line manager of the user who placed it!',
NO_LOCATION: 'Location does not exist on the system!',
},
},
token: {
Expand Down Expand Up @@ -142,7 +143,11 @@ const strings = {
chat: {
EMPTY_HISTORY: 'Chat history is empty!',
CHAT_HISTORY: 'Chats retrieved successfully!',
}
},
bookmarks: {
YOUR_BOOKMARKS: 'Your bookmarks are retrieved successfully!',
NO_BOOKMARKS: 'No bookmarks found!',
},
};

export default strings;

0 comments on commit da35efe

Please sign in to comment.