Skip to content

Commit

Permalink
165 Filter statements by createAt() and also fix toggling multiple an…
Browse files Browse the repository at this point in the history
…swers not updating properly (#166)

* initial commit

* fixed bug where user can't toggle multiple I_agree and others_agree at the same time

* added createdAt() filter to replace id filter, examined time-related bugs

* reset emails.js file for production

* deleted comments
  • Loading branch information
acao22 committed Jul 19, 2024
1 parent 4481e22 commit 1b1150f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 27 deletions.
9 changes: 7 additions & 2 deletions client/src/pages/Dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ const Dashboard: React.FC = () => {
// For I_agree: handleCheckboxChange(id, 'I_agree')
// For others_agree: handleCheckboxChange(id, 'others_agree')
const handleCheckboxChange = async (id: number, type: 'I_agree' | 'others_agree') => {
const stateKey = type === 'I_agree' ? 'agreeCheckboxStates' : 'checkboxStates';
const stateKey = type === 'I_agree' ? 'agreeCheckboxStates' : 'othersAgreeCheckboxStates';
const setState = type === 'I_agree' ? setAgreeCheckboxStates : setOthersAgreeCheckboxStates;
const newCheckedState = !((type === 'I_agree' ? agreeCheckboxStates : othersAgreeCheckboxStates)[id]);
const currentAnswer = answerList.find(answer => answer.id === id);
Expand All @@ -147,7 +147,12 @@ const Dashboard: React.FC = () => {
perceived_commonsense: 0,
sessionId: surveySession,
});
console.error(`${type} updated`, response.data);
console.log(`${type} updated`, response.data);
if (type == 'I_agree') {
currentAnswer.I_agree = newCheckedState;
} else {
currentAnswer.others_agree = newCheckedState;
}
} catch (error) {
console.error(`Error updating ${type}`, error);
// Revert the state change in case of an error
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/emails.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const ses = new aws.SES({
},
});

// create Nodemailer SES transporter
//create Nodemailer SES transporter
let transporter = nodemailer.createTransport({
SES: { ses, aws },
});
Expand Down
5 changes: 4 additions & 1 deletion server/routes/answers.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ router.post(
i = 0;
while (i < result.length) {
if (item.statementId == result[i].statementId) {
if (item.id < result[i].id) {
if (
new Date(item.createdAt).getTime() <
new Date(result[i].createdAt).getTime()
) {
return false;
}
}
Expand Down
89 changes: 66 additions & 23 deletions server/routes/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ const calculateAgreementPercentage = async (statementIds) => {
attributes: [],
},
],
attributes: ["sessionId", "id", "statementId", "I_agree", "others_agree"],
attributes: [
"sessionId",
"createdAt",
"statementId",
"I_agree",
"others_agree",
],
raw: true,
where: {
statementId: statementIds,
Expand All @@ -186,7 +192,10 @@ const calculateAgreementPercentage = async (statementIds) => {
i = 0;
while (i < answersForOneStatement.length) {
if (item.sessionId == answersForOneStatement[i].sessionId) {
if (item.id < answersForOneStatement[i].id) {
if (
new Date(item.createdAt).getTime() <
new Date(answersForOneStatement[i].createdAt).getTime()
) {
return false;
}
}
Expand All @@ -196,29 +205,63 @@ const calculateAgreementPercentage = async (statementIds) => {
});

const totalAnswers = answersForStatement.length;
const actualIAgree = answersForStatement.reduce(
(acc, answer) => acc + (answer.I_agree ? 1 : 0),
0
);
const actualIAgreePercentage =
totalAnswers === 1
? actualIAgree
? 100
: 0
: (actualIAgree / totalAnswers) * 100;

let count = 0;
for (let i = 0; i < totalAnswers; i++) {
if (answersForStatement[i].I_agree === 1) {
count++;
//hardcoded values for if the user is the only one that has answered this statement
if (totalAnswers == 1) {
if (
answersForStatement[0].I_agree == 1 &&
answersForStatement[0].others_agree == 1
) {
result[statementId] = {
I_agree: 100,
others_agree: 100,
};
}
if (
answersForStatement[0].I_agree == 0 &&
answersForStatement[0].others_agree == 1
) {
result[statementId] = {
I_agree: 0,
others_agree: 0,
};
}
if (
answersForStatement[0].I_agree == 1 &&
answersForStatement[0].others_agree == 0
) {
result[statementId] = {
I_agree: 100,
others_agree: 100,
};
}
if (
answersForStatement[0].I_agree == 0 &&
answersForStatement[0].others_agree == 0
) {
result[statementId] = {
I_agree: 0,
others_agree: 0,
};
}
//if the there is more than one user who has answered the statement -- more likely case
} else {
const actualIAgree = answersForStatement.reduce(
(acc, answer) => acc + (answer.I_agree ? 1 : 0),
0
);
const actualIAgreePercentage = (actualIAgree / totalAnswers) * 100;
let count = 0;
for (let i = 0; i < totalAnswers; i++) {
if (answersForStatement[i].I_agree === 1) {
count++;
}
}
const perceptionAccuracy = (count / totalAnswers) * 100;
result[statementId] = {
I_agree: actualIAgreePercentage,
others_agree: perceptionAccuracy,
};
}
const perceptionAccuracy =
totalAnswers === 1 ? 100 : (count / totalAnswers) * 100;
result[statementId] = {
I_agree: actualIAgreePercentage,
others_agree: perceptionAccuracy,
};
}
return result;
};
Expand Down

0 comments on commit 1b1150f

Please sign in to comment.