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
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
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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
288
289
290
291
292
293
294
295
296
297
298
299
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 })
})
}
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,
Andrei Cioromila
committed
fragmentHelper,
Andrei Cioromila
committed
}
}
}
module.exports = Notification