2022-01-24 20:53:10 +03:00
|
|
|
const should = require('should');
|
|
|
|
const EventRepository = require('../../../../lib/repositories/event');
|
2022-01-25 14:20:34 +03:00
|
|
|
const sinon = require('sinon');
|
2022-01-24 20:53:10 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
|
|
|
|
describe('EventRepository', function () {
|
|
|
|
describe('getNQLSubset', function () {
|
|
|
|
let eventRepository;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
eventRepository = new EventRepository({
|
|
|
|
EmailRecipient: null,
|
|
|
|
MemberSubscribeEvent: null,
|
|
|
|
MemberPaymentEvent: null,
|
|
|
|
MemberStatusEvent: null,
|
|
|
|
MemberLoginEvent: null,
|
|
|
|
MemberPaidSubscriptionEvent: null,
|
|
|
|
labsService: null
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when using properties that aren\'t in the allowlist', function () {
|
|
|
|
should.throws(() => {
|
|
|
|
eventRepository.getNQLSubset('(types:1)');
|
|
|
|
}, errors.IncorrectUsageError);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('throws when using an OR', function () {
|
|
|
|
should.throws(() => {
|
|
|
|
eventRepository.getNQLSubset('type:1,data.created_at:1');
|
|
|
|
}, errors.IncorrectUsageError);
|
|
|
|
|
|
|
|
should.throws(() => {
|
|
|
|
eventRepository.getNQLSubset('type:1+data.created_at:1,data.member_id:1');
|
|
|
|
}, errors.IncorrectUsageError);
|
|
|
|
|
|
|
|
should.throws(() => {
|
|
|
|
eventRepository.getNQLSubset('type:1,data.created_at:1+data.member_id:1');
|
|
|
|
}, errors.IncorrectUsageError);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes when using it correctly with one filter', function () {
|
|
|
|
const res = eventRepository.getNQLSubset('type:email_delivered_event');
|
2022-10-27 13:13:24 +03:00
|
|
|
res.should.be.an.Array();
|
|
|
|
res.should.have.lengthOf(2);
|
|
|
|
|
|
|
|
res[0].should.eql({
|
|
|
|
type: 'email_delivered_event'
|
2022-01-24 20:53:10 +03:00
|
|
|
});
|
2022-10-27 13:13:24 +03:00
|
|
|
should(res[1]).be.undefined();
|
2022-01-24 20:53:10 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('passes when using it correctly with multiple filters', function () {
|
|
|
|
const res = eventRepository.getNQLSubset('type:-[email_delivered_event,email_opened_event,email_failed_event]+data.created_at:<0+data.member_id:123');
|
2022-10-27 13:13:24 +03:00
|
|
|
res.should.be.an.Array();
|
|
|
|
res.should.have.lengthOf(2);
|
|
|
|
|
|
|
|
res[0].should.eql({
|
|
|
|
type: {
|
|
|
|
$nin: [
|
|
|
|
'email_delivered_event',
|
|
|
|
'email_opened_event',
|
|
|
|
'email_failed_event'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
res[1].should.eql({
|
|
|
|
$and: [{
|
|
|
|
'data.created_at': {
|
|
|
|
$lt: 0
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'data.member_id': 123
|
|
|
|
}]
|
2022-01-24 20:53:10 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('passes when using it correctly with multiple filters used several times', function () {
|
|
|
|
const res = eventRepository.getNQLSubset('type:-email_delivered_event+data.created_at:<0+data.member_id:123+type:-[email_opened_event,email_failed_event]+data.created_at:>10');
|
2022-10-27 13:13:24 +03:00
|
|
|
res.should.be.an.Array();
|
|
|
|
res.should.have.lengthOf(2);
|
|
|
|
res[0].should.eql({
|
|
|
|
$and: [{
|
|
|
|
type: {
|
|
|
|
$ne: 'email_delivered_event'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
type: {
|
|
|
|
$nin: [
|
|
|
|
'email_opened_event',
|
|
|
|
'email_failed_event'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
res[1].should.eql({
|
|
|
|
$and: [{
|
|
|
|
'data.created_at': {
|
|
|
|
$lt: 0
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'data.member_id': 123
|
|
|
|
}, {
|
|
|
|
'data.created_at': {
|
|
|
|
$gt: 10
|
|
|
|
}
|
|
|
|
}]
|
2022-01-24 20:53:10 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-01-25 14:20:34 +03:00
|
|
|
|
|
|
|
describe('getNewsletterSubscriptionEvents', function () {
|
|
|
|
let eventRepository;
|
|
|
|
let fake;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
fake = sinon.fake.returns({data: [{toJSON: () => {}}]});
|
|
|
|
eventRepository = new EventRepository({
|
|
|
|
EmailRecipient: null,
|
|
|
|
MemberSubscribeEvent: {
|
|
|
|
findPage: fake
|
|
|
|
},
|
|
|
|
MemberPaymentEvent: null,
|
|
|
|
MemberStatusEvent: null,
|
|
|
|
MemberLoginEvent: null,
|
|
|
|
MemberPaidSubscriptionEvent: null,
|
|
|
|
labsService: null
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
fake.resetHistory();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works when setting no filters', async function () {
|
|
|
|
await eventRepository.getNewsletterSubscriptionEvents({
|
|
|
|
filter: 'no used'
|
|
|
|
}, {
|
|
|
|
type: 'unused'
|
|
|
|
});
|
2022-10-27 13:13:24 +03:00
|
|
|
sinon.assert.calledOnce(fake);
|
|
|
|
fake.getCall(0).firstArg.should.match({
|
2022-04-26 15:21:50 +03:00
|
|
|
withRelated: ['member', 'newsletter'],
|
2022-10-27 13:13:24 +03:00
|
|
|
filter: 'custom:true'
|
|
|
|
});
|
2022-01-25 14:20:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('works when setting a created_at filter', async function () {
|
|
|
|
await eventRepository.getNewsletterSubscriptionEvents({}, {
|
|
|
|
'data.created_at': 'data.created_at:123'
|
|
|
|
});
|
2022-10-27 13:13:24 +03:00
|
|
|
|
|
|
|
sinon.assert.calledOnce(fake);
|
|
|
|
fake.getCall(0).firstArg.should.match({
|
2022-04-26 15:21:50 +03:00
|
|
|
withRelated: ['member', 'newsletter'],
|
2022-10-27 13:13:24 +03:00
|
|
|
filter: 'custom:true'
|
|
|
|
});
|
2022-01-25 14:20:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('works when setting a combination of filters', async function () {
|
|
|
|
await eventRepository.getNewsletterSubscriptionEvents({}, {
|
|
|
|
'data.created_at': 'data.created_at:123+data.created_at:<99999',
|
|
|
|
'data.member_id': 'data.member_id:-[3,4,5]+data.member_id:-[1,2,3]'
|
|
|
|
});
|
2022-10-27 13:13:24 +03:00
|
|
|
|
|
|
|
sinon.assert.calledOnce(fake);
|
|
|
|
fake.getCall(0).firstArg.should.match({
|
2022-04-26 15:21:50 +03:00
|
|
|
withRelated: ['member', 'newsletter'],
|
2022-10-27 13:13:24 +03:00
|
|
|
filter: 'custom:true'
|
|
|
|
});
|
2022-01-25 14:20:34 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getEmailFailedEvents', function () {
|
|
|
|
let eventRepository;
|
|
|
|
let fake;
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
fake = sinon.fake.returns({data: [{get: () => {}, related: () => ({toJSON: () => {}})}]});
|
|
|
|
eventRepository = new EventRepository({
|
|
|
|
EmailRecipient: {
|
|
|
|
findPage: fake
|
|
|
|
},
|
|
|
|
MemberSubscribeEvent: null,
|
|
|
|
MemberPaymentEvent: null,
|
|
|
|
MemberStatusEvent: null,
|
|
|
|
MemberLoginEvent: null,
|
|
|
|
MemberPaidSubscriptionEvent: null,
|
|
|
|
labsService: null
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
fake.resetHistory();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works when setting no filters', async function () {
|
|
|
|
await eventRepository.getEmailFailedEvents({
|
2022-02-01 17:47:15 +03:00
|
|
|
filter: 'no used',
|
2022-10-27 13:13:24 +03:00
|
|
|
order: 'created_at desc, id desc'
|
2022-01-25 14:20:34 +03:00
|
|
|
}, {
|
|
|
|
type: 'unused'
|
|
|
|
});
|
2022-10-27 13:13:24 +03:00
|
|
|
|
|
|
|
fake.calledOnce.should.be.eql(true);
|
|
|
|
fake.getCall(0).firstArg.should.match({
|
2022-01-25 14:20:34 +03:00
|
|
|
withRelated: ['member', 'email'],
|
2022-10-27 13:13:24 +03:00
|
|
|
filter: 'failed_at:-null+custom:true',
|
|
|
|
order: 'failed_at desc, id desc'
|
|
|
|
});
|
2022-01-25 14:20:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('works when setting a created_at filter', async function () {
|
2022-02-01 17:47:15 +03:00
|
|
|
await eventRepository.getEmailDeliveredEvents({
|
2022-10-27 13:13:24 +03:00
|
|
|
order: 'created_at desc, id desc'
|
2022-02-01 17:47:15 +03:00
|
|
|
}, {
|
2022-01-25 14:20:34 +03:00
|
|
|
'data.created_at': 'data.created_at:123'
|
|
|
|
});
|
2022-10-27 13:13:24 +03:00
|
|
|
|
|
|
|
fake.calledOnce.should.be.eql(true);
|
|
|
|
fake.getCall(0).firstArg.should.match({
|
2022-01-25 14:20:34 +03:00
|
|
|
withRelated: ['member', 'email'],
|
2022-10-27 13:13:24 +03:00
|
|
|
filter: 'delivered_at:-null+custom:true',
|
|
|
|
order: 'delivered_at desc, id desc'
|
|
|
|
});
|
2022-01-25 14:20:34 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('works when setting a combination of filters', async function () {
|
2022-02-01 17:47:15 +03:00
|
|
|
await eventRepository.getEmailOpenedEvents({
|
2022-10-27 13:13:24 +03:00
|
|
|
order: 'created_at desc, id desc'
|
2022-02-01 17:47:15 +03:00
|
|
|
}, {
|
2022-01-25 14:20:34 +03:00
|
|
|
'data.created_at': 'data.created_at:123+data.created_at:<99999',
|
|
|
|
'data.member_id': 'data.member_id:-[3,4,5]+data.member_id:-[1,2,3]'
|
|
|
|
});
|
2022-10-27 13:13:24 +03:00
|
|
|
|
|
|
|
fake.calledOnce.should.be.eql(true);
|
|
|
|
fake.getCall(0).firstArg.should.match({
|
2022-01-25 14:20:34 +03:00
|
|
|
withRelated: ['member', 'email'],
|
2022-10-27 13:13:24 +03:00
|
|
|
filter: 'opened_at:-null+custom:true',
|
|
|
|
order: 'opened_at desc, id desc'
|
|
|
|
});
|
2022-01-25 14:20:34 +03:00
|
|
|
});
|
|
|
|
});
|
2022-01-24 20:53:10 +03:00
|
|
|
});
|