2019-10-02 07:00:03 +03:00
|
|
|
import Component from '@ember/component';
|
2019-10-03 20:42:33 +03:00
|
|
|
import moment from 'moment';
|
|
|
|
import {computed} from '@ember/object';
|
2020-02-21 11:12:35 +03:00
|
|
|
import {gt} from '@ember/object/computed';
|
2019-10-02 07:00:03 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2020-08-21 14:35:45 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2019-10-02 07:00:03 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
2020-06-11 12:05:40 +03:00
|
|
|
membersUtils: service(),
|
2019-10-02 07:00:03 +03:00
|
|
|
feature: service(),
|
|
|
|
config: service(),
|
|
|
|
mediaQueries: service(),
|
2020-08-21 14:35:45 +03:00
|
|
|
ghostPaths: service(),
|
|
|
|
ajax: service(),
|
|
|
|
store: service(),
|
2019-10-02 07:00:03 +03:00
|
|
|
|
|
|
|
// Allowed actions
|
|
|
|
setProperty: () => {},
|
|
|
|
|
2019-12-12 21:22:31 +03:00
|
|
|
hasMultipleSubscriptions: gt('member.stripe', 1),
|
2020-05-20 18:39:06 +03:00
|
|
|
|
2020-06-29 20:37:11 +03:00
|
|
|
canShowStripeInfo: computed('member.isNew', 'membersUtils.isStripeEnabled', function () {
|
2020-06-11 15:46:04 +03:00
|
|
|
let stripeEnabled = this.membersUtils.isStripeEnabled;
|
2020-02-24 12:08:47 +03:00
|
|
|
|
2020-05-20 18:39:06 +03:00
|
|
|
if (this.member.get('isNew') || !stripeEnabled) {
|
2020-02-24 12:08:47 +03:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}),
|
2019-12-12 21:22:31 +03:00
|
|
|
|
2019-10-04 17:31:56 +03:00
|
|
|
subscriptions: computed('member.stripe', function () {
|
2019-10-03 20:42:33 +03:00
|
|
|
let subscriptions = this.member.get('stripe');
|
|
|
|
if (subscriptions && subscriptions.length > 0) {
|
2019-10-04 17:31:56 +03:00
|
|
|
return subscriptions.map((subscription) => {
|
2020-11-25 13:45:18 +03:00
|
|
|
const statusLabel = subscription.status ? subscription.status.replace('_', ' ') : '';
|
2019-10-04 17:31:56 +03:00
|
|
|
return {
|
2019-12-12 16:00:57 +03:00
|
|
|
id: subscription.id,
|
2019-10-04 17:31:56 +03:00
|
|
|
customer: subscription.customer,
|
2019-10-10 11:24:19 +03:00
|
|
|
name: subscription.name || '',
|
2019-10-04 17:31:56 +03:00
|
|
|
email: subscription.email || '',
|
|
|
|
status: subscription.status,
|
2020-10-27 12:57:31 +03:00
|
|
|
statusLabel: statusLabel,
|
2020-09-15 08:33:58 +03:00
|
|
|
startDate: subscription.start_date ? moment(subscription.start_date).format('D MMM YYYY') : '-',
|
2019-10-10 11:24:19 +03:00
|
|
|
plan: subscription.plan,
|
2020-03-04 08:31:39 +03:00
|
|
|
amount: parseInt(subscription.plan.amount) ? (subscription.plan.amount / 100) : 0,
|
2019-12-12 16:00:57 +03:00
|
|
|
cancelAtPeriodEnd: subscription.cancel_at_period_end,
|
2020-11-24 13:20:21 +03:00
|
|
|
cancellationReason: subscription.cancellation_reason,
|
2020-09-15 08:33:58 +03:00
|
|
|
validUntil: subscription.current_period_end ? moment(subscription.current_period_end).format('D MMM YYYY') : '-'
|
2019-10-07 06:47:26 +03:00
|
|
|
};
|
2019-10-04 17:31:56 +03:00
|
|
|
}).reverse();
|
2019-10-03 20:42:33 +03:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}),
|
|
|
|
|
2019-10-02 07:00:03 +03:00
|
|
|
actions: {
|
|
|
|
setProperty(property, value) {
|
|
|
|
this.setProperty(property, value);
|
2020-12-09 22:32:31 +03:00
|
|
|
},
|
|
|
|
setLabels(labels) {
|
|
|
|
this.member.set('labels', labels);
|
2019-10-02 07:00:03 +03:00
|
|
|
}
|
2020-08-21 14:35:45 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
cancelSubscription: task(function* (subscriptionId) {
|
|
|
|
let url = this.get('ghostPaths.url').api('members', this.member.get('id'), 'subscriptions', subscriptionId);
|
|
|
|
|
|
|
|
let response = yield this.ajax.put(url, {
|
|
|
|
data: {
|
|
|
|
cancel_at_period_end: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.store.pushPayload('member', response);
|
|
|
|
return response;
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
continueSubscription: task(function* (subscriptionId) {
|
|
|
|
let url = this.get('ghostPaths.url').api('members', this.member.get('id'), 'subscriptions', subscriptionId);
|
|
|
|
|
|
|
|
let response = yield this.ajax.put(url, {
|
|
|
|
data: {
|
|
|
|
cancel_at_period_end: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.store.pushPayload('member', response);
|
|
|
|
return response;
|
|
|
|
}).drop()
|
2019-10-02 07:00:03 +03:00
|
|
|
});
|