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
This commit is contained in:
Naz 2023-04-25 15:47:29 +02:00
parent 235b39ab7a
commit e541ea8746
No known key found for this signature in database
4 changed files with 31 additions and 11 deletions

View File

@ -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;
}
}

View File

@ -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;

View File

@ -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({

View File

@ -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) {