Swapped member limit verification trigger event

closes https://github.com/TryGhost/Toolbox/issues/399

- The MemberCreatedEvent event is more accurate representation of the limit nature - counting the number of members created. The previous MemberSubscribeEvent was slightly hacky solution because a member could be subscribed/unsubscribed multiple times and distorting the limit counts.
This commit is contained in:
Naz 2022-10-06 13:50:27 +08:00 committed by naz
parent 0370dd258d
commit 5e9d1d3178
3 changed files with 23 additions and 16 deletions

View File

@ -267,6 +267,10 @@ module.exports = class EventRepository {
};
}
return this.getCreatedEvents(options, filters);
}
async getCreatedEvents(options = {}, filters = {}) {
options = {
...options,
withRelated: ['member', 'postAttribution', 'userAttribution', 'tagAttribution'],
@ -278,6 +282,9 @@ module.exports = class EventRepository {
if (filters['data.member_id']) {
options.filter.push(filters['data.member_id'].replace(/data.member_id:/g, 'member_id:'));
}
if (filters['data.source']) {
options.filter.push(filters['data.source'].replace(/data.source:/g, 'source:'));
}
options.filter = options.filter.join('+');
const {data: models, meta} = await this._MemberCreatedEvent.findPage(options);

View File

@ -1,6 +1,6 @@
const errors = require('@tryghost/errors');
const DomainEvents = require('@tryghost/domain-events');
const {MemberSubscribeEvent} = require('@tryghost/member-events');
const {MemberCreatedEvent} = require('@tryghost/member-events');
const messages = {
emailVerificationNeeded: `We're hard at work processing your import. To make sure you get great deliverability, we'll need to enable some extra features for your account. A member of our team will be in touch with you by email to review your account make sure everything is configured correctly so you're ready to go.`,
@ -45,15 +45,15 @@ class VerificationTrigger {
this._Settings = Settings;
this._eventRepository = eventRepository;
this._handleMemberSubscribeEvent = this._handleMemberSubscribeEvent.bind(this);
DomainEvents.subscribe(MemberSubscribeEvent, this._handleMemberSubscribeEvent);
this._handleMemberCreatedEvent = this._handleMemberCreatedEvent.bind(this);
DomainEvents.subscribe(MemberCreatedEvent, this._handleMemberCreatedEvent);
}
/**
*
* @param {MemberSubscribeEvent} event
* @param {MemberCreatedEvent} event
*/
async _handleMemberSubscribeEvent(event) {
async _handleMemberCreatedEvent(event) {
const source = event.data?.source;
let sourceThreshold;
@ -66,7 +66,7 @@ class VerificationTrigger {
if (['api', 'admin'].includes(source) && isFinite(sourceThreshold)) {
const createdAt = new Date();
createdAt.setDate(createdAt.getDate() - 30);
const events = await this._eventRepository.getNewsletterSubscriptionEvents({}, {
const events = await this._eventRepository.getCreatedEvents({}, {
'data.source': `data.source:'${source}'`,
'data.created_at': `data.created_at:>'${createdAt.toISOString().replace('T', ' ').substring(0, 19)}'`
});
@ -99,7 +99,7 @@ class VerificationTrigger {
const createdAt = new Date();
createdAt.setDate(createdAt.getDate() - 30);
const events = await this._eventRepository.getNewsletterSubscriptionEvents({}, {
const events = await this._eventRepository.getCreatedEvents({}, {
'data.source': `data.source:'import'`,
'data.created_at': `data.created_at:>'${createdAt.toISOString().replace('T', ' ').substring(0, 19)}'`
});

View File

@ -4,7 +4,7 @@ const sinon = require('sinon');
require('./utils');
const VerificationTrigger = require('../index');
const DomainEvents = require('@tryghost/domain-events');
const {MemberSubscribeEvent} = require('@tryghost/member-events');
const {MemberCreatedEvent} = require('@tryghost/member-events');
describe('Import threshold', function () {
it('Creates a threshold based on config', async function () {
@ -175,11 +175,11 @@ describe('Email verification flow', function () {
isVerificationRequired: () => false,
sendVerificationEmail: emailStub,
eventRepository: {
getNewsletterSubscriptionEvents: eventStub
getCreatedEvents: eventStub
}
});
DomainEvents.dispatch(MemberSubscribeEvent.create({
DomainEvents.dispatch(MemberCreatedEvent.create({
memberId: 'hello!',
source: 'api'
}, new Date()));
@ -214,7 +214,7 @@ describe('Email verification flow', function () {
isVerificationRequired: () => false,
sendVerificationEmail: emailStub,
eventRepository: {
getNewsletterSubscriptionEvents: eventStub
getCreatedEvents: eventStub
}
});
@ -257,11 +257,11 @@ describe('Email verification flow', function () {
isVerificationRequired: () => false,
sendVerificationEmail: emailStub,
eventRepository: {
getNewsletterSubscriptionEvents: eventStub
getCreatedEvents: eventStub
}
});
await trigger._handleMemberSubscribeEvent({
await trigger._handleMemberCreatedEvent({
data: {
source: 'admin'
}
@ -305,11 +305,11 @@ describe('Email verification flow', function () {
isVerificationRequired: () => false,
sendVerificationEmail: emailStub,
eventRepository: {
getNewsletterSubscriptionEvents: eventStub
getCreatedEvents: eventStub
}
});
await trigger._handleMemberSubscribeEvent({
await trigger._handleMemberCreatedEvent({
data: {
source: 'api'
}
@ -352,7 +352,7 @@ describe('Email verification flow', function () {
isVerificationRequired: () => false,
sendVerificationEmail: emailStub,
eventRepository: {
getNewsletterSubscriptionEvents: eventStub
getCreatedEvents: eventStub
}
});