Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
index.js 1.78 KiB
import { get } from 'lodash'

export const isHEToManuscript = (state, collectionId) => {
  const currentUserId = get(state, 'currentUser.user.id')
  const collections = get(state, 'collections') || []
  const collection = collections.find(c => c.id === collectionId) || {}
  return get(collection, 'handlingEditor.id') === currentUserId
}

export const canMakeRecommendation = (state, project) => {
  const isHE = isHEToManuscript(state, project.id)
  return isHE && get(project, 'status') === 'reviewCompleted'
}

export const currentUserIs = ({ currentUser: { user } }, role) => {
  const isAdmin = get(user, 'admin')
  const isEic = get(user, 'editorInChief')
  const isHe = get(user, 'handlingEditor')
  switch (role) {
    case 'isHE':
      return isHe
    case 'staff':
      return isAdmin || isEic || isHe
    case 'adminEiC':
      return isAdmin || isEic
    default:
      return false
  }
}

export const canInviteReviewers = ({ currentUser: { user } }, project) => {
  const handlingEditor = get(project, 'handlingEditor')
  const isAdmin = get(user, 'admin')
  const isEic = get(user, 'editorInChief')
  const isAccepted = get(handlingEditor, 'isAccepted')
  const heId = get(handlingEditor, 'id')
  return isAccepted && (user.id === heId || isAdmin || isEic)
}

export const getUserToken = ({ currentUser }) => get(currentUser, 'user.token')

export const getHERecommendation = (state, collectionId, fragmentId) => {
  const recommendations =
    get(state, `fragments.${fragmentId}.recommendations`) || []
  const collection = get(state, 'collections').find(c => c.id === collectionId)
  const heId = get(collection, `handlingEditor.id`)

  return (
    recommendations.find(
      ({ userId, recommendationType }) =>
        recommendationType === 'editorRecommendation' && userId === heId,
    ) || {}
  )
}