Ghost/ghost/admin/app/helpers/parse-member-event.js

203 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import moment from 'moment';
import {getNonDecimal, getSymbol} from 'ghost-admin/utils/currency';
export default function parseMemberEvent(event, hasMultipleNewsletters) {
let subject = event.data.member.name || event.data.member.email;
let icon = getIcon(event);
let action = getAction(event);
let object = getObject(event, hasMultipleNewsletters);
let info = getInfo(event);
const url = getURL(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,
url,
timestamp
};
}
/* internal helper functions */
function getIcon(event) {
let icon;
if (event.type === 'signup_event') {
icon = 'signed-up';
}
if (event.type === 'login_event') {
icon = 'logged-in';
}
if (event.type === 'payment_event') {
icon = 'subscriptions';
}
if (event.type === 'newsletter_event') {
if (event.data.subscribed) {
icon = 'subscribed-to-email';
} else {
icon = 'unsubscribed-from-email';
}
}
if (event.type === 'subscription_event') {
icon = 'subscriptions';
if (event.data.type === 'canceled') {
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';
}
if (event.type === 'comment_event') {
icon = 'comment';
}
return 'event-' + icon;
}
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') {
if (event.data.type === 'created') {
return 'started';
}
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';
}
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';
}
if (event.type === 'comment_event') {
if (event.data.parent) {
return 'replied to a comment on';
}
return 'commented on';
}
}
function getObject(event, hasMultipleNewsletters) {
if (event.type === 'newsletter_event') {
if (hasMultipleNewsletters && event.data.newsletter && event.data.newsletter.name) {
return 'newsletter ' + event.data.newsletter.name;
}
return 'newsletter';
}
if (event.type === 'subscription_event') {
return 'their subscription';
}
if (event.type.match?.(/^email_/)) {
return 'an email';
}
if (event.type === 'subscription_event') {
return 'their subscription';
}
if (event.type === 'comment_event') {
if (event.type === 'comment_event') {
if (event.data.post) {
return event.data.post.title;
}
}
}
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)})`;
}
// TODO: we can include the post title
/*if (event.type === 'comment_event') {
if (event.data.post) {
return event.data.post.title;
}
}*/
return;
}
/**
* Make the object clickable
*/
function getURL(event) {
if (event.type === 'comment_event') {
if (event.data.post) {
return event.data.post.url;
}
}
return;
}