discourse-subscriptions/assets/javascripts/discourse/models/admin-plan.js

65 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2019-10-09 22:52:55 -04:00
import { ajax } from "discourse/lib/ajax";
2024-01-16 11:51:44 -05:00
import discourseComputed from "discourse-common/utils/decorators";
import Plan from "discourse/plugins/discourse-subscriptions/discourse/models/plan";
2019-10-09 22:52:55 -04:00
2019-10-30 20:41:01 -04:00
const AdminPlan = Plan.extend({
2019-10-23 00:55:06 -04:00
isNew: false,
2019-10-14 18:40:49 -04:00
name: "",
interval: "month",
unit_amount: 0,
2019-10-14 18:40:49 -04:00
intervals: ["day", "week", "month", "year"],
2019-10-22 20:50:54 -04:00
metadata: {},
2019-10-14 18:40:49 -04:00
@discourseComputed("trial_period_days")
parseTrialPeriodDays(trialDays) {
if (trialDays) {
return parseInt(0 + trialDays, 10);
2019-10-24 23:00:59 -04:00
} else {
2019-10-23 00:55:06 -04:00
return 0;
}
},
2019-10-14 18:40:49 -04:00
save() {
const data = {
2019-10-17 05:34:26 -04:00
nickname: this.nickname,
2019-10-14 18:40:49 -04:00
interval: this.interval,
amount: this.unit_amount,
2019-11-30 01:44:16 -05:00
currency: this.currency,
2019-10-23 00:55:06 -04:00
trial_period_days: this.parseTrialPeriodDays,
type: this.type,
2019-10-22 20:50:54 -04:00
product: this.product,
metadata: this.metadata,
2020-09-16 10:53:50 -04:00
active: this.active,
2019-10-14 18:40:49 -04:00
};
return ajax("/s/admin/plans", { method: "post", data });
2019-10-23 00:55:06 -04:00
},
update() {
const data = {
nickname: this.nickname,
trial_period_days: this.parseTrialPeriodDays,
metadata: this.metadata,
2020-09-16 10:53:50 -04:00
active: this.active,
2019-10-23 00:55:06 -04:00
};
return ajax(`/s/admin/plans/${this.id}`, { method: "patch", data });
2020-09-16 10:53:50 -04:00
},
2019-10-09 22:52:55 -04:00
});
AdminPlan.reopenClass({
2019-10-21 00:28:45 -04:00
findAll(data) {
2020-09-16 10:53:50 -04:00
return ajax("/s/admin/plans", { method: "get", data }).then((result) =>
result.map((plan) => AdminPlan.create(plan))
2019-10-09 22:52:55 -04:00
);
2019-10-17 05:34:26 -04:00
},
find(id) {
2020-09-16 10:53:50 -04:00
return ajax(`/s/admin/plans/${id}`, { method: "get" }).then((plan) =>
2019-10-17 05:34:26 -04:00
AdminPlan.create(plan)
);
2020-09-16 10:53:50 -04:00
},
2019-10-09 22:52:55 -04:00
});
export default AdminPlan;