Added bulk posts action events dispatching

refs https://github.com/TryGhost/Arch/issues/16

- Allows to subscribe to bulk unpublish/featured/unfeatured DomainEvents elsewhere in the system, for example, Collections.
This commit is contained in:
Naz 2023-07-26 14:02:03 +08:00 committed by naz
parent f4143a8939
commit 18a4fa8cd9
6 changed files with 89 additions and 2 deletions

View File

@ -0,0 +1,13 @@
export class PostsBulkFeaturedEvent {
data: string[];
timestamp: Date;
constructor(data: string[], timestamp: Date) {
this.data = data;
this.timestamp = timestamp;
}
static create(data: string[], timestamp = new Date()) {
return new PostsBulkFeaturedEvent(data, timestamp);
}
}

View File

@ -0,0 +1,13 @@
export class PostsBulkUnfeaturedEvent {
data: string[];
timestamp: Date;
constructor(data: string[], timestamp: Date) {
this.data = data;
this.timestamp = timestamp;
}
static create(data: string[], timestamp = new Date()) {
return new PostsBulkUnfeaturedEvent(data, timestamp);
}
}

View File

@ -0,0 +1,13 @@
export class PostsBulkUnpublishedEvent {
data: string[];
timestamp: Date;
constructor(data: string[], timestamp: Date) {
this.data = data;
this.timestamp = timestamp;
}
static create(data: string[], timestamp = new Date()) {
return new PostsBulkUnpublishedEvent(data, timestamp);
}
}

View File

@ -1 +1,4 @@
export * from './PostsBulkDestroyedEvent';
export * from './PostsBulkUnpublishedEvent';
export * from './PostsBulkFeaturedEvent';
export * from './PostsBulkUnfeaturedEvent';

View File

@ -1,5 +1,10 @@
import assert from 'assert/strict';
import {PostsBulkDestroyedEvent} from '../src/index';
import {
PostsBulkDestroyedEvent,
PostsBulkUnpublishedEvent,
PostsBulkFeaturedEvent,
PostsBulkUnfeaturedEvent
} from '../src/index';
describe('Post Events', function () {
it('Can instantiate BulkDestroyEvent', function () {
@ -7,4 +12,22 @@ describe('Post Events', function () {
assert.ok(event);
assert.equal(event.data.length, 3);
});
it('Can instantiate PostsBulkUnpublishedEvent', function () {
const event = PostsBulkUnpublishedEvent.create(['1', '2', '3']);
assert.ok(event);
assert.equal(event.data.length, 3);
});
it('Can instantiate PostsBulkFeaturedEvent', function () {
const event = PostsBulkFeaturedEvent.create(['1', '2', '3']);
assert.ok(event);
assert.equal(event.data.length, 3);
});
it('Can instantiate PostsBulkUnfeaturedEvent', function () {
const event = PostsBulkUnfeaturedEvent.create(['1', '2', '3']);
assert.ok(event);
assert.equal(event.data.length, 3);
});
});

View File

@ -5,7 +5,12 @@ const errors = require('@tryghost/errors');
const ObjectId = require('bson-objectid').default;
const pick = require('lodash/pick');
const DomainEvents = require('@tryghost/domain-events/lib/DomainEvents');
const {PostsBulkDestroyedEvent} = require('@tryghost/post-events');
const {
PostsBulkDestroyedEvent,
PostsBulkUnpublishedEvent,
PostsBulkFeaturedEvent,
PostsBulkUnfeaturedEvent
} = require('@tryghost/post-events');
const messages = {
invalidVisibilityFilter: 'Invalid visibility filter.',
@ -458,6 +463,23 @@ class PostsService {
});
}
if (options.actionName) {
let bulkActionEvent;
switch (options.actionName) {
case 'unpublished':
bulkActionEvent = PostsBulkUnpublishedEvent.create(editIds);
break;
case 'featured':
bulkActionEvent = PostsBulkFeaturedEvent.create(editIds);
break;
case 'unfeatured':
bulkActionEvent = PostsBulkUnfeaturedEvent.create(editIds);
break;
}
DomainEvents.dispatch(bulkActionEvent);
}
return result;
}