show the product name in subscription lists

This commit is contained in:
Rimian Perkins 2019-11-27 10:48:30 +11:00
parent 3e4fd76891
commit ca00b530aa
5 changed files with 46 additions and 4 deletions

View File

@ -9,16 +9,24 @@ module DiscoursePatrons
def index def index
begin begin
plans = ::Stripe::Plan.list(
expand: ['data.product']
)
customers = ::Stripe::Customer.list( customers = ::Stripe::Customer.list(
email: current_user.email, email: current_user.email,
expand: ['data.subscriptions'] expand: ['data.subscriptions']
) )
# TODO: Serialize and remove stuff
subscriptions = customers[:data].map do |customer| subscriptions = customers[:data].map do |customer|
customer[:subscriptions][:data] customer[:subscriptions][:data]
end.flatten(1) end.flatten(1)
subscriptions.map! do |subscription|
plan = plans[:data].find { |p| p[:id] == subscription[:plan][:id] }
subscription.to_h.merge(product: plan[:product].to_h.slice(:id, :name))
end
render_json_dump subscriptions render_json_dump subscriptions
rescue ::Stripe::InvalidRequestError => e rescue ::Stripe::InvalidRequestError => e
@ -39,7 +47,7 @@ module DiscoursePatrons
deleted = ::Stripe::Subscription.delete(params[:id]) deleted = ::Stripe::Subscription.delete(params[:id])
render_json_dump deleted render_json_dump deleted
else else
render_json_error "Customer ID not found" render_json_error I18n.t('discourse_patrons.customer_id_not_found')
end end
rescue ::Stripe::InvalidRequestError => e rescue ::Stripe::InvalidRequestError => e

View File

@ -5,6 +5,7 @@
<table class="table discourse-patrons-user-table"> <table class="table discourse-patrons-user-table">
<thead> <thead>
<th>{{i18n 'discourse_patrons.user.subscriptions.id'}}</th> <th>{{i18n 'discourse_patrons.user.subscriptions.id'}}</th>
<th>{{i18n 'discourse_patrons.user.plans.product'}}</th>
<th>{{i18n 'discourse_patrons.user.plans.rate'}}</th> <th>{{i18n 'discourse_patrons.user.plans.rate'}}</th>
<th>{{i18n 'discourse_patrons.user.subscriptions.status'}}</th> <th>{{i18n 'discourse_patrons.user.subscriptions.status'}}</th>
<th>{{i18n 'discourse_patrons.user.subscriptions.created_at'}}</th> <th>{{i18n 'discourse_patrons.user.subscriptions.created_at'}}</th>
@ -13,6 +14,7 @@
{{#each model as |subscription|}} {{#each model as |subscription|}}
<tr> <tr>
<td>{{subscription.id}}</td> <td>{{subscription.id}}</td>
<td>{{subscription.product.name}}</td>
<td>{{subscription.plan.subscriptionRate}}</td> <td>{{subscription.plan.subscriptionRate}}</td>
<td>{{subscription.status}}</td> <td>{{subscription.status}}</td>
<td>{{format-unix-date subscription.created}}</td> <td>{{format-unix-date subscription.created}}</td>

View File

@ -27,6 +27,7 @@ en:
user: user:
plans: plans:
rate: Rate rate: Rate
product: Name
subscriptions_help: You have no subscriptions. subscriptions_help: You have no subscriptions.
subscriptions: subscriptions:
title: Subscriptions title: Subscriptions

View File

@ -0,0 +1,3 @@
en:
discourse_patrons:
customer_id_not_found: Customer ID not found

View File

@ -28,18 +28,40 @@ module DiscoursePatrons
end end
describe "index" do describe "index" do
let(:plans) do
{
data: [
{
id: "plan_1",
product: { name: 'ACME Subscriptions' },
},
{
id: "plan_2",
product: { name: 'ACME Other Subscriptions' },
}
]
}
end
let(:customers) do let(:customers) do
{ {
data: [{ data: [{
id: "cus_23456", id: "cus_23456",
subscriptions: { subscriptions: {
data: [{ id: "sub_1234" }, { id: "sub_4567" }] data: [
{ id: "sub_1234", plan: { id: "plan_1" } },
{ id: "sub_4567", plan: { id: "plan_2" } }
]
}, },
}] }]
} }
end end
it "gets subscriptions" do it "gets subscriptions" do
::Stripe::Plan.expects(:list).with(
expand: ['data.product']
).returns(plans)
::Stripe::Customer.expects(:list).with( ::Stripe::Customer.expects(:list).with(
email: user.email, email: user.email,
expand: ['data.subscriptions'] expand: ['data.subscriptions']
@ -47,7 +69,13 @@ module DiscoursePatrons
get "/patrons/user/subscriptions.json" get "/patrons/user/subscriptions.json"
expect(JSON.parse(response.body)).to eq([{ "id" => "sub_1234" }, { "id" => "sub_4567" }]) subscription = JSON.parse(response.body).first
expect(subscription).to eq(
"id" => "sub_1234",
"plan" => { "id" => "plan_1" },
"product" => { "name" => "ACME Subscriptions" }
)
end end
end end