Skip to content
Snippets Groups Projects
Fragment.js 2.81 KiB
Newer Older
Sebastian Mihalache's avatar
Sebastian Mihalache committed
const get = require('lodash/get')

const setFragmentOwners = (fragment = {}, author = {}) => {
  const { owners = [] } = fragment
  if (author.isSubmitting) {
    const authorAlreadyOwner = owners.includes(author.id)
    if (!authorAlreadyOwner) {
      return [author.id, ...owners]
    }
  }
  return owners
}

class Fragment {
  constructor({ fragment }) {
    this.fragment = fragment
  }
Sebastian Mihalache's avatar
Sebastian Mihalache committed

  set _fragment(newFragment) {
    this.fragment = newFragment
  }

  async getFragmentData({ handlingEditor = {} }) {
Sebastian Mihalache's avatar
Sebastian Mihalache committed
    const { fragment: { metadata = {}, recommendations = [], id } } = this
    const heRecommendation = recommendations.find(
      rec => rec.userId === handlingEditor.id,
    )
Sebastian Mihalache's avatar
Sebastian Mihalache committed
    let { title = '', abstract = '' } = metadata
    const { type } = metadata
    title = title.replace(/<(.|\n)*?>/g, '')
    abstract = abstract ? abstract.replace(/<(.|\n)*?>/g, '') : ''

    return {
      id,
      type,
      title,
      abstract,
      recommendations,
      heRecommendation,
    }
  }

  async addAuthor({ user, isSubmitting, isCorresponding }) {
    const { fragment } = this
    fragment.authors = fragment.authors || []
    const author = {
      firstName: user.firstName || '',
      lastName: user.lastName || '',
      email: user.email,
      title: user.title || '',
      affiliation: user.affiliation || '',
      isSubmitting,
      isCorresponding,
    }
    fragment.authors.push(author)
    fragment.owners = setFragmentOwners(fragment, author)
Sebastian Mihalache's avatar
Sebastian Mihalache committed
    const { fragment: { authors = [] } } = this
    const submittingAuthorData = authors.find(author => author.isSubmitting)
Sebastian Mihalache's avatar
Sebastian Mihalache committed
    try {
      const submittingAuthor = await UserModel.find(
        get(submittingAuthorData, 'id'),
      )

      const authorsPromises = authors.map(async author => {
        const user = await UserModel.find(author.id)
        return `${user.firstName} ${user.lastName}`
      })
      const authorsList = await Promise.all(authorsPromises)

      return {
        authorsList,
        submittingAuthor,
      }
    } catch (e) {
      throw e
    const { fragment: { invitations = [] } } = this
    return agree
      ? invitations.filter(
          inv =>
            inv.role === 'reviewer' &&
            inv.hasAnswer === true &&
            inv.isAccepted === true,
        )
      : invitations.filter(
          inv => inv.role === 'reviewer' && inv.hasAnswer === false,
        )
  }

  getHeRequestToRevision() {
    const { fragment: { recommendations = [] } } = this
    return recommendations.find(
      rec =>
        rec.recommendationType === 'editorRecommendation' &&
        (rec.recommendation === 'minor' || rec.recommendation === 'major'),
    )
  }