generated from UniversityOfHelsinkiCS/toskaboiler
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Feedback visibility update based on text content, not id
- Loading branch information
1 parent
e138eb2
commit 489a5ae
Showing
8 changed files
with
91 additions
and
71 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
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
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,63 @@ | ||
const { sequelize } = require('../../db/dbConnection') | ||
const { Feedback, UserFeedbackTarget } = require('../../models') | ||
const { ApplicationError } = require('../../util/customErrors') | ||
const { getFeedbackTargetContext } = require('./getFeedbackTargetContext') | ||
|
||
const hideFeedback = async ({ feedbackTargetId, questionId, hidden, user, feedbackContent }) => { | ||
if (typeof hidden !== 'boolean') { | ||
throw new ApplicationError('Invalid value for hidden', 400) | ||
} | ||
|
||
// check access | ||
const { feedbackTarget, access } = await getFeedbackTargetContext({ | ||
feedbackTargetId, | ||
user, | ||
}) | ||
if (!access?.canHideFeedback()) ApplicationError.Forbidden('Must be responsible teacher, organisation admin or admin') | ||
|
||
const allFeedbacks = await Feedback.findAll({ | ||
include: { | ||
model: UserFeedbackTarget, | ||
as: 'userFeedbackTarget', | ||
where: { | ||
feedbackTargetId, | ||
}, | ||
}, | ||
}) | ||
|
||
const feedbacksToUpdate = allFeedbacks | ||
.map(feedback => { | ||
if (!Array.isArray(feedback.data)) return { updated: false } | ||
|
||
let updated = false | ||
feedback.data = feedback.data.map(answer => { | ||
if (answer.data === feedbackContent && answer.questionId === questionId) { | ||
updated = true | ||
return { ...answer, hidden } | ||
} | ||
return answer | ||
}) | ||
|
||
return { updated, feedback } | ||
}) | ||
.filter(({ updated }) => updated) | ||
.map(({ feedback }) => feedback) | ||
|
||
if (feedbacksToUpdate.length === 0) return ApplicationError.BadRequest('Matching feedback not found') | ||
|
||
await sequelize.transaction(async transaction => { | ||
await feedbackTarget.increment( | ||
{ hiddenCount: hidden ? feedbacksToUpdate.length : -feedbacksToUpdate.length }, | ||
{ transaction } | ||
) | ||
for (const feedback of feedbacksToUpdate) { | ||
feedback.save() | ||
} | ||
}) | ||
|
||
return feedbacksToUpdate.length | ||
} | ||
|
||
module.exports = { | ||
hideFeedback, | ||
} |
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