From 8d7184191815418856607b94c11f75c8fc09b9b2 Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Wed, 26 Jul 2023 11:17:59 +0200 Subject: [PATCH] Mapped tag.deleted event to TagDeletedEvent refs https://github.com/TryGhost/Arch/issues/61 Because the tags system is still written in the old way, the tag.deleted bookshelf event needs to be mapped to the DomainEvents to bridge the gap with the collections package. --- .../src/ModelToDomainEventInterceptor.ts | 8 +++-- .../model-to-domain-event-interceptor.test.ts | 31 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/ghost/model-to-domain-event-interceptor/src/ModelToDomainEventInterceptor.ts b/ghost/model-to-domain-event-interceptor/src/ModelToDomainEventInterceptor.ts index 7e62306754..c8b9e5618a 100644 --- a/ghost/model-to-domain-event-interceptor/src/ModelToDomainEventInterceptor.ts +++ b/ghost/model-to-domain-event-interceptor/src/ModelToDomainEventInterceptor.ts @@ -1,4 +1,4 @@ -import { PostDeletedEvent, PostAddedEvent, PostEditedEvent } from '@tryghost/collections'; +import { PostDeletedEvent, PostAddedEvent, PostEditedEvent, TagDeletedEvent } from '@tryghost/collections'; type ModelToDomainEventInterceptorDeps = { ModelEvents: { @@ -25,7 +25,8 @@ export class ModelToDomainEventInterceptor { 'post.deleted', 'post.edited', // NOTE: currently unmapped and unused event - 'tag.added' + 'tag.added', + 'tag.deleted' ]; for (const modelEventName of ghostModelUpdateEvents) { @@ -83,6 +84,9 @@ export class ModelToDomainEventInterceptor { } }); break; + case 'tag.deleted': + event = TagDeletedEvent.create({id: data.id, slug: data.attributes.slug}); + break; default: } diff --git a/ghost/model-to-domain-event-interceptor/test/model-to-domain-event-interceptor.test.ts b/ghost/model-to-domain-event-interceptor/test/model-to-domain-event-interceptor.test.ts index f64db882eb..088929fcee 100644 --- a/ghost/model-to-domain-event-interceptor/test/model-to-domain-event-interceptor.test.ts +++ b/ghost/model-to-domain-event-interceptor/test/model-to-domain-event-interceptor.test.ts @@ -5,7 +5,8 @@ import DomainEvents from '@tryghost/domain-events'; import { PostDeletedEvent, PostEditedEvent, - PostAddedEvent + PostAddedEvent, + TagDeletedEvent } from '@tryghost/collections'; import {ModelToDomainEventInterceptor} from '../src'; @@ -149,6 +150,34 @@ describe('ModelToDomainEventInterceptor', function () { assert.ok(interceptedEvent); }); + it('Intercepts tag.deleted Model event and dispatches TagDeletedEvent Domain event', async function () { + let eventRegistry = new EventRegistry(); + const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({ + ModelEvents: eventRegistry, + DomainEvents: DomainEvents + }); + + modelToDomainEventInterceptor.init(); + + let interceptedEvent; + DomainEvents.subscribe(TagDeletedEvent, (event: TagDeletedEvent) => { + assert.equal(event.id, '1234-deleted'); + assert.equal(event.data.slug, 'tag-slug'); + interceptedEvent = event; + }); + + eventRegistry.emit('tag.deleted', { + id: '1234-deleted', + attributes: { + slug: 'tag-slug' + } + }); + + await DomainEvents.allSettled(); + + assert.ok(interceptedEvent); + }); + it('Intercepts unmapped Model event and dispatches nothing', async function () { let eventRegistry = new EventRegistry(); const modelToDomainEventInterceptor = new ModelToDomainEventInterceptor({