From ddc1a58c842c5c4cad507cf247b2b2d00137aa65 Mon Sep 17 00:00:00 2001 From: Ronald Langeveld Date: Thu, 1 Feb 2024 16:11:01 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixed=20members=20events=20for?= =?UTF-8?q?=20archived=20newsletters=20(#19638)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes https://linear.app/tryghost/issue/ENG-604/🐛-members-events-show-member-subscribed-to-archived-newsletter - This fixes a bug where it doesn't take archived newsletters into account and would create an Event for subscribing back to those newsletters even though its not the case, which causes some confusion for publishers in Member Events and wastes rows in the DB. --- ghost/members-api/lib/repositories/MemberRepository.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ghost/members-api/lib/repositories/MemberRepository.js b/ghost/members-api/lib/repositories/MemberRepository.js index 4f03192d21..8b81ba29cd 100644 --- a/ghost/members-api/lib/repositories/MemberRepository.js +++ b/ghost/members-api/lib/repositories/MemberRepository.js @@ -559,7 +559,6 @@ module.exports = class MemberRepository { if (needsNewsletters) { const existingNewsletters = initialMember.related('newsletters').models; - // This maps the old subscribed property to the new newsletters field and is only used to keep backward compatibility if (!memberData.newsletters) { if (memberData.subscribed === false) { @@ -572,12 +571,13 @@ module.exports = class MemberRepository { // only ever populated with active newsletters - never archived ones if (memberData.newsletters) { + const archivedNewsletters = existingNewsletters.filter(n => n.get('status') === 'archived').map(n => n.id); const existingNewsletterIds = existingNewsletters .filter(newsletter => newsletter.attributes.status !== 'archived') .map(newsletter => newsletter.id); const incomingNewsletterIds = memberData.newsletters.map(newsletter => newsletter.id); - - newslettersToAdd = _.differenceWith(incomingNewsletterIds, existingNewsletterIds); + // make sure newslettersToAdd does not contain newslettersToIgnore (archived newsletters since that creates false events) + newslettersToAdd = _.differenceWith(_.differenceWith(incomingNewsletterIds, existingNewsletterIds), archivedNewsletters); newslettersToRemove = _.differenceWith(existingNewsletterIds, incomingNewsletterIds); }