mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-20 22:42:53 -05:00
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.
This commit is contained in:
parent
2a6ae77221
commit
01855389a5
3 changed files with 99 additions and 144 deletions
|
@ -1,11 +1,7 @@
|
||||||
const Promise = require('bluebird');
|
const Promise = require('bluebird');
|
||||||
const {i18n} = require('../../lib/common');
|
const {i18n} = require('../../lib/common');
|
||||||
const logging = require('../../../shared/logging');
|
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
const security = require('@tryghost/security');
|
const invites = require('../../services/invites/invites');
|
||||||
const mailService = require('../../services/mail');
|
|
||||||
const urlUtils = require('../../../shared/url-utils');
|
|
||||||
const settingsCache = require('../../services/settings/cache');
|
|
||||||
const models = require('../../models');
|
const models = require('../../models');
|
||||||
const api = require('./index');
|
const api = require('./index');
|
||||||
const ALLOWED_INCLUDES = [];
|
const ALLOWED_INCLUDES = [];
|
||||||
|
@ -111,72 +107,15 @@ module.exports = {
|
||||||
unsafeAttrs: UNSAFE_ATTRS
|
unsafeAttrs: UNSAFE_ATTRS
|
||||||
},
|
},
|
||||||
query(frame) {
|
query(frame) {
|
||||||
let invite;
|
return invites.add({
|
||||||
let emailData;
|
api,
|
||||||
|
InviteModel: models.Invite,
|
||||||
// CASE: ensure we destroy the invite before
|
invites: frame.data.invites,
|
||||||
return models.Invite.findOne({email: frame.data.invites[0].email}, frame.options)
|
options: frame.options,
|
||||||
.then((existingInvite) => {
|
user: {
|
||||||
if (!existingInvite) {
|
name: frame.user.get('name'),
|
||||||
return;
|
email: frame.user.get('email')
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,7 @@
|
||||||
const Promise = require('bluebird');
|
const Promise = require('bluebird');
|
||||||
const {i18n} = require('../../lib/common');
|
const {i18n} = require('../../lib/common');
|
||||||
const logging = require('../../../shared/logging');
|
|
||||||
const errors = require('@tryghost/errors');
|
const errors = require('@tryghost/errors');
|
||||||
const security = require('@tryghost/security');
|
const invites = require('../../services/invites/invites');
|
||||||
const mailService = require('../../services/mail');
|
|
||||||
const urlUtils = require('../../../shared/url-utils');
|
|
||||||
const settingsCache = require('../../services/settings/cache');
|
|
||||||
const models = require('../../models');
|
const models = require('../../models');
|
||||||
const api = require('./index');
|
const api = require('./index');
|
||||||
const ALLOWED_INCLUDES = [];
|
const ALLOWED_INCLUDES = [];
|
||||||
|
@ -111,72 +107,15 @@ module.exports = {
|
||||||
unsafeAttrs: UNSAFE_ATTRS
|
unsafeAttrs: UNSAFE_ATTRS
|
||||||
},
|
},
|
||||||
query(frame) {
|
query(frame) {
|
||||||
let invite;
|
return invites.add({
|
||||||
let emailData;
|
api,
|
||||||
|
InviteModel: models.Invite,
|
||||||
// CASE: ensure we destroy the invite before
|
invites: frame.data.invites,
|
||||||
return models.Invite.findOne({email: frame.data.invites[0].email}, frame.options)
|
options: frame.options,
|
||||||
.then((existingInvite) => {
|
user: {
|
||||||
if (!existingInvite) {
|
name: frame.user.get('name'),
|
||||||
return;
|
email: frame.user.get('email')
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
77
core/server/services/invites/invites.js
Normal file
77
core/server/services/invites/invites.js
Normal file
|
@ -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;
|
Loading…
Add table
Reference in a new issue