Newer
Older
const config = require('config')
Andrei Cioromila
committed
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,
Andrei Cioromila
committed
targetUserName: reviewerLastName,
emailType: 'he-review-submitted',
})
async notifyHEWhenEiCMakesDecision() {
const { eicName, titleText } = this.props
Andrei Cioromila
committed
const handlingEditorId = get(this.collection, 'handlingEditor.id')
const heUser = await this.UserModel.find(handlingEditorId)
const email = new Email({
type: 'user',
toUser: {
Andrei Cioromila
committed
email: heUser.email,
name: heUser.lastName,
},
fromEmail: `${eicName} <${staffEmail}>`,
content: {
signatureName: eicName,
ctaText: 'MANUSCRIPT DETAILS',
Andrei Cioromila
committed
subject: `${this.collection.customId}: A review has been submitted`,
unsubscribeLink: services.createUrl(this.baseUrl, unsubscribeSlug, {
Andrei Cioromila
committed
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,
Andrei Cioromila
committed
targetUserName: eicName,
emailType: 'he-review-submitted',
})
}
Andrei Cioromila
committed
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 })
}
Andrei Cioromila
committed
async notifyEAWhenEiCRequestsEQAApproval() {
const { eicName } = await this.getNotificationProperties()
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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 })
}
Andrei Cioromila
committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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,
})
Andrei Cioromila
committed
const {
submittingAuthor,
activeAuthors,
} = await fragmentHelper.getAuthorData({
UserModel: this.UserModel,
})
Andrei Cioromila
committed
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
Andrei Cioromila
committed
return {
recommendation,
recommendationType,
eicName,
titleText,
submittingAuthor,
activeAuthors,
parsedFragment,
}
}
}
module.exports = Notification