2021-09-13 14:10:25 +03:00
|
|
|
import Component from '@glimmer/component';
|
2022-09-23 20:15:08 +03:00
|
|
|
import moment from 'moment-timezone';
|
2020-12-10 14:38:38 +03:00
|
|
|
import {action} from '@ember/object';
|
2021-09-13 14:10:25 +03:00
|
|
|
import {getNonDecimal, getSymbol} from 'ghost-admin/utils/currency';
|
2019-10-02 07:00:03 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-09-13 14:10:25 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class extends Component {
|
2022-02-10 13:41:36 +03:00
|
|
|
@service membersUtils;
|
|
|
|
@service ghostPaths;
|
|
|
|
@service ajax;
|
|
|
|
@service store;
|
|
|
|
@service feature;
|
|
|
|
@service settings;
|
2021-09-13 14:10:25 +03:00
|
|
|
|
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
|
|
|
this.member = this.args.member;
|
|
|
|
this.scratchMember = this.args.scratchMember;
|
|
|
|
}
|
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
@tracked showMemberTierModal = false;
|
|
|
|
@tracked tiersList;
|
2022-04-14 17:40:04 +03:00
|
|
|
@tracked newslettersList;
|
2021-09-13 14:10:25 +03:00
|
|
|
|
|
|
|
get isAddComplimentaryAllowed() {
|
2022-05-24 17:53:03 +03:00
|
|
|
if (!this.membersUtils.paidMembersEnabled) {
|
2021-09-13 14:10:25 +03:00
|
|
|
return false;
|
|
|
|
}
|
2019-10-02 07:00:03 +03:00
|
|
|
|
2022-03-18 19:15:42 +03:00
|
|
|
if (this.member.get('isNew')) {
|
2021-09-13 14:10:25 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-10 14:38:38 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
if (this.member.get('tiers')?.length > 0) {
|
2022-03-18 19:15:42 +03:00
|
|
|
return false;
|
2022-02-03 16:42:41 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
// complimentary subscriptions are assigned to tiers so it only
|
2022-03-18 19:15:42 +03:00
|
|
|
// makes sense to show the "add complimentary" buttons when there's a
|
2022-05-11 20:11:54 +03:00
|
|
|
// tier to assign the complimentary subscription to
|
|
|
|
const hasAnActivePaidTier = !!this.tiersList?.length;
|
2022-03-18 19:15:42 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
return hasAnActivePaidTier;
|
2022-03-18 19:15:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get isCreatingComplimentary() {
|
|
|
|
return this.args.isSaveRunning;
|
2021-09-13 14:10:25 +03:00
|
|
|
}
|
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
get tiers() {
|
2021-09-13 14:10:25 +03:00
|
|
|
let subscriptions = this.member.get('subscriptions') || [];
|
2022-03-18 19:15:42 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
// Create the tiers from `subscriptions.price.tier`
|
|
|
|
let tiers = subscriptions
|
|
|
|
.map(subscription => (subscription.tier || subscription.price.tier))
|
2022-03-18 19:15:42 +03:00
|
|
|
.filter((value, index, self) => {
|
|
|
|
// Deduplicate by taking the first object by `id`
|
2022-05-11 20:11:54 +03:00
|
|
|
return typeof value.id !== 'undefined' && self.findIndex(element => (element.tier_id || element.id) === (value.tier_id || value.id)) === index;
|
2022-03-18 19:15:42 +03:00
|
|
|
});
|
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
let subscriptionData = subscriptions.filter((sub) => {
|
|
|
|
return !!sub.price;
|
|
|
|
}).map((sub) => {
|
2022-08-09 16:49:54 +03:00
|
|
|
const data = {
|
2021-09-13 14:10:25 +03:00
|
|
|
...sub,
|
2022-09-23 18:20:11 +03:00
|
|
|
attribution: {
|
|
|
|
...sub.attribution,
|
|
|
|
referrerSource: sub.attribution?.referrer_source || 'Unknown',
|
|
|
|
referrerMedium: sub.attribution?.referrer_medium || '-'
|
|
|
|
},
|
2021-09-13 14:10:25 +03:00
|
|
|
startDate: sub.start_date ? moment(sub.start_date).format('D MMM YYYY') : '-',
|
|
|
|
validUntil: sub.current_period_end ? moment(sub.current_period_end).format('D MMM YYYY') : '-',
|
|
|
|
cancellationReason: sub.cancellation_reason,
|
|
|
|
price: {
|
|
|
|
...sub.price,
|
|
|
|
currencySymbol: getSymbol(sub.price.currency),
|
|
|
|
nonDecimalAmount: getNonDecimal(sub.price.amount)
|
|
|
|
},
|
|
|
|
isComplimentary: !sub.id
|
|
|
|
};
|
2022-11-18 12:09:25 +03:00
|
|
|
if (sub.trial_end_at) {
|
2022-08-09 16:49:54 +03:00
|
|
|
const inTrialMode = moment(sub.trial_end_at).isAfter(new Date(), 'day');
|
|
|
|
if (inTrialMode) {
|
|
|
|
data.trialUntil = moment(sub.trial_end_at).format('D MMM YYYY');
|
|
|
|
}
|
|
|
|
}
|
2022-08-19 14:17:55 +03:00
|
|
|
|
2022-11-18 12:09:25 +03:00
|
|
|
if (!sub.id && sub.tier?.expiry_at) {
|
2022-08-19 14:17:55 +03:00
|
|
|
data.compExpiry = moment(sub.tier.expiry_at).format('D MMM YYYY');
|
|
|
|
}
|
2022-08-09 16:49:54 +03:00
|
|
|
return data;
|
2021-09-13 14:10:25 +03:00
|
|
|
});
|
2022-05-11 20:11:54 +03:00
|
|
|
return tiers.map((tier) => {
|
|
|
|
let tierSubscriptions = subscriptionData.filter((subscription) => {
|
|
|
|
return subscription?.price?.tier?.tier_id === (tier.tier_id || tier.id);
|
2021-09-13 14:10:25 +03:00
|
|
|
});
|
2022-03-22 19:28:21 +03:00
|
|
|
return {
|
2022-05-11 20:11:54 +03:00
|
|
|
...tier,
|
|
|
|
subscriptions: tierSubscriptions
|
2022-03-22 19:28:21 +03:00
|
|
|
};
|
|
|
|
});
|
2021-09-13 14:10:25 +03:00
|
|
|
}
|
2020-02-24 12:08:47 +03:00
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
get customer() {
|
|
|
|
let firstSubscription = this.member.get('subscriptions').firstObject;
|
|
|
|
let customer = firstSubscription?.customer;
|
2019-10-03 20:42:33 +03:00
|
|
|
|
2021-01-06 14:56:15 +03:00
|
|
|
if (customer) {
|
2021-09-13 14:10:25 +03:00
|
|
|
return {
|
|
|
|
...customer,
|
|
|
|
startDate: firstSubscription?.startDate
|
|
|
|
};
|
2021-01-06 14:56:15 +03:00
|
|
|
}
|
|
|
|
return null;
|
2021-09-13 14:10:25 +03:00
|
|
|
}
|
|
|
|
|
2022-11-18 14:55:21 +03:00
|
|
|
get canShowSingleNewsletter() {
|
|
|
|
return (
|
|
|
|
this.newslettersList?.length === 1
|
|
|
|
&& this.settings.editorDefaultEmailRecipients !== 'disabled'
|
|
|
|
&& !this.feature.get('suppressionList')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
get canShowMultipleNewsletters() {
|
|
|
|
return (
|
|
|
|
(this.newslettersList?.length > 1 || this.feature.get('suppressionList'))
|
|
|
|
&& this.settings.editorDefaultEmailRecipients !== 'disabled'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-25 15:10:29 +03:00
|
|
|
@action
|
|
|
|
updateNewsletterPreference(event) {
|
|
|
|
if (!event.target.checked) {
|
|
|
|
this.member.set('newsletters', []);
|
|
|
|
} else if (this.newslettersList.firstObject) {
|
|
|
|
const newsletter = this.newslettersList.firstObject;
|
|
|
|
this.member.set('newsletters', [newsletter]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 16:42:41 +03:00
|
|
|
@action
|
|
|
|
setup() {
|
2022-05-11 20:11:54 +03:00
|
|
|
this.fetchTiers.perform();
|
2022-05-25 10:50:29 +03:00
|
|
|
this.fetchNewsletters.perform();
|
2022-02-03 16:42:41 +03:00
|
|
|
}
|
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
@action
|
|
|
|
setProperty(property, value) {
|
|
|
|
this.args.setProperty(property, value);
|
|
|
|
}
|
|
|
|
|
2022-10-10 15:59:49 +03:00
|
|
|
@action
|
|
|
|
updateProperty(event){
|
|
|
|
this.args.setProperty(event.currentTarget.name, event.target.value);
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
@action
|
|
|
|
setLabels(labels) {
|
|
|
|
this.member.set('labels', labels);
|
|
|
|
}
|
|
|
|
|
2022-04-14 17:40:04 +03:00
|
|
|
@action
|
|
|
|
setMemberNewsletters(newsletters) {
|
|
|
|
this.member.set('newsletters', newsletters);
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
@action
|
2022-05-11 20:11:54 +03:00
|
|
|
closeMemberTierModal() {
|
|
|
|
this.showMemberTierModal = false;
|
2021-09-13 14:10:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelSubscription(subscriptionId) {
|
|
|
|
this.cancelSubscriptionTask.perform(subscriptionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
2022-05-11 20:11:54 +03:00
|
|
|
removeComplimentary(tierId) {
|
|
|
|
this.removeComplimentaryTask.perform(tierId);
|
2021-09-13 14:10:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
continueSubscription(subscriptionId) {
|
|
|
|
this.continueSubscriptionTask.perform(subscriptionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@task({drop: true})
|
|
|
|
*cancelSubscriptionTask(subscriptionId) {
|
|
|
|
let url = this.ghostPaths.url.api('members', this.member.get('id'), 'subscriptions', subscriptionId);
|
2020-08-21 14:35:45 +03:00
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
let response = yield this.ajax.put(url, {
|
|
|
|
data: {
|
|
|
|
cancel_at_period_end: true
|
|
|
|
}
|
|
|
|
});
|
2020-12-10 14:38:38 +03:00
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
this.store.pushPayload('member', response);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
@task({drop: true})
|
2022-05-11 20:11:54 +03:00
|
|
|
*removeComplimentaryTask(tierId) {
|
2021-09-13 14:10:25 +03:00
|
|
|
let url = this.ghostPaths.url.api(`members/${this.member.get('id')}`);
|
2022-05-11 20:11:54 +03:00
|
|
|
let tiers = this.member.get('tiers') || [];
|
2022-03-18 19:15:42 +03:00
|
|
|
|
2022-05-11 20:11:54 +03:00
|
|
|
const updatedTiers = tiers
|
|
|
|
.filter(tier => tier.id !== tierId)
|
|
|
|
.map(tier => ({id: tier.id}));
|
2020-08-21 14:35:45 +03:00
|
|
|
|
|
|
|
let response = yield this.ajax.put(url, {
|
|
|
|
data: {
|
2021-09-13 14:10:25 +03:00
|
|
|
members: [{
|
|
|
|
id: this.member.get('id'),
|
|
|
|
email: this.member.get('email'),
|
2022-05-11 20:11:54 +03:00
|
|
|
tiers: updatedTiers
|
2021-09-13 14:10:25 +03:00
|
|
|
}]
|
2020-08-21 14:35:45 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.store.pushPayload('member', response);
|
|
|
|
return response;
|
2021-09-13 14:10:25 +03:00
|
|
|
}
|
2020-08-21 14:35:45 +03:00
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*continueSubscriptionTask(subscriptionId) {
|
|
|
|
let url = this.ghostPaths.url.api('members', this.member.get('id'), 'subscriptions', subscriptionId);
|
2020-08-21 14:35:45 +03:00
|
|
|
|
|
|
|
let response = yield this.ajax.put(url, {
|
|
|
|
data: {
|
|
|
|
cancel_at_period_end: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.store.pushPayload('member', response);
|
|
|
|
return response;
|
2021-09-13 14:10:25 +03:00
|
|
|
}
|
2022-02-03 16:42:41 +03:00
|
|
|
|
|
|
|
@task({drop: true})
|
2022-05-11 20:11:54 +03:00
|
|
|
*fetchTiers() {
|
|
|
|
this.tiersList = yield this.store.query('tier', {filter: 'type:paid+active:true', include: 'monthly_price,yearly_price'});
|
2022-02-03 16:42:41 +03:00
|
|
|
}
|
2022-04-14 17:40:04 +03:00
|
|
|
|
|
|
|
@task({drop: true})
|
|
|
|
*fetchNewsletters() {
|
|
|
|
this.newslettersList = yield this.store.query('newsletter', {filter: 'status:active'});
|
|
|
|
if (this.member.get('isNew')) {
|
|
|
|
const defaultNewsletters = this.newslettersList.filter((newsletter) => {
|
|
|
|
return newsletter.subscribeOnSignup && newsletter.visibility === 'members';
|
|
|
|
});
|
|
|
|
this.setMemberNewsletters(defaultNewsletters);
|
|
|
|
}
|
|
|
|
}
|
2021-09-13 14:10:25 +03:00
|
|
|
}
|