-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from andela/ft-users-provide-feedback-167733848
167733848: Users provide feedback
- Loading branch information
Showing
7 changed files
with
231 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* eslint-disable require-jsdoc */ | ||
import models from '../models'; | ||
|
||
const { AccommodationFeedback } = models; | ||
|
||
export default class FeedbackController { | ||
/** | ||
* @param {Object} req the user's feedback | ||
* @param {Object} res success or error object | ||
* @returns {Object} details of the posted feedback | ||
* @description Post a new feedback about an accommodation | ||
*/ | ||
static async postFeedback(req, res) { | ||
try { | ||
const userId = req.user.id; | ||
const { feedbackBody } = req.body; | ||
const { accommodationId } = req.params; | ||
const newFeedback = await AccommodationFeedback.create({ | ||
feedbackBody, accommodationId, userId | ||
}); | ||
|
||
|
||
res.status(201).json({ | ||
status: 'success', | ||
data: { | ||
newFeedback: newFeedback.feedbackBody, | ||
'User ID': newFeedback.userId | ||
} | ||
}); | ||
} catch (error) { | ||
res.status(500).json({ status: 'error', error: error.message }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/database/migrations/20190915135223-create-accomodation-feedback.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('AccommodationFeedbacks', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
feedbackBody: { | ||
type: Sequelize.STRING | ||
}, | ||
accommodationId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Accommodation', | ||
key: 'id', | ||
as: 'accommodationId', | ||
}, | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
as: 'userId', | ||
}, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
|
||
down: (queryInterface) => queryInterface.dropTable('AccommodationFeedbacks') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* eslint-disable no-mixed-operators */ | ||
import helpers from '../helpers/Helpers'; | ||
|
||
|
||
const { extractErrors } = helpers; | ||
|
||
export default (req, res, next) => { | ||
/** | ||
* @param {Object} req the user feedback | ||
* @param {Object} res summary of the above feedback | ||
* @param {function} next pass control to next middleware | ||
* @description Validate the user's feedback inputs | ||
*/ | ||
req | ||
.checkParams('accommodationId', 'The accommodation ID must be a number') | ||
.notEmpty() | ||
.isInt(); | ||
const errors = req.validationErrors(); | ||
if (errors) { | ||
return res.status(400).json({ | ||
status: 'error', | ||
error: extractErrors(errors), | ||
}); | ||
} | ||
return next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
export default (sequelize, DataTypes) => { | ||
const AccommodationFeedback = sequelize.define('AccommodationFeedback', { | ||
feedbackBody: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
deletedAt: { | ||
type: DataTypes.DATE, | ||
allowNull: true | ||
} | ||
}, { | ||
paranoid: true | ||
}); | ||
|
||
AccommodationFeedback.associate = (models) => { | ||
AccommodationFeedback.belongsTo(models.Accommodation, { | ||
foreignKey: 'accommodationId', | ||
onDelete: 'CASCADE', | ||
}); | ||
AccommodationFeedback.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
onDelete: 'CASCADE', | ||
}); | ||
}; | ||
|
||
return AccommodationFeedback; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import server from '../index'; | ||
import models from '../models'; | ||
import helper from '../helpers/jwtHelper'; | ||
|
||
const { generateToken } = helper; | ||
|
||
let userId; | ||
let accommodationId; | ||
let token; | ||
|
||
const { | ||
Accommodation, | ||
User | ||
} = models; | ||
|
||
const { expect } = chai; | ||
chai.should(); | ||
chai.use(chaiHttp); | ||
|
||
before((done) => { | ||
// Create a user | ||
User.create({ | ||
firstName: 'TestUser', | ||
lastName: 'Neo', | ||
email: 'testEmail@domain.com', | ||
password: 'password', | ||
isVerified: true | ||
}).then((user) => { | ||
userId = user.id; | ||
token = generateToken({ | ||
id: userId, | ||
email: user.email, | ||
isVerified: user.isVerified | ||
}); | ||
|
||
Accommodation.create({ | ||
userId, | ||
name: 'Kampala', | ||
location: 'Southern Uganda', | ||
type: 'chateau', | ||
totalNumberOfRooms: 5, | ||
owner: userId, | ||
}).then((accommodation) => { | ||
accommodationId = accommodation.id; | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('POST /accommodation/:accommodationId/feedback, Creating a new feedback', () => { | ||
it('Should successfully create a feedback and return details of the new feedback', (done) => { | ||
chai.request(server) | ||
.post(`/api/v1/accommodation/${accommodationId}/feedback`) | ||
.set('Authorization', `Bearer ${token}`) | ||
.send({ | ||
feedbackBody: 'This is the feedback body', | ||
userId, | ||
accommodationId | ||
}) | ||
.end((err, res) => { | ||
// eslint-disable-next-line no-unused-expressions | ||
expect(err).to.be.null; | ||
expect(res).to.has.status(201); | ||
expect(res.body.status).to.equal('success'); | ||
expect(res.body).to.haveOwnProperty('data'); | ||
done(); | ||
}); | ||
}); | ||
it('Should return 400 if not token is provided', (done) => { | ||
chai.request(server) | ||
.post('/api/v1/accommodation/1/feedback') | ||
.send({ | ||
feedbackBody: 'This is the feedback body', | ||
userId, | ||
accommodationId | ||
}) | ||
.end((err, res) => { | ||
// eslint-disable-next-line no-unused-expressions | ||
expect(res).to.has.status(400); | ||
expect(res.body).to.haveOwnProperty('status'); | ||
expect(res.body).to.haveOwnProperty('error'); | ||
done(); | ||
}); | ||
}); | ||
}); |