From e541ea87465f12a392a357d2891ab0d4825b0628 Mon Sep 17 00:00:00 2001 From: Naz Date: Tue, 25 Apr 2023 15:47:29 +0200 Subject: [PATCH] Extracted announcement visibility values to single place refs https://github.com/TryGhost/Team/issues/3010 - Having all possible values in within single packages will make it easier to look for "source of truth" and is more maintainable rather than having values scattered all over the codebase --- .../lib/AnnouncementBarSettings.js | 14 +++++++------- .../lib/AnnouncementVisibilityValues.js | 11 +++++++++++ .../test/AnnouncementBarSettings.test.js | 9 +++++++++ .../endpoints/utils/validators/input/settings.js | 8 ++++---- 4 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 ghost/announcement-bar-settings/lib/AnnouncementVisibilityValues.js diff --git a/ghost/announcement-bar-settings/lib/AnnouncementBarSettings.js b/ghost/announcement-bar-settings/lib/AnnouncementBarSettings.js index 5e22cf143f..d7088a68b2 100644 --- a/ghost/announcement-bar-settings/lib/AnnouncementBarSettings.js +++ b/ghost/announcement-bar-settings/lib/AnnouncementBarSettings.js @@ -1,6 +1,10 @@ +const AnnouncementVisibilityValues = require('./AnnouncementVisibilityValues'); + class AnnouncementBarSettings { #getAnnouncementSettings; + static VisibilityValues = AnnouncementVisibilityValues; + /** * * @param {Object} deps @@ -25,18 +29,14 @@ class AnnouncementBarSettings { const visibilities = announcementSettings.announcement_visibility; const announcementContent = announcementSettings.announcement; - // Available visibilities: - // 'visitors', // Logged out visitors - // 'free_members', // Free members - // 'paid_members' // Paid members (aka non-free members) if (visibilities.length === 0) { announcement = undefined; } else { - if (visibilities.includes('visitors') && !member) { + if (visibilities.includes(AnnouncementVisibilityValues.VISITORS) && !member) { announcement = announcementContent; - } else if (visibilities.includes('free_members') && (member?.status === 'free')) { + } else if (visibilities.includes(AnnouncementVisibilityValues.FREE_MEMBERS) && (member?.status === 'free')) { announcement = announcementContent; - } else if (visibilities.includes('paid_members') && (member && member.status !== 'free')) { + } else if (visibilities.includes(AnnouncementVisibilityValues.PAID_MEMBERS) && (member && member.status !== 'free')) { announcement = announcementContent; } } diff --git a/ghost/announcement-bar-settings/lib/AnnouncementVisibilityValues.js b/ghost/announcement-bar-settings/lib/AnnouncementVisibilityValues.js new file mode 100644 index 0000000000..b2d34701f6 --- /dev/null +++ b/ghost/announcement-bar-settings/lib/AnnouncementVisibilityValues.js @@ -0,0 +1,11 @@ +// Available visibilities: +// 'visitors' - logged out visitors +// 'free_members' - free members +// 'paid_members' - paid members (aka non-free members) +class AnnouncementVisibilityValues { + static VISITORS = 'visitors'; + static FREE_MEMBERS = 'free_members'; + static PAID_MEMBERS = 'paid_members'; +} + +module.exports = AnnouncementVisibilityValues; diff --git a/ghost/announcement-bar-settings/test/AnnouncementBarSettings.test.js b/ghost/announcement-bar-settings/test/AnnouncementBarSettings.test.js index b7b38a6201..4d12e47dea 100644 --- a/ghost/announcement-bar-settings/test/AnnouncementBarSettings.test.js +++ b/ghost/announcement-bar-settings/test/AnnouncementBarSettings.test.js @@ -14,6 +14,15 @@ describe('AnnouncementBarSettings', function () { assert.ok(announcementBarSettings); }); + describe('AnnouncementVisibilityValues', function () { + it('has static VisibilityValues property', function () { + assert.ok(AnnouncementBarSettings.VisibilityValues); + assert.equal(AnnouncementBarSettings.VisibilityValues.VISITORS, 'visitors'); + assert.equal(AnnouncementBarSettings.VisibilityValues.FREE_MEMBERS, 'free_members'); + assert.equal(AnnouncementBarSettings.VisibilityValues.PAID_MEMBERS, 'paid_members'); + }); + }); + describe('getAnnouncementSettings', function () { const testVisibility = (announcementSettings, member, expected) => { const announcementBarSettings = new AnnouncementBarSettings({ diff --git a/ghost/core/core/server/api/endpoints/utils/validators/input/settings.js b/ghost/core/core/server/api/endpoints/utils/validators/input/settings.js index 150414a1c9..a604891fba 100644 --- a/ghost/core/core/server/api/endpoints/utils/validators/input/settings.js +++ b/ghost/core/core/server/api/endpoints/utils/validators/input/settings.js @@ -3,6 +3,7 @@ const _ = require('lodash'); const {ValidationError} = require('@tryghost/errors'); const validator = require('@tryghost/validator'); const tpl = require('@tryghost/tpl'); +const AnnouncementBarSettings = require('@tryghost/announcement-bar-settings'); const messages = { invalidEmailReceived: 'Please send a valid email', @@ -68,11 +69,10 @@ module.exports = { // NOTE: safe to parse because of array validation up top const visibilityValues = JSON.parse(setting.value); - // NOTE: combination of 'free_members' & 'paid_members' makes just a 'members' filter const validVisibilityValues = [ - 'visitors', // Logged out visitors - 'free_members', // Free members - 'paid_members' // Paid members + AnnouncementBarSettings.VisibilityValues.VISITORS, + AnnouncementBarSettings.VisibilityValues.FREE_MEMBERS, + AnnouncementBarSettings.VisibilityValues.PAID_MEMBERS ]; if (visibilityValues.length) {