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;
|
|
|
|
|
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,
|
2022-09-27 22:28:06 +03:00
|
|
|
referrer_source: attribution?.referrerSource ?? null,
|
|
|
|
referrer_medium: attribution?.referrerMedium ?? null,
|
|
|
|
referrer_url: attribution?.referrerUrl ?? 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;
|
|
|
|
|
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,
|
2022-09-27 22:28:06 +03:00
|
|
|
referrer_source: attribution?.referrerSource ?? null,
|
|
|
|
referrer_medium: attribution?.referrerMedium ?? null,
|
|
|
|
referrer_url: attribution?.referrerUrl ?? null
|
2022-08-18 18:38:42 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 17:41:59 +03:00
|
|
|
module.exports = EventStorage;
|