diff --git a/ghost/email-service/lib/email-event-storage.js b/ghost/email-service/lib/email-event-storage.js index 0cb4fbbc06..749b4edeb1 100644 --- a/ghost/email-service/lib/email-event-storage.js +++ b/ghost/email-service/lib/email-event-storage.js @@ -129,7 +129,11 @@ class EmailEventStorage { } async unsubscribeFromNewsletters(event) { - await this.#membersRepository.update({newsletters: []}, {id: event.memberId}); + try { + await this.#membersRepository.update({newsletters: []}, {id: event.memberId}); + } catch (err) { + logging.error(err); + } } } diff --git a/ghost/email-service/test/email-event-storage.test.js b/ghost/email-service/test/email-event-storage.test.js index 08af50657a..a116372fbd 100644 --- a/ghost/email-service/test/email-event-storage.test.js +++ b/ghost/email-service/test/email-event-storage.test.js @@ -466,6 +466,27 @@ describe('Email Event Storage', function () { assert(update.firstCall.args[0].newsletters.length === 0); }); + it('Handles unsubscribe with a non-existent member', async function () { + const event = EmailUnsubscribedEvent.create({ + email: 'example@example.com', + memberId: '123', + emailId: '456', + timestamp: new Date(0) + }); + + const error = new Error('Member not found'); + const update = sinon.stub().throws(error); + + const eventHandler = new EmailEventStorage({ + membersRepository: { + update + } + }); + await eventHandler.handleUnsubscribed(event); + assert(update.calledOnce); + assert(update.firstCall.args[0].newsletters.length === 0); + }); + it('Handles complaints', async function () { const event = SpamComplaintEvent.create({ email: 'example@example.com',