diff --git a/ghost/members-api/lib/repositories/MemberRepository.js b/ghost/members-api/lib/repositories/MemberRepository.js index 8b81ba29cd..44e8c6f906 100644 --- a/ghost/members-api/lib/repositories/MemberRepository.js +++ b/ghost/members-api/lib/repositories/MemberRepository.js @@ -576,7 +576,7 @@ module.exports = class MemberRepository { .filter(newsletter => newsletter.attributes.status !== 'archived') .map(newsletter => newsletter.id); const incomingNewsletterIds = memberData.newsletters.map(newsletter => newsletter.id); - // make sure newslettersToAdd does not contain newslettersToIgnore (archived newsletters since that creates false events) + // make sure newslettersToAdd does not contain archived newsletters (since that creates false events) newslettersToAdd = _.differenceWith(_.differenceWith(incomingNewsletterIds, existingNewsletterIds), archivedNewsletters); newslettersToRemove = _.differenceWith(existingNewsletterIds, incomingNewsletterIds); } diff --git a/ghost/members-api/test/unit/lib/repositories/member.test.js b/ghost/members-api/test/unit/lib/repositories/member.test.js index 0baf70977d..9aa4fd386a 100644 --- a/ghost/members-api/test/unit/lib/repositories/member.test.js +++ b/ghost/members-api/test/unit/lib/repositories/member.test.js @@ -143,6 +143,91 @@ describe('MemberRepository', function () { }); }); + describe('newsletter subscriptions', function () { + let Member; + let MemberProductEvent; + let productRepository; + let stripeAPIService; + let existingNewsletters; + let MemberSubscribeEvent; + + beforeEach(async function () { + sinon.spy(); + existingNewsletters = [ + { + id: 'newsletter_id_123', + attributes: { + status: 'active' + }, + get: sinon.stub().withArgs('status').returns('active') + }, + { + id: 'newsletter_id_1234_archive', + attributes: { + status: 'archived' + }, + get: sinon.stub().withArgs('status').returns('archived') + } + ]; + + Member = { + findOne: sinon.stub().resolves({ + get: sinon.stub().returns('member_id_123'), + related: sinon.stub().withArgs('newsletters').returns({ + models: existingNewsletters + }), + toJSON: sinon.stub().returns({}) + }), + edit: sinon.stub().resolves({ + attributes: {}, + _previousAttributes: {} + }) + }; + + stripeAPIService = { + configured: false + }; + + MemberSubscribeEvent = { + add: sinon.stub().resolves() + }; + }); + + it('Does not create false archived newsletter events', async function () { + const repo = new MemberRepository({ + Member, + MemberProductEvent, + productRepository, + stripeAPIService, + MemberSubscribeEventModel: MemberSubscribeEvent, + OfferRedemption: mockOfferRedemption + }); + + await repo.update({ + email: 'test@email.com', + newsletters: [{ + id: 'newsletter_id_123' + }, + { + id: 'newsletter_id_456' + }, + { + id: 'newsletter_id_new' + }, + { + id: 'newsletter_id_1234_archive' + }] + },{ + transacting: { + executionPromise: Promise.resolve() + }, + context: {} + }); + + MemberSubscribeEvent.add.calledTwice.should.be.true(); + }); + }); + describe('linkSubscription', function (){ let Member; let MemberPaidSubscriptionEvent;