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

fix(ReviewerNumbering): Moved getReviewerNumber logic on component-helper-service (WIP)

parent 16f808ab
No related branches found
No related tags found
3 merge requests!176Sprint 24,!171Sprint 24,!158Hin 1115
const { flatten, isEmpty, maxBy, get } = require('lodash')
class Collection { class Collection {
constructor({ collection = {} }) { constructor({ collection = {} }) {
this.collection = collection this.collection = collection
...@@ -103,6 +105,60 @@ class Collection { ...@@ -103,6 +105,60 @@ class Collection {
const [firstName, lastName] = this.collection.handlingEditor.name.split(' ') const [firstName, lastName] = this.collection.handlingEditor.name.split(' ')
return lastName || firstName return lastName || firstName
} }
async getAllCollectionFragments({ FragmentModel }) {
const allCollectionFragments = await Promise.all(
this.collection.fragments.map(async fragmentId => {
const fragment = await FragmentModel.find(fragmentId)
return fragment
}),
)
return allCollectionFragments
}
async getAllCollectionInvitations({ FragmentModel }) {
const allCollectionFragments = await this.getAllCollectionFragments({
FragmentModel,
})
const allCollectionInvitations = flatten(
allCollectionFragments.map(fragment => fragment.invitations),
)
return allCollectionInvitations
}
async getAllNumberedInvitationsForUser({ reqUser, FragmentModel }) {
const allCollectionInvitations = await this.getAllCollectionInvitations({
FragmentModel,
})
return allCollectionInvitations
.filter(invite => invite.userId === reqUser.id)
.filter(invite => invite.reviewerNumber)
}
async getMaxReviewerNumber({ FragmentModel }) {
const allCollectionInvitations = await this.getAllCollectionInvitations({
FragmentModel,
})
const maxReviewerNumber = get(
maxBy(allCollectionInvitations, 'reviewerNumber'),
'reviewerNumber',
0,
)
return maxReviewerNumber
}
async getReviewerNumber({ reqUser, FragmentModel }) {
// maybe i should get fragments here and pass them further
const allNumberedInvitationsForUser = await this.getAllNumberedInvitationsForUser(
{
reqUser,
FragmentModel,
},
)
if (isEmpty(allNumberedInvitationsForUser))
return this.getMaxReviewerNumber() + 1
return allNumberedInvitationsForUser[0].reviewerNumber
}
} }
module.exports = Collection module.exports = Collection
const uuid = require('uuid') const uuid = require('uuid')
const { const { pick, get, set, has, isEmpty, last, findIndex } = require('lodash')
pick,
get,
set,
has,
isEmpty,
last,
findIndex,
maxBy,
flatten,
} = require('lodash')
const config = require('config') const config = require('config')
const { v4 } = require('uuid') const { v4 } = require('uuid')
...@@ -42,31 +32,6 @@ const sendMTSPackage = async (collection, fragment, isEQA = false) => { ...@@ -42,31 +32,6 @@ const sendMTSPackage = async (collection, fragment, isEQA = false) => {
const Notification = require('../../notifications/notification') const Notification = require('../../notifications/notification')
const getReviewerNumber = async ({ collection, fragment, models, reqUser }) => {
const allCollectionFragments = await Promise.all(
collection.fragments.map(async fragmentId => {
const fragment = await models.Fragment.find(fragmentId)
return fragment
}),
)
const allCollectionInvitations = flatten(
allCollectionFragments.map(fragment => fragment.invitations),
)
const allNumberedUserInvitations = allCollectionInvitations
.filter(invite => invite.userId === reqUser.id)
.filter(invite => invite.reviewerNumber)
if (!isEmpty(allNumberedUserInvitations))
return allNumberedUserInvitations[0].reviewerNumber
const maxReviewerNumber = get(
maxBy(allCollectionInvitations, 'reviewerNumber'),
'reviewerNumber',
0,
)
return maxReviewerNumber + 1
}
module.exports = models => async (req, res) => { module.exports = models => async (req, res) => {
const { recommendation, comments, recommendationType } = req.body const { recommendation, comments, recommendationType } = req.body
if (!services.checkForUndefinedParams(recommendationType)) if (!services.checkForUndefinedParams(recommendationType))
...@@ -148,9 +113,8 @@ module.exports = models => async (req, res) => { ...@@ -148,9 +113,8 @@ module.exports = models => async (req, res) => {
newRecommendation.recommendation = recommendation || undefined newRecommendation.recommendation = recommendation || undefined
newRecommendation.comments = comments || undefined newRecommendation.comments = comments || undefined
const collectionHelper = new Collection({ collection })
if (recommendationType === 'editorRecommendation') { if (recommendationType === 'editorRecommendation') {
const collectionHelper = new Collection({ collection })
collectionHelper.updateStatusOnRecommendation({ collectionHelper.updateStatusOnRecommendation({
isEditorInChief, isEditorInChief,
recommendation, recommendation,
...@@ -237,15 +201,14 @@ module.exports = models => async (req, res) => { ...@@ -237,15 +201,14 @@ module.exports = models => async (req, res) => {
'userId', 'userId',
reqUser.id, reqUser.id,
]) ])
if (invitationIndex >= 0) if (invitationIndex >= 0) {
fragment.invitations[ const FragmentModel = models.Fragment
invitationIndex const reviewerNumber = await collectionHelper.getReviewerNumber({
].reviewerNumber = await getReviewerNumber({
collection,
fragment,
models,
reqUser, reqUser,
FragmentModel,
}) })
fragment.invitations[invitationIndex].reviewerNumber = reviewerNumber
}
fragment.recommendations.push(newRecommendation) fragment.recommendations.push(newRecommendation)
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