From 127c61d4c633b41b9c00b85172d9ee90f47e9fcd Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Fri, 1 Sep 2023 12:44:45 +0700 Subject: [PATCH] Moved OfferRepository to core As per our architecture guidelines we want to keep bookshelf implementations of Repositories in Ghost core, so that all the bookshelf code is kept together, and the packages implementing business logic with entities and services require less dependencies to test. This separation should also help us inadvertently add business logic to repository implementations by having a more "physical" boundary between them. --- .../services/offers/OfferBookshelfRepository.js} | 8 ++++---- ghost/core/core/server/services/offers/service.js | 10 +++++++--- ghost/offers/lib/OffersModule.js | 13 ++++--------- 3 files changed, 15 insertions(+), 16 deletions(-) rename ghost/{offers/lib/application/OfferRepository.js => core/core/server/services/offers/OfferBookshelfRepository.js} (96%) diff --git a/ghost/offers/lib/application/OfferRepository.js b/ghost/core/core/server/services/offers/OfferBookshelfRepository.js similarity index 96% rename from ghost/offers/lib/application/OfferRepository.js rename to ghost/core/core/server/services/offers/OfferBookshelfRepository.js index 69ed24e7db..0c104c68f5 100644 --- a/ghost/offers/lib/application/OfferRepository.js +++ b/ghost/core/core/server/services/offers/OfferBookshelfRepository.js @@ -1,7 +1,7 @@ const {flowRight} = require('lodash'); const {mapKeyValues, mapQuery} = require('@tryghost/mongo-utils'); const DomainEvents = require('@tryghost/domain-events'); -const Offer = require('../domain/models/Offer'); +const {Offer} = require('@tryghost/members-offers'); const statusTransformer = mapKeyValues({ key: { @@ -40,7 +40,7 @@ const mongoTransformer = flowRight(statusTransformer, rejectNonStatusTransformer * @prop {string} filter */ -class OfferRepository { +class OfferBookshelfRepository { /** * @param {{forge: (data: object) => import('bookshelf').Model}} OfferModel * @param {{forge: (data: object) => import('bookshelf').Model}} OfferRedemptionModel @@ -177,7 +177,7 @@ class OfferRepository { } /** - * @param {Offer} offer + * @param {import('@tryghost/members-offers').Offer} offer * @param {BaseOptions} [options] * @returns {Promise} */ @@ -219,4 +219,4 @@ class OfferRepository { } } -module.exports = OfferRepository; +module.exports = OfferBookshelfRepository; diff --git a/ghost/core/core/server/services/offers/service.js b/ghost/core/core/server/services/offers/service.js index 7f51761138..0ff7a5746b 100644 --- a/ghost/core/core/server/services/offers/service.js +++ b/ghost/core/core/server/services/offers/service.js @@ -4,6 +4,7 @@ const OffersModule = require('@tryghost/members-offers'); const config = require('../../../shared/config'); const urlUtils = require('../../../shared/url-utils'); const models = require('../../models'); +const OfferBookshelfRepository = require('./OfferBookshelfRepository'); let redirectManager; @@ -15,10 +16,13 @@ module.exports = { return urlUtils.urlJoin(urlUtils.getSubdir(), pathname); } }); + const repository = new OfferBookshelfRepository( + models.Offer, + models.OfferRedemption + ); const offersModule = OffersModule.create({ - OfferModel: models.Offer, - OfferRedemptionModel: models.OfferRedemption, - redirectManager + redirectManager, + repository }); this.api = offersModule.api; diff --git a/ghost/offers/lib/OffersModule.js b/ghost/offers/lib/OffersModule.js index a21cfa51b4..ace06f865f 100644 --- a/ghost/offers/lib/OffersModule.js +++ b/ghost/offers/lib/OffersModule.js @@ -5,14 +5,13 @@ const DomainEvents = require('@tryghost/domain-events'); const OfferCodeChangeEvent = require('./domain/events/OfferCodeChangeEvent'); const OfferCreatedEvent = require('./domain/events/OfferCreatedEvent'); const Offer = require('./domain/models/Offer'); -const OfferRepository = require('./application/OfferRepository'); const OffersAPI = require('./application/OffersAPI'); class OffersModule { /** * @param {OffersAPI} offersAPI * @param {import('@tryghost/express-dynamic-redirects')} redirectManager - * @param {OfferRepository} repository + * @param {any} repository */ constructor(offersAPI, redirectManager, repository) { this.api = offersAPI; @@ -57,15 +56,13 @@ class OffersModule { /** * @param {object} deps * @param {import('@tryghost/express-dynamic-redirects')} deps.redirectManager - * @param {any} deps.OfferModel - * @param {any} deps.OfferRedemptionModel + * @param {any} deps.repository * * @returns {OffersModule} */ static create(deps) { - const repository = new OfferRepository(deps.OfferModel, deps.OfferRedemptionModel); - const offersAPI = new OffersAPI(repository); - return new OffersModule(offersAPI, deps.redirectManager, repository); + const offersAPI = new OffersAPI(deps.repository); + return new OffersModule(offersAPI, deps.redirectManager, deps.repository); } static events = { @@ -73,8 +70,6 @@ class OffersModule { OfferCodeChangeEvent }; - static OfferRepository = OfferRepository; - static Offer = Offer; static OffersAPI = OffersAPI;