2021-09-13 14:10:25 +03:00
|
|
|
import Component from '@glimmer/component';
|
2019-10-03 20:42:33 +03:00
|
|
|
import moment from 'moment';
|
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-02-03 16:42:41 +03:00
|
|
|
@tracked showMemberProductModal = false;
|
|
|
|
@tracked productsList;
|
2021-09-13 14:10:25 +03:00
|
|
|
|
|
|
|
get canShowStripeInfo() {
|
|
|
|
return !this.member.get('isNew') && this.membersUtils.isStripeEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isAddComplimentaryAllowed() {
|
|
|
|
if (!this.membersUtils.isStripeEnabled) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-02 07:00:03 +03:00
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
let products = this.member.get('products');
|
|
|
|
if (products && products.length > 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-10 14:38:38 +03:00
|
|
|
|
2022-02-03 16:42:41 +03:00
|
|
|
if (this.feature.get('multipleProducts')) {
|
|
|
|
return !!this.productsList?.length;
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
get products() {
|
|
|
|
let products = this.member.get('products') || [];
|
|
|
|
let subscriptions = this.member.get('subscriptions') || [];
|
|
|
|
let subscriptionData = subscriptions.filter((sub) => {
|
|
|
|
return !!sub.price;
|
|
|
|
}).map((sub) => {
|
|
|
|
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') : '-',
|
|
|
|
cancellationReason: sub.cancellation_reason,
|
|
|
|
price: {
|
|
|
|
...sub.price,
|
|
|
|
currencySymbol: getSymbol(sub.price.currency),
|
|
|
|
nonDecimalAmount: getNonDecimal(sub.price.amount)
|
|
|
|
},
|
|
|
|
isComplimentary: !sub.id
|
|
|
|
};
|
|
|
|
});
|
2019-10-02 07:00:03 +03:00
|
|
|
|
2021-09-13 14:10:25 +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;
|
|
|
|
}
|
2020-05-20 18:39:06 +03:00
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
return products;
|
|
|
|
}
|
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-02-03 16:42:41 +03:00
|
|
|
@action
|
|
|
|
setup() {
|
|
|
|
this.fetchProducts.perform();
|
|
|
|
}
|
|
|
|
|
2021-09-13 14:10:25 +03:00
|
|
|
get isCreatingComplimentary() {
|
|
|
|
return this.args.isSaveRunning;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setProperty(property, value) {
|
|
|
|
this.args.setProperty(property, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setLabels(labels) {
|
|
|
|
this.member.set('labels', labels);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
closeMemberProductModal() {
|
|
|
|
this.showMemberProductModal = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelSubscription(subscriptionId) {
|
|
|
|
this.cancelSubscriptionTask.perform(subscriptionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
removeComplimentary(productId) {
|
|
|
|
this.removeComplimentaryTask.perform(productId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
continueSubscription(subscriptionId) {
|
|
|
|
this.continueSubscriptionTask.perform(subscriptionId);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
addCompedSubscription() {
|
|
|
|
this.args.setProperty('comped', true);
|
|
|
|
this.args.saveMember();
|
|
|
|
}
|
|
|
|
|
|
|
|
@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})
|
|
|
|
*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}));
|
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'),
|
|
|
|
products: updatedProducts
|
|
|
|
}]
|
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})
|
|
|
|
*fetchProducts() {
|
|
|
|
this.productsList = yield this.store.query('product', {filter: 'type:paid+active:true', include: 'monthly_price,yearly_price'});
|
|
|
|
}
|
2021-09-13 14:10:25 +03:00
|
|
|
}
|