diff --git a/core/server/data/migrations/versions/3.12/01-remove-legacy-is-paid-flag-from-settings.js b/core/server/data/migrations/versions/3.12/01-remove-legacy-is-paid-flag-from-settings.js new file mode 100644 index 0000000000..d2613ffec1 --- /dev/null +++ b/core/server/data/migrations/versions/3.12/01-remove-legacy-is-paid-flag-from-settings.js @@ -0,0 +1,93 @@ +const common = require('../../../../lib/common'); +const debug = require('ghost-ignition').debug('migrations'); + +module.exports.config = { + transaction: true +}; + +module.exports.up = (options) => { + const settingsKey = 'members_subscription_settings'; + + return options + .transacting('settings') + .where('key', settingsKey) + .select('value') + .first() + .then((subscriptionSettingsEntry) => { + debug(subscriptionSettingsEntry); + if (!subscriptionSettingsEntry) { + common.logging.warn(`Cannot find ${settingsKey} settings.`); + return; + } + + let subscriptionSettings = JSON.parse(subscriptionSettingsEntry.value); + + debug('before cleanup'); + debug(JSON.stringify(subscriptionSettings, null, 2)); + + const hasIsPaid = Object.prototype.hasOwnProperty.call(subscriptionSettings, 'isPaid'); + + if (hasIsPaid) { + debug('Removing legacy isPaid flag from members settings'); + delete subscriptionSettings.isPaid; + + debug('after cleanup'); + debug(JSON.stringify(subscriptionSettings, null, 2)); + + return options + .transacting('settings') + .where('key', settingsKey) + .update({ + value: JSON.stringify(subscriptionSettings) + }); + } + }); +}; + +module.exports.down = (options) => { + const settingsKey = 'members_subscription_settings'; + + return options + .transacting('settings') + .where('key', settingsKey) + .select('value') + .first() + .then((subscriptionSettingsEntry) => { + debug(subscriptionSettingsEntry); + if (!subscriptionSettingsEntry) { + common.logging.warn(`Cannot find ${settingsKey} settings.`); + return; + } + + let subscriptionSettings = JSON.parse(subscriptionSettingsEntry.value); + + debug('before cleanup'); + debug(JSON.stringify(subscriptionSettings, null, 2)); + + let isPaid = false; + + const stripePaymentProcessor = subscriptionSettings.paymentProcessors.find( + paymentProcessor => paymentProcessor.adapter === 'stripe' + ); + + if (stripePaymentProcessor && stripePaymentProcessor.config.public_token && stripePaymentProcessor.config.secret_token) { + isPaid = true; + } + + subscriptionSettings.isPaid = isPaid; + + debug('after cleanup'); + debug(JSON.stringify(subscriptionSettings, null, 2)); + + return options + .transacting('settings') + .where('key', settingsKey) + .update({ + value: JSON.stringify(subscriptionSettings) + }); + }); +}; + +module.exports.config = { + transaction: true +}; diff --git a/core/server/data/schema/default-settings.json b/core/server/data/schema/default-settings.json index 7981fec1a0..6153a67901 100644 --- a/core/server/data/schema/default-settings.json +++ b/core/server/data/schema/default-settings.json @@ -207,7 +207,7 @@ "defaultValue": "public" }, "members_subscription_settings": { - "defaultValue": "{\"isPaid\":false,\"fromAddress\":\"noreply\",\"allowSelfSignup\":true,\"paymentProcessors\":[{\"adapter\":\"stripe\",\"config\":{\"secret_token\":\"\",\"public_token\":\"\",\"product\":{\"name\":\"Ghost Subscription\"},\"plans\":[{\"name\":\"Monthly\",\"currency\":\"usd\",\"interval\":\"month\",\"amount\":\"\"},{\"name\":\"Yearly\",\"currency\":\"usd\",\"interval\":\"year\",\"amount\":\"\"}]}}]}" + "defaultValue": "{\"fromAddress\":\"noreply\",\"allowSelfSignup\":true,\"paymentProcessors\":[{\"adapter\":\"stripe\",\"config\":{\"secret_token\":\"\",\"public_token\":\"\",\"product\":{\"name\":\"Ghost Subscription\"},\"plans\":[{\"name\":\"Monthly\",\"currency\":\"usd\",\"interval\":\"month\",\"amount\":\"\"},{\"name\":\"Yearly\",\"currency\":\"usd\",\"interval\":\"year\",\"amount\":\"\"}]}}]}" } }, "bulk_email": {