diff --git a/packages/component-helper-service/src/services/Fragment.js b/packages/component-helper-service/src/services/Fragment.js index 15267b2d0349ac73ed0ecc4d28126fedcc7ac32b..95405686ba3aa71a5873066de475c660de9fc7dc 100644 --- a/packages/component-helper-service/src/services/Fragment.js +++ b/packages/component-helper-service/src/services/Fragment.js @@ -126,6 +126,29 @@ class Fragment { ), ) } + + async getSubmittedReviewers({ UserModel }) { + const submittedInvitations = await this.getInvitationsForSubmittingReviewers() + return Promise.all( + submittedInvitations.map(inv => UserModel.find(inv.userId)), + ) + } + + async getAcceptedReviewers({ UserModel }) { + const agreedInvitations = this.getReviewerInvitations({ + agree: true, + }) + return Promise.all(agreedInvitations.map(inv => UserModel.find(inv.userId))) + } + + async getPendingReviewers({ UserModel }) { + const pendingInvitations = this.getReviewerInvitations({ + agree: false, + }) + return Promise.all( + pendingInvitations.map(inv => UserModel.find(inv.userId)), + ) + } } module.exports = Fragment diff --git a/packages/component-helper-service/src/services/email/Email.1.js b/packages/component-helper-service/src/services/email/Email.1.js index 22d203fc1411f3aeda9136bd60ad52f7b1a4e663..88d829176fbb51e51ca42cdcfb0efa4d2e519f5c 100644 --- a/packages/component-helper-service/src/services/email/Email.1.js +++ b/packages/component-helper-service/src/services/email/Email.1.js @@ -151,121 +151,6 @@ class Email { ) } - async setupAuthorsEmail({ - requestToRevision = false, - publish = false, - FragmentModel, - }) { - const { - baseUrl, - UserModel, - collection, - parsedFragment: { heRecommendation, id, title, newComments }, - authors: { - submittingAuthor: { - id: submittingAuthorId, - email, - firstName, - lastName, - }, - }, - } = this - let comments = get(heRecommendation, 'comments', []) - if (requestToRevision) comments = newComments - const authorNote = comments.find(comm => comm.public) - const content = get(authorNote, 'content') - const authorNoteText = content ? `Reason & Details: "${content}"` : '' - let emailType = requestToRevision - ? 'author-request-to-revision' - : 'author-manuscript-rejected' - if (publish) emailType = 'author-manuscript-published' - let toAuthors = null - if (emailType === 'author-request-to-revision') { - toAuthors = [ - { - id: submittingAuthorId, - email, - name: `${firstName} ${lastName}`, - }, - ] - } else { - const fragment = await FragmentModel.find(id) - const userHelper = new User({ UserModel }) - const activeAuthors = await userHelper.getActiveAuthors(fragment.authors) - - toAuthors = activeAuthors.map(author => ({ - id: author.id, - email: author.email, - name: `${author.firstName} ${author.lastName}`, - })) - } - toAuthors.forEach(toAuthor => { - mailService.sendNotificationEmail({ - emailType, - toId: toAuthor.id, - toEmail: toAuthor.email, - meta: { - handlingEditorName: get(collection, 'handlingEditor.name'), - baseUrl, - collection, - authorNoteText, - fragment: { - id, - title, - authorName: toAuthor.name, - submittingAuthorName: `${firstName} ${lastName}`, - }, - }, - }) - }) - } - - async setupHandlingEditorEmail({ - publish = false, - returnWithComments = false, - reviewSubmitted = false, - reviewerName = '', - }) { - const { - baseUrl, - UserModel, - collection, - parsedFragment: { eicComments = '', title, id }, - authors: { submittingAuthor: { firstName = '', lastName = '' } }, - } = this - const user = get(collection, 'handlingEditor', {}) - if (!get(user, 'notifications.email.user')) return - - const userHelper = new User({ UserModel }) - const eic = await userHelper.getEditorInChief() - let emailType = publish - ? 'he-manuscript-published' - : 'he-manuscript-rejected' - if (reviewSubmitted) emailType = 'review-submitted' - if (returnWithComments) emailType = 'he-manuscript-return-with-comments' - - mailService.sendNotificationEmail({ - user, - toId: user.id, - toEmail: user.email, - emailType, - meta: { - baseUrl, - collection, - reviewerName, - eicComments, - eicName: `${eic.firstName} ${eic.lastName}`, - emailSubject: `${collection.customId}: Manuscript Decision`, - handlingEditorName: user.name || '', - fragment: { - id, - title, - authorName: `${firstName} ${lastName}`, - }, - }, - }) - } - async setupEiCEmail({ recommendation, comments }) { const { baseUrl, diff --git a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js index 0ee596f81ff579622fec436f937ba0300e0762aa..ea393b6e63a3679112e9636c1ae862c5ecd128e3 100644 --- a/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js +++ b/packages/component-manuscript-manager/src/routes/fragmentsRecommendations/post.js @@ -68,17 +68,16 @@ module.exports = models => async (req, res) => { const parsedFragment = await fragmentHelper.getFragmentData({ handlingEditor: collection.handlingEditor, }) + const fragmentAuthors = await fragmentHelper.getAuthorData({ UserModel }) const baseUrl = services.getBaseUrl(req) - const authors = await fragmentHelper.getAuthorData({ UserModel }) - const email = new Email({ type: 'user', content: { subject: `${collection.customId}: Manuscript`, titleText: `the manuscript titled "${parsedFragment.title}" by ${ - authors.submittingAuthor.firstName - } ${authors.submittingAuthor.firstName}`, + fragmentAuthors.submittingAuthor.firstName + } ${fragmentAuthors.submittingAuthor.firstName}`, }, signatureName: get(collection, 'handlingEditor.name'), comments: parsedFragment.newComments, @@ -107,43 +106,35 @@ module.exports = models => async (req, res) => { collectionHelper.updateFinalStatusByRecommendation({ recommendation, }) - - sendAuthorsEmail({ recommendation, email, baseUrl, authors }) - - // email.parsedFragment.recommendations = fragment.recommendations - // email.setupReviewersEmail({ - // recommendation, - // isSubmitted: true, - // agree: true, - // FragmentModel, - // }) } } else if (recommendationType === 'editorRecommendation') { collectionHelper.updateStatusByRecommendation({ recommendation, isHandlingEditor: true, }) - // email.setupReviewersEmail({ - // recommendation, - // agree: true, - // FragmentModel, - // }) - // email.setupReviewersEmail({ agree: false, FragmentModel: models.Fragment }) - // email.setupEiCEmail({ - // recommendation, - // comments: newRecommendation.comments, - // }) if (['minor', 'major'].includes(recommendation)) { fragment.revision = pick(fragment, ['authors', 'files', 'metadata']) - - // email.parsedFragment.newComments = newRecommendation.comments - // email.setupAuthorsEmail({ - // requestToRevision: true, - // }) } } + if (recommendationType === 'editor-recommendation') { + sendAuthorsEmail({ + recommendation, + email, + baseUrl, + isEditorInChief: reqUser.editorInChief || reqUser.admin, + fragmentAuthors, + }) + sendReviewersEmail({ + email, + baseUrl, + fragmentHelper, + recommendation, + isEditorInChief: reqUser.editorInChief || reqUser.admin, + }) + } + fragment.recommendations.push(newRecommendation) await fragment.save() return res.status(200).json(newRecommendation) @@ -184,14 +175,30 @@ const sendHandlingEditorEmail = ({ email.sendEmail({ html, text }) } -const sendAuthorsEmail = ({ recommendation, email, baseUrl, authors }) => { - let emailType - if (recommendation === 'publish') { - emailType = 'author-manuscript-published' - email.content.subject = `${email.content.subject} Published` +const sendAuthorsEmail = async ({ + recommendation, + email, + baseUrl, + isEditorInChief, + fragmentAuthors, +}) => { + let emailType, authors + if (isEditorInChief) { + if (recommendation === 'publish') { + emailType = 'author-manuscript-published' + email.content.subject = `${email.content.subject} Published` + } else { + emailType = 'author-manuscript-rejected' + email.content.subject = `${email.content.subject} Rejected` + } + authors = fragmentAuthors.authorsList.map(author => ({ + ...author, + emailType, + })) } else { - emailType = 'author-manuscript-rejected' - email.content.subject = `${email.content.subject} Rejected` + email.content.comments = newComments + emailType = 'author-request-to-revision' + authors = [{ ...fragmentAuthors.submittingAuthor, emailType }] } authors.forEach(author => { @@ -206,7 +213,77 @@ const sendAuthorsEmail = ({ recommendation, email, baseUrl, authors }) => { id: author.id, }, ) - const { html, text } = email.getBody(emailType) + const { html, text } = email.getBody(author.emailType) email.sendEmail({ html, text }) }) } + +const sendReviewersEmail = async ({ + email, + baseUrl, + eicName, + UserModel, + fragmentHelper, + recommendation, + isEditorInChief, + handlingEditorName, +}) => { + let reviewers + if (isEditorInChief) { + email.content.subject = `${email.content.subject} Decision` + + reviewers = (await fragmentHelper.getSubmittedReviewers({ + UserModel, + })).map(rev => ({ + ...rev, + emailType: 'submitted-reviewers-after-recommendation', + })) + + email.content.signatureName = eicName + } else { + email.content.signatureName = handlingEditorName + email.contet.subject = `${ + email.content.subject + } ${getSubjectByRecommendation(recommendation)}` + const acceptedReviewers = (await fragmentHelper.getAcceptedReviewers({ + UserModel, + })).map(rev => ({ + ...rev, + emailType: 'accepted-reviewers-after-recommendation', + })) + + const pendingReviewers = (await fragmentHelper.getPendingReviewers({ + UserModel, + })).map(rev => ({ + ...rev, + emailType: 'pending-reviewers-after-recommendation', + })) + + reviewers = [...acceptedReviewers, ...pendingReviewers] + } + + reviewers = reviewers + .filter(rev => rev.isActive) + .filter(rev => get(rev, 'notifications.email.user')) + + reviewers.forEach(reviewer => { + email.toUser = { + email: reviewer.email, + name: `${reviewer.firstName} ${reviewer.lastName}`, + } + email.content.unsubscribeLink = services.createUrl( + baseUrl, + unsubscribeSlug, + { + id: reviewer.id, + }, + ) + const { html, text } = email.getBody(reviewer.emailType) + email.sendEmail({ html, text }) + }) +} + +const getSubjectByRecommendation = recommendation => + ['minor', 'major'].includes(recommendation) + ? 'Revision Requested' + : 'Recommendation Submitted'