2022-11-23 13:33:44 +03:00
|
|
|
const tpl = require('@tryghost/tpl');
|
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
|
|
|
|
const messages = {
|
|
|
|
noneFilterError: 'Cannot send email to "none" recipient filter',
|
|
|
|
newsletterVisibilityError: 'Unexpected visibility value "{value}". Use one of the valid: "members", "paid".'
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} MembersRepository
|
|
|
|
* @prop {(options) => Promise<any>} list
|
|
|
|
*/
|
|
|
|
|
|
|
|
class EmailSegmenter {
|
|
|
|
#membersRepository;
|
|
|
|
|
|
|
|
/**
|
2022-12-01 22:03:28 +03:00
|
|
|
*
|
|
|
|
* @param {object} dependencies
|
2022-11-23 13:33:44 +03:00
|
|
|
* @param {MembersRepository} dependencies.membersRepository
|
|
|
|
*/
|
|
|
|
constructor({
|
|
|
|
membersRepository
|
|
|
|
}) {
|
|
|
|
this.#membersRepository = membersRepository;
|
|
|
|
}
|
|
|
|
|
|
|
|
getMemberFilterForSegment(newsletter, emailRecipientFilter, segment) {
|
2023-07-24 12:47:57 +03:00
|
|
|
const filter = [`newsletters.id:${newsletter.id}`, 'email_disabled:0'];
|
2022-12-01 22:03:28 +03:00
|
|
|
|
2022-11-23 13:33:44 +03:00
|
|
|
switch (emailRecipientFilter) {
|
|
|
|
case 'all':
|
|
|
|
break;
|
|
|
|
case 'none':
|
|
|
|
throw new errors.InternalServerError({
|
|
|
|
message: tpl(messages.noneFilterError)
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
filter.push(`(${emailRecipientFilter})`);
|
|
|
|
break;
|
|
|
|
}
|
2022-12-01 22:03:28 +03:00
|
|
|
|
2022-11-23 13:33:44 +03:00
|
|
|
const visibility = newsletter.get('visibility');
|
|
|
|
switch (visibility) {
|
|
|
|
case 'members':
|
|
|
|
// No need to add a member status filter as the email is available to all members
|
|
|
|
break;
|
|
|
|
case 'paid':
|
|
|
|
filter.push(`status:-free`);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new errors.InternalServerError({
|
|
|
|
message: tpl(messages.newsletterVisibilityError, {
|
|
|
|
value: visibility
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (segment) {
|
|
|
|
filter.push(`(${segment})`);
|
|
|
|
}
|
2022-12-01 22:03:28 +03:00
|
|
|
|
2022-11-23 13:33:44 +03:00
|
|
|
return filter.join('+');
|
|
|
|
}
|
2022-12-01 22:03:28 +03:00
|
|
|
|
2022-11-23 13:33:44 +03:00
|
|
|
async getMembersCount(newsletter, emailRecipientFilter, segment) {
|
|
|
|
const filter = this.getMemberFilterForSegment(newsletter, emailRecipientFilter, segment);
|
|
|
|
const {meta: {pagination: {total: membersCount}}} = await this.#membersRepository.list({filter});
|
|
|
|
|
|
|
|
return membersCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EmailSegmenter;
|