mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-30 21:40:39 +03:00
eeb7546abb
refs https://www.notion.so/ghost/Marketing-Milestone-email-campaigns-1d2c9dee3cfa4029863edb16092ad5c4?pvs=4 - When milestones will be activated we would send out emails to users that are way above the achieved milestone, as we didn't record milestones before - The plan is to implement a 0 milestone and don't send an email for achieving those and also add all achieved milestones in the first run until a first milestone is stored in the DB, then increment from there. - This change takes care of two cases: 1. Milestones gets enabled and runs initially. We don't want to send emails unless there's already at least one milestone achieved. For that we add a 0 milestone helper and add a `initial` reason to the meta object for the milestone event, so we can choose not to ping Slack and also disable email sending for all milestones achieved in this initial run. 2. All achieved milestones will be stored in the DB, even when that means we skip some. This introduces the `skipped` reason which also doesn't send emails for the skipped milestones, but will do for correctly achieved milestones (always the highest one). - Added handling for slack notifications to not attempt sending when reason is `skipped` or `initial`
120 lines
3.5 KiB
JavaScript
120 lines
3.5 KiB
JavaScript
/**
|
|
* @typedef {import('./Milestone')} Milestone
|
|
* @typedef {import('./MilestonesService').IMilestoneRepository} IMilestoneRepository
|
|
*/
|
|
|
|
/**
|
|
* @implements {IMilestoneRepository}
|
|
*/
|
|
module.exports = class InMemoryMilestoneRepository {
|
|
/** @type {Milestone[]} */
|
|
#store = [];
|
|
|
|
/** @type {Object.<string, true>} */
|
|
#ids = {};
|
|
|
|
/** @type {import('@tryghost/domain-events')} */
|
|
#DomainEvents;
|
|
|
|
/**
|
|
* @param {object} deps
|
|
* @param {import('@tryghost/domain-events')} deps.DomainEvents
|
|
*/
|
|
constructor(deps) {
|
|
this.#DomainEvents = deps.DomainEvents;
|
|
}
|
|
|
|
/**
|
|
* @param {Milestone} milestone
|
|
*
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async save(milestone) {
|
|
if (this.#ids[milestone.id.toHexString()]) {
|
|
const existingIndex = this.#store.findIndex((item) => {
|
|
return item.id.equals(milestone.id);
|
|
});
|
|
this.#store.splice(existingIndex, 1, milestone);
|
|
} else {
|
|
this.#store.push(milestone);
|
|
this.#ids[milestone.id.toHexString()] = true;
|
|
}
|
|
|
|
for (const event of milestone.events) {
|
|
this.#DomainEvents.dispatch(event);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {'arr'|'members'} type
|
|
* @param {string} [currency]
|
|
*
|
|
* @returns {Promise<Milestone>}
|
|
*/
|
|
async getLatestByType(type, currency = 'usd') {
|
|
const allMilestonesForType = await this.getAllByType(type, currency);
|
|
return allMilestonesForType?.[0];
|
|
}
|
|
|
|
/**
|
|
* @returns {Promise<Milestone>}
|
|
*/
|
|
async getLastEmailSent() {
|
|
return this.#store
|
|
.filter(item => item.emailSentAt)
|
|
// sort by emailSentAt desc
|
|
.sort((a, b) => (b.emailSentAt.valueOf() - a.emailSentAt.valueOf()))
|
|
// if we end up with more values with the same datetime, pick the highest value
|
|
.sort((a, b) => b.value - a.value)[0];
|
|
}
|
|
|
|
/**
|
|
* @param {number} value
|
|
* @param {string} [currency]
|
|
*
|
|
* @returns {Promise<Milestone>}
|
|
*/
|
|
async getByARR(value, currency = 'usd') {
|
|
// find a milestone of the ARR type by a given value
|
|
return this.#store.find((item) => {
|
|
return item.value === value && item.type === 'arr' && item.currency === currency;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param {number} value
|
|
*
|
|
* @returns {Promise<Milestone>}
|
|
*/
|
|
async getByCount(value) {
|
|
// find a milestone of the members type by a given value
|
|
return this.#store.find((item) => {
|
|
return item.value === value && item.type === 'members';
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @param {'arr'|'members'} type
|
|
* @param {string} [currency]
|
|
*
|
|
* @returns {Promise<Milestone[]>}
|
|
*/
|
|
async getAllByType(type, currency = 'usd') {
|
|
if (type === 'arr') {
|
|
return this.#store
|
|
.filter(item => item.type === type && item.currency === currency)
|
|
// sort by created at desc
|
|
.sort((a, b) => (b.createdAt.valueOf() - a.createdAt.valueOf()))
|
|
// sort by highest value
|
|
.sort((a, b) => b.value - a.value);
|
|
} else {
|
|
return this.#store
|
|
.filter(item => item.type === type)
|
|
// sort by created at desc
|
|
.sort((a, b) => (b.createdAt.valueOf() - a.createdAt.valueOf()))
|
|
// sort by highest value
|
|
.sort((a, b) => b.value - a.value);
|
|
}
|
|
}
|
|
};
|