const config = require('config') const { get, isEmpty } = require('lodash') const Email = require('@pubsweet/component-email-templating') const unsubscribeSlug = config.get('unsubscribe.url') const { getEmailCopy } = require('./emailCopy') const { User, services, Fragment, Collection, } = require('pubsweet-component-helper-service') // const { getEmailCopy } = require('./emailCopy') const helpers = require('./helpers') // const editorialAssistantEmail = config.get('journal.staffEmail') const { name: journalName, staffEmail } = config.get('journal') class Notification { constructor({ baseUrl = '', fragment = {}, UserModel = {}, collection = {}, newRecommendation = {}, }) { this.baseUrl = baseUrl this.fragment = fragment this.UserModel = UserModel this.collection = collection this.newRecommendation = newRecommendation } async notifyHEWhenReviewerSubmitsReport(reviewerLastName) { const { eicName, titleText } = await this.getNotificationProperties() const handlingEditorId = get(this.collection, 'handlingEditor.id') const heUser = await this.UserModel.find(handlingEditorId) const email = new Email({ type: 'user', toUser: { email: heUser.email, name: heUser.lastName, }, fromEmail: `${eicName} <${staffEmail}>`, content: { signatureName: eicName, ctaText: 'MANUSCRIPT DETAILS', subject: `${this.collection.customId}: A review has been submitted`, unsubscribeLink: services.createUrl(this.baseUrl, unsubscribeSlug, { id: heUser.id, token: heUser.accessTokens.unsubscribe, }), ctaLink: services.createUrl( this.baseUrl, `/projects/${this.collection.id}/versions/${ this.fragment.id }/details`, ), }, }) helpers.sendHandlingEditorEmail({ email, titleText, targetUserName: reviewerLastName, emailType: 'he-review-submitted', }) } async notifyHEWhenEiCMakesDecision() { const { eicName, titleText } = this.props const handlingEditorId = get(this.collection, 'handlingEditor.id') const heUser = await this.UserModel.find(handlingEditorId) const email = new Email({ type: 'user', toUser: { email: heUser.email, name: heUser.lastName, }, fromEmail: `${eicName} <${staffEmail}>`, content: { signatureName: eicName, ctaText: 'MANUSCRIPT DETAILS', subject: `${this.collection.customId}: A review has been submitted`, unsubscribeLink: services.createUrl(this.baseUrl, unsubscribeSlug, { id: heUser.id, token: heUser.accessTokens.unsubscribe, }), ctaLink: services.createUrl( this.baseUrl, `/projects/${this.collection.id}/versions/${ this.fragment.id }/details`, ), }, }) helpers.sendHandlingEditorEmail({ email, titleText, targetUserName: eicName, emailType: 'he-review-submitted', }) } async notifyEAWhenEiCMakesFinalDecision() { const { eicName, titleText } = await this.getNotificationProperties() const subjectBaseText = `${this.collection.customId}: Manuscript` const email = new Email({ type: 'system', toUser: { email: staffEmail, }, fromEmail: `${journalName} <${staffEmail}>`, content: { subject: `${subjectBaseText} decision finalized`, unsubscribeLink: this.baseUrl, }, }) const { html, text } = email.getNotificationBody({ emailBodyProps: getEmailCopy({ eicName, titleText, emailType: 'eqa-manuscript-published', }), }) email.sendEmail({ html, text }) } async notifyEAWhenEiCRequestsEQAApproval() { const { eicName } = await this.getNotificationProperties() const subjectBaseText = `${this.collection.customId}: Manuscript` const email = new Email({ type: 'system', toUser: { email: staffEmail, }, fromEmail: `${journalName} <${staffEmail}>`, content: { subject: `${subjectBaseText} Request for EQA approval`, unsubscribeLink: this.baseUrl, ctaText: 'MAKE DECISION', ctaLink: services.createUrl( this.baseUrl, config.get('eqa-decision.url'), { collectionId: this.collection.id, customId: this.collection.customId, token: this.collection.technicalChecks.token, }, ), }, }) const { html, text } = email.getNotificationBody({ emailBodyProps: getEmailCopy({ eicName, customId: this.collection.customId, emailType: 'eqa-manuscript-request-for-approval', }), }) email.sendEmail({ html, text }) } async notifyAuthorsWhenEiCMakesDecision() { const { eicName, titleText, activeAuthors, recommendation, parsedFragment, } = await this.getNotificationProperties() activeAuthors.forEach(async author => { const subjectOpts = { publish: `${this.collection.customId}: Manuscript accepted`, reject: `${this.collection.customId}: Manuscript rejected`, } const subject = subjectOpts[recommendation] if (isEmpty(subject)) { throw new Error(`Undefined recommendation: ${recommendation}`) } const email = new Email({ type: 'user', toUser: { email: author.email, name: author.lastName, }, fromEmail: `${eicName} <${staffEmail}>`, content: { signatureName: eicName, ctaText: 'MANUSCRIPT DETAILS', subject, unsubscribeLink: services.createUrl(this.baseUrl, unsubscribeSlug, { id: author.id, token: author.accessTokens.unsubscribe, }), ctaLink: services.createUrl( this.baseUrl, `/projects/${this.collection.id}/versions/${ this.fragment.id }/details`, ), signatureJournal: journalName, }, }) const hasPeerReview = !isEmpty(this.collection.handlingEditor) const emailType = helpers.getEmailTypeByRecommendationForAuthors({ recommendation, hasPeerReview, }) let comments if (hasPeerReview) { comments = helpers.getHEComments({ heRecommendation: parsedFragment.heRecommendation, }) } else { comments = this.newRecommendation.comments[0].content } const { html, text } = email.getNotificationBody({ emailBodyProps: getEmailCopy({ titleText, emailType, comments, }), }) email.sendEmail({ html, text }) }) } notifyReviewersWhenEiCMakesDecision() {} notifySAWhenHERequestsRevision() {} notifyReviewersWhenHEMakesRecommendation() {} notifyEiCWhenHEMakesRecommendation() {} async getNotificationProperties() { const fragmentHelper = new Fragment({ fragment: this.fragment }) const parsedFragment = await fragmentHelper.getFragmentData({ handlingEditor: this.collection.handlingEditor, }) const { submittingAuthor, activeAuthors, } = await fragmentHelper.getAuthorData({ UserModel: this.UserModel, }) const userHelper = new User({ UserModel: this.UserModel }) const eicName = await userHelper.getEiCName() const titleText = `the manuscript titled "${parsedFragment.title}" by ${ submittingAuthor.firstName } ${submittingAuthor.lastName}` const { recommendation, recommendationType } = this.newRecommendation return { recommendation, recommendationType, eicName, titleText, submittingAuthor, activeAuthors, parsedFragment, } } } module.exports = Notification