mirror of
https://github.com/discourse/discourse-subscriptions.git
synced 2025-02-16 16:34:43 +00:00
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
|
||||
|
||||
|
5
db/migrate/20191127233735_add_product_to_customer.rb
Normal file
5
db/migrate/20191127233735_add_product_to_customer.rb
Normal file
@ -0,0 +1,5 @@
|
||||
class AddProductToCustomer < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
add_column :discourse_patrons_customers, :product_id, :string
|
||||
end
|
||||
end
|
5
db/migrate/20191128044206_remove_customer_index.rb
Normal file
5
db/migrate/20191128044206_remove_customer_index.rb
Normal file
@ -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
|
||||
RSpec.describe ProductsController do
|
||||
context "authenticated" do
|
||||
let(:user) { Fabricate(:user) }
|
||||
|
||||
let(:product) do
|
||||
{
|
||||
id: "prodct_23456",
|
||||
@ -15,6 +18,10 @@ module DiscoursePatrons
|
||||
}
|
||||
end
|
||||
|
||||
before do
|
||||
sign_in(user)
|
||||
end
|
||||
|
||||
describe "index" do
|
||||
it "gets products" do
|
||||
::Stripe::Product.expects(:list).with(active: true).returns(data: [product])
|
||||
@ -24,9 +31,28 @@ module DiscoursePatrons
|
||||
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"
|
||||
"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
|
||||
@ -37,9 +63,11 @@ module DiscoursePatrons
|
||||
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"
|
||||
"description" => "Many people listened to my phone call with the Ukrainian President while it was being made",
|
||||
"subscribed" => false
|
||||
)
|
||||
end
|
||||
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 },
|
||||
)
|
||||
).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…
x
Reference in New Issue
Block a user