Ghost/ghost/member-analytics-service/lib/EventHandler.js
Fabien O'Carroll ed6d305fb2 Renamed components of member-analytic-service
no-issue

This removes the concept of "Module" which simplifies the code
2021-09-21 13:33:38 +02:00

35 lines
997 B
JavaScript

const DomainEvents = require('@tryghost/domain-events');
const {MemberEntryViewEvent} = require('@tryghost/member-events');
const AnalyticEvent = require('./AnalyticEvent');
class EventHandler {
/**
* @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);
});
}
}
module.exports = EventHandler;