From 01855389a56c39fb23c57b19eaf73d91fb0a9217 Mon Sep 17 00:00:00 2001 From: Naz Date: Thu, 14 Jan 2021 19:25:16 +1300 Subject: [PATCH] Moved invites controller code into a service module refs #12537 - Part of the controller code refactor into services to avoid code duplication during introduction of a new API. --- core/server/api/canary/invites.js | 83 ++++--------------------- core/server/api/v2/invites.js | 83 ++++--------------------- core/server/services/invites/invites.js | 77 +++++++++++++++++++++++ 3 files changed, 99 insertions(+), 144 deletions(-) create mode 100644 core/server/services/invites/invites.js diff --git a/core/server/api/canary/invites.js b/core/server/api/canary/invites.js index 3ccb88776e..2d598cbbda 100644 --- a/core/server/api/canary/invites.js +++ b/core/server/api/canary/invites.js @@ -1,11 +1,7 @@ const Promise = require('bluebird'); const {i18n} = require('../../lib/common'); -const logging = require('../../../shared/logging'); const errors = require('@tryghost/errors'); -const security = require('@tryghost/security'); -const mailService = require('../../services/mail'); -const urlUtils = require('../../../shared/url-utils'); -const settingsCache = require('../../services/settings/cache'); +const invites = require('../../services/invites/invites'); const models = require('../../models'); const api = require('./index'); const ALLOWED_INCLUDES = []; @@ -111,73 +107,16 @@ module.exports = { unsafeAttrs: UNSAFE_ATTRS }, query(frame) { - let invite; - let emailData; - - // CASE: ensure we destroy the invite before - return models.Invite.findOne({email: frame.data.invites[0].email}, frame.options) - .then((existingInvite) => { - if (!existingInvite) { - return; - } - - return existingInvite.destroy(frame.options); - }) - .then(() => { - return models.Invite.add(frame.data.invites[0], frame.options); - }) - .then((createdInvite) => { - invite = createdInvite; - - const adminUrl = urlUtils.urlFor('admin', true); - - emailData = { - blogName: settingsCache.get('title'), - invitedByName: frame.user.get('name'), - invitedByEmail: frame.user.get('email'), - resetLink: urlUtils.urlJoin(adminUrl, 'signup', security.url.encodeBase64(invite.get('token')), '/') - }; - - return mailService.utils.generateContent({data: emailData, template: 'invite-user'}); - }) - .then((emailContent) => { - const payload = { - mail: [{ - message: { - to: invite.get('email'), - subject: i18n.t('common.api.users.mail.invitedByName', { - invitedByName: emailData.invitedByName, - blogName: emailData.blogName - }), - html: emailContent.html, - text: emailContent.text - }, - options: {} - }] - }; - - return api.mail.send(payload, {context: {internal: true}}); - }) - .then(() => { - return models.Invite.edit({ - status: 'sent' - }, Object.assign({id: invite.id}, frame.options)); - }) - .then((editedInvite) => { - return editedInvite; - }) - .catch((err) => { - if (err && err.errorType === 'EmailError') { - const errorMessage = i18n.t('errors.api.invites.errorSendingEmail.error', { - message: err.message - }); - const helpText = i18n.t('errors.api.invites.errorSendingEmail.help'); - err.message = `${errorMessage} ${helpText}`; - logging.warn(err.message); - } - - return Promise.reject(err); - }); + return invites.add({ + api, + InviteModel: models.Invite, + invites: frame.data.invites, + options: frame.options, + user: { + name: frame.user.get('name'), + email: frame.user.get('email') + } + }); } } }; diff --git a/core/server/api/v2/invites.js b/core/server/api/v2/invites.js index b6f88c4d63..2d598cbbda 100644 --- a/core/server/api/v2/invites.js +++ b/core/server/api/v2/invites.js @@ -1,11 +1,7 @@ const Promise = require('bluebird'); const {i18n} = require('../../lib/common'); -const logging = require('../../../shared/logging'); const errors = require('@tryghost/errors'); -const security = require('@tryghost/security'); -const mailService = require('../../services/mail'); -const urlUtils = require('../../../shared/url-utils'); -const settingsCache = require('../../services/settings/cache'); +const invites = require('../../services/invites/invites'); const models = require('../../models'); const api = require('./index'); const ALLOWED_INCLUDES = []; @@ -111,73 +107,16 @@ module.exports = { unsafeAttrs: UNSAFE_ATTRS }, query(frame) { - let invite; - let emailData; - - // CASE: ensure we destroy the invite before - return models.Invite.findOne({email: frame.data.invites[0].email}, frame.options) - .then((existingInvite) => { - if (!existingInvite) { - return; - } - - return existingInvite.destroy(frame.options); - }) - .then(() => { - return models.Invite.add(frame.data.invites[0], frame.options); - }) - .then((_invite) => { - invite = _invite; - - const adminUrl = urlUtils.urlFor('admin', true); - - emailData = { - blogName: settingsCache.get('title'), - invitedByName: frame.user.get('name'), - invitedByEmail: frame.user.get('email'), - resetLink: urlUtils.urlJoin(adminUrl, 'signup', security.url.encodeBase64(invite.get('token')), '/') - }; - - return mailService.utils.generateContent({data: emailData, template: 'invite-user'}); - }) - .then((emailContent) => { - const payload = { - mail: [{ - message: { - to: invite.get('email'), - subject: i18n.t('common.api.users.mail.invitedByName', { - invitedByName: emailData.invitedByName, - blogName: emailData.blogName - }), - html: emailContent.html, - text: emailContent.text - }, - options: {} - }] - }; - - return api.mail.send(payload, {context: {internal: true}}); - }) - .then(() => { - return models.Invite.edit({ - status: 'sent' - }, Object.assign({id: invite.id}, frame.options)); - }) - .then((editedInvite) => { - return editedInvite; - }) - .catch((err) => { - if (err && err.errorType === 'EmailError') { - const errorMessage = i18n.t('errors.api.invites.errorSendingEmail.error', { - message: err.message - }); - const helpText = i18n.t('errors.api.invites.errorSendingEmail.help'); - err.message = `${errorMessage} ${helpText}`; - logging.warn(err.message); - } - - return Promise.reject(err); - }); + return invites.add({ + api, + InviteModel: models.Invite, + invites: frame.data.invites, + options: frame.options, + user: { + name: frame.user.get('name'), + email: frame.user.get('email') + } + }); } } }; diff --git a/core/server/services/invites/invites.js b/core/server/services/invites/invites.js new file mode 100644 index 0000000000..edff87088e --- /dev/null +++ b/core/server/services/invites/invites.js @@ -0,0 +1,77 @@ +const security = require('@tryghost/security'); +const mailService = require('../../services/mail'); +const urlUtils = require('../../../shared/url-utils'); +const settingsCache = require('../../services/settings/cache'); +const logging = require('../../../shared/logging'); +const {i18n} = require('../../lib/common'); + +const add = ({api, InviteModel, invites, options, user}) => { + let invite; + let emailData; + + return InviteModel.findOne({email: invites[0].email}, options) + .then((existingInvite) => { + if (!existingInvite) { + return; + } + + return existingInvite.destroy(options); + }) + .then(() => { + return InviteModel.add(invites[0], options); + }) + .then((createdInvite) => { + invite = createdInvite; + + const adminUrl = urlUtils.urlFor('admin', true); + + emailData = { + blogName: settingsCache.get('title'), + invitedByName: user.name, + invitedByEmail: user.email, + resetLink: urlUtils.urlJoin(adminUrl, 'signup', security.url.encodeBase64(invite.get('token')), '/') + }; + + return mailService.utils.generateContent({data: emailData, template: 'invite-user'}); + }) + .then((emailContent) => { + const payload = { + mail: [{ + message: { + to: invite.get('email'), + subject: i18n.t('common.api.users.mail.invitedByName', { + invitedByName: emailData.invitedByName, + blogName: emailData.blogName + }), + html: emailContent.html, + text: emailContent.text + }, + options: {} + }] + }; + + return api.mail.send(payload, {context: {internal: true}}); + }) + .then(() => { + return InviteModel.edit({ + status: 'sent' + }, Object.assign({id: invite.id}, options)); + }) + .then((editedInvite) => { + return editedInvite; + }) + .catch((err) => { + if (err && err.errorType === 'EmailError') { + const errorMessage = i18n.t('errors.api.invites.errorSendingEmail.error', { + message: err.message + }); + const helpText = i18n.t('errors.api.invites.errorSendingEmail.help'); + err.message = `${errorMessage} ${helpText}`; + logging.warn(err.message); + } + + return Promise.reject(err); + }); +}; + +module.exports.add = add;