Newer
Older
Sebastian Mihalache
committed
const config = require('config')
const { get } = require('lodash')
Sebastian Mihalache
committed
const Email = require('@pubsweet/component-email-templating')
Sebastian Mihalache
committed
const {
User,
services,
Fragment,
} = require('pubsweet-component-helper-service')

Sebastian Mihalache
committed
const resetPath = config.get('invite-reset-password.url')
const { name: journalName, staffEmail } = config.get('journal')
Sebastian Mihalache
committed
const unsubscribeSlug = config.get('unsubscribe.url')
const { getEmailCopy } = require('./emailCopy')
Sebastian Mihalache
committed
module.exports = {
Andrei Cioromila
committed
async sendHandlingEditorEmail({ baseUrl, fragment, UserModel, collection }) {
Sebastian Mihalache
committed
const fragmentHelper = new Fragment({ fragment })
const handlingEditor = get(collection, 'handlingEditor')
Sebastian Mihalache
committed
const parsedFragment = await fragmentHelper.getFragmentData({
Sebastian Mihalache
committed
})
Andrei Cioromila
committed
const { paragraph, ...bodyProps } = getEmailCopy({
emailType: 'he-new-version-submitted',
titleText: `the manuscript titled "${parsedFragment.title}"`,
Sebastian Mihalache
committed
Andrei Cioromila
committed
const heUser = await UserModel.find(handlingEditor.id)
Sebastian Mihalache
committed
const email = new Email({
type: 'user',
fromEmail: `${journalName} <${staffEmail}>`,
Andrei Cioromila
committed
toUser: {
email: heUser.email,
},
Sebastian Mihalache
committed
content: {
Andrei Cioromila
committed
subject: `${collection.customId}: Revision submitted`,
paragraph,
signatureJournal: journalName,
Sebastian Mihalache
committed
ctaLink: services.createUrl(
baseUrl,
`/projects/${collection.id}/versions/${fragment.id}/details`,
),
ctaText: 'MANUSCRIPT DETAILS',
Andrei Cioromila
committed
unsubscribeLink: services.createUrl(baseUrl, unsubscribeSlug, {
id: heUser.id,
token: heUser.accessTokens.unsubscribe,
}),
Sebastian Mihalache
committed
},
Andrei Cioromila
committed
bodyProps,
Sebastian Mihalache
committed
})
Andrei Cioromila
committed
return email.sendEmail()
Sebastian Mihalache
committed
},
Andrei Cioromila
committed
async sendReviewersEmail({
baseUrl,
fragment,
Sebastian Mihalache
committed
UserModel,
Andrei Cioromila
committed
collection,
previousVersion,
}) {
const fragmentHelper = new Fragment({ fragment: previousVersion })
const handlingEditor = get(collection, 'handlingEditor')
const parsedFragment = await fragmentHelper.getFragmentData({
handlingEditor,
})
Sebastian Mihalache
committed
Andrei Cioromila
committed
const reviewers = await fragmentHelper.getReviewers({
UserModel,
type: 'submitted',
})
Sebastian Mihalache
committed
Andrei Cioromila
committed
const { paragraph, ...bodyProps } = getEmailCopy({
emailType: 'submitted-reviewers-after-revision',
titleText: `the manuscript titled "${parsedFragment.title}"`,
expectedDate: services.getExpectedDate({ daysExpected: 14 }),
Sebastian Mihalache
committed
})
Andrei Cioromila
committed
reviewers.forEach(reviewer => {
const email = new Email({
type: 'user',
fromEmail: `${handlingEditor.name} <${staffEmail}>`,
toUser: {
email: reviewer.email,
name: `${reviewer.lastName}`,
},
content: {
subject: `${
collection.customId
}: A manuscript you reviewed has been revised`,
paragraph,
signatureName: handlingEditor.name,
signatureJournal: journalName,
ctaLink: services.createUrl(
baseUrl,
`/projects/${collection.id}/versions/${fragment.id}/details`,
),
ctaText: 'MANUSCRIPT DETAILS',
unsubscribeLink: services.createUrl(baseUrl, unsubscribeSlug, {
id: reviewer.id,
token: reviewer.accessTokens.unsubscribe,
}),
},
bodyProps,
})
Sebastian Mihalache
committed
Andrei Cioromila
committed
return email.sendEmail()
Andrei Cioromila
committed
})
},
Sebastian Mihalache
committed
Andrei Cioromila
committed
async sendEQSEmail({ baseUrl, fragment, UserModel, collection }) {
const userHelper = new User({ UserModel })
const eicName = await userHelper.getEiCName()
Sebastian Mihalache
committed
Andrei Cioromila
committed
const { paragraph, ...bodyProps } = getEmailCopy({
emailType: 'eqs-manuscript-submitted',
customId: collection.customId,
Andrei Cioromila
committed
})
Sebastian Mihalache
committed
Andrei Cioromila
committed
const email = new Email({
type: 'user',
fromEmail: `${journalName} <${staffEmail}>`,
toUser: {
email: staffEmail,
name: 'Editorial Assistant',
},
content: {
subject: `Manuscript Submitted`,
paragraph,
signatureName: eicName,
signatureJournal: journalName,
ctaLink: services.createUrl(baseUrl, config.get('eqs-decision.url'), {
collectionId: collection.id,
customId: collection.customId,
token: collection.technicalChecks.token,
}),
ctaText: 'MAKE DECISION',
unsubscribeLink: baseUrl,
},
bodyProps,
})

Sebastian Mihalache
committed
Andrei Cioromila
committed
return email.sendEmail()
Andrei Cioromila
committed
},

Sebastian Mihalache
committed
Andrei Cioromila
committed
async sendAuthorsEmail({ baseUrl, fragment, UserModel, collection }) {
const fragmentHelper = new Fragment({ fragment })
const handlingEditor = get(collection, 'handlingEditor')
const parsedFragment = await fragmentHelper.getFragmentData({
handlingEditor,
})
const {
submittingAuthor,
activeAuthors: fragmentAuthors,
} = await fragmentHelper.getAuthorData({
UserModel,
})
const submittingAuthorName = `${submittingAuthor.firstName} ${
submittingAuthor.lastName
}`
const userEmailData = await Promise.all(
fragmentAuthors.map(async author => {
const { paragraph, ...bodyProps } = getEmailCopy({

Sebastian Mihalache
committed
emailType: author.isSubmitting
? 'submitting-author-manuscript-submitted'
: 'coauthors-manuscript-submitted',
titleText: author.isSubmitting
Andrei Cioromila
committed
? `the manuscript titled "${parsedFragment.title}"`
: `The manuscript titled "${
parsedFragment.title
}" has been submitted to ${journalName} by ${submittingAuthorName}`,
})
const user = await UserModel.find(author.id)
return {
author: Object.assign(author, {
isConfirmed: user.isConfirmed,
unsubscribeToken: user.accessTokens.unsubscribe,
passwordResetToken: user.accessTokens.passwordReset,
}),
paragraph,
bodyProps,
}
}),

Sebastian Mihalache
committed
)
Andrei Cioromila
committed
userEmailData.forEach(({ author, paragraph, bodyProps }) => {
const email = new Email({
type: 'user',
fromEmail: `${journalName} <${staffEmail}>`,
toUser: {
email: author.email,
name: `${author.lastName}`,
},
content: {
subject: `Manuscript submitted to ${journalName}`,
paragraph,
signatureName: '',
signatureJournal: journalName,
ctaLink: services.createUrl(
baseUrl,
`/projects/${collection.id}/versions/${fragment.id}/details`,
),
ctaText: 'MANUSCRIPT DETAILS',
unsubscribeLink: services.createUrl(baseUrl, unsubscribeSlug, {
id: author.id,
token: author.unsubscribeToken,
}),
},
bodyProps,

Sebastian Mihalache
committed
})
Andrei Cioromila
committed
if (author.isSubmitting) {
email.content.ctaLink = services.createUrl(
baseUrl,
`/projects/${collection.id}/versions/${fragment.id}/details`,
)
email.content.ctaText = 'MANUSCRIPT DETAILS'
} else if (author.isConfirmed) {
email.content.ctaLink = services.createUrl(baseUrl, '')
email.content.ctaText = 'LOG IN'
} else {
email.content.ctaLink = services.createUrl(baseUrl, resetPath, {
email: author.email,
token: author.passwordResetToken,
firstName: author.firstName,
lastName: author.lastName,
affiliation: author.affiliation,
title: author.title,
country: author.country,
})
email.content.ctaText = 'CONFIRM ACCOUNT'
}
Andrei Cioromila
committed
return email.sendEmail()

Sebastian Mihalache
committed
})
Andrei Cioromila
committed
},