Added handling for new members support/reply email addresses

no issue

- Updated magic link generation and validation methods for email update API to handle new support address
- Updated importer to ignore the new support address as it can only be updated via verification
- Updated members service to listen on settings edit for new support/reply address fields as well
- Updated tests to include the new settings
This commit is contained in:
Rish 2020-08-26 13:15:01 +05:30 committed by Rishabh Garg
parent 8784769f60
commit cbb3664bdd
5 changed files with 41 additions and 19 deletions

View File

@ -74,15 +74,19 @@ module.exports = {
}
},
validateMembersFromEmail: {
validateMembersEmailUpdate: {
options: [
'token'
'token',
'action'
],
permissions: false,
validation: {
options: {
token: {
required: true
},
action: {
values: ['fromaddressupdate', 'supportaddressupdate']
}
}
},
@ -90,14 +94,19 @@ module.exports = {
// This is something you have to do if you want to use the "framework" with access to the raw req/res
frame.response = async function (req, res) {
try {
const updatedFromAddress = membersService.settings.getEmailFromToken({token: frame.options.token});
if (updatedFromAddress) {
const {token, action} = frame.options;
const updatedEmailAddress = membersService.settings.getEmailFromToken({token});
const actionToKeyMapping = {
fromAddressUpdate: 'members_from_address',
supportAddressUpdate: 'members_support_address'
};
if (updatedEmailAddress) {
return models.Settings.edit({
key: 'members_from_address',
value: updatedFromAddress
key: actionToKeyMapping[action],
value: updatedEmailAddress
}).then(() => {
// Redirect to Ghost-Admin settings page
const adminLink = membersService.settings.getAdminRedirectLink();
const adminLink = membersService.settings.getAdminRedirectLink({type: action});
res.redirect(adminLink);
});
} else {
@ -115,21 +124,32 @@ module.exports = {
}
},
updateMembersFromEmail: {
updateMembersEmail: {
permissions: {
method: 'edit'
},
data: [
'email',
'type'
],
async query(frame) {
const email = frame.data.from_address;
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.sendFromAddressUpdateMagicLink({
email
await membersService.settings.sendEmailAddressUpdateMagicLink({
email,
type
});
} catch (err) {
throw new BadRequestError({

View File

@ -143,7 +143,7 @@ class SettingsImporter extends BaseImporter {
}
// CASE: we do not import "from address" for members settings as that needs to go via validation with magic link
if (obj.key === 'members_from_address') {
if ((obj.key === 'members_from_address') || (obj.key === 'members_support_address')) {
obj.value = null;
}

View File

@ -39,6 +39,8 @@ events.on('settings.edited', function updateSettingFromModel(settingModel) {
if (![
'members_allow_free_signup',
'members_from_address',
'members_support_address',
'members_reply_address',
'stripe_publishable_key',
'stripe_secret_key',
'stripe_product_name',

View File

@ -70,21 +70,21 @@ function createSettingsInstance(config) {
getSubject
});
const sendFromAddressUpdateMagicLink = ({email, payload = {}}) => {
return magicLinkService.sendMagicLink({email, payload, subject: email, type: 'updateFromAddress'});
const sendEmailAddressUpdateMagicLink = ({email, payload = {}, type = 'fromAddressUpdate'}) => {
return magicLinkService.sendMagicLink({email, payload, subject: email, type});
};
const getEmailFromToken = ({token}) => {
return magicLinkService.getUserFromToken(token);
};
const getAdminRedirectLink = () => {
const getAdminRedirectLink = ({type}) => {
const adminUrl = urlUtils.urlFor('admin', true);
return urlUtils.urlJoin(adminUrl, '#/settings/labs/?fromAddressUpdate=success');
return urlUtils.urlJoin(adminUrl, `#/settings/labs/?${type}=success`);
};
return {
sendFromAddressUpdateMagicLink,
sendEmailAddressUpdateMagicLink,
getEmailFromToken,
getAdminRedirectLink
};

View File

@ -61,8 +61,8 @@ module.exports = function apiRoutes() {
router.get('/settings', mw.authAdminApi, http(apiCanary.settings.browse));
router.get('/settings/:key', mw.authAdminApi, http(apiCanary.settings.read));
router.put('/settings', mw.authAdminApi, http(apiCanary.settings.edit));
router.get('/settings/members/email', http(apiCanary.settings.validateMembersFromEmail));
router.post('/settings/members/email', mw.authAdminApi, http(apiCanary.settings.updateMembersFromEmail));
router.get('/settings/members/email', http(apiCanary.settings.validateMembersEmailUpdate));
router.post('/settings/members/email', mw.authAdminApi, http(apiCanary.settings.updateMembersEmail));
router.del('/settings/stripe/connect', mw.authAdminApi, http(apiCanary.settings.disconnectStripeConnectIntegration));
// ## Users