Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
resolvers.js 3.50 KiB
const logger = require('@pubsweet/logger')
const rfr = require('rfr')

const { ManuscriptManager, OrganizationManager } = rfr('server/xpub-model')

const resolvers = {
  Query: {
    async manuscript(_, { id }, { user }) {
      if (!user) {
        throw new Error('You are not authenticated!')
      }

      return ManuscriptManager.findById(id, user)
    },
    async manuscripts(_, vars, { user }) {
      if (!user) {
        throw new Error('You are not authenticated!')
      }

      return ManuscriptManager.all(user)
    },
    async checkDuplicates(_, { id, pmid, title }, { user }) {
      if (!user) {
        throw new Error('You are not authenticated!')
      }
      return ManuscriptManager.checkDuplicates(id, pmid, title, user)
    },
    async countByStatus(_, vars, { user }) {
      if (!user) {
        throw new Error('You are not authenticated!')
      }
      return ManuscriptManager.countByStatus()
    },
    async adminManuscripts(_, vars, { user }) {
      if (!user) {
        throw new Error('You are not authenticated!')
      }
      const statuses = ['submitted', 'xml-qa', 'xml-triage']
      return ManuscriptManager.findByStatus(statuses, -1)
    },
    async findByStatus(_, { query, page, pageSize }, { user }) {
      if (!user) {
        throw new Error('You are not authenticated!')
      }
      return ManuscriptManager.findByStatus([query], page, pageSize, user)
    },
    async searchManuscripts(_, { query, page, pageSize }, { user }) {
      if (!user) {
        throw new Error('You are not authenticated!')
      }

      return ManuscriptManager.search(query, page, pageSize, user)
    },
    async searchArticleIds(_, { id }, { user }) {
      if (!user) {
        throw new Error('You are not authenticated!')
      }

      return ManuscriptManager.findByArticleId(id, user)
    },
  },

  Mutation: {
    async createManuscript(_, vars, { user }) {
      if (!user) {
        throw new Error('Not logged in')
      }

      const organizationId = await OrganizationManager.getEuropePMCPlusID()
      const saved = await ManuscriptManager.create(user, organizationId)
      logger.debug('saved: ', saved)
      return saved
    },

    // TODO restrict this in production
    async deleteManuscript(_, { id }, { user }) {
      if (!user) {
        throw new Error('Not logged in')
      }

      return ManuscriptManager.delete(id, user)
    },

    async updateManuscript(_, { data }, { user }) {
      if (!user) {
        throw new Error('Not logged in')
      }

      const updatedMan = await ManuscriptManager.update(data, user)
      return updatedMan
    },
    async claimManuscript(_, { id }, { user }) {
      if (!user) {
        throw new Error('Not logged in')
      }

      const updatedMan = await ManuscriptManager.changeClaim(id, user)
      return updatedMan
    },
    async unclaimManuscript(_, { id }, { user }) {
      if (!user) {
        throw new Error('Not logged in')
      }

      const updatedMan = await ManuscriptManager.changeClaim(id, user, true)
      return updatedMan
    },

    async submitManuscript(_, { data }, { user }) {
      if (!user) {
        throw new Error('You are not authenticated!')
      }
      const submittedMan = await ManuscriptManager.submit(data, user)
      return submittedMan
    },

    async rejectManuscript(_, { data }, { user }) {
      if (!user) {
        throw new Error('You are not authenticated!')
      }
      const rejectedMan = await ManuscriptManager.reject(data, user)
      return rejectedMan
    },
  },
}

module.exports = resolvers