user removed from the group in admin

This commit is contained in:
Rimian Perkins 2019-11-29 10:37:32 +11:00
parent 7580b68711
commit 3b7ff90672
2 changed files with 45 additions and 4 deletions

View File

@ -4,7 +4,7 @@ module DiscoursePatrons
module Admin module Admin
class SubscriptionsController < ::Admin::AdminController class SubscriptionsController < ::Admin::AdminController
include DiscoursePatrons::Stripe include DiscoursePatrons::Stripe
include DiscoursePatrons::Group
before_action :set_api_key before_action :set_api_key
def index def index
@ -22,11 +22,17 @@ module DiscoursePatrons
subscription = ::Stripe::Subscription.delete(params[:id]) subscription = ::Stripe::Subscription.delete(params[:id])
customer = DiscoursePatrons::Customer.find_by( customer = DiscoursePatrons::Customer.find_by(
product_id: subscription[:plan][:product][:id], product_id: subscription[:plan][:product],
customer_id: subscription[:customer] customer_id: subscription[:customer]
) )
customer.delete if customer if customer
customer.delete
user = ::User.find(customer.user_id)
group = plan_group(subscription[:plan])
group.remove(user) if group
end
render_json_dump subscription render_json_dump subscription

View File

@ -36,24 +36,59 @@ module DiscoursePatrons
end end
describe "destroy" do describe "destroy" do
let(:group) { Fabricate(:group, name: 'subscribers') }
before do before do
DiscoursePatrons::Customer.create( DiscoursePatrons::Customer.create(
user_id: user.id, user_id: user.id,
customer_id: 'c_123', customer_id: 'c_123',
product_id: 'pr_34578' product_id: 'pr_34578'
) )
group.add(user)
end end
it "deletes a customer" do it "deletes a customer" do
::Stripe::Subscription ::Stripe::Subscription
.expects(:delete) .expects(:delete)
.with('sub_12345') .with('sub_12345')
.returns(customer: 'c_123', plan: { product: { id: 'pr_34578' } }) .returns(
plan: { product: 'pr_34578' },
customer: 'c_123'
)
expect { expect {
delete "/patrons/admin/subscriptions/sub_12345.json" delete "/patrons/admin/subscriptions/sub_12345.json"
}.to change { DiscoursePatrons::Customer.count }.by(-1) }.to change { DiscoursePatrons::Customer.count }.by(-1)
end end
it "removes the user from the group" do
::Stripe::Subscription
.expects(:delete)
.with('sub_12345')
.returns(
plan: { product: 'pr_34578', metadata: { group_name: 'subscribers' } },
customer: 'c_123'
)
expect {
delete "/patrons/admin/subscriptions/sub_12345.json"
}.to change { user.groups.count }.by(-1)
end
it "does not remove the user from the group" do
::Stripe::Subscription
.expects(:delete)
.with('sub_12345')
.returns(
plan: { product: 'pr_34578', metadata: { group_name: 'group_does_not_exist' } },
customer: 'c_123'
)
expect {
delete "/patrons/admin/subscriptions/sub_12345.json"
}.not_to change { user.groups.count }
end
end end
end end
end end