2019-12-17 00:31:58 -05:00
|
|
|
import EmberObject from "@ember/object";
|
2020-05-28 11:32:57 -04:00
|
|
|
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";
|
2020-08-19 16:08:38 -04:00
|
|
|
import I18n from "I18n";
|
2019-10-31 22:43:09 -04:00
|
|
|
|
2019-12-17 00:31:58 -05:00
|
|
|
const UserSubscription = EmberObject.extend({
|
2020-05-28 11:32:57 -04:00
|
|
|
@discourseComputed("status")
|
2019-10-31 22:43:09 -04:00
|
|
|
canceled(status) {
|
|
|
|
return status === "canceled";
|
2019-11-04 00:37:21 -05:00
|
|
|
},
|
|
|
|
|
2020-08-19 16:08:38 -04: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");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-01-07 16:25:44 -05:00
|
|
|
@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() {
|
2019-12-02 18:29:44 -05:00
|
|
|
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;
|