name space user subscription request
This commit is contained in:
parent
e3ac6f7dac
commit
2b3c52bdd4
|
@ -0,0 +1,30 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module DiscoursePatrons
|
||||
module User
|
||||
class SubscriptionsController < ::ApplicationController
|
||||
include DiscoursePatrons::Stripe
|
||||
before_action :set_api_key
|
||||
requires_login
|
||||
|
||||
def index
|
||||
begin
|
||||
customers = ::Stripe::Customer.list(
|
||||
email: current_user.email,
|
||||
expand: ['data.subscriptions']
|
||||
)
|
||||
|
||||
# TODO: Serialize and remove stuff
|
||||
subscriptions = customers[:data].map do |customer|
|
||||
customer[:subscriptions][:data]
|
||||
end.flatten(1)
|
||||
|
||||
render_json_dump subscriptions
|
||||
|
||||
rescue ::Stripe::InvalidRequestError => e
|
||||
return render_json_error e.message
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,23 @@
|
|||
import computed from "ember-addons/ember-computed-decorators";
|
||||
import { ajax } from "discourse/lib/ajax";
|
||||
import Plan from "discourse/plugins/discourse-patrons/discourse/models/plan";
|
||||
|
||||
const UserSubscription = Discourse.Model.extend({
|
||||
@computed("status")
|
||||
canceled(status) {
|
||||
return status === "canceled";
|
||||
}
|
||||
});
|
||||
|
||||
UserSubscription.reopenClass({
|
||||
findAll() {
|
||||
return ajax("/patrons/user/subscriptions", { method: "get" }).then(result =>
|
||||
result.map(subscription => {
|
||||
subscription.plan = Plan.create(subscription.plan);
|
||||
return UserSubscription.create(subscription);
|
||||
})
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
export default UserSubscription;
|
|
@ -1,8 +1,8 @@
|
|||
import Subscription from "discourse/plugins/discourse-patrons/discourse/models/subscription";
|
||||
import UserSubscription from "discourse/plugins/discourse-patrons/discourse/models/user-subscription";
|
||||
|
||||
export default Discourse.Route.extend({
|
||||
model() {
|
||||
return Subscription.findAll();
|
||||
return UserSubscription.findAll();
|
||||
},
|
||||
|
||||
setupController(controller, model) {
|
||||
|
|
|
@ -12,6 +12,10 @@ DiscoursePatrons::Engine.routes.draw do
|
|||
resources :products
|
||||
end
|
||||
|
||||
namespace :user do
|
||||
resources :subscriptions, only: [:index]
|
||||
end
|
||||
|
||||
resources :customers, only: [:create]
|
||||
resources :invoices, only: [:index]
|
||||
resources :patrons, only: [:index, :create]
|
||||
|
|
|
@ -51,6 +51,7 @@ after_initialize do
|
|||
"../app/controllers/admin/plans_controller",
|
||||
"../app/controllers/admin/products_controller",
|
||||
"../app/controllers/admin/subscriptions_controller",
|
||||
"../app/controllers/user/subscriptions_controller",
|
||||
"../app/controllers/customers_controller",
|
||||
"../app/controllers/invoices_controller",
|
||||
"../app/controllers/patrons_controller",
|
||||
|
|
|
@ -5,11 +5,6 @@ require 'rails_helper'
|
|||
module DiscoursePatrons
|
||||
RSpec.describe SubscriptionsController do
|
||||
context "not authenticated" do
|
||||
it "does not get the subscriptions" do
|
||||
::Stripe::Customer.expects(:list).never
|
||||
get "/patrons/subscriptions.json"
|
||||
end
|
||||
|
||||
it "does not create a subscription" do
|
||||
::Stripe::Plan.expects(:retrieve).never
|
||||
::Stripe::Subscription.expects(:create).never
|
||||
|
@ -23,7 +18,7 @@ module DiscoursePatrons
|
|||
end
|
||||
|
||||
context "authenticated" do
|
||||
let(:user) { Fabricate(:user, email: 'hello.2@example.com') }
|
||||
let(:user) { Fabricate(:user) }
|
||||
|
||||
before do
|
||||
sign_in(user)
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
module DiscoursePatrons
|
||||
RSpec.describe User::SubscriptionsController do
|
||||
it 'is a subclass of ApplicationController' do
|
||||
expect(DiscoursePatrons::User::SubscriptionsController < ::ApplicationController).to eq(true)
|
||||
end
|
||||
|
||||
context "not authenticated" do
|
||||
it "does not get the subscriptions" do
|
||||
::Stripe::Customer.expects(:list).never
|
||||
get "/patrons/user/subscriptions.json"
|
||||
end
|
||||
end
|
||||
|
||||
context "authenticated" do
|
||||
let(:user) { Fabricate(:user, email: 'beanie@example.com') }
|
||||
|
||||
before do
|
||||
sign_in(user)
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
let(:customers) do
|
||||
{
|
||||
data: [{
|
||||
id: "cus_23456",
|
||||
subscriptions: {
|
||||
data: [{ id: "sub_1234" }, { id: "sub_4567" }]
|
||||
},
|
||||
}]
|
||||
}
|
||||
end
|
||||
|
||||
it "gets subscriptions" do
|
||||
::Stripe::Customer.expects(:list).with(
|
||||
email: user.email,
|
||||
expand: ['data.subscriptions']
|
||||
).returns(customers)
|
||||
|
||||
get "/patrons/user/subscriptions.json"
|
||||
|
||||
expect(JSON.parse(response.body)).to eq([{ "id" => "sub_1234" }, { "id" => "sub_4567" }])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue