const config = require('config') const helpers = require('./helpers') const SendEmail = require('@pubsweet/component-send-email') const logger = require('@pubsweet/logger') const mainFromEmail = config.get('journal.staffEmail') class Email { constructor({ type = 'system', fromEmail = mainFromEmail, toUser = { id: '', email: '', name: '', }, content = { subject: '', ctaLink: '', ctaText: '', signatureName: '', unsubscribeLink: '', signatureJournal: '', }, }) { this.type = type this.toUser = toUser this.content = content this.fromEmail = fromEmail } set _toUser(newToUser) { this.toUser = newToUser } set _subject(newSubject) { this.subject = newSubject } set _content(newContent) { this.content = newContent } getBody({ body = {}, isReviewerInvitation = false }) { if (isReviewerInvitation) { return { html: helpers.getInvitationBody({ replacements: { toUserName: this.toUser.name, ...this.content, ...body, }, }), text: `${this.content.signatureName}`, } } return { html: helpers.getNotificationBody({ replacements: { ...body, toUserName: this.toUser.name, ...this.content, }, }), text: `${body.paragraph} ${this.content.ctaLink} ${ this.content.ctaText } ${this.content.signatureName}`, } } sendEmail({ text, html }) { const { fromEmail: from } = this const { email: to } = this.toUser const { subject } = this.content const mailData = { to, text, html, from, subject, } logger.info( `EMAIL: Sent email from ${from} to ${to} with subject '${subject}'`, ) SendEmail.send(mailData) } } module.exports = Email