Newer
Older
const config = require('config')
Sebastian Mihalache
committed
const { last } = require('lodash')
const Email = require('@pubsweet/component-email-templating')
Sebastian Mihalache
committed
const { name: journalName, staffEmail } = config.get('journal')
Sebastian Mihalache
committed
const {
User,
services,
Fragment,
} = require('pubsweet-component-helper-service')
Andrei Cioromila
committed
const { getEmailCopy } = require('./emailCopy')
const unsubscribeSlug = config.get('unsubscribe.url')
Sebastian Mihalache
committed
module.exports = {
Andrei Cioromila
committed
sendInvitedHEEmail: async ({
Sebastian Mihalache
committed
baseUrl,
invitedHE,
collection,
isCanceled = false,
models: { User: UserModel, Fragment: FragmentModel },
Andrei Cioromila
committed
}) => {
Sebastian Mihalache
committed
const fragmentId = last(collection.fragments)
const fragment = await FragmentModel.find(fragmentId)
const fragmentHelper = new Fragment({ fragment })
const { title } = await fragmentHelper.getFragmentData()
const { submittingAuthor } = await fragmentHelper.getAuthorData({
UserModel,
})
const titleText = `the manuscript titled "${title}" by ${
submittingAuthor.firstName
Sebastian Mihalache
committed
const userHelper = new User({ UserModel })
const eics = await userHelper.getEditorsInChief()
const eic = eics[0]
Sebastian Mihalache
committed
const eicName = `${eic.firstName} ${eic.lastName}`
const { customId } = collection
Sebastian Mihalache
committed
Andrei Cioromila
committed
const { paragraph, ...bodyProps } = getEmailCopy({
titleText,
targetUserName: eicName,
emailType: isCanceled ? 'he-revoked' : 'he-assigned',
})
Sebastian Mihalache
committed
const email = new Email({
type: 'user',
fromEmail: `${journalName} <${staffEmail}>`,
Andrei Cioromila
committed
toUser: {
email: invitedHE.email,
},
content: {
subject: isCanceled
? `${customId}: Editor invitation cancelled`
: `${customId}: Invitation to edit a manuscript`,
paragraph,
signatureName: eicName,
ctaLink: services.createUrl(
baseUrl,
`/projects/${collection.id}/versions/${fragment.id}/details`,
),
ctaText: 'MANUSCRIPT DETAILS',
unsubscribeLink: services.createUrl(baseUrl, unsubscribeSlug, {
id: invitedHE.id,
token: invitedHE.accessTokens.unsubscribe,
}),
},
bodyProps,
})
Andrei Cioromila
committed
return email.sendEmail()
Andrei Cioromila
committed
},
notifyAuthorWhenHERemoved: async ({
baseUrl,
collection,
models: { User: UserModel, Fragment: FragmentModel },
}) => {
const fragmentId = last(collection.fragments)
const fragment = await FragmentModel.find(fragmentId)
const fragmentHelper = new Fragment({ fragment })
const { title: titleText } = await fragmentHelper.getFragmentData()
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
117
118
119
120
121
122
123
124
125
const { submittingAuthor } = await fragmentHelper.getAuthorData({
UserModel,
})
const submittingAuthorName = `${submittingAuthor.firstName} ${
submittingAuthor.lastName
}`
const userHelper = new User({ UserModel })
const eics = await userHelper.getEditorsInChief()
const eic = eics[0]
const eicName = `${eic.firstName} ${eic.lastName}`
const { customId } = collection
const { paragraph, ...bodyProps } = getEmailCopy({
titleText,
targetUserName: submittingAuthorName,
emailType: 'author-he-removed',
})
const email = new Email({
type: 'user',
fromEmail: `${eicName} <${staffEmail}>`,
toUser: {
email: submittingAuthor.email,
name: submittingAuthorName,
},
content: {
subject: `${customId}: Your manuscript's editor was changed`,
paragraph,
signatureName: eicName,
signatureJournal: journalName,
unsubscribeLink: services.createUrl(baseUrl, unsubscribeSlug, {
id: submittingAuthor.id,
token: submittingAuthor.accessTokens.unsubscribe,
}),
},
bodyProps,
})
return email.sendEmail()
},
Andrei Cioromila
committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
sendEiCEmail: async ({
reason,
baseUrl,
invitedHE,
collection,
isAccepted = false,
models: { User: UserModel, Fragment: FragmentModel },
}) => {
const fragmentId = last(collection.fragments)
const fragment = await FragmentModel.find(fragmentId)
const fragmentHelper = new Fragment({ fragment })
const { title } = await fragmentHelper.getFragmentData()
const { submittingAuthor } = await fragmentHelper.getAuthorData({
UserModel,
})
const titleText = `the manuscript titled "${title}" by ${
submittingAuthor.firstName
} ${submittingAuthor.lastName}`
const userHelper = new User({ UserModel })
const eics = await userHelper.getEditorsInChief()
const eic = eics[0]
const eicName = `${eic.firstName} ${eic.lastName}`
const { customId } = collection
const emailType = isAccepted ? 'he-accepted' : 'he-declined'
const { paragraph, ...bodyProps } = getEmailCopy({
emailType,
titleText,
comments: reason ? `Reason: "${reason}"` : '',
targetUserName: `${invitedHE.lastName}`,
})
const email = new Email({
type: 'user',
fromEmail: `${journalName} <${staffEmail}>`,
toUser: {
email: eic.email,
},
Sebastian Mihalache
committed
content: {
Andrei Cioromila
committed
subject: isAccepted
? `${customId}: Editor invitation accepted`
: `${customId}: Editor invitation declined`,
paragraph,
Sebastian Mihalache
committed
signatureName: eicName,
ctaLink: services.createUrl(
baseUrl,
`/projects/${collection.id}/versions/${fragment.id}/details`,
),
ctaText: 'MANUSCRIPT DETAILS',
Andrei Cioromila
committed
unsubscribeLink: services.createUrl(baseUrl),
Sebastian Mihalache
committed
},
Andrei Cioromila
committed
bodyProps,
Sebastian Mihalache
committed
})
Andrei Cioromila
committed
return email.sendEmail()