2021-09-21 12:25:45 +03:00
|
|
|
const DomainEvents = require('@tryghost/domain-events');
|
|
|
|
const {MemberEntryViewEvent} = require('@tryghost/member-events');
|
|
|
|
|
|
|
|
const AnalyticEvent = require('./AnalyticEvent');
|
|
|
|
|
2021-09-21 14:33:38 +03:00
|
|
|
class EventHandler {
|
2021-09-21 12:25:45 +03:00
|
|
|
/**
|
|
|
|
* @param {import('./AnalyticEventRepository')} repository
|
|
|
|
*/
|
|
|
|
constructor(repository) {
|
|
|
|
/** @private */
|
|
|
|
this.repository = repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listens for member events and handles creating analytic events and storing them.
|
|
|
|
*/
|
|
|
|
setupSubscribers() {
|
|
|
|
DomainEvents.subscribe(MemberEntryViewEvent, async (ev) => {
|
|
|
|
const event = AnalyticEvent.create({
|
|
|
|
name: 'entry_view',
|
|
|
|
memberId: ev.data.memberId,
|
|
|
|
memberStatus: ev.data.memberStatus,
|
|
|
|
entryId: ev.data.entryId,
|
|
|
|
sourceUrl: ev.data.entryUrl,
|
|
|
|
timestamp: ev.timestamp
|
|
|
|
});
|
|
|
|
|
|
|
|
await this.repository.save(event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 14:33:38 +03:00
|
|
|
module.exports = EventHandler;
|