backend returns if user is already subscribed
This commit is contained in:
parent
c7dcc18923
commit
0fddb5e3b9
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
class AddProductToCustomer < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
add_column :discourse_patrons_customers, :product_id, :string
|
||||
end
|
||||
end
|
|
@ -0,0 +1,5 @@
|
|||
class RemoveCustomerIndex < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
remove_index :discourse_patrons_customers, :customer_id
|
||||
end
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue