2021-04-26 19:28:39 +03:00
|
|
|
import Component from '@glimmer/component';
|
2021-04-15 18:58:35 +03:00
|
|
|
import moment from 'moment';
|
|
|
|
import {action} from '@ember/object';
|
2021-04-26 19:28:39 +03:00
|
|
|
import {getNonDecimal, getSymbol} from 'ghost-admin/utils/currency';
|
2021-04-15 18:58:35 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2021-04-26 19:28:39 +03:00
|
|
|
import {task} from 'ember-concurrency-decorators';
|
|
|
|
import {tracked} from '@glimmer/tracking';
|
|
|
|
|
|
|
|
export default class extends Component {
|
|
|
|
@service
|
|
|
|
membersUtils
|
|
|
|
@service
|
|
|
|
ghostPaths
|
|
|
|
@service
|
|
|
|
ajax
|
|
|
|
@service
|
|
|
|
store
|
2021-06-17 10:39:43 +03:00
|
|
|
@service
|
|
|
|
feature
|
2021-04-26 19:28:39 +03:00
|
|
|
|
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
|
|
|
this.member = this.args.member;
|
|
|
|
this.scratchMember = this.args.scratchMember;
|
|
|
|
}
|
|
|
|
|
|
|
|
@tracked
|
|
|
|
showMemberProductModal = false;
|
|
|
|
|
|
|
|
get canShowStripeInfo() {
|
|
|
|
return !this.member.get('isNew') && this.membersUtils.isStripeEnabled;
|
|
|
|
}
|
|
|
|
|
2021-05-19 20:05:55 +03:00
|
|
|
get isAddComplimentaryAllowed() {
|
2021-06-04 10:49:44 +03:00
|
|
|
if (!this.membersUtils.isStripeEnabled) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-17 10:39:43 +03:00
|
|
|
if (this.feature.get('multipleProducts')) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-19 20:05:55 +03:00
|
|
|
let subscriptions = this.member.get('subscriptions') || [];
|
2021-06-07 19:33:04 +03:00
|
|
|
const hasZeroPriceSub = subscriptions.filter((sub) => {
|
|
|
|
return ['active', 'trialing', 'unpaid', 'past_due'].includes(sub.status);
|
|
|
|
}).find((sub) => {
|
2021-05-19 20:05:55 +03:00
|
|
|
return !sub?.price?.amount;
|
|
|
|
});
|
|
|
|
return !hasZeroPriceSub;
|
|
|
|
}
|
|
|
|
|
2021-04-26 19:28:39 +03:00
|
|
|
get products() {
|
|
|
|
let products = this.member.get('products') || [];
|
|
|
|
let subscriptions = this.member.get('subscriptions') || [];
|
2021-05-06 19:15:27 +03:00
|
|
|
let subscriptionData = subscriptions.filter((sub) => {
|
|
|
|
return !!sub.price;
|
|
|
|
}).map((sub) => {
|
2021-04-26 19:28:39 +03:00
|
|
|
return {
|
|
|
|
...sub,
|
|
|
|
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') : '-',
|
2021-06-15 14:30:56 +03:00
|
|
|
cancellationReason: sub.cancellation_reason,
|
2021-04-26 19:28:39 +03:00
|
|
|
price: {
|
|
|
|
...sub.price,
|
|
|
|
currencySymbol: getSymbol(sub.price.currency),
|
|
|
|
nonDecimalAmount: getNonDecimal(sub.price.amount)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
2021-04-15 18:58:35 +03:00
|
|
|
|
2021-04-26 19:28:39 +03:00
|
|
|
for (let product of products) {
|
|
|
|
let productSubscriptions = subscriptionData.filter((subscription) => {
|
|
|
|
if (subscription.status === 'canceled') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return subscription?.price?.product?.product_id === product.id;
|
|
|
|
});
|
|
|
|
product.subscriptions = productSubscriptions;
|
|
|
|
}
|
2021-04-15 18:58:35 +03:00
|
|
|
|
2021-04-26 19:28:39 +03:00
|
|
|
return products;
|
|
|
|
}
|
2021-04-15 18:58:35 +03:00
|
|
|
|
2021-04-26 19:28:39 +03:00
|
|
|
get customer() {
|
|
|
|
let firstSubscription = this.member.get('subscriptions').firstObject;
|
|
|
|
let customer = firstSubscription?.customer;
|
2021-04-15 18:58:35 +03:00
|
|
|
|
|
|
|
if (customer) {
|
2021-04-26 19:28:39 +03:00
|
|
|
return {
|
|
|
|
...customer,
|
|
|
|
startDate: firstSubscription?.startDate
|
|
|
|
};
|
2021-04-15 18:58:35 +03:00
|
|
|
}
|
|
|
|
return null;
|
2021-04-26 19:28:39 +03:00
|
|
|
}
|
|
|
|
|
2021-05-19 20:47:59 +03:00
|
|
|
get isCreatingComplimentary() {
|
2021-05-19 21:45:22 +03:00
|
|
|
return this.args.isSaveRunning;
|
2021-05-19 20:47:59 +03:00
|
|
|
}
|
|
|
|
|
2021-04-26 19:28:39 +03:00
|
|
|
@action
|
|
|
|
setProperty(property, value) {
|
|
|
|
this.args.setProperty(property, value);
|
|
|
|
}
|
2021-04-15 18:58:35 +03:00
|
|
|
|
2021-04-26 19:28:39 +03:00
|
|
|
@action
|
|
|
|
setLabels(labels) {
|
|
|
|
this.member.set('labels', labels);
|
|
|
|
}
|
2021-04-15 18:58:35 +03:00
|
|
|
|
2021-04-26 19:28:39 +03:00
|
|
|
@action
|
|
|
|
closeMemberProductModal() {
|
|
|
|
this.showMemberProductModal = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelSubscription(subscriptionId) {
|
|
|
|
this.cancelSubscriptionTask.perform(subscriptionId);
|
|
|
|
}
|
|
|
|
|
2021-06-21 10:02:20 +03:00
|
|
|
@action
|
|
|
|
removeComplimentary(productId) {
|
|
|
|
this.removeComplimentaryTask.perform(productId);
|
|
|
|
}
|
|
|
|
|
2021-04-26 19:28:39 +03:00
|
|
|
@action
|
|
|
|
continueSubscription(subscriptionId) {
|
|
|
|
this.continueSubscriptionTask.perform(subscriptionId);
|
|
|
|
}
|
2021-05-19 20:47:59 +03:00
|
|
|
|
2021-05-19 20:05:55 +03:00
|
|
|
@action
|
|
|
|
addCompedSubscription() {
|
|
|
|
this.args.setProperty('comped', true);
|
|
|
|
this.args.saveMember();
|
|
|
|
}
|
2021-04-26 19:28:39 +03:00
|
|
|
|
|
|
|
@task({drop: true})
|
|
|
|
*cancelSubscriptionTask(subscriptionId) {
|
|
|
|
let url = this.ghostPaths.url.api('members', this.member.get('id'), 'subscriptions', subscriptionId);
|
2021-04-15 18:58:35 +03:00
|
|
|
|
|
|
|
let response = yield this.ajax.put(url, {
|
|
|
|
data: {
|
|
|
|
cancel_at_period_end: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.store.pushPayload('member', response);
|
|
|
|
return response;
|
2021-04-26 19:28:39 +03:00
|
|
|
}
|
2021-04-15 18:58:35 +03:00
|
|
|
|
2021-06-21 10:02:20 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*removeComplimentaryTask(productId) {
|
|
|
|
let url = this.ghostPaths.url.api(`members/${this.member.get('id')}`);
|
|
|
|
let products = this.member.get('products') || [];
|
|
|
|
const updatedProducts = products.filter(product => product.id !== productId).map(product => ({id: product.id}));
|
|
|
|
|
|
|
|
let response = yield this.ajax.put(url, {
|
|
|
|
data: {
|
|
|
|
members: [{
|
|
|
|
id: this.member.get('id'),
|
|
|
|
email: this.member.get('email'),
|
|
|
|
products: updatedProducts
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.store.pushPayload('member', response);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2021-04-26 19:28:39 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*continueSubscriptionTask(subscriptionId) {
|
|
|
|
let url = this.ghostPaths.url.api('members', this.member.get('id'), 'subscriptions', subscriptionId);
|
2021-04-15 18:58:35 +03:00
|
|
|
|
|
|
|
let response = yield this.ajax.put(url, {
|
|
|
|
data: {
|
|
|
|
cancel_at_period_end: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.store.pushPayload('member', response);
|
|
|
|
return response;
|
2021-04-26 19:28:39 +03:00
|
|
|
}
|
|
|
|
}
|