From b6234d6e967558334557115b53dcb47b095c90c9 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Fri, 22 Oct 2021 13:41:09 +0200 Subject: [PATCH] Allowed OfferTitle to be empty refs https://github.com/TryGhost/Team/issues/1163 This allows users to not provide a title for an Offer. We store the lack of a title as `NULL` in the DB, but we will always provide a string to the API so that the title can safely be used in HTML. --- ghost/offers/lib/application/OfferRepository.js | 4 ++-- ghost/offers/lib/domain/models/OfferDescription.js | 2 +- ghost/offers/lib/domain/models/OfferTitle.js | 7 +++++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/ghost/offers/lib/application/OfferRepository.js b/ghost/offers/lib/application/OfferRepository.js index 4c782dcc0c..7784d73b6a 100644 --- a/ghost/offers/lib/application/OfferRepository.js +++ b/ghost/offers/lib/application/OfferRepository.js @@ -168,8 +168,8 @@ class OfferRepository { id: offer.id, name: offer.name.value, code: offer.code.value, - portal_title: offer.displayTitle.value, - portal_description: offer.displayDescription.value, + portal_title: offer.displayTitle.value || null, + portal_description: offer.displayDescription.value || null, discount_type: offer.type.value === 'fixed' ? 'amount' : 'percent', discount_amount: offer.amount.value, interval: offer.cadence.value, diff --git a/ghost/offers/lib/domain/models/OfferDescription.js b/ghost/offers/lib/domain/models/OfferDescription.js index c656a98390..64a3959314 100644 --- a/ghost/offers/lib/domain/models/OfferDescription.js +++ b/ghost/offers/lib/domain/models/OfferDescription.js @@ -21,7 +21,7 @@ class OfferDescription extends ValueObject { }); } - return new OfferDescription(description); + return new OfferDescription(description.trim()); } } diff --git a/ghost/offers/lib/domain/models/OfferTitle.js b/ghost/offers/lib/domain/models/OfferTitle.js index 870233b381..0676b362bc 100644 --- a/ghost/offers/lib/domain/models/OfferTitle.js +++ b/ghost/offers/lib/domain/models/OfferTitle.js @@ -5,7 +5,10 @@ const InvalidOfferTitle = require('../errors').InvalidOfferTitle; class OfferTitle extends ValueObject { /** @param {unknown} title */ static create(title) { - if (!title || typeof title !== 'string') { + if (title === null || title === undefined) { + return new OfferTitle(''); + } + if (typeof title !== 'string') { throw new InvalidOfferTitle({ message: 'Offer `display_title` must be a string.' }); @@ -17,7 +20,7 @@ class OfferTitle extends ValueObject { }); } - return new OfferTitle(title); + return new OfferTitle(title.trim()); } }