REFACTOR: Simplify controller setup (#24)

The code in the plugin needed a dramatic cleanup. This refactor collapses the Plan/Product/Subscription controllers on the backend into one new controller: `SubscribeController`.

This reduces N+1 calls to the back end during the subscription process and simplifies use of the code. 

I've also removed a bunch of dead code and refactored some logic into methods for easier readability. No feature/functionality changes in this commit; only refactoring. However, refactoring will allow for implementation of better anonymous user handling, so this is largely a foundation to enable making that change.
This commit is contained in:
Justin DiRose
2020-10-21 13:36:31 -05:00
committed by GitHub
parent 3428429d77
commit 3a5078ded6
20 changed files with 389 additions and 582 deletions
@@ -1,5 +1,4 @@
import Controller from "@ember/controller";
import Customer from "discourse/plugins/discourse-subscriptions/discourse/models/customer";
import Subscription from "discourse/plugins/discourse-subscriptions/discourse/models/subscription";
import Transaction from "discourse/plugins/discourse-subscriptions/discourse/models/transaction";
import I18n from "I18n";
@@ -28,16 +27,12 @@ export default Controller.extend({
this.set("loading", false);
return result;
} else {
const customer = Customer.create({ source: result.token.id });
return customer.save().then((c) => {
const subscription = Subscription.create({
customer: c.id,
plan: plan.get("id"),
});
return subscription.save();
const subscription = Subscription.create({
source: result.token.id,
plan: plan.get("id"),
});
return subscription.save();
}
});
},
@@ -1,14 +0,0 @@
import { ajax } from "discourse/lib/ajax";
import EmberObject from "@ember/object";
const Customer = EmberObject.extend({
save() {
const data = {
source: this.source,
};
return ajax("/s/customers", { method: "post", data });
},
});
export default Customer;
@@ -1,19 +0,0 @@
import { ajax } from "discourse/lib/ajax";
import EmberObject from "@ember/object";
const Group = EmberObject.extend({});
Group.reopenClass({
subscriptionGroup:
Discourse.SiteSettings.discourse_patrons_subscription_group,
find() {
return ajax(`/groups/${this.subscriptionGroup}`, { method: "get" }).then(
(result) => {
return Group.create(result.group);
}
);
},
});
export default Group;
@@ -1,6 +1,5 @@
import EmberObject from "@ember/object";
import discourseComputed from "discourse-common/utils/decorators";
import { ajax } from "discourse/lib/ajax";
const Plan = EmberObject.extend({
amountDollars: Ember.computed("unit_amount", {
@@ -24,12 +23,4 @@ const Plan = EmberObject.extend({
},
});
Plan.reopenClass({
findAll(data) {
return ajax("/s/plans", { method: "get", data }).then((result) =>
result.map((plan) => Plan.create(plan))
);
},
});
export default Plan;
@@ -5,16 +5,10 @@ const Product = EmberObject.extend({});
Product.reopenClass({
findAll() {
return ajax("/s/products", { method: "get" }).then((result) =>
return ajax("/s", { method: "get" }).then((result) =>
result.map((product) => Product.create(product))
);
},
find(id) {
return ajax(`/s/products/${id}`, { method: "get" }).then((product) =>
Product.create(product)
);
},
});
export default Product;
@@ -10,19 +10,17 @@ const Subscription = EmberObject.extend({
save() {
const data = {
customer: this.customer,
source: this.source,
plan: this.plan,
};
return ajax("/s/subscriptions", { method: "post", data });
return ajax("/s/create", { method: "post", data });
},
});
Subscription.reopenClass({
findAll() {
return ajax("/s/subscriptions", { method: "get" }).then((result) =>
result.map((subscription) => Subscription.create(subscription))
);
show(id) {
return ajax(`/s/${id}`, { method: "get" });
},
});
@@ -7,6 +7,6 @@ export default {
plan: plan,
};
return ajax("/s/subscriptions/finalize", { method: "post", data });
return ajax("/s/finalize", { method: "post", data });
},
};
@@ -1,23 +0,0 @@
import Route from "@ember/routing/route";
import { ajax } from "discourse/lib/ajax";
export default Route.extend({
queryParams: {
order: {
refreshModel: true,
},
descending: {
refreshModel: true,
},
},
model(params) {
return ajax("/patrons/admin", {
method: "get",
data: {
order: params.order,
descending: params.descending,
},
}).then((results) => results);
},
});
@@ -1,15 +1,19 @@
import Route from "@ember/routing/route";
import Product from "discourse/plugins/discourse-subscriptions/discourse/models/product";
import Plan from "discourse/plugins/discourse-subscriptions/discourse/models/plan";
import { hash } from "rsvp";
import Subscription from "discourse/plugins/discourse-subscriptions/discourse/models/subscription";
export default Route.extend({
model(params) {
const product_id = params["subscription-id"];
const product = Product.find(product_id);
const plans = Plan.findAll({ product_id });
return Subscription.show(product_id).then((result) => {
result.product = Product.create(result.product);
result.plans = result.plans.map((plan) => {
return Plan.create(plan);
});
return hash({ plans, product });
return result;
});
},
});