2019-08-09 17:11:24 +03:00
|
|
|
const Promise = require('bluebird');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const models = require('../../models');
|
2021-06-28 14:09:02 +03:00
|
|
|
const routeSettings = require('../../services/route-settings');
|
2020-09-09 15:28:12 +03:00
|
|
|
const frontendSettings = require('../../../frontend/services/settings');
|
2021-05-03 19:29:44 +03:00
|
|
|
const i18n = require('../../../shared/i18n');
|
2021-09-20 21:05:58 +03:00
|
|
|
const {BadRequestError} = require('@tryghost/errors');
|
2020-09-09 15:28:12 +03:00
|
|
|
const settingsService = require('../../services/settings');
|
2020-05-28 13:40:50 +03:00
|
|
|
const membersService = require('../../services/members');
|
2019-08-09 17:11:24 +03:00
|
|
|
|
2021-09-20 16:40:34 +03:00
|
|
|
const settingsBREADService = settingsService.getSettingsBREADServiceInstance();
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
module.exports = {
|
|
|
|
docName: 'settings',
|
|
|
|
|
|
|
|
browse: {
|
2021-01-26 14:15:21 +03:00
|
|
|
options: ['group'],
|
2019-08-09 17:11:24 +03:00
|
|
|
permissions: true,
|
|
|
|
query(frame) {
|
2021-09-21 12:57:30 +03:00
|
|
|
return settingsBREADService.browse(frame.options.context);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
read: {
|
|
|
|
options: ['key'],
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
key: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
identifier(frame) {
|
|
|
|
return frame.options.key;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
query(frame) {
|
2021-09-20 16:40:34 +03:00
|
|
|
return settingsBREADService.read(frame.options.key, frame.options.context);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-08-26 10:45:01 +03:00
|
|
|
validateMembersEmailUpdate: {
|
2020-06-05 19:20:04 +03:00
|
|
|
options: [
|
2020-08-26 10:45:01 +03:00
|
|
|
'token',
|
|
|
|
'action'
|
2020-06-05 19:20:04 +03:00
|
|
|
],
|
|
|
|
permissions: false,
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
token: {
|
|
|
|
required: true
|
2020-08-26 10:45:01 +03:00
|
|
|
},
|
|
|
|
action: {
|
|
|
|
values: ['fromaddressupdate', 'supportaddressupdate']
|
2020-06-05 19:20:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async query(frame) {
|
|
|
|
// 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 {
|
2020-08-26 10:45:01 +03:00
|
|
|
const {token, action} = frame.options;
|
2020-09-24 20:31:28 +03:00
|
|
|
const updatedEmailAddress = await membersService.settings.getEmailFromToken({token});
|
2020-08-26 10:45:01 +03:00
|
|
|
const actionToKeyMapping = {
|
|
|
|
fromAddressUpdate: 'members_from_address',
|
|
|
|
supportAddressUpdate: 'members_support_address'
|
|
|
|
};
|
|
|
|
if (updatedEmailAddress) {
|
2020-06-05 19:20:04 +03:00
|
|
|
return models.Settings.edit({
|
2020-08-26 10:45:01 +03:00
|
|
|
key: actionToKeyMapping[action],
|
|
|
|
value: updatedEmailAddress
|
2020-06-05 19:20:04 +03:00
|
|
|
}).then(() => {
|
|
|
|
// Redirect to Ghost-Admin settings page
|
2020-08-26 10:45:01 +03:00
|
|
|
const adminLink = membersService.settings.getAdminRedirectLink({type: action});
|
2020-06-05 19:20:04 +03:00
|
|
|
res.redirect(adminLink);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Promise.reject(new BadRequestError({
|
|
|
|
message: 'Invalid token!'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return Promise.reject(new BadRequestError({
|
|
|
|
err,
|
|
|
|
message: 'Invalid token!'
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-08-26 10:45:01 +03:00
|
|
|
updateMembersEmail: {
|
2020-06-05 19:20:04 +03:00
|
|
|
permissions: {
|
|
|
|
method: 'edit'
|
|
|
|
},
|
2020-08-26 10:45:01 +03:00
|
|
|
data: [
|
|
|
|
'email',
|
|
|
|
'type'
|
|
|
|
],
|
2020-06-05 19:20:04 +03:00
|
|
|
async query(frame) {
|
2020-08-26 10:45:01 +03:00
|
|
|
const {email, type} = frame.data;
|
|
|
|
|
2020-06-05 19:20:04 +03:00
|
|
|
try {
|
|
|
|
// Send magic link to update fromAddress
|
2020-08-26 10:45:01 +03:00
|
|
|
await membersService.settings.sendEmailAddressUpdateMagicLink({
|
|
|
|
email,
|
|
|
|
type
|
2020-06-05 19:20:04 +03:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
throw new BadRequestError({
|
|
|
|
err,
|
|
|
|
message: i18n.t('errors.mail.failedSendingEmail.error')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-06-18 19:07:02 +03:00
|
|
|
disconnectStripeConnectIntegration: {
|
|
|
|
permissions: {
|
|
|
|
method: 'edit'
|
|
|
|
},
|
|
|
|
async query(frame) {
|
|
|
|
const hasActiveStripeSubscriptions = await membersService.api.hasActiveStripeSubscriptions();
|
|
|
|
if (hasActiveStripeSubscriptions) {
|
|
|
|
throw new BadRequestError({
|
|
|
|
message: 'Cannot disconnect Stripe whilst you have active subscriptions.'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-07 19:25:53 +03:00
|
|
|
await membersService.api.disconnectStripe();
|
2021-06-08 18:58:16 +03:00
|
|
|
|
2020-06-29 17:22:42 +03:00
|
|
|
return models.Settings.edit([{
|
|
|
|
key: 'stripe_connect_publishable_key',
|
|
|
|
value: null
|
|
|
|
}, {
|
|
|
|
key: 'stripe_connect_secret_key',
|
|
|
|
value: null
|
|
|
|
}, {
|
|
|
|
key: 'stripe_connect_livemode',
|
|
|
|
value: null
|
|
|
|
}, {
|
|
|
|
key: 'stripe_connect_display_name',
|
|
|
|
value: null
|
|
|
|
}, {
|
|
|
|
key: 'stripe_connect_account_id',
|
|
|
|
value: null
|
2021-09-07 19:32:07 +03:00
|
|
|
}, {
|
|
|
|
key: 'members_stripe_webhook_id',
|
|
|
|
value: null
|
|
|
|
}, {
|
|
|
|
key: 'members_stripe_webhook_secret',
|
|
|
|
value: null
|
2020-06-29 17:22:42 +03:00
|
|
|
}], frame.options);
|
2020-06-18 19:07:02 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-08-09 17:11:24 +03:00
|
|
|
edit: {
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
permissions: {
|
2019-10-09 11:26:54 +03:00
|
|
|
unsafeAttrsObject(frame) {
|
|
|
|
return _.find(frame.data.settings, {key: 'labs'});
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
2020-05-28 13:40:47 +03:00
|
|
|
async query(frame) {
|
2021-09-20 20:06:16 +03:00
|
|
|
let stripeConnectData;
|
2020-05-28 13:40:50 +03:00
|
|
|
const stripeConnectIntegrationToken = frame.data.settings.find(setting => setting.key === 'stripe_connect_integration_token');
|
|
|
|
|
2020-06-02 19:58:30 +03:00
|
|
|
if (stripeConnectIntegrationToken && stripeConnectIntegrationToken.value) {
|
2020-05-28 13:40:50 +03:00
|
|
|
const getSessionProp = prop => frame.original.session[prop];
|
2021-09-20 20:06:16 +03:00
|
|
|
|
|
|
|
stripeConnectData = await settingsBREADService.getStripeConnectData(
|
|
|
|
stripeConnectIntegrationToken,
|
|
|
|
getSessionProp,
|
|
|
|
membersService.stripeConnect.getStripeConnectTokenData
|
|
|
|
);
|
2020-05-28 13:40:50 +03:00
|
|
|
}
|
|
|
|
|
2021-09-20 20:06:16 +03:00
|
|
|
return await settingsBREADService.edit(frame.data.settings, frame.options, stripeConnectData);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
upload: {
|
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'edit'
|
|
|
|
},
|
2020-09-09 15:28:12 +03:00
|
|
|
async query(frame) {
|
2021-09-23 19:31:13 +03:00
|
|
|
await routeSettings.api.setFromFilePath(frame.file.path);
|
2021-09-23 15:31:58 +03:00
|
|
|
const getRoutesHash = () => frontendSettings.getCurrentHash();
|
2020-09-09 15:28:12 +03:00
|
|
|
await settingsService.syncRoutesHash(getRoutesHash);
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
download: {
|
|
|
|
headers: {
|
|
|
|
disposition: {
|
|
|
|
type: 'yaml',
|
|
|
|
value: 'routes.yaml'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
format: 'plain'
|
|
|
|
},
|
|
|
|
permissions: {
|
|
|
|
method: 'browse'
|
|
|
|
},
|
|
|
|
query() {
|
2021-09-23 19:31:13 +03:00
|
|
|
return routeSettings.api.get();
|
2019-08-09 17:11:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|