From c17442cf4b6dba1d8bc8fe997a492b3acfad4dc7 Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Wed, 25 Aug 2021 13:30:49 +0200 Subject: [PATCH] Added memberService to members-api no-issue The idea of this service is to sit infront of the repository and handle application logic which does not belong at the data layer. The exact naming and structure is TBC but this gives us a place to start pulling logic out of the controllers, without having to mash it all into the repository. Also important to note is that is does not return instances of bookshelf models, but a JSON representation of the model, this allows us to not leak internal implementation to consumers. --- ghost/members-api/lib/MembersAPI.js | 6 ++++ .../members-api/lib/services/member-bread.js | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 ghost/members-api/lib/services/member-bread.js diff --git a/ghost/members-api/lib/MembersAPI.js b/ghost/members-api/lib/MembersAPI.js index 18ced47ec3..43149c0e70 100644 --- a/ghost/members-api/lib/MembersAPI.js +++ b/ghost/members-api/lib/MembersAPI.js @@ -7,6 +7,7 @@ const StripeAPIService = require('./services/stripe-api'); const StripeWebhookService = require('./services/stripe-webhook'); const TokenService = require('./services/token'); const GeolocationSerice = require('./services/geolocation'); +const MemberBREADService = require('./services/member-bread'); const MemberRepository = require('./repositories/member'); const EventRepository = require('./repositories/event'); const ProductRepository = require('./repositories/product'); @@ -108,6 +109,10 @@ module.exports = function MembersAPI({ MemberLoginEvent }); + const memberBREADService = new MemberBREADService({ + memberRepository + }); + const stripeWebhookService = new StripeWebhookService({ StripeWebhook, stripeAPIService, @@ -399,6 +404,7 @@ module.exports = function MembersAPI({ getMagicLink, hasActiveStripeSubscriptions, members: users, + memberBREADService, events: eventRepository, productRepository }; diff --git a/ghost/members-api/lib/services/member-bread.js b/ghost/members-api/lib/services/member-bread.js new file mode 100644 index 0000000000..eb5efd970d --- /dev/null +++ b/ghost/members-api/lib/services/member-bread.js @@ -0,0 +1,34 @@ +module.exports = class MemberBREADService { + /** + * @param {object} deps + * @param {import('../repositories/member')} deps.memberRepository + */ + constructor({memberRepository}) { + this._memberRepository = memberRepository; + } + + async read(data, options = {}) { + const defaultWithRelated = [ + 'labels', + 'stripeSubscriptions', + 'stripeSubscriptions.customer', + 'stripeSubscriptions.stripePrice', + 'stripeSubscriptions.stripePrice.stripeProduct' + ]; + + const withRelated = new Set((options.withRelated || []).concat(defaultWithRelated)); + + if (withRelated.has('email_recipients')) { + withRelated.add('email_recipients.email'); + } + + const model = await this._memberRepository.get(data, { + ...options, + withRelated: Array.from(withRelated) + }); + + const member = model.toJSON(options); + + return member; + } +};