From 0fddb5e3b98a939ad99fcc2b62da5bbda982d4e4 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Thu, 28 Nov 2019 17:43:03 +1100 Subject: [PATCH] backend returns if user is already subscribed --- app/controllers/products_controller.rb | 10 ++- app/controllers/subscriptions_controller.rb | 8 +- .../20191127233735_add_product_to_customer.rb | 5 ++ .../20191128044206_remove_customer_index.rb | 5 ++ spec/requests/products_controller_spec.rb | 90 ++++++++++++------- .../requests/subscriptions_controller_spec.rb | 13 ++- 6 files changed, 93 insertions(+), 38 deletions(-) create mode 100644 db/migrate/20191127233735_add_product_to_customer.rb create mode 100644 db/migrate/20191128044206_remove_customer_index.rb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index a943996..8ab971f 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -38,8 +38,16 @@ module DiscoursePatrons { id: product[:id], name: product[:name], - description: product[:metadata][:description] + description: product[:metadata][:description], + subscribed: current_user_products.include?(product[:id]) } end + + def current_user_products + ::DiscoursePatrons::Customer + .select(:product_id) + .where(user_id: current_user.id) + .map { |c| c.product_id }.compact + end end end diff --git a/app/controllers/subscriptions_controller.rb b/app/controllers/subscriptions_controller.rb index 5a647b4..35857e1 100644 --- a/app/controllers/subscriptions_controller.rb +++ b/app/controllers/subscriptions_controller.rb @@ -40,9 +40,11 @@ module DiscoursePatrons group.add(current_user) end - unless DiscoursePatrons::Customer.exists?(user_id: current_user.id) - DiscoursePatrons::Customer.create(user_id: current_user.id, customer_id: params[:customer]) - end + DiscoursePatrons::Customer.create( + user_id: current_user.id, + customer_id: params[:customer], + product_id: plan[:product] + ) render_json_dump @subscription diff --git a/db/migrate/20191127233735_add_product_to_customer.rb b/db/migrate/20191127233735_add_product_to_customer.rb new file mode 100644 index 0000000..b4932e5 --- /dev/null +++ b/db/migrate/20191127233735_add_product_to_customer.rb @@ -0,0 +1,5 @@ +class AddProductToCustomer < ActiveRecord::Migration[6.0] + def change + add_column :discourse_patrons_customers, :product_id, :string + end +end diff --git a/db/migrate/20191128044206_remove_customer_index.rb b/db/migrate/20191128044206_remove_customer_index.rb new file mode 100644 index 0000000..b40cfe4 --- /dev/null +++ b/db/migrate/20191128044206_remove_customer_index.rb @@ -0,0 +1,5 @@ +class RemoveCustomerIndex < ActiveRecord::Migration[6.0] + def change + remove_index :discourse_patrons_customers, :customer_id + end +end diff --git a/spec/requests/products_controller_spec.rb b/spec/requests/products_controller_spec.rb index 3e77075..9170ab6 100644 --- a/spec/requests/products_controller_spec.rb +++ b/spec/requests/products_controller_spec.rb @@ -4,41 +4,69 @@ require 'rails_helper' module DiscoursePatrons RSpec.describe ProductsController do - let(:product) do - { - id: "prodct_23456", - name: "Very Special Product", - metadata: { - description: "Many people listened to my phone call with the Ukrainian President while it was being made" - }, - otherstuff: true, - } - end + context "authenticated" do + let(:user) { Fabricate(:user) } - describe "index" do - it "gets products" do - ::Stripe::Product.expects(:list).with(active: true).returns(data: [product]) - - get "/patrons/products.json" - - expect(JSON.parse(response.body)).to eq([{ - "id" => "prodct_23456", - "name" => "Very Special Product", - "description" => "Many people listened to my phone call with the Ukrainian President while it was being made" - }]) + let(:product) do + { + id: "prodct_23456", + name: "Very Special Product", + metadata: { + description: "Many people listened to my phone call with the Ukrainian President while it was being made" + }, + otherstuff: true, + } end - end - describe 'show' do - it 'retrieves the product' do - ::Stripe::Product.expects(:retrieve).with('prod_walterwhite').returns(product) - get "/patrons/products/prod_walterwhite.json" + before do + sign_in(user) + end - expect(JSON.parse(response.body)).to eq( - "id" => "prodct_23456", - "name" => "Very Special Product", - "description" => "Many people listened to my phone call with the Ukrainian President while it was being made" - ) + describe "index" do + it "gets products" do + ::Stripe::Product.expects(:list).with(active: true).returns(data: [product]) + + get "/patrons/products.json" + + expect(JSON.parse(response.body)).to eq([{ + "id" => "prodct_23456", + "name" => "Very Special Product", + "description" => "Many people listened to my phone call with the Ukrainian President while it was being made", + "subscribed" => false + }]) + end + + it "is subscribed" do + ::DiscoursePatrons::Customer.create(product_id: product[:id], user_id: user.id, customer_id: 'x') + ::Stripe::Product.expects(:list).with(active: true).returns(data: [product]) + + get "/patrons/products.json" + data = JSON.parse(response.body) + expect(data.first["subscribed"]).to eq true + end + + it "is not subscribed" do + ::DiscoursePatrons::Customer.delete_all + ::Stripe::Product.expects(:list).with(active: true).returns(data: [product]) + + get "/patrons/products.json" + data = JSON.parse(response.body) + expect(data.first["subscribed"]).to eq false + end + end + + describe 'show' do + it 'retrieves the product' do + ::Stripe::Product.expects(:retrieve).with('prod_walterwhite').returns(product) + get "/patrons/products/prod_walterwhite.json" + + expect(JSON.parse(response.body)).to eq( + "id" => "prodct_23456", + "name" => "Very Special Product", + "description" => "Many people listened to my phone call with the Ukrainian President while it was being made", + "subscribed" => false + ) + end end end end diff --git a/spec/requests/subscriptions_controller_spec.rb b/spec/requests/subscriptions_controller_spec.rb index 4429210..e2f7b8c 100644 --- a/spec/requests/subscriptions_controller_spec.rb +++ b/spec/requests/subscriptions_controller_spec.rb @@ -21,13 +21,20 @@ module DiscoursePatrons describe "create" do it "creates a subscription" do - ::Stripe::Plan.expects(:retrieve).returns(metadata: { group_name: 'awesome' }) + ::Stripe::Plan.expects(:retrieve).returns( + product: 'product_12345', + metadata: { group_name: 'awesome' } + ) + ::Stripe::Subscription.expects(:create).with( customer: 'cus_1234', items: [ plan: 'plan_1234' ], metadata: { user_id: user.id, username: user.username_lower }, - ) - post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' } + ).returns(status: 'active') + + expect { + post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' } + }.to change { DiscoursePatrons::Customer.count } end it "creates a customer model" do