Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix admin results bug #415

Merged
merged 2 commits into from
Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions v2/client/src/components/common/BehavioralInsight/Trainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ function drawBarValues() {

class BehavioralTrainerResults extends Component {
componentDidMount() {
const { trainerId, fetchbehavioralInsight } = this.props;

fetchbehavioralInsight(`/api/behavioral-insight/trainer/${trainerId}`);
const { trainerId, fetchbehavioralInsight, role } = this.props;
if (role === 'admin') {
fetchbehavioralInsight('/api/behavioral-insight/admin');
} else {
fetchbehavioralInsight(`/api/behavioral-insight/trainer/${trainerId}`);
}
}

render() {
Expand Down
10 changes: 8 additions & 2 deletions v2/client/src/components/pages/UserResults/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ const panels = {
reach: { text: 'Reach', render: props => <Reach data={props.results} /> },
behavior: {
text: 'Behavioural',
render: props => <TrainerBehavioralInsight trainerId={props.trainerId} />,
render: props => (
<TrainerBehavioralInsight trainerId={props.trainerId} {...props} />
),
},
feedback: {
text: 'Trainer feedback',
Expand Down Expand Up @@ -153,7 +155,11 @@ class UserResults extends Component {
>
{Object.keys(panels).map(panel => (
<Panel header={panels[panel].text} key={panel}>
{panels[panel].render({ results, trainerId: selectedUserId })}
{panels[panel].render({
results,
trainerId: selectedUserId,
role,
})}
</Panel>
))}
</Collapse>
Expand Down
3 changes: 2 additions & 1 deletion v2/server/__test__/database/queries/feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ describe('Test trainer feedback query', () => {
done();
});
});

test('throws an error with invalid trainerId', async done => {
feedback('dont exist').catch(err => {
feedback('trainerId dont exist', 'sessionId dont exist').catch(err => {
expect(err).toBeDefined();
done();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const boom = require('boom');
const getTrainerBehavioral = require('../../database/queries/behavioralInsight/trainer');

const { getAllPins } = require('../../database/queries/users');

const trainerBehavioralFormulae = require('../../helpers/trainerBehavioral');

module.exports = async (req, res, next) => {
const { role } = req.user;
if (role !== 'admin') {
return next(boom.forbidden());
}
try {
const results = await getAllPins();

const PINs = results.reduce((prev, curr) => {
prev.push(curr.PIN);
return prev;
}, []);

return getTrainerBehavioral(PINs)
.then(result => {
const calculatedFormulae = trainerBehavioralFormulae(result);
res.json({ ...calculatedFormulae });
})
.catch(err => {
return next(boom.badImplementation('error in getting the data'));
});
} catch (error) {
return next(boom.badImplementation());
}
};
3 changes: 0 additions & 3 deletions v2/server/controllers/survey/storeSurvey.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ module.exports = async (req, res, next) => {
try {
const { errors, isValid } = await validateSurveyInput(req.body);

console.log('server reached', req.body);

const {
PIN,
sessionId,
Expand Down Expand Up @@ -62,7 +60,6 @@ module.exports = async (req, res, next) => {

return res.status(200).json({});
} catch (error) {
console.log('ERRO', error);
return next(boom.badImplementation());
}
};
178 changes: 117 additions & 61 deletions v2/server/database/queries/feedback/feedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,77 +8,133 @@ const mongoose = require('mongoose');
const Response = require('../../models/Response');

module.exports.feedback = async (trainerId, sessionId, surveyType) => {
const match = () => {
// feedback for individ. survey
if (sessionId && surveyType) {
return {
$and: [{ session: mongoose.Types.ObjectId(sessionId) }, { surveyType }],
};
}
// feedback for indiv. session
if (sessionId) {
let feedbackArray;
if (!sessionId && !surveyType) {
// for admin feedback
feedbackArray = await Response.aggregate([
// get all answers for responses
{
$lookup: {
from: 'answers',
localField: '_id',
foreignField: 'response',
as: 'answers',
},
},
{
$project: {
_id: 0,
answers: 1,
surveyType: 1,
},
},
{ $unwind: '$answers' },
// get all questions for response answers
{
$lookup: {
from: 'questions',
localField: 'answers.question',
foreignField: '_id',
as: 'questions',
},
},
{ $unwind: '$questions' },
{
$match: {
$and: [
{
$or: [
{ 'questions.group.text': 'about your trainer' },
{ 'questions.group.text': 'about your usual way of teaching' },
],
},
],
},
},
{
$project: {
surveyType: 1,
questionText: '$questions.text',
answer: '$answers.answer',
},
},
]);
} else {
const match = () => {
// feedback for individ. survey
if (sessionId && surveyType) {
return {
$and: [
{ session: mongoose.Types.ObjectId(sessionId) },
{ surveyType },
],
};
}
// feedback for indiv. session
if (sessionId) {
return {
session: mongoose.Types.ObjectId(sessionId),
};
}
// feedback for indiv. trainer
return {
session: mongoose.Types.ObjectId(sessionId),
trainers: mongoose.Types.ObjectId(trainerId),
};
}
// feedback for indiv. trainer
return {
trainers: mongoose.Types.ObjectId(trainerId),
};
};

const feedbackArray = await Response.aggregate([
{
$match: match(),
},
feedbackArray = await Response.aggregate([
{
$match: match(),
},

// get all answers for responses
{
$lookup: {
from: 'answers',
localField: '_id',
foreignField: 'response',
as: 'answers',
// get all answers for responses
{
$lookup: {
from: 'answers',
localField: '_id',
foreignField: 'response',
as: 'answers',
},
},
},
{
$project: {
_id: 0,
answers: 1,
surveyType: 1,
{
$project: {
_id: 0,
answers: 1,
surveyType: 1,
},
},
},
{ $unwind: '$answers' },
// get all questions for response answers
{
$lookup: {
from: 'questions',
localField: 'answers.question',
foreignField: '_id',
as: 'questions',
{ $unwind: '$answers' },
// get all questions for response answers
{
$lookup: {
from: 'questions',
localField: 'answers.question',
foreignField: '_id',
as: 'questions',
},
},
},
{ $unwind: '$questions' },
{
$match: {
$and: [
{
$or: [
{ 'questions.group.text': 'about your trainer' },
{ 'questions.group.text': 'about your usual way of teaching' },
],
},
],
{ $unwind: '$questions' },
{
$match: {
$and: [
{
$or: [
{ 'questions.group.text': 'about your trainer' },
{ 'questions.group.text': 'about your usual way of teaching' },
],
},
],
},
},
},
{
$project: {
surveyType: 1,
questionText: '$questions.text',
answer: '$answers.answer',
{
$project: {
surveyType: 1,
questionText: '$questions.text',
answer: '$answers.answer',
},
},
},
]);
]);
}

// group array by question text
// {questionTxt: [{surveyType, questionTxt, answer}, ...], ...}
Expand Down
2 changes: 2 additions & 0 deletions v2/server/database/queries/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ module.exports.findUserByToken = token =>
'resetToken.value': token,
'resetToken.expiresIn': { $gt: Date.now() },
});

module.exports.getAllPins = () => Participant.find({}, { PIN: 1 });
10 changes: 8 additions & 2 deletions v2/server/database/queries/users/loaclLead.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ const getTrainerGroupSurveys = async leadId => {
participants: 0,
type: 'Post Session 3',
},
'pre-special': {
_id: 'pre-special',
responses: 0,
participants: 0,
type: 'Pre 2-day Intensive',
},
'post-special': {
_id: 'post-special',
responses: 0,
Expand Down Expand Up @@ -109,7 +115,7 @@ const getTrainerGroupSurveys = async leadId => {
},
};

uniqueResponses.map(response => {
uniqueResponses.forEach(response => {
result[response.surveyType]._id = response.surveyType;
result[response.surveyType].responses += 1;
result[response.surveyType].participants += response.participants;
Expand Down Expand Up @@ -174,7 +180,7 @@ const getTrainerGroupSessions = async leadId => {
},
};

uniqueSessions.map(session => {
uniqueSessions.forEach(session => {
result[session.type]._id = session.type;
result[session.type].sessions += 1;
result[session.type].participants += session.numberOfAttendees;
Expand Down
7 changes: 7 additions & 0 deletions v2/server/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const usersRouter = require('./users');
const getParticipantBehavioralInsight = require('./../controllers/behavioralInsight/getParticipantBehavioralInsight');
const getSessionBehavioralInsight = require('./../controllers/behavioralInsight/getSessionBehavioralInsight');
const getTrainerBehavioralInsight = require('./../controllers/behavioralInsight/getTrainerBehavioralInsight');
const getAdminBehavioralInsight = require('./../controllers/behavioralInsight/getAdminBehavioralInsight');
const getFeedback = require('../controllers/feedback/getFeedback');
const logoutController = require('../controllers/logout');

Expand Down Expand Up @@ -66,6 +67,12 @@ router.get(
);
router.get('/behavioral-insight/trainer/:id', getTrainerBehavioralInsight);

router.get(
'/behavioral-insight/admin',
authentication(),
getAdminBehavioralInsight
);

router.post('/feedback/', getFeedback);

router.get('/feedback/participant/:PIN', feedbackFromParticipant);
Expand Down