mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-29 07:09:48 +03:00
74ecde73db
fixes https://github.com/TryGhost/Team/issues/1821 This change moves all the event storage logic to one new place: the event storage class in the MembersEventsService, which is initialised in a new members events service wrapper. Apart from this, this includes some improvements: - Removed DomainEvents from the constructor arguments to the subscribe method (to make it more clear where to subscribe to and decrease dependencies) - LastSeenAtUpdater doesn't subscribe in the constructor any longer (removes unclear side effect) - Moved LastSeenAtUpdater initialisation to new members events service wrapper - Added missing tests to LastSeenAtUpdater to assure that the MembersEventsService package has 100% coverage.
66 lines
2.2 KiB
JavaScript
66 lines
2.2 KiB
JavaScript
const {MemberCreatedEvent, SubscriptionCreatedEvent} = require('@tryghost/member-events');
|
|
|
|
/**
|
|
* Store events in the database
|
|
*/
|
|
class EventStorage {
|
|
/**
|
|
*
|
|
* @param {Object} deps
|
|
* @param {Object} deps.labsService
|
|
* @param {Object} deps.models
|
|
* @param {Object} deps.models.MemberCreatedEvent
|
|
* @param {Object} deps.models.SubscriptionCreatedEvent
|
|
*/
|
|
constructor({labsService, models}) {
|
|
this.models = models;
|
|
this.labsService = labsService;
|
|
}
|
|
|
|
/**
|
|
* Subscribe to events of this domainEvents service
|
|
* @param {Object} domainEvents The DomainEvents service
|
|
*/
|
|
subscribe(domainEvents) {
|
|
domainEvents.subscribe(MemberCreatedEvent, async (event) => {
|
|
let attribution = event.data.attribution;
|
|
|
|
if (!this.labsService.isSet('memberAttribution')){
|
|
// Prevent storing attribution
|
|
// Can replace this later with a privacy toggle
|
|
attribution = {};
|
|
}
|
|
|
|
await this.models.MemberCreatedEvent.add({
|
|
member_id: event.data.memberId,
|
|
created_at: event.timestamp,
|
|
attribution_id: attribution?.id ?? null,
|
|
attribution_url: attribution?.url ?? null,
|
|
attribution_type: attribution?.type ?? null,
|
|
source: event.data.source
|
|
});
|
|
});
|
|
|
|
domainEvents.subscribe(SubscriptionCreatedEvent, async (event) => {
|
|
let attribution = event.data.attribution;
|
|
|
|
if (!this.labsService.isSet('memberAttribution')){
|
|
// Prevent storing attribution
|
|
// Can replace this later with a privacy toggle
|
|
attribution = {};
|
|
}
|
|
|
|
await this.models.SubscriptionCreatedEvent.add({
|
|
member_id: event.data.memberId,
|
|
subscription_id: event.data.subscriptionId,
|
|
created_at: event.timestamp,
|
|
attribution_id: attribution?.id ?? null,
|
|
attribution_url: attribution?.url ?? null,
|
|
attribution_type: attribution?.type ?? null
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = EventStorage;
|