Removed method complexity in settings API 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
- The code causing the complexity warning clearly belonged in the validation layer, so has been moved to it's propper location
This commit is contained in:
Naz 2021-09-20 20:05:58 +02:00 committed by naz
parent e7ec197da1
commit ae3b2fad7c
2 changed files with 24 additions and 13 deletions

View File

@ -1,11 +1,10 @@
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} = require('@tryghost/errors');
const {BadRequestError} = require('@tryghost/errors');
const settingsService = require('../../services/settings');
const settingsCache = require('../../../shared/settings-cache');
const membersService = require('../../services/members');
@ -121,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({

View File

@ -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
});
}
}
};