2023-02-13 15:07:53 +03:00
|
|
|
const {MemberCreatedEvent, SubscriptionCancelledEvent, SubscriptionActivatedEvent} = require('@tryghost/member-events');
|
2023-01-25 16:10:29 +03:00
|
|
|
const {MentionCreatedEvent} = require('@tryghost/webmentions');
|
2023-02-23 12:20:13 +03:00
|
|
|
const {MilestoneCreatedEvent} = require('@tryghost/milestones');
|
2022-09-09 17:23:43 +03:00
|
|
|
|
2022-11-22 07:25:11 +03:00
|
|
|
// @NOTE: 'StaffService' is a vague name that does not describe what it's actually doing.
|
|
|
|
// Possibly, "StaffNotificationService" or "StaffEventNotificationService" would be a more accurate name
|
2022-08-25 10:25:36 +03:00
|
|
|
class StaffService {
|
2023-01-25 16:10:29 +03:00
|
|
|
constructor({logging, models, mailer, settingsCache, settingsHelpers, urlUtils, DomainEvents, labs}) {
|
2022-08-25 10:25:36 +03:00
|
|
|
this.logging = logging;
|
2023-01-25 16:10:29 +03:00
|
|
|
this.labs = labs;
|
2022-08-25 10:25:36 +03:00
|
|
|
/** @private */
|
|
|
|
this.settingsCache = settingsCache;
|
2022-08-25 17:48:56 +03:00
|
|
|
this.models = models;
|
2022-09-09 17:23:43 +03:00
|
|
|
this.DomainEvents = DomainEvents;
|
2022-08-25 10:25:36 +03:00
|
|
|
|
|
|
|
const Emails = require('./emails');
|
2022-08-25 17:48:56 +03:00
|
|
|
|
2022-08-25 10:25:36 +03:00
|
|
|
/** @private */
|
|
|
|
this.emails = new Emails({
|
|
|
|
logging,
|
|
|
|
models,
|
|
|
|
mailer,
|
2022-09-02 17:57:59 +03:00
|
|
|
settingsHelpers,
|
2022-08-25 10:25:36 +03:00
|
|
|
settingsCache,
|
|
|
|
urlUtils
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:23:43 +03:00
|
|
|
/** @private */
|
|
|
|
getSerializedData({member, tier = null, subscription = null, offer = null}) {
|
|
|
|
return {
|
|
|
|
offer: offer ? {
|
|
|
|
name: offer.name,
|
|
|
|
type: offer.discount_type,
|
|
|
|
currency: offer.currency,
|
|
|
|
duration: offer.duration,
|
|
|
|
durationInMonths: offer.duration_in_months,
|
|
|
|
amount: offer.discount_amount
|
|
|
|
} : null,
|
|
|
|
subscription: subscription ? {
|
|
|
|
id: subscription.id,
|
|
|
|
amount: subscription.plan?.amount,
|
|
|
|
interval: subscription.plan?.interval,
|
|
|
|
currency: subscription.plan?.currency,
|
|
|
|
startDate: subscription.start_date,
|
|
|
|
cancelAt: subscription.current_period_end,
|
|
|
|
cancellationReason: subscription.cancellation_reason
|
|
|
|
} : null,
|
|
|
|
member: member ? {
|
|
|
|
id: member.id,
|
|
|
|
name: member.name,
|
|
|
|
email: member.email,
|
|
|
|
geolocation: member.geolocation,
|
|
|
|
status: member.status,
|
|
|
|
created_at: member.created_at
|
|
|
|
} : null,
|
|
|
|
tier: tier ? {
|
|
|
|
id: tier.id,
|
|
|
|
name: tier.name
|
|
|
|
} : null
|
|
|
|
};
|
2022-08-25 10:25:36 +03:00
|
|
|
}
|
|
|
|
|
2022-09-09 17:23:43 +03:00
|
|
|
/** @private */
|
|
|
|
async getDataFromIds({memberId, tierId = null, subscriptionId = null, offerId = null}) {
|
|
|
|
const memberModel = memberId ? await this.models.Member.findOne({id: memberId}) : null;
|
|
|
|
const tierModel = tierId ? await this.models.Product.findOne({id: tierId}) : null;
|
|
|
|
const subscriptionModel = subscriptionId ? await this.models.StripeCustomerSubscription.findOne({id: subscriptionId}) : null;
|
|
|
|
const offerModel = offerId ? await this.models.Offer.findOne({id: offerId}) : null;
|
|
|
|
|
|
|
|
return this.getSerializedData({
|
|
|
|
member: memberModel?.toJSON(),
|
|
|
|
tier: tierModel?.toJSON(),
|
|
|
|
subscription: subscriptionModel?.toJSON(),
|
|
|
|
offer: offerModel?.toJSON()
|
|
|
|
});
|
2022-08-25 10:25:36 +03:00
|
|
|
}
|
|
|
|
|
2022-09-09 17:23:43 +03:00
|
|
|
/** @private */
|
|
|
|
async handleEvent(type, event) {
|
2023-02-23 13:42:14 +03:00
|
|
|
if (type === MentionCreatedEvent && event.data.mention && this.labs.isSet('webmentions') && this.labs.isSet('webmentionEmails')) {
|
|
|
|
return await this.emails.notifyMentionReceived(event.data);
|
2023-01-25 16:10:29 +03:00
|
|
|
}
|
2023-02-23 12:20:13 +03:00
|
|
|
|
|
|
|
if (type === MilestoneCreatedEvent && event.data.milestone && this.labs.isSet('milestoneEmails')) {
|
|
|
|
await this.emails.notifyMilestoneReceived(event.data);
|
|
|
|
}
|
|
|
|
|
2022-09-09 17:23:43 +03:00
|
|
|
if (!['api', 'member'].includes(event.data.source)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {member, tier, subscription, offer} = await this.getDataFromIds({
|
|
|
|
memberId: event.data.memberId,
|
|
|
|
tierId: event.data.tierId,
|
|
|
|
subscriptionId: event.data.subscriptionId,
|
|
|
|
offerId: event.data.offerId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (type === MemberCreatedEvent && member.status === 'free') {
|
|
|
|
await this.emails.notifyFreeMemberSignup(member);
|
2023-02-13 15:07:53 +03:00
|
|
|
} else if (type === SubscriptionActivatedEvent) {
|
2022-09-09 17:23:43 +03:00
|
|
|
await this.emails.notifyPaidSubscriptionStarted({
|
|
|
|
member,
|
|
|
|
offer,
|
|
|
|
tier,
|
|
|
|
subscription
|
|
|
|
});
|
|
|
|
} else if (type === SubscriptionCancelledEvent) {
|
|
|
|
subscription.canceledAt = event.timestamp;
|
|
|
|
await this.emails.notifyPaidSubscriptionCanceled({
|
|
|
|
member,
|
|
|
|
tier,
|
|
|
|
subscription
|
|
|
|
});
|
2022-08-25 10:25:36 +03:00
|
|
|
}
|
|
|
|
}
|
2022-09-09 17:23:43 +03:00
|
|
|
|
|
|
|
subscribeEvents() {
|
|
|
|
// Trigger email for free member signup
|
|
|
|
this.DomainEvents.subscribe(MemberCreatedEvent, async (event) => {
|
|
|
|
try {
|
|
|
|
await this.handleEvent(MemberCreatedEvent, event);
|
|
|
|
} catch (e) {
|
2023-02-13 15:07:53 +03:00
|
|
|
this.logging.error(e, `Failed to notify free member signup - ${event?.data?.memberId}`);
|
2022-09-09 17:23:43 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Trigger email on paid subscription start
|
2023-02-13 15:07:53 +03:00
|
|
|
this.DomainEvents.subscribe(SubscriptionActivatedEvent, async (event) => {
|
2022-09-09 17:23:43 +03:00
|
|
|
try {
|
2023-02-13 15:07:53 +03:00
|
|
|
await this.handleEvent(SubscriptionActivatedEvent, event);
|
2022-09-09 17:23:43 +03:00
|
|
|
} catch (e) {
|
2023-02-13 15:07:53 +03:00
|
|
|
this.logging.error(e, `Failed to notify paid member subscription start - ${event?.data?.memberId}`);
|
2022-09-09 17:23:43 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Trigger email when a member cancels their subscription
|
|
|
|
this.DomainEvents.subscribe(SubscriptionCancelledEvent, async (event) => {
|
|
|
|
try {
|
|
|
|
await this.handleEvent(SubscriptionCancelledEvent, event);
|
|
|
|
} catch (e) {
|
2023-02-13 15:07:53 +03:00
|
|
|
this.logging.error(e, `Failed to notify paid member subscription cancel - ${event?.data?.memberId}`);
|
2022-09-09 17:23:43 +03:00
|
|
|
}
|
|
|
|
});
|
2023-01-25 16:10:29 +03:00
|
|
|
|
|
|
|
// Trigger email when a new webmention is received
|
|
|
|
this.DomainEvents.subscribe(MentionCreatedEvent, async (event) => {
|
|
|
|
try {
|
|
|
|
await this.handleEvent(MentionCreatedEvent, event);
|
|
|
|
} catch (e) {
|
2023-02-13 15:07:53 +03:00
|
|
|
this.logging.error(e, `Failed to notify webmention`);
|
2023-01-25 16:10:29 +03:00
|
|
|
}
|
|
|
|
});
|
2023-02-23 12:20:13 +03:00
|
|
|
|
|
|
|
// Trigger email when a new milestone is reached
|
|
|
|
this.DomainEvents.subscribe(MilestoneCreatedEvent, async (event) => {
|
|
|
|
try {
|
|
|
|
await this.handleEvent(MilestoneCreatedEvent, event);
|
|
|
|
} catch (e) {
|
|
|
|
this.logging.error(e, `Failed to notify milestone`);
|
|
|
|
}
|
|
|
|
});
|
2022-09-09 17:23:43 +03:00
|
|
|
}
|
2022-08-25 10:25:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = StaffService;
|