discourse-subscriptions/assets/javascripts/discourse/models/user-subscription.js.es6

57 lines
1.5 KiB
Plaintext
Raw Normal View History

2019-12-17 00:31:58 -05:00
import EmberObject from "@ember/object";
import discourseComputed from "discourse-common/utils/decorators";
2019-10-31 22:43:09 -04:00
import { ajax } from "discourse/lib/ajax";
2019-12-02 02:58:14 -05:00
import Plan from "discourse/plugins/discourse-subscriptions/discourse/models/plan";
import I18n from "I18n";
2019-10-31 22:43:09 -04:00
2019-12-17 00:31:58 -05:00
const UserSubscription = EmberObject.extend({
@discourseComputed("status")
2019-10-31 22:43:09 -04:00
canceled(status) {
return status === "canceled";
2019-11-04 00:37:21 -05:00
},
@discourseComputed("current_period_end", "canceled_at")
endDate(current_period_end, canceled_at) {
if (!canceled_at) {
return moment.unix(current_period_end).format("LL");
} else {
return I18n.t("discourse_subscriptions.user.subscriptions.cancelled");
}
},
@discourseComputed("discount")
discounted(discount) {
if (discount) {
const amount_off = discount.coupon.amount_off;
const percent_off = discount.coupon.percent_off;
if (amount_off) {
return `${parseFloat(amount_off * 0.01).toFixed(2)}`;
} else if (percent_off) {
return `${percent_off}%`;
}
} else {
return I18n.t("no_value");
}
},
2019-11-04 00:37:21 -05:00
destroy() {
return ajax(`/s/user/subscriptions/${this.id}`, {
2020-09-16 10:53:50 -04:00
method: "delete",
}).then((result) => UserSubscription.create(result));
},
2019-10-31 22:43:09 -04:00
});
UserSubscription.reopenClass({
findAll() {
2020-09-16 10:53:50 -04:00
return ajax("/s/user/subscriptions", { method: "get" }).then((result) =>
result.map((subscription) => {
2019-10-31 22:43:09 -04:00
subscription.plan = Plan.create(subscription.plan);
return UserSubscription.create(subscription);
})
);
2020-09-16 10:53:50 -04:00
},
2019-10-31 22:43:09 -04:00
});
export default UserSubscription;