2021-10-04 14:01:12 +03:00
|
|
|
import Controller from '@ember/controller';
|
2022-03-14 13:52:04 +03:00
|
|
|
import LinkOfferModal from '../components/modals/offers/link';
|
2021-10-08 06:58:17 +03:00
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
2022-02-09 13:49:38 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2021-10-04 14:55:17 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2021-10-04 14:01:12 +03:00
|
|
|
|
2021-10-13 18:53:21 +03:00
|
|
|
const TYPES = [{
|
|
|
|
name: 'Active',
|
|
|
|
value: 'active'
|
|
|
|
},{
|
|
|
|
name: 'Archived',
|
|
|
|
value: 'archived'
|
|
|
|
}];
|
|
|
|
|
2021-10-04 14:55:17 +03:00
|
|
|
export default class MembersController extends Controller {
|
2021-10-08 06:58:17 +03:00
|
|
|
@service modals;
|
2021-10-13 18:53:21 +03:00
|
|
|
@service router;
|
|
|
|
|
|
|
|
@tracked offers = [];
|
2022-05-11 20:11:54 +03:00
|
|
|
@tracked tiers = [];
|
2021-10-13 18:53:21 +03:00
|
|
|
@tracked type = 'active';
|
|
|
|
|
|
|
|
queryParams = [
|
|
|
|
'type'
|
|
|
|
];
|
2021-10-06 17:29:15 +03:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
2021-10-13 18:53:21 +03:00
|
|
|
this.availableTypes = TYPES;
|
|
|
|
}
|
|
|
|
|
|
|
|
get filteredOffers() {
|
|
|
|
return this.offers.filter((offer) => {
|
2022-05-11 20:11:54 +03:00
|
|
|
const tier = this.tiers.find((p) => {
|
2022-02-02 13:52:44 +03:00
|
|
|
return p.id === offer.tier.id;
|
|
|
|
});
|
2022-05-11 20:11:54 +03:00
|
|
|
const price = offer.cadence === 'month' ? tier.monthlyPrice : tier.yearlyPrice;
|
2022-02-02 13:52:44 +03:00
|
|
|
return offer.status === this.type && !!price;
|
2021-10-20 14:49:16 +03:00
|
|
|
}).map((offer) => {
|
2022-05-11 20:11:54 +03:00
|
|
|
const tier = this.tiers.find((p) => {
|
2021-10-20 14:49:16 +03:00
|
|
|
return p.id === offer.tier.id;
|
|
|
|
});
|
2022-05-11 20:11:54 +03:00
|
|
|
const price = offer.cadence === 'month' ? tier.monthlyPrice : tier.yearlyPrice;
|
2022-05-16 21:51:49 +03:00
|
|
|
offer.finalCurrency = offer.currency || tier.currency;
|
|
|
|
offer.originalPrice = price;
|
2022-08-10 13:32:34 +03:00
|
|
|
if (offer.type !== 'trial') {
|
|
|
|
offer.updatedPrice = offer.type === 'fixed' ? (price - offer.amount) : (price - ((price * offer.amount) / 100));
|
|
|
|
} else {
|
|
|
|
offer.updatedPrice = offer.originalPrice;
|
|
|
|
}
|
2021-10-20 14:49:16 +03:00
|
|
|
return offer;
|
2021-10-13 18:53:21 +03:00
|
|
|
});
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
get offersExist() {
|
|
|
|
return this.offers.length > 0;
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:53:21 +03:00
|
|
|
get selectedType() {
|
|
|
|
return this.type ? TYPES.find((d) => {
|
|
|
|
return this.type === d.value;
|
|
|
|
}) : TYPES[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
onTypeChange(type) {
|
|
|
|
this.type = type.value;
|
|
|
|
}
|
|
|
|
|
2021-10-08 06:58:17 +03:00
|
|
|
@action
|
|
|
|
openLinkDialog(offer) {
|
2022-03-14 13:52:04 +03:00
|
|
|
this.advancedModal = this.modals.open(LinkOfferModal, {
|
2021-10-08 06:58:17 +03:00
|
|
|
offer: offer
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:29:15 +03:00
|
|
|
@task({restartable: true})
|
|
|
|
*fetchOffersTask() {
|
2022-05-11 20:11:54 +03:00
|
|
|
this.tiers = yield this.store.query('tier', {
|
2022-01-17 21:53:43 +03:00
|
|
|
filter: 'type:paid', include: 'monthly_price,yearly_price'
|
|
|
|
});
|
2021-10-06 17:29:15 +03:00
|
|
|
this.offers = yield this.store.query('offer', {limit: 'all'});
|
2021-10-20 14:49:16 +03:00
|
|
|
return this.offers;
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|
|
|
|
}
|