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],
|
id: product[:id],
|
||||||
name: product[:name],
|
name: product[:name],
|
||||||
description: product[:metadata][:description]
|
description: product[:metadata][:description],
|
||||||
|
subscribed: current_user_products.include?(product[:id])
|
||||||
}
|
}
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -40,9 +40,11 @@ module DiscoursePatrons
|
||||||
group.add(current_user)
|
group.add(current_user)
|
||||||
end
|
end
|
||||||
|
|
||||||
unless DiscoursePatrons::Customer.exists?(user_id: current_user.id)
|
DiscoursePatrons::Customer.create(
|
||||||
DiscoursePatrons::Customer.create(user_id: current_user.id, customer_id: params[:customer])
|
user_id: current_user.id,
|
||||||
end
|
customer_id: params[:customer],
|
||||||
|
product_id: plan[:product]
|
||||||
|
)
|
||||||
|
|
||||||
render_json_dump @subscription
|
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,6 +4,9 @@ require 'rails_helper'
|
||||||
|
|
||||||
module DiscoursePatrons
|
module DiscoursePatrons
|
||||||
RSpec.describe ProductsController do
|
RSpec.describe ProductsController do
|
||||||
|
context "authenticated" do
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
|
||||||
let(:product) do
|
let(:product) do
|
||||||
{
|
{
|
||||||
id: "prodct_23456",
|
id: "prodct_23456",
|
||||||
|
@ -15,6 +18,10 @@ module DiscoursePatrons
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
sign_in(user)
|
||||||
|
end
|
||||||
|
|
||||||
describe "index" do
|
describe "index" do
|
||||||
it "gets products" do
|
it "gets products" do
|
||||||
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
|
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
|
||||||
|
@ -24,9 +31,28 @@ module DiscoursePatrons
|
||||||
expect(JSON.parse(response.body)).to eq([{
|
expect(JSON.parse(response.body)).to eq([{
|
||||||
"id" => "prodct_23456",
|
"id" => "prodct_23456",
|
||||||
"name" => "Very Special Product",
|
"name" => "Very Special Product",
|
||||||
"description" => "Many people listened to my phone call with the Ukrainian President while it was being made"
|
"description" => "Many people listened to my phone call with the Ukrainian President while it was being made",
|
||||||
|
"subscribed" => false
|
||||||
}])
|
}])
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe 'show' do
|
describe 'show' do
|
||||||
|
@ -37,9 +63,11 @@ module DiscoursePatrons
|
||||||
expect(JSON.parse(response.body)).to eq(
|
expect(JSON.parse(response.body)).to eq(
|
||||||
"id" => "prodct_23456",
|
"id" => "prodct_23456",
|
||||||
"name" => "Very Special Product",
|
"name" => "Very Special Product",
|
||||||
"description" => "Many people listened to my phone call with the Ukrainian President while it was being made"
|
"description" => "Many people listened to my phone call with the Ukrainian President while it was being made",
|
||||||
|
"subscribed" => false
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
|
@ -21,13 +21,20 @@ module DiscoursePatrons
|
||||||
|
|
||||||
describe "create" do
|
describe "create" do
|
||||||
it "creates a subscription" 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(
|
::Stripe::Subscription.expects(:create).with(
|
||||||
customer: 'cus_1234',
|
customer: 'cus_1234',
|
||||||
items: [ plan: 'plan_1234' ],
|
items: [ plan: 'plan_1234' ],
|
||||||
metadata: { user_id: user.id, username: user.username_lower },
|
metadata: { user_id: user.id, username: user.username_lower },
|
||||||
)
|
).returns(status: 'active')
|
||||||
|
|
||||||
|
expect {
|
||||||
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
post "/patrons/subscriptions.json", params: { plan: 'plan_1234', customer: 'cus_1234' }
|
||||||
|
}.to change { DiscoursePatrons::Customer.count }
|
||||||
end
|
end
|
||||||
|
|
||||||
it "creates a customer model" do
|
it "creates a customer model" do
|
||||||
|
|
Loading…
Reference in New Issue