From c6f2c985f1561b976a2779da7bbbb99110ede1ba Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Fri, 22 Sep 2023 15:16:56 +0700 Subject: [PATCH] Wired up events to CollectionsBookshelfRepository refs https://github.com/TryGhost/Arch/issues/95 This handles actually dispatching the events --- .../collections/BookshelfCollectionsRepository.js | 11 ++++++++++- .../core/core/server/services/collections/service.js | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ghost/core/core/server/services/collections/BookshelfCollectionsRepository.js b/ghost/core/core/server/services/collections/BookshelfCollectionsRepository.js index 855226d756..09054cf95d 100644 --- a/ghost/core/core/server/services/collections/BookshelfCollectionsRepository.js +++ b/ghost/core/core/server/services/collections/BookshelfCollectionsRepository.js @@ -12,9 +12,12 @@ const {default: ObjectID} = require('bson-objectid'); module.exports = class BookshelfCollectionsRepository { #model; #relationModel; - constructor(model, relationModel) { + /** @type {import('@tryghost/domain-events')} */ + #DomainEvents; + constructor(model, relationModel, DomainEvents) { this.#model = model; this.#relationModel = relationModel; + this.#DomainEvents = DomainEvents; } async createTransaction(cb) { @@ -241,6 +244,12 @@ module.exports = class BookshelfCollectionsRepository { if (collectionPostRelationsToDeleteIds.length > 0) { await this.#relationModel.query().delete().whereIn('id', collectionPostRelationsToDeleteIds).transacting(options.transaction); } + + options.transaction.executionPromise.then(() => { + for (const event of collection.events) { + this.#DomainEvents.dispatch(event); + } + }); } } }; diff --git a/ghost/core/core/server/services/collections/service.js b/ghost/core/core/server/services/collections/service.js index 3bdc9faf72..86f017b869 100644 --- a/ghost/core/core/server/services/collections/service.js +++ b/ghost/core/core/server/services/collections/service.js @@ -12,7 +12,7 @@ class CollectionsServiceWrapper { const DomainEvents = require('@tryghost/domain-events'); const postsRepository = require('./PostsRepository').getInstance(); const models = require('../../models'); - const collectionsRepositoryInMemory = new BookshelfCollectionsRepository(models.Collection, models.CollectionPost); + const collectionsRepositoryInMemory = new BookshelfCollectionsRepository(models.Collection, models.CollectionPost, DomainEvents); const collectionsService = new CollectionsService({ collectionsRepository: collectionsRepositoryInMemory,