Skip to content
Snippets Groups Projects
Commit 650d0270 authored by Sebastian Mihalache's avatar Sebastian Mihalache :hammer_pick:
Browse files

refactor(email-templating): add props to constructors

parent e0ae33d4
No related branches found
No related tags found
3 merge requests!160Update staging with master features,!156Develop,!149Hin 1089
const config = require('config') const config = require('config')
const helpers = require('./helpers') const helpers = require('./helpers')
const SendEmail = require('@pubsweet/component-send-email') const SendEmail = require('@pubsweet/component-send-email')
const logger = require('@pubsweet/logger')
const configData = { const configData = {
logo: config.get('journal.logo'), logo: config.get('journal.logo'),
...@@ -10,9 +11,10 @@ const configData = { ...@@ -10,9 +11,10 @@ const configData = {
logoLink: config.get('journal.logoLink'), logoLink: config.get('journal.logoLink'),
publisher: config.get('journal.publisher'), publisher: config.get('journal.publisher'),
} }
class Email { class EmailTemplate {
constructor({ constructor({
type = 'system', type = 'system',
templateType = 'notification',
fromEmail = config.get('journal.staffEmail'), fromEmail = config.get('journal.staffEmail'),
toUser = { toUser = {
id: '', id: '',
...@@ -23,15 +25,19 @@ class Email { ...@@ -23,15 +25,19 @@ class Email {
subject: '', subject: '',
ctaLink: '', ctaLink: '',
ctaText: '', ctaText: '',
paragraph: '',
signatureName: '', signatureName: '',
unsubscribeLink: '', unsubscribeLink: '',
signatureJournal: '', signatureJournal: '',
}, },
bodyProps = { hasLink: false, hasIntro: false, hasSignature: false },
}) { }) {
this.type = type this.type = type
this.toUser = toUser this.toUser = toUser
this.content = content this.content = content
this.bodyProps = bodyProps
this.fromEmail = fromEmail this.fromEmail = fromEmail
this.templateType = templateType
} }
set _toUser(newToUser) { set _toUser(newToUser) {
...@@ -46,7 +52,7 @@ class Email { ...@@ -46,7 +52,7 @@ class Email {
this.content = newContent this.content = newContent
} }
getInvitationBody({ emailBodyProps }) { _getInvitationBody({ emailBodyProps }) {
return { return {
html: helpers.getCompiledInvitationBody({ html: helpers.getCompiledInvitationBody({
replacements: { replacements: {
...@@ -63,7 +69,7 @@ class Email { ...@@ -63,7 +69,7 @@ class Email {
} }
} }
getNotificationBody({ emailBodyProps }) { _getNotificationBody({ emailBodyProps }) {
return { return {
html: helpers.getCompiledNotificationBody({ html: helpers.getCompiledNotificationBody({
replacements: { replacements: {
...@@ -80,7 +86,12 @@ class Email { ...@@ -80,7 +86,12 @@ class Email {
} }
} }
async sendEmail({ text, html }) { async sendEmail() {
const { html, text } =
this.templateType === 'notification'
? this._getNotificationBody()
: this._getInvitationBody()
const { fromEmail: from } = this const { fromEmail: from } = this
const { email: to } = this.toUser const { email: to } = this.toUser
const { subject } = this.content const { subject } = this.content
...@@ -95,10 +106,17 @@ class Email { ...@@ -95,10 +106,17 @@ class Email {
try { try {
await SendEmail.send(mailData) await SendEmail.send(mailData)
logger.info(
`Sent email from: ${from} to: ${to} with subject: "${subject}"`,
)
logger.debug(
`Sent email from: ${from} to: ${to} with subject: "${subject}"`,
)
} catch (e) { } catch (e) {
logger.error(e)
throw new Error(e) throw new Error(e)
} }
} }
} }
module.exports = Email module.exports = EmailTemplate
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment