Newer
Older
Sebastian Mihalache
committed
const User = require('./User')
class Fragment {
constructor({ fragment }) {
this.fragment = fragment
}
set _fragment(newFragment) {
this.fragment = newFragment
}
static setFragmentOwners(fragment = {}, author = {}) {
const { owners = [] } = fragment
if (author.isSubmitting) {
const authorAlreadyOwner = owners.includes(author.id)
if (!authorAlreadyOwner) {
return [author.id, ...owners]
}
}
return owners
}
Sebastian Mihalache
committed
async getFragmentData({ handlingEditor } = {}) {
const { fragment: { metadata = {}, recommendations = [], id } } = this
Sebastian Mihalache
committed
let heRecommendation
if (handlingEditor) {
heRecommendation = recommendations.find(
rec => rec.userId === handlingEditor.id,
)
}
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 = this.constructor.setFragmentOwners(fragment, author)
await fragment.save()
Sebastian Mihalache
committed
async getAuthorData({ UserModel }) {
const { fragment: { authors = [] } } = this
const submittingAuthorData = authors.find(author => author.isSubmitting)
Sebastian Mihalache
committed
try {
const submittingAuthor = await UserModel.find(
get(submittingAuthorData, 'id'),
)
Sebastian Mihalache
committed
const userHelper = new User({ UserModel })
const activeAuthors = await userHelper.getActiveAuthors(authors)
Sebastian Mihalache
committed
Sebastian Mihalache
committed
}
}
getInvitations({ isAccepted = true, role = 'reviewer', type }) {
const { fragment: { invitations = [], recommendations = [] } } = this
let filteredInvitations = isAccepted
Sebastian Mihalache
committed
? invitations.filter(
inv =>
Sebastian Mihalache
committed
inv.hasAnswer === true &&
inv.isAccepted === true,
)
: invitations.filter(inv => inv.role === role && inv.hasAnswer === false)
Sebastian Mihalache
committed
if (type === 'submitted') {
filteredInvitations = filteredInvitations.filter(inv =>
recommendations.find(
rec =>
rec.recommendationType === 'review' &&
rec.submittedOn &&
inv.userId === rec.userId,
),
)
}
return filteredInvitations
Sebastian Mihalache
committed
}
Sebastian Mihalache
committed
getLatestHERequestToRevision() {
const { fragment: { recommendations = [] } } = this
Sebastian Mihalache
committed
return recommendations
.filter(
rec =>
rec.recommendationType === 'editorRecommendation' &&
(rec.recommendation === 'minor' || rec.recommendation === 'major'),
)
.sort((a, b) => b.createdOn - a.createdOn)[0]
}
Sebastian Mihalache
committed
async getReviewers({ UserModel, type }) {
const isAccepted = type !== 'pending'
const invitations = await this.getInvitations({ isAccepted, type })
Sebastian Mihalache
committed
return (await Promise.all(
invitations.map(inv => UserModel.find(inv.userId)),
))
.filter(rev => rev.isActive)
.filter(rev => get(rev, 'notifications.email.user'))
}
module.exports = Fragment