From 3dfa261c19256d9ff02a6bd9196abc2ed217ffc2 Mon Sep 17 00:00:00 2001 From: Rimian Perkins Date: Fri, 1 Nov 2019 10:18:57 +1100 Subject: [PATCH] serialize and order the plans --- app/controllers/plans_controller.rb | 7 +++++-- .../discourse/helpers/format-currency.js.es6 | 2 -- spec/requests/plans_controller_spec.rb | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 8a72737..2680bab 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -10,8 +10,11 @@ module DiscoursePatrons begin plans = ::Stripe::Plan.list(active: true) - # TODO: Serialize. Remove some attributes like meta.group_name - render_json_dump plans.data + serialized = plans[:data].map do |plan| + plan.to_h.slice(:id, :amount, :currency, :interval) + end.sort_by { |plan| plan[:amount] } + + render_json_dump serialized rescue ::Stripe::InvalidRequestError => e return render_json_error e.message diff --git a/assets/javascripts/discourse/helpers/format-currency.js.es6 b/assets/javascripts/discourse/helpers/format-currency.js.es6 index ef67ec3..491d26c 100644 --- a/assets/javascripts/discourse/helpers/format-currency.js.es6 +++ b/assets/javascripts/discourse/helpers/format-currency.js.es6 @@ -13,7 +13,5 @@ export default Ember.Helper.helper(function(params) { currencySign = "$"; } - - return currencySign + params.map(p => p.toUpperCase()).join(' '); }); diff --git a/spec/requests/plans_controller_spec.rb b/spec/requests/plans_controller_spec.rb index 93e65e1..3a11c0c 100644 --- a/spec/requests/plans_controller_spec.rb +++ b/spec/requests/plans_controller_spec.rb @@ -9,6 +9,24 @@ module DiscoursePatrons ::Stripe::Plan.expects(:list).with(active: true) get "/patrons/plans.json" end + + it "orders and serialises the plans" do + ::Stripe::Plan.expects(:list).returns({ + data: [ + { id: 'plan_id123', amount: 1220, currency: 'aud', interval: 'year', metadata: {} }, + { id: 'plan_id234', amount: 1399, currency: 'usd', interval: 'year', metadata: {} }, + { id: 'plan_id678', amount: 1000, currency: 'aud', interval: 'week', metadata: {} } + ] + }) + + get "/patrons/plans.json" + + expect(JSON.parse(response.body)).to eq([ + { "amount" => 1000, "currency" => "aud", "id" => "plan_id678", "interval"=>"week" }, + { "amount" => 1220, "currency" => "aud", "id" => "plan_id123", "interval"=>"year" }, + { "amount" => 1399, "currency" => "usd", "id" => "plan_id234", "interval"=>"year" } + ]) + end end end end