🐛 Fixed members events for archived newsletters (#19638)

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.
This commit is contained in:
Ronald Langeveld 2024-02-01 16:11:01 +02:00 committed by GitHub
parent 1843963e19
commit ddc1a58c84
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

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