Skip to content
Snippets Groups Projects
Commit 7d34ecf9 authored by Mihail Hagiu's avatar Mihail Hagiu
Browse files

refactor(ReviewerNumbering): Fixed CR issues

parent d8687f6f
No related branches found
No related tags found
3 merge requests!176Sprint 24,!171Sprint 24,!158Hin 1115
const { flatten, isEmpty, maxBy, get } = require('lodash') const { isEmpty, maxBy, get, flatMap } = require('lodash')
class Collection { class Collection {
constructor({ collection = {} }) { constructor({ collection = {} }) {
...@@ -120,8 +120,9 @@ class Collection { ...@@ -120,8 +120,9 @@ class Collection {
const allCollectionFragments = await this.getAllCollectionFragments({ const allCollectionFragments = await this.getAllCollectionFragments({
FragmentModel, FragmentModel,
}) })
const allCollectionInvitations = flatten( const allCollectionInvitations = flatMap(
allCollectionFragments.map(fragment => fragment.invitations), allCollectionFragments,
fragment => fragment.invitations,
) )
const allNumberedInvitationsForUser = allCollectionInvitations const allNumberedInvitationsForUser = allCollectionInvitations
.filter(invite => invite.userId === userId) .filter(invite => invite.userId === userId)
......
const { findIndex } = require('lodash') const { find } = require('lodash')
const { const {
services, services,
authsome: authsomeHelper, authsome: authsomeHelper,
...@@ -9,6 +9,7 @@ const Notification = require('../../notifications/notification') ...@@ -9,6 +9,7 @@ const Notification = require('../../notifications/notification')
module.exports = models => async (req, res) => { module.exports = models => async (req, res) => {
const { collectionId, fragmentId, recommendationId } = req.params const { collectionId, fragmentId, recommendationId } = req.params
const userId = req.user
let collection, fragment let collection, fragment
try { try {
collection = await models.Collection.find(collectionId) collection = await models.Collection.find(collectionId)
...@@ -26,7 +27,7 @@ module.exports = models => async (req, res) => { ...@@ -26,7 +27,7 @@ module.exports = models => async (req, res) => {
if (!recommendation) if (!recommendation)
return res.status(404).json({ error: 'Recommendation not found.' }) return res.status(404).json({ error: 'Recommendation not found.' })
if (recommendation.userId !== req.user) if (recommendation.userId !== userId)
return res.status(403).json({ return res.status(403).json({
error: 'Unauthorized.', error: 'Unauthorized.',
}) })
...@@ -36,14 +37,14 @@ module.exports = models => async (req, res) => { ...@@ -36,14 +37,14 @@ module.exports = models => async (req, res) => {
fragment, fragment,
path: req.route.path, path: req.route.path,
} }
const canPatch = await authsome.can(req.user, 'PATCH', target) const canPatch = await authsome.can(userId, 'PATCH', target)
if (!canPatch) if (!canPatch)
return res.status(403).json({ return res.status(403).json({
error: 'Unauthorized.', error: 'Unauthorized.',
}) })
const UserModel = models.User const UserModel = models.User
const reviewer = await UserModel.find(req.user) const reviewer = await UserModel.find(userId)
Object.assign(recommendation, req.body) Object.assign(recommendation, req.body)
recommendation.updatedOn = Date.now() recommendation.updatedOn = Date.now()
...@@ -63,22 +64,17 @@ module.exports = models => async (req, res) => { ...@@ -63,22 +64,17 @@ module.exports = models => async (req, res) => {
const collectionHelper = new Collection({ collection }) const collectionHelper = new Collection({ collection })
collectionHelper.updateStatus({ newStatus: 'reviewCompleted' }) collectionHelper.updateStatus({ newStatus: 'reviewCompleted' })
} }
}
if (req.body.submittedOn) {
const collectionHelper = new Collection({ collection }) const collectionHelper = new Collection({ collection })
const invitationIndex = findIndex(fragment.invitations, [ const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({
userId: req.authInfo.id,
FragmentModel,
})
find(fragment.invitations, [
'userId', 'userId',
req.authInfo.id, userId,
]) ]).reviewerNumber = reviewerNumber
if (invitationIndex >= 0) {
const FragmentModel = models.Fragment
const reviewerNumber = await collectionHelper.getReviewerNumber({
userId: req.authInfo.id,
FragmentModel,
})
fragment.invitations[invitationIndex].reviewerNumber = reviewerNumber
}
} }
fragment.save() fragment.save()
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment