2022-01-25 14:50:10 +03:00
|
|
|
|
import moment from 'moment';
|
|
|
|
|
import {getNonDecimal, getSymbol} from 'ghost-admin/utils/currency';
|
|
|
|
|
|
2022-05-04 11:21:42 +03:00
|
|
|
|
export default function parseMemberEvent(event, hasMultipleNewsletters) {
|
2022-01-25 14:50:10 +03:00
|
|
|
|
let subject = event.data.member.name || event.data.member.email;
|
|
|
|
|
let icon = getIcon(event);
|
|
|
|
|
let action = getAction(event);
|
2022-05-04 11:21:42 +03:00
|
|
|
|
let object = getObject(event, hasMultipleNewsletters);
|
2022-01-25 14:50:10 +03:00
|
|
|
|
let info = getInfo(event);
|
|
|
|
|
let timestamp = moment(event.data.created_at);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
memberId: event.data.member_id,
|
|
|
|
|
member: event.data.member,
|
|
|
|
|
emailId: event.data.email_id,
|
|
|
|
|
email: event.data.email,
|
|
|
|
|
icon,
|
|
|
|
|
subject,
|
|
|
|
|
action,
|
|
|
|
|
object,
|
|
|
|
|
info,
|
|
|
|
|
timestamp
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* internal helper functions */
|
|
|
|
|
|
|
|
|
|
function getIcon(event) {
|
2022-01-26 12:52:51 +03:00
|
|
|
|
let icon;
|
|
|
|
|
|
|
|
|
|
if (event.type === 'signup_event') {
|
|
|
|
|
icon = 'signed-up';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'login_event') {
|
|
|
|
|
icon = 'logged-in';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'payment_event') {
|
2022-03-09 15:20:41 +03:00
|
|
|
|
icon = 'subscriptions';
|
2022-01-26 12:52:51 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'newsletter_event') {
|
|
|
|
|
if (event.data.subscribed) {
|
|
|
|
|
icon = 'subscribed-to-email';
|
|
|
|
|
} else {
|
|
|
|
|
icon = 'unsubscribed-from-email';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'subscription_event') {
|
2022-03-09 15:20:41 +03:00
|
|
|
|
icon = 'subscriptions';
|
2022-02-15 19:07:28 +03:00
|
|
|
|
|
2022-04-13 12:28:13 +03:00
|
|
|
|
if (event.data.type === 'canceled') {
|
2022-01-26 12:52:51 +03:00
|
|
|
|
icon = 'canceled-subscription';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'email_opened_event') {
|
|
|
|
|
icon = 'opened-email';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'email_delivered_event') {
|
|
|
|
|
icon = 'received-email';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'email_failed_event') {
|
|
|
|
|
icon = 'email-delivery-failed';
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 12:41:32 +03:00
|
|
|
|
if (event.type === 'comment_event') {
|
|
|
|
|
icon = 'comment';
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 12:52:51 +03:00
|
|
|
|
return 'event-' + icon;
|
2022-01-25 14:50:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getAction(event) {
|
|
|
|
|
if (event.type === 'signup_event') {
|
|
|
|
|
return 'signed up';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'login_event') {
|
|
|
|
|
return 'logged in';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'payment_event') {
|
|
|
|
|
return 'made a payment';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'newsletter_event') {
|
|
|
|
|
if (event.data.subscribed) {
|
|
|
|
|
return 'subscribed to';
|
|
|
|
|
} else {
|
|
|
|
|
return 'unsubscribed from';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'subscription_event') {
|
2022-04-13 12:28:13 +03:00
|
|
|
|
if (event.data.type === 'created') {
|
2022-01-25 14:50:10 +03:00
|
|
|
|
return 'started';
|
|
|
|
|
}
|
2022-04-13 12:28:13 +03:00
|
|
|
|
if (event.data.type === 'updated') {
|
|
|
|
|
return 'changed';
|
|
|
|
|
}
|
|
|
|
|
if (event.data.type === 'canceled') {
|
|
|
|
|
return 'canceled';
|
|
|
|
|
}
|
|
|
|
|
if (event.data.type === 'reactivated') {
|
|
|
|
|
return 'reactivated';
|
|
|
|
|
}
|
|
|
|
|
if (event.data.type === 'expired') {
|
|
|
|
|
return 'ended';
|
2022-01-25 14:50:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'changed';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'email_opened_event') {
|
|
|
|
|
return 'opened';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'email_delivered_event') {
|
|
|
|
|
return 'received';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'email_failed_event') {
|
|
|
|
|
return 'failed to receive';
|
|
|
|
|
}
|
2022-07-22 12:41:32 +03:00
|
|
|
|
|
|
|
|
|
if (event.type === 'comment_event') {
|
|
|
|
|
if (event.data.parent_id) {
|
|
|
|
|
return 'replied on';
|
|
|
|
|
}
|
|
|
|
|
return 'commented on';
|
|
|
|
|
}
|
2022-01-25 14:50:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 11:21:42 +03:00
|
|
|
|
function getObject(event, hasMultipleNewsletters) {
|
2022-01-25 14:50:10 +03:00
|
|
|
|
if (event.type === 'newsletter_event') {
|
2022-05-04 11:21:42 +03:00
|
|
|
|
if (hasMultipleNewsletters && event.data.newsletter && event.data.newsletter.name) {
|
2022-05-03 15:37:45 +03:00
|
|
|
|
return 'newsletter – ' + event.data.newsletter.name;
|
2022-05-03 15:21:29 +03:00
|
|
|
|
}
|
2022-05-04 11:21:42 +03:00
|
|
|
|
return 'newsletter';
|
2022-01-25 14:50:10 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'subscription_event') {
|
|
|
|
|
return 'their subscription';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type.match?.(/^email_/)) {
|
|
|
|
|
return 'an email';
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 12:41:32 +03:00
|
|
|
|
if (event.type === 'subscription_event') {
|
|
|
|
|
return 'their subscription';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (event.type === 'comment_event') {
|
|
|
|
|
if (event.data.parent_id) {
|
|
|
|
|
return 'a comment';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'a post';
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-25 14:50:10 +03:00
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getInfo(event) {
|
|
|
|
|
if (event.type === 'subscription_event') {
|
|
|
|
|
let mrrDelta = getNonDecimal(event.data.mrr_delta, event.data.currency);
|
|
|
|
|
if (mrrDelta === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let sign = mrrDelta > 0 ? '+' : '-';
|
|
|
|
|
let symbol = getSymbol(event.data.currency);
|
|
|
|
|
return `(MRR ${sign}${symbol}${Math.abs(mrrDelta)})`;
|
|
|
|
|
}
|
2022-07-22 12:41:32 +03:00
|
|
|
|
|
|
|
|
|
// TODO: we can include the post title
|
|
|
|
|
/*if (event.type === 'comment_event') {
|
|
|
|
|
if (event.data.post) {
|
|
|
|
|
return event.data.post.title;
|
|
|
|
|
}
|
|
|
|
|
}*/
|
2022-01-25 14:50:10 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|