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.
This commit is contained in:
Fabien O'Carroll 2021-08-25 13:30:49 +02:00
parent a2120fc2d6
commit c17442cf4b
2 changed files with 40 additions and 0 deletions

View File

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

View File

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