mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-19 00:11:49 +03:00
f4fdb4fa6c
fixes https://github.com/TryGhost/Team/issues/2310 This moves the processing of the events from the event-processor to a new email-event-processor in the email-service package. - The `EmailEventProcessor` only translates events from providerId/emailId to their known emailId, memberId and recipientId, and dispatches the corresponding events. - Since `EmailEventProcessor` runs in a separate worker thread, we can't listen for the dispatched events on the main thread. To accomplish this communication, the events dispatched from the `EmailEventProcessor` class are 'posted' via the postMessage method and redispatched on the main thread. - A new `EmailEventStorage` class reacts to the email events and stores it in the database. This code mostly corresponds to the (now deleted) subclass of the old `EmailEventProcessor` - Updating a members last_seen_at timestamp has moved to the lastSeenAtUpdater. - Email events no longer store `ObjectID` because these are not encodable across threads via postMessage - Includes new E2E tests that test the storage of all supported Mailgun events. Note that in these tests we run the processing on the main thread instead of on a separate thread (couldn't do this because stubbing is not possible across threads) There are some missing pieces that will get added in later PRs (this PR focuses on porting the existing functionality): - Handling temporary failures/bounces - Capturing the error messages of bounce events
173 lines
5.2 KiB
JavaScript
173 lines
5.2 KiB
JavaScript
// Switch these lines once there are useful utils
|
|
// const testUtils = require('./utils');
|
|
require('./utils');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const {
|
|
EmailAnalyticsService
|
|
} = require('..');
|
|
const EventProcessingResult = require('../lib/event-processing-result');
|
|
|
|
describe('EmailAnalyticsService', function () {
|
|
let eventProcessor;
|
|
beforeEach(function () {
|
|
eventProcessor = {};
|
|
eventProcessor.handleDelivered = sinon.stub().callsFake(({emailId}) => {
|
|
return {
|
|
emailId,
|
|
emailRecipientId: emailId,
|
|
memberId: 1
|
|
};
|
|
});
|
|
eventProcessor.handleOpened = sinon.stub().callsFake(({emailId}) => {
|
|
return {
|
|
emailId,
|
|
emailRecipientId: emailId,
|
|
memberId: 1
|
|
};
|
|
});
|
|
});
|
|
|
|
describe('fetchAll', function () {
|
|
let providers;
|
|
let queries;
|
|
|
|
beforeEach(function () {
|
|
providers = {
|
|
testing: {
|
|
async fetchAll(batchHandler) {
|
|
const result = new EventProcessingResult();
|
|
|
|
// first page
|
|
result.merge(await batchHandler([{
|
|
type: 'delivered',
|
|
emailId: 1,
|
|
memberId: 1
|
|
}, {
|
|
type: 'delivered',
|
|
emailId: 1,
|
|
memberId: 1
|
|
}]));
|
|
|
|
// second page
|
|
result.merge(await batchHandler([{
|
|
type: 'opened',
|
|
emailId: 1,
|
|
memberId: 1
|
|
}, {
|
|
type: 'opened',
|
|
emailId: 1,
|
|
memberId: 1
|
|
}]));
|
|
|
|
return result;
|
|
}
|
|
}
|
|
};
|
|
|
|
queries = {
|
|
shouldFetchStats: sinon.fake.resolves(true)
|
|
};
|
|
});
|
|
|
|
it('uses passed-in providers', async function () {
|
|
const service = new EmailAnalyticsService({
|
|
queries,
|
|
eventProcessor,
|
|
providers
|
|
});
|
|
|
|
const result = await service.fetchAll();
|
|
|
|
queries.shouldFetchStats.calledOnce.should.be.true();
|
|
eventProcessor.handleDelivered.calledTwice.should.be.true();
|
|
|
|
result.should.deepEqual(new EventProcessingResult({
|
|
delivered: 2,
|
|
opened: 2,
|
|
emailIds: [1],
|
|
memberIds: [1]
|
|
}));
|
|
});
|
|
|
|
it('skips if queries.shouldFetchStats is falsy', async function () {
|
|
queries.shouldFetchStats = sinon.fake.resolves(false);
|
|
|
|
const service = new EmailAnalyticsService({
|
|
queries,
|
|
eventProcessor,
|
|
providers
|
|
});
|
|
|
|
const result = await service.fetchAll();
|
|
|
|
queries.shouldFetchStats.calledOnce.should.be.true();
|
|
eventProcessor.handleDelivered.called.should.be.false();
|
|
|
|
result.should.deepEqual(new EventProcessingResult());
|
|
});
|
|
});
|
|
|
|
describe('fetchLatest', function () {
|
|
|
|
});
|
|
|
|
describe('processEventBatch', function () {
|
|
it('uses passed-in event processor', async function () {
|
|
const service = new EmailAnalyticsService({
|
|
eventProcessor
|
|
});
|
|
|
|
const result = await service.processEventBatch([{
|
|
type: 'delivered',
|
|
emailId: 1
|
|
}, {
|
|
type: 'delivered',
|
|
emailId: 2
|
|
}, {
|
|
type: 'opened',
|
|
emailId: 1
|
|
}]);
|
|
|
|
eventProcessor.handleDelivered.callCount.should.eql(2);
|
|
|
|
result.should.deepEqual(new EventProcessingResult({
|
|
delivered: 2,
|
|
opened: 1,
|
|
unprocessable: 0,
|
|
emailIds: [1, 2],
|
|
memberIds: [1]
|
|
}));
|
|
});
|
|
});
|
|
|
|
describe('aggregateStats', function () {
|
|
let service;
|
|
|
|
beforeEach(function () {
|
|
service = new EmailAnalyticsService({
|
|
queries: {
|
|
aggregateEmailStats: sinon.spy(),
|
|
aggregateMemberStats: sinon.spy()
|
|
}
|
|
});
|
|
});
|
|
|
|
it('calls appropriate query for each email id and member id', async function () {
|
|
await service.aggregateStats({
|
|
emailIds: ['e-1', 'e-2'],
|
|
memberIds: ['m-1', 'm-2']
|
|
});
|
|
|
|
service.queries.aggregateEmailStats.calledTwice.should.be.true();
|
|
service.queries.aggregateEmailStats.calledWith('e-1').should.be.true();
|
|
service.queries.aggregateEmailStats.calledWith('e-2').should.be.true();
|
|
|
|
service.queries.aggregateMemberStats.calledTwice.should.be.true();
|
|
service.queries.aggregateMemberStats.calledWith('m-1').should.be.true();
|
|
service.queries.aggregateMemberStats.calledWith('m-2').should.be.true();
|
|
});
|
|
});
|
|
});
|