2022-08-18 18:38:42 +03:00
|
|
|
const {MemberCreatedEvent, SubscriptionCreatedEvent} = require('@tryghost/member-events');
|
|
|
|
|
2022-09-07 17:41:59 +03:00
|
|
|
/**
|
|
|
|
* Store events in the database
|
|
|
|
*/
|
|
|
|
class EventStorage {
|
2022-08-22 12:36:24 +03:00
|
|
|
/**
|
2022-09-21 12:34:23 +03:00
|
|
|
*
|
|
|
|
* @param {Object} deps
|
2022-08-22 12:36:24 +03:00
|
|
|
* @param {Object} deps.labsService
|
|
|
|
* @param {Object} deps.models
|
|
|
|
* @param {Object} deps.models.MemberCreatedEvent
|
|
|
|
* @param {Object} deps.models.SubscriptionCreatedEvent
|
|
|
|
*/
|
2022-09-07 17:41:59 +03:00
|
|
|
constructor({labsService, models}) {
|
2022-08-22 12:36:24 +03:00
|
|
|
this.models = models;
|
2022-08-18 18:38:42 +03:00
|
|
|
this.labsService = labsService;
|
|
|
|
}
|
|
|
|
|
2022-09-07 17:41:59 +03:00
|
|
|
/**
|
|
|
|
* Subscribe to events of this domainEvents service
|
|
|
|
* @param {Object} domainEvents The DomainEvents service
|
|
|
|
*/
|
|
|
|
subscribe(domainEvents) {
|
|
|
|
domainEvents.subscribe(MemberCreatedEvent, async (event) => {
|
2022-08-18 18:38:42 +03:00
|
|
|
let attribution = event.data.attribution;
|
|
|
|
|
|
|
|
if (!this.labsService.isSet('memberAttribution')){
|
|
|
|
// Prevent storing attribution
|
|
|
|
// Can replace this later with a privacy toggle
|
|
|
|
attribution = {};
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:36:24 +03:00
|
|
|
await this.models.MemberCreatedEvent.add({
|
2022-08-18 18:38:42 +03:00
|
|
|
member_id: event.data.memberId,
|
|
|
|
created_at: event.timestamp,
|
|
|
|
attribution_id: attribution?.id ?? null,
|
|
|
|
attribution_url: attribution?.url ?? null,
|
|
|
|
attribution_type: attribution?.type ?? null,
|
2022-09-21 12:34:23 +03:00
|
|
|
source: event.data.source,
|
|
|
|
referrer_source: attribution?.refSource ?? null,
|
|
|
|
referrer_medium: attribution?.refMedium ?? null,
|
|
|
|
referrer_url: attribution?.refUrl ?? null
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-07 17:41:59 +03:00
|
|
|
domainEvents.subscribe(SubscriptionCreatedEvent, async (event) => {
|
2022-08-18 18:38:42 +03:00
|
|
|
let attribution = event.data.attribution;
|
|
|
|
|
|
|
|
if (!this.labsService.isSet('memberAttribution')){
|
|
|
|
// Prevent storing attribution
|
|
|
|
// Can replace this later with a privacy toggle
|
|
|
|
attribution = {};
|
|
|
|
}
|
|
|
|
|
2022-08-22 12:36:24 +03:00
|
|
|
await this.models.SubscriptionCreatedEvent.add({
|
2022-08-18 18:38:42 +03:00
|
|
|
member_id: event.data.memberId,
|
|
|
|
subscription_id: event.data.subscriptionId,
|
|
|
|
created_at: event.timestamp,
|
|
|
|
attribution_id: attribution?.id ?? null,
|
|
|
|
attribution_url: attribution?.url ?? null,
|
2022-09-21 12:34:23 +03:00
|
|
|
attribution_type: attribution?.type ?? null,
|
|
|
|
referrer_source: attribution?.refSource ?? null,
|
|
|
|
referrer_medium: attribution?.refMedium ?? null,
|
|
|
|
referrer_url: attribution?.refUrl ?? null
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 17:41:59 +03:00
|
|
|
module.exports = EventStorage;
|