-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows a super admin/travel admin to get all feedback for an accommodation [Finishes #168250889]
- Loading branch information
1 parent
1e7e090
commit 4fd3c36
Showing
7 changed files
with
250 additions
and
7 deletions.
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,41 @@ | ||
import models from '../db/models'; | ||
import Response from '../utils/response.utils'; | ||
|
||
const { Feedback, Facility, User } = models; | ||
|
||
/** | ||
* This class controls the feedback for facilities | ||
*/ | ||
export default class FeedbackController { | ||
/** | ||
* @param {Object} req Contains the param from the URL | ||
* @param {Object} res List of responses returned to the user | ||
* @returns {array} An array of objects containing feedback | ||
*/ | ||
static findAll(req, res) { | ||
Facility.findByPk(req.params.id) | ||
.then((data) => { | ||
if (!data) { | ||
return Response.CustomError(res, 404, 'error', | ||
`No facility found with ID: ${req.params.id}`, 'Consult the facility Admin'); | ||
} | ||
Feedback.findAll({ | ||
where: { facility_id: req.params.id }, | ||
attributes: ['id', 'facility_id', 'feedback', 'created_at', 'updated_at'], | ||
include: [{ | ||
model: User, | ||
as: 'user', | ||
attributes: ['id', 'first_name', 'last_name'] | ||
}], | ||
}) | ||
.then((data) => { | ||
if (data.length) { | ||
return Response.Success(res, data, 200); | ||
} | ||
return Response.CustomError(res, 404, 'error', 'No feedback found for this facility', | ||
'Please check again later'); | ||
}); | ||
}) | ||
.catch((err) => Response.InternalServerError(res, err)); | ||
} | ||
} |
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
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,10 @@ | ||
import express from 'express'; | ||
import Auth from '../../middlewares/auth.middleware'; | ||
import FeedbackController from '../../controllers/feedback.controller'; | ||
import Validator from '../../validation/index'; | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/feedback/facility/:id', Auth.verifyToken, Auth.isAdmin, Validator.facilityID, FeedbackController.findAll); | ||
|
||
export default router; |
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,125 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import sinon from 'sinon'; | ||
import Sinonchai from 'sinon-chai'; | ||
import app from '../index'; | ||
import JWTService from '../services/jwt.service'; | ||
import FeedbackController from '../controllers/feedback.controller'; | ||
|
||
chai.use(Sinonchai); | ||
chai.use(chaiHttp); | ||
chai.should(); | ||
|
||
const feedbackEndpoint = '/api/v1/feedback/facility'; | ||
|
||
let superAdminToken = `Bearer ${JWTService.generateToken({ id: 3, role: 'super_admin'})}`; | ||
let travelAdminToken = `Bearer ${JWTService.generateToken({ id: 9, role: 'travel_admin'})}`; | ||
let userToken = `Bearer ${JWTService.generateToken({ id: 1, role: 'requester'})}`; | ||
|
||
describe('/GET FEEDBACK', () => { | ||
|
||
it('should allow only logged in users', (done) => { | ||
chai.request(app) | ||
.get(`${feedbackEndpoint}/1`) | ||
.set('Authorization', '') | ||
.end((err, res) => { | ||
res.should.have.status(403); | ||
res.body.should.have.property('status').eql('error'); | ||
done(); | ||
}) | ||
}); | ||
|
||
it('should allow Super Admins', (done) => { | ||
chai.request(app) | ||
.get(`${feedbackEndpoint}/1`) | ||
.set('Authorization', superAdminToken) | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
res.body.should.have.property('status').eql('success'); | ||
done(); | ||
}) | ||
}); | ||
|
||
it('should allow Travel Admins', (done) => { | ||
chai.request(app) | ||
.get(`${feedbackEndpoint}/1`) | ||
.set('Authorization', travelAdminToken) | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
res.body.should.have.property('status').eql('success'); | ||
done(); | ||
}) | ||
}); | ||
|
||
it('should deny users who are NOT Super Admins', (done) => { | ||
chai.request(app) | ||
.get(`${feedbackEndpoint}/1`) | ||
.set('Authorization', userToken) | ||
.end((err, res) => { | ||
res.should.have.status(401); | ||
res.body.should.have.property('status').eql('error'); | ||
done(); | ||
}) | ||
}); | ||
|
||
it('should return 404 if facility does not exist', (done) => { | ||
chai.request(app) | ||
.get(`${feedbackEndpoint}/1000`) | ||
.set('Authorization', superAdminToken) | ||
.end((err, res) => { | ||
res.should.have.status(404); | ||
res.body.should.have.property('status').eql('error'); | ||
done(); | ||
}) | ||
}); | ||
|
||
it('should return 404 if no feedback is found for a facility', (done) => { | ||
chai.request(app) | ||
.get(`${feedbackEndpoint}/3`) | ||
.set('Authorization', superAdminToken) | ||
.end((err, res) => { | ||
res.should.have.status(404); | ||
res.body.should.have.property('status').eql('error'); | ||
done(); | ||
}) | ||
}); | ||
|
||
it('should return 200 if feedback is found for a facility', (done) => { | ||
chai.request(app) | ||
.get(`${feedbackEndpoint}/1`) | ||
.set('Authorization', superAdminToken) | ||
.end((err, res) => { | ||
res.should.have.status(200); | ||
res.body.should.have.property('status').eql('success'); | ||
done(); | ||
}) | ||
}); | ||
|
||
it('should return 400 on invalid facility ID', (done) => { | ||
chai.request(app) | ||
.get(`${feedbackEndpoint}/a`) | ||
.set('Authorization', superAdminToken) | ||
.end((err, res) => { | ||
res.should.have.status(400); | ||
res.body.should.have.property('status').eql('error'); | ||
done(); | ||
}) | ||
}); | ||
|
||
it('should return 500 if something goes wrong', (done) => { | ||
const req = { | ||
params: {} | ||
}; | ||
|
||
const res = { | ||
status() {}, | ||
send() {} | ||
}; | ||
|
||
sinon.stub(res, 'status').returnsThis(); | ||
|
||
FeedbackController.findAll(req, res); | ||
(res.status).should.have.callCount(0); | ||
done(); | ||
}); | ||
}); |
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