Added EmailRecipient events to getEventTimeline

refs https://github.com/TryGhost/Team/issues/1277

- Adds 3 new requests to the `email_recipients` table in the `getEventTimeline` method
- This allows to extract new member events from the table: `email_delivered_event`, `email_opened_event`, `email_failed_event`
This commit is contained in:
Thibaut Patel 2022-01-18 15:53:51 +01:00
parent f11e1e411d
commit ae967c449c
2 changed files with 86 additions and 4 deletions

View File

@ -36,6 +36,7 @@ module.exports = function MembersAPI({
getSubject
},
models: {
EmailRecipient,
StripeCustomer,
StripeCustomerSubscription,
Member,
@ -91,11 +92,13 @@ module.exports = function MembersAPI({
});
const eventRepository = new EventRepository({
EmailRecipient,
MemberSubscribeEvent,
MemberPaidSubscriptionEvent,
MemberPaymentEvent,
MemberStatusEvent,
MemberLoginEvent
MemberLoginEvent,
labsService
});
const memberBREADService = new MemberBREADService({

View File

@ -1,16 +1,20 @@
module.exports = class EventRepository {
constructor({
EmailRecipient,
MemberSubscribeEvent,
MemberPaymentEvent,
MemberStatusEvent,
MemberLoginEvent,
MemberPaidSubscriptionEvent
MemberPaidSubscriptionEvent,
labsService
}) {
this._MemberSubscribeEvent = MemberSubscribeEvent;
this._MemberPaidSubscriptionEvent = MemberPaidSubscriptionEvent;
this._MemberPaymentEvent = MemberPaymentEvent;
this._MemberStatusEvent = MemberStatusEvent;
this._MemberLoginEvent = MemberLoginEvent;
this._EmailRecipient = EmailRecipient;
this._labsService = labsService;
}
async registerPayment(data) {
@ -106,6 +110,75 @@ module.exports = class EventRepository {
};
}
async getEmailDelieveredEvents(options = {}) {
options.filter = 'delivered_at:-null';
const {data: models, meta} = await this._EmailRecipient.findPage(
options
);
const data = models.map((data) => {
return {
type: 'email_delivered_event',
data: {
member_id: data.get('member_id'),
created_at: data.get('delivered_at'),
email_id: data.get('email_id')
}
};
});
return {
data,
meta
};
}
async getEmailOpenedEvents(options = {}) {
options.filter = 'opened_at:-null';
const {data: models, meta} = await this._EmailRecipient.findPage(
options
);
const data = models.map((data) => {
return {
type: 'email_opened_event',
data: {
member_id: data.member_id,
created_at: data.opened_at,
email_id: data.email_id
}
};
});
return {
data,
meta
};
}
async getEmailFailedEvents(options = {}) {
options.filter = 'failed_at:-null';
const {data: models, meta} = await this._EmailRecipient.findPage(
options
);
const data = models.map((data) => {
return {
type: 'email_failed_event',
data: {
member_id: data.member_id,
created_at: data.failed_at,
email_id: data.email_id
}
};
});
return {
data,
meta
};
}
async getEventTimeline(options = {}) {
if (!options.limit) {
options.limit = 10;
@ -113,12 +186,18 @@ module.exports = class EventRepository {
options.order = 'created_at desc';
const allEventPages = await Promise.all([
const pages = [
this.getNewsletterSubscriptionEvents(options),
this.getSubscriptionEvents(options),
this.getLoginEvents(options),
this.getSignupEvents(options)
]);
];
if (this._labsService.isSet('membersActivityFeed') && this._EmailRecipient) {
pages.push(this.getEmailDelieveredEvents(options));
pages.push(this.getEmailOpenedEvents(options));
pages.push(this.getEmailFailedEvents(options));
}
const allEventPages = await Promise.all(pages);
const allEvents = allEventPages.reduce((allEvents, page) => allEvents.concat(page.data), []);