Newer
Older
const config = require('config')
const { get, isEmpty, chain } = require('lodash')
const Email = require('@pubsweet/component-email-templating')
const {
User,
services,
Fragment,
Collection,
} = require('pubsweet-component-helper-service')
const helpers = require('./helpers')
const { getEmailCopy } = require('./emailCopy')
const { name: journalName, staffEmail } = config.get('journal')
const unsubscribeSlug = config.get('unsubscribe.url')
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',
})
// EiC decided to either publish or reject the manuscript
async notifyHEWhenEiCMakesDecision() {
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const {
eicName,
titleText,
recommendation,
} = 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,
},
fromEmail: `${journalName} <${staffEmail}>`,
content: {
ctaText: 'MANUSCRIPT DETAILS',
subject: `${this.collection.customId}: Editorial decision confirmed`,
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`,
),
},
})
delete email.content.signatureJournal
helpers.sendHandlingEditorEmail({
email,
titleText,
targetUserName: eicName,
emailType:
recommendation === 'publish'
? 'he-manuscript-published'
: 'he-manuscript-rejected',
})
}
async notifyHEWhenEiCReturnsToHE() {
const { eicName, titleText } = await this.getNotificationProperties()
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: {
subject: `${
this.collection.customId
}: Editorial decision returned with comments`,
signatureName: eicName,
ctaText: 'MANUSCRIPT DETAILS',
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`,
),
},
})
delete email.content.signatureJournal
const eicComments = chain(this.newRecommendation)
.get('comments')
.find(comm => !comm.public)
.get('content')
.value()
helpers.sendHandlingEditorEmail({
email,
titleText,
comments: eicComments,
Andrei Cioromila
committed
targetUserName: eicName,
emailType: 'he-manuscript-return-with-comments',
})
}
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()
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
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
async notifyAuthorsWhenEiCMakesDecision() {
const {
eicName,
titleText,
activeAuthors,
recommendation,
parsedFragment,
} = await this.getNotificationProperties()
Andrei Cioromila
committed
const subjectOpts = {
publish: `${this.collection.customId}: Manuscript accepted`,
reject: `${this.collection.customId}: Manuscript rejected`,
}
const subject = subjectOpts[recommendation]
Andrei Cioromila
committed
Andrei Cioromila
committed
if (isEmpty(subject)) {
throw new Error(`Undefined recommendation: ${recommendation}`)
}
Andrei Cioromila
committed
Andrei Cioromila
committed
const hasPeerReview = !isEmpty(this.collection.handlingEditor)
const emailType = helpers.getEmailTypeByRecommendationForAuthors({
recommendation,
hasPeerReview,
})
const comments = hasPeerReview
? helpers.getHEComments({
heRecommendation: parsedFragment.heRecommendation,
})
: this.newRecommendation.comments[0].content
activeAuthors.forEach(async author => {
Andrei Cioromila
committed
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
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 { html, text } = email.getNotificationBody({
emailBodyProps: getEmailCopy({
titleText,
emailType,
comments,
}),
})
Andrei Cioromila
committed
Andrei Cioromila
committed
email.sendEmail({ html, text })
})
}
Andrei Cioromila
committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
async notifyReviewersWhenEiCMakesDecision() {
const {
eicName,
titleText,
recommendation,
fragmentHelper,
} = await this.getNotificationProperties()
const emailType =
recommendation === 'publish'
? 'submitted-reviewers-after-publish'
: 'submitted-reviewers-after-reject'
const subject =
recommendation === 'publish'
? 'A manuscript you reviewed has been accepted'
: 'A manuscript you reviewed has been rejected'
const reviewers = await fragmentHelper.getReviewers({
UserModel: this.UserModel,
type: 'submitted',
})
const emailBodyProps = getEmailCopy({
emailType,
titleText,
})
reviewers.forEach(reviewer => {
const email = new Email({
type: 'user',
toUser: {
email: reviewer.email,
name: reviewer.lastName,
},
fromEmail: `${eicName} <${staffEmail}>`,
content: {
signatureName: eicName,
ctaText: 'MANUSCRIPT DETAILS',
subject: `${this.collection.customId}: ${subject}`,
unsubscribeLink: services.createUrl(this.baseUrl, unsubscribeSlug, {
id: reviewer.id,
token: reviewer.accessTokens.unsubscribe,
}),
ctaLink: services.createUrl(
this.baseUrl,
`/projects/${this.collection.id}/versions/${
this.fragment.id
}/details`,
),
signatureJournal: journalName,
},
})
const { html, text } = email.getNotificationBody({ emailBodyProps })
email.sendEmail({ html, text })
})
}
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
async notifySAWhenHERequestsRevision() {
const {
eicName,
submittingAuthor,
parsedFragment: { title },
} = await this.getNotificationProperties()
const authorNoteText = helpers.getPrivateNoteTextForAuthor({
newRecommendation: this.newRecommendation,
})
const author = {
...submittingAuthor,
...getEmailCopy({
emailType: 'author-request-to-revision',
titleText: `your submission "${title}" to ${journalName}`,
comments: authorNoteText,
}),
}
const signatureName = get(this.collection, 'handlingEditor.name', eicName)
const email = new Email({
type: 'user',
toUser: {
email: author.email,
name: author.lastName,
},
fromEmail: `${signatureName} <${staffEmail}>`,
content: {
signatureName,
ctaText: 'MANUSCRIPT DETAILS',
signatureJournal: journalName,
subject: `${this.collection.customId}: Revision requested`,
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`,
),
},
})
const { html, text } = email.getNotificationBody({
emailBodyProps: {
paragraph: author.paragraph,
hasLink: author.hasLink,
hasIntro: author.hasIntro,
hasSignature: author.hasSignature,
},
})
email.sendEmail({ html, text })
}
Andrei Cioromila
committed
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
async notifyReviewersWhenHEMakesRecommendation() {
const {
eicName,
titleText,
fragmentHelper,
} = await this.getNotificationProperties()
const signatureName = get(this.collection, 'handlingEditor.name', eicName)
const acceptedReviewers = await fragmentHelper.getReviewers({
UserModel: this.UserModel,
type: 'accepted',
})
const acceptedReviewersEmailBody = getEmailCopy({
emailType: 'accepted-reviewers-after-recommendation',
titleText,
})
const pendingReviewers = await fragmentHelper.getReviewers({
UserModel: this.UserModel,
type: 'pending',
})
const pendingReviewersEmailBody = getEmailCopy({
emailType: 'pending-reviewers-after-recommendation',
titleText,
})
const buildSendEmailFunction = emailBodyProps => reviewer => {
const email = new Email({
type: 'user',
toUser: {
email: reviewer.email,
name: reviewer.lastName,
},
fromEmail: `${signatureName} <${staffEmail}>`,
Andrei Cioromila
committed
content: {
signatureName,
ctaText: 'MANUSCRIPT DETAILS',
subject: `${this.collection.customId}: Review no longer required`,
Andrei Cioromila
committed
unsubscribeLink: services.createUrl(this.baseUrl, unsubscribeSlug, {
id: reviewer.id,
token: reviewer.accessTokens.unsubscribe,
}),
ctaLink: services.createUrl(
this.baseUrl,
`/projects/${this.collection.id}/versions/${
this.fragment.id
}/details`,
),
signatureJournal: journalName,
},
})
const { html, text } = email.getNotificationBody({ emailBodyProps })
email.sendEmail({ html, text })
}
acceptedReviewers.forEach(
buildSendEmailFunction(acceptedReviewersEmailBody),
)
pendingReviewers.forEach(buildSendEmailFunction(pendingReviewersEmailBody))
}
Andrei Cioromila
committed
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
async notifyEiCWhenHEMakesRecommendation() {
const {
eicName,
titleText,
recommendation,
} = await this.getNotificationProperties()
let emailType, subject
switch (recommendation) {
case 'minor':
case 'major':
emailType = 'eic-request-revision-from-he'
subject = `${this.collection.customId}: Revision requested`
break
case 'publish':
emailType = 'eic-recommend-to-publish-from-he'
subject = `${this.collection.customId}: Recommendation to publish`
break
case 'reject':
emailType = 'eic-recommend-to-reject-from-he'
subject = `${this.collection.customId}: Recommendation to reject`
break
default:
throw new Error(`undefined recommendation: ${recommendation} `)
}
const privateNote = this.newRecommendation.comments.find(
comm => !comm.public,
)
const content = get(privateNote, 'content')
const comments = content
? `The editor provided the following comments: "${content}"`
: ''
const collHelper = new Collection({ collection: this.collection })
const targetUserName = collHelper.getHELastName()
const userHelper = new User({ UserModel: this.UserModel })
const editors = await userHelper.getEditorsInChief()
const emailBodyProps = getEmailCopy({
comments,
emailType,
titleText,
targetUserName,
})
editors.forEach(eic => {
const email = new Email({
type: 'user',
toUser: {
email: eic.email,
name: `${eic.firstName} ${eic.lastName}`,
},
fromEmail: `${journalName} <${staffEmail}>`,
content: {
signatureName: eicName,
subject,
ctaText: 'MANUSCRIPT DETAILS',
unsubscribeLink: this.baseUrl,
ctaLink: services.createUrl(
this.baseUrl,
`/projects/${this.collection.id}/versions/${
this.fragment.id
}/details`,
),
},
})
const { html, text } = email.getNotificationBody({ emailBodyProps })
email.sendEmail({ html, text })
})
}
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,
Andrei Cioromila
committed
fragmentHelper,
Andrei Cioromila
committed
}
}
}
module.exports = Notification