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.
This commit is contained in:
Fabien "egg" O'Carroll 2023-09-01 12:44:45 +07:00 committed by Fabien 'egg' O'Carroll
parent b64be18e74
commit 127c61d4c6
3 changed files with 15 additions and 16 deletions

View File

@ -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<Offer.OfferProps>}} OfferModel
* @param {{forge: (data: object) => import('bookshelf').Model<any>}} OfferRedemptionModel
@ -177,7 +177,7 @@ class OfferRepository {
}
/**
* @param {Offer} offer
* @param {import('@tryghost/members-offers').Offer} offer
* @param {BaseOptions} [options]
* @returns {Promise<void>}
*/
@ -219,4 +219,4 @@ class OfferRepository {
}
}
module.exports = OfferRepository;
module.exports = OfferBookshelfRepository;

View File

@ -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;

View File

@ -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;