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.
This commit is contained in:
Fabien "egg" O'Carroll 2023-07-26 11:17:59 +02:00 committed by Fabien 'egg' O'Carroll
parent 90f92ab2a4
commit 8d71841918
2 changed files with 36 additions and 3 deletions

View File

@ -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:
}

View File

@ -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({