Ghost/ghost/members-events-service/lib/event-storage.js
Simon Backx 076e3c02b2
Added linking between member and subscription created events (#15693)
fixes https://github.com/TryGhost/Team/issues/2160

- Adds a `batch_id` to both events that contain the same ID if they were created at the same time.
- Removes duplicate signup/conversion events using the batch_id
- Requires an update in mongo-knex to work (refs https://ghost.slack.com/archives/C02G9E68C/p1666773313272409?thread_ts=1666767872.375009&cid=C02G9E68C)
- Some dependencies needed an update to load the latest mongo-knex
- Added tiers to membersUtils, loaded on startup (we can start to use this instead of fetching it every time)
2022-10-27 11:44:19 +02:00

62 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;
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,
referrer_source: attribution?.referrerSource ?? null,
referrer_medium: attribution?.referrerMedium ?? null,
referrer_url: attribution?.referrerUrl ?? null,
batch_id: event.data.batchId ?? null
});
});
domainEvents.subscribe(SubscriptionCreatedEvent, async (event) => {
let attribution = event.data.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,
referrer_source: attribution?.referrerSource ?? null,
referrer_medium: attribution?.referrerMedium ?? null,
referrer_url: attribution?.referrerUrl ?? null,
batch_id: event.data.batchId ?? null
});
});
}
}
module.exports = EventStorage;