From 2b3c52bdd423b446dece3e085498252432de2422 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Fri, 1 Nov 2019 13:43:09 +1100 Subject: [PATCH] name space user subscription request --- .../user/subscriptions_controller.rb | 30 +++++++++++ .../discourse/models/user-subscription.js.es6 | 23 +++++++++ .../routes/user-subscriptions.js.es6 | 4 +- config/routes.rb | 4 ++ plugin.rb | 1 + .../requests/subscriptions_controller_spec.rb | 7 +-- .../user/subscriptions_controller_spec.rb | 50 +++++++++++++++++++ 7 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 app/controllers/user/subscriptions_controller.rb create mode 100644 assets/javascripts/discourse/models/user-subscription.js.es6 create mode 100644 spec/requests/user/subscriptions_controller_spec.rb diff --git a/app/controllers/user/subscriptions_controller.rb b/app/controllers/user/subscriptions_controller.rb new file mode 100644 index 0000000..c9a7595 --- /dev/null +++ b/app/controllers/user/subscriptions_controller.rb @@ -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 diff --git a/assets/javascripts/discourse/models/user-subscription.js.es6 b/assets/javascripts/discourse/models/user-subscription.js.es6 new file mode 100644 index 0000000..31e66d7 --- /dev/null +++ b/assets/javascripts/discourse/models/user-subscription.js.es6 @@ -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; diff --git a/assets/javascripts/discourse/routes/user-subscriptions.js.es6 b/assets/javascripts/discourse/routes/user-subscriptions.js.es6 index 7ffe763..a02059b 100644 --- a/assets/javascripts/discourse/routes/user-subscriptions.js.es6 +++ b/assets/javascripts/discourse/routes/user-subscriptions.js.es6 @@ -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) { diff --git a/config/routes.rb b/config/routes.rb index a7ccc7d..17d3ff6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/plugin.rb b/plugin.rb index ea73e8d..87f84ab 100644 --- a/plugin.rb +++ b/plugin.rb @@ -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", diff --git a/spec/requests/subscriptions_controller_spec.rb b/spec/requests/subscriptions_controller_spec.rb index 2e39e94..b66c8d8 100644 --- a/spec/requests/subscriptions_controller_spec.rb +++ b/spec/requests/subscriptions_controller_spec.rb @@ -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) diff --git a/spec/requests/user/subscriptions_controller_spec.rb b/spec/requests/user/subscriptions_controller_spec.rb new file mode 100644 index 0000000..b99aa60 --- /dev/null +++ b/spec/requests/user/subscriptions_controller_spec.rb @@ -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