mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
Removed method complexity in settings API v3 controller
refs https://github.com/TryGhost/Team/issues/694 refs https://linear.app/tryghost/issue/CORE-13 - The controller code is not meant to contain complex business logic. Removed complexity in settings.edit method - Have brought up to sync v3 controller code to the changes that were done in v4. Didn't touch v2 controller as it had slight API differences, so avoided going on another trip into the unknown - Migrating v3 controller was pretty straigh forward as it's an exact copy of the v4 one (at least for the methods that were extracted)
This commit is contained in:
parent
ae3b2fad7c
commit
d76ba2852e
2 changed files with 35 additions and 112 deletions
|
@ -1,15 +1,16 @@
|
|||
const Promise = require('bluebird');
|
||||
const _ = require('lodash');
|
||||
const validator = require('@tryghost/validator');
|
||||
const models = require('../../models');
|
||||
const routeSettings = require('../../services/route-settings');
|
||||
const frontendSettings = require('../../../frontend/services/settings');
|
||||
const i18n = require('../../../shared/i18n');
|
||||
const {BadRequestError, NoPermissionError, NotFoundError} = require('@tryghost/errors');
|
||||
const {BadRequestError, NoPermissionError} = require('@tryghost/errors');
|
||||
const settingsService = require('../../services/settings');
|
||||
const settingsCache = require('../../../shared/settings-cache');
|
||||
const membersService = require('../../services/members');
|
||||
|
||||
const settingsBREADService = settingsService.getSettingsBREADServiceInstance();
|
||||
|
||||
module.exports = {
|
||||
docName: 'settings',
|
||||
|
||||
|
@ -55,41 +56,7 @@ module.exports = {
|
|||
}
|
||||
},
|
||||
query(frame) {
|
||||
let setting;
|
||||
if (frame.options.key === 'slack') {
|
||||
const slackURL = settingsCache.get('slack_url', {resolve: false});
|
||||
const slackUsername = settingsCache.get('slack_username', {resolve: false});
|
||||
|
||||
setting = slackURL || slackUsername;
|
||||
setting.key = 'slack';
|
||||
setting.value = [{
|
||||
url: slackURL && slackURL.value,
|
||||
username: slackUsername && slackUsername.value
|
||||
}];
|
||||
} else {
|
||||
setting = settingsCache.get(frame.options.key, {resolve: false});
|
||||
}
|
||||
|
||||
if (!setting) {
|
||||
return Promise.reject(new NotFoundError({
|
||||
message: i18n.t('errors.api.settings.problemFindingSetting', {
|
||||
key: frame.options.key
|
||||
})
|
||||
}));
|
||||
}
|
||||
|
||||
// @TODO: handle in settings model permissible fn
|
||||
if (setting.group === 'core' && !(frame.options.context && frame.options.context.internal)) {
|
||||
return Promise.reject(new NoPermissionError({
|
||||
message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')
|
||||
}));
|
||||
}
|
||||
|
||||
setting = settingsService.hideValueIfSecret(setting);
|
||||
|
||||
return {
|
||||
[frame.options.key]: setting
|
||||
};
|
||||
return settingsBREADService.read(frame.options.key, frame.options.context);
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -153,17 +120,7 @@ module.exports = {
|
|||
],
|
||||
async query(frame) {
|
||||
const {email, type} = frame.data;
|
||||
if (typeof email !== 'string' || !validator.isEmail(email)) {
|
||||
throw new BadRequestError({
|
||||
message: i18n.t('errors.api.settings.invalidEmailReceived')
|
||||
});
|
||||
}
|
||||
|
||||
if (!type || !['fromAddressUpdate', 'supportAddressUpdate'].includes(type)) {
|
||||
throw new BadRequestError({
|
||||
message: 'Invalid email type recieved'
|
||||
});
|
||||
}
|
||||
try {
|
||||
// Send magic link to update fromAddress
|
||||
await membersService.settings.sendEmailAddressUpdateMagicLink({
|
||||
|
@ -232,76 +189,20 @@ module.exports = {
|
|||
}
|
||||
},
|
||||
async query(frame) {
|
||||
let stripeConnectData;
|
||||
const stripeConnectIntegrationToken = frame.data.settings.find(setting => setting.key === 'stripe_connect_integration_token');
|
||||
|
||||
const settings = frame.data.settings.filter((setting) => {
|
||||
// The `stripe_connect_integration_token` "setting" is only used to set the `stripe_connect_*` settings.
|
||||
return ![
|
||||
'stripe_connect_integration_token',
|
||||
'stripe_connect_publishable_key',
|
||||
'stripe_connect_secret_key',
|
||||
'stripe_connect_livemode',
|
||||
'stripe_connect_account_id',
|
||||
'stripe_connect_display_name'
|
||||
].includes(setting.key)
|
||||
// Remove obfuscated settings
|
||||
&& !(setting.value === settingsService.obfuscatedSetting && settingsService.isSecretSetting(setting));
|
||||
});
|
||||
|
||||
const getSetting = setting => settingsCache.get(setting.key, {resolve: false});
|
||||
|
||||
const firstUnknownSetting = settings.find(setting => !getSetting(setting));
|
||||
|
||||
if (firstUnknownSetting) {
|
||||
throw new NotFoundError({
|
||||
message: i18n.t('errors.api.settings.problemFindingSetting', {
|
||||
key: firstUnknownSetting.key
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
if (!(frame.options.context && frame.options.context.internal)) {
|
||||
const firstCoreSetting = settings.find(setting => getSetting(setting).group === 'core');
|
||||
if (firstCoreSetting) {
|
||||
throw new NoPermissionError({
|
||||
message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (stripeConnectIntegrationToken && stripeConnectIntegrationToken.value) {
|
||||
const getSessionProp = prop => frame.original.session[prop];
|
||||
try {
|
||||
const data = await membersService.stripeConnect.getStripeConnectTokenData(stripeConnectIntegrationToken.value, getSessionProp);
|
||||
settings.push({
|
||||
key: 'stripe_connect_publishable_key',
|
||||
value: data.public_key
|
||||
});
|
||||
settings.push({
|
||||
key: 'stripe_connect_secret_key',
|
||||
value: data.secret_key
|
||||
});
|
||||
settings.push({
|
||||
key: 'stripe_connect_livemode',
|
||||
value: data.livemode
|
||||
});
|
||||
settings.push({
|
||||
key: 'stripe_connect_display_name',
|
||||
value: data.display_name
|
||||
});
|
||||
settings.push({
|
||||
key: 'stripe_connect_account_id',
|
||||
value: data.account_id
|
||||
});
|
||||
} catch (err) {
|
||||
throw new BadRequestError({
|
||||
err,
|
||||
message: 'The Stripe Connect token could not be parsed.'
|
||||
});
|
||||
}
|
||||
|
||||
stripeConnectData = await settingsBREADService.getStripeConnectData(
|
||||
stripeConnectIntegrationToken,
|
||||
getSessionProp,
|
||||
membersService.stripeConnect.getStripeConnectTokenData
|
||||
);
|
||||
}
|
||||
|
||||
return models.Settings.edit(settings, frame.options);
|
||||
return await settingsBREADService.edit(frame.data.settings, frame.options, stripeConnectData);
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
@ -1,7 +1,13 @@
|
|||
const Promise = require('bluebird');
|
||||
const _ = require('lodash');
|
||||
const i18n = require('../../../../../../shared/i18n');
|
||||
const {NotFoundError, ValidationError} = require('@tryghost/errors');
|
||||
const {NotFoundError, ValidationError, BadRequestError} = require('@tryghost/errors');
|
||||
const validator = require('@tryghost/validator');
|
||||
|
||||
const messages = {
|
||||
invalidEmailReceived: 'Please send a valid email',
|
||||
invalidEmailTypeReceived: 'Invalid email type received'
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
read(apiConfig, frame) {
|
||||
|
@ -62,5 +68,21 @@ module.exports = {
|
|||
if (errors.length) {
|
||||
return Promise.reject(errors[0]);
|
||||
}
|
||||
},
|
||||
|
||||
updateMembersEmail(apiConfig, frame) {
|
||||
const {email, type} = frame.data;
|
||||
|
||||
if (typeof email !== 'string' || !validator.isEmail(email)) {
|
||||
throw new BadRequestError({
|
||||
message: messages.invalidEmailReceived
|
||||
});
|
||||
}
|
||||
|
||||
if (!type || !['fromAddressUpdate', 'supportAddressUpdate'].includes(type)) {
|
||||
throw new BadRequestError({
|
||||
message: messages.invalidEmailTypeReceived
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue