From ce80d250bf8b264341d5805d93c4b02e1a688e5e Mon Sep 17 00:00:00 2001 From: Rishabh Date: Tue, 9 Aug 2022 08:40:57 +0530 Subject: [PATCH] Handled null trial days on tiers refs https://github.com/TryGhost/Ghost/commit/e26c977c6653fb870b4e29b5e6058375fbe45f21 - handles null trial days in admin and API, sets trial days as 0 for null values --- ghost/admin/app/models/tier.js | 2 +- .../endpoints/utils/serializers/input/tiers.js | 2 +- ghost/core/test/e2e-api/admin/tiers.test.js | 16 ++++++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/ghost/admin/app/models/tier.js b/ghost/admin/app/models/tier.js index 2b47052e60..b62246eec8 100644 --- a/ghost/admin/app/models/tier.js +++ b/ghost/admin/app/models/tier.js @@ -14,6 +14,6 @@ export default Model.extend(ValidationEngine, { currency: attr('string'), monthlyPrice: attr('number'), yearlyPrice: attr('number'), - trialDays: attr('number'), + trialDays: attr('number', {defaultValue: 0}), benefits: attr('tier-benefits') }); diff --git a/ghost/core/core/server/api/endpoints/utils/serializers/input/tiers.js b/ghost/core/core/server/api/endpoints/utils/serializers/input/tiers.js index 8fee3393ce..e005addd34 100644 --- a/ghost/core/core/server/api/endpoints/utils/serializers/input/tiers.js +++ b/ghost/core/core/server/api/endpoints/utils/serializers/input/tiers.js @@ -24,7 +24,7 @@ function convertTierInput(input) { }; if (labs.isSet('freeTrial')) { - converted.trial_days = input.trial_days; + converted.trial_days = input.trial_days || 0; } if (input.monthly_price && input.currency) { diff --git a/ghost/core/test/e2e-api/admin/tiers.test.js b/ghost/core/test/e2e-api/admin/tiers.test.js index 8a9bd2ef1c..f1a97d36ca 100644 --- a/ghost/core/test/e2e-api/admin/tiers.test.js +++ b/ghost/core/test/e2e-api/admin/tiers.test.js @@ -110,4 +110,20 @@ describe('Tiers API', function () { assert(updatedTier.visibility === visibility, `The visibility of the Tier should have been updated to ${visibility}`); }); + + it('Can save with trial_days as null', async function () { + const {body: {tiers: [tier]}} = await agent.get('/tiers/?limit=1'); + + await agent.put(`/tiers/${tier.id}/`) + .body({ + tiers: [{ + trial_days: null + }] + }) + .expectStatus(200); + + const {body: {tiers: [updatedTier]}} = await agent.get(`/tiers/${tier.id}/`); + + assert(updatedTier.trial_days === 0, `The trial_days should have been set to 0`); + }); });