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; + } +};