diff --git a/app/controllers/admin/products_controller.rb b/app/controllers/admin/products_controller.rb index 0cdde2f..fb7f5d4 100644 --- a/app/controllers/admin/products_controller.rb +++ b/app/controllers/admin/products_controller.rb @@ -19,7 +19,7 @@ module DiscoursePatrons name: params[:name], active: params[:active], metadata: { - group_name: params[:groupName] + group_name: params[:group_name] } ) @@ -29,6 +29,17 @@ module DiscoursePatrons return render_json_error e.message end end + + def destroy + begin + product = ::Stripe::Product.delete(params[:id]) + + render_json_dump product + + rescue ::Stripe::InvalidRequestError => e + return render_json_error e.message + end + end end end end diff --git a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-index.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-index.js.es6 index e240694..6f1cd71 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-index.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-index.js.es6 @@ -2,14 +2,6 @@ import DiscourseURL from "discourse/lib/url"; export default Ember.Controller.extend({ actions: { - destroyProduct(product) { - product.destroy().then(() => - this.controllerFor("adminPluginsDiscoursePatronsProductsIndex") - .get("model") - .removeObject(product) - ); - }, - editProduct(id) { return DiscourseURL.redirectTo( `/admin/plugins/discourse-patrons/products/${id}` diff --git a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-show.js.es6 b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-show.js.es6 index 165c325..7533876 100644 --- a/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-plugins-discourse-patrons-products-show.js.es6 @@ -4,8 +4,11 @@ export default Ember.Controller.extend({ actions: { createProduct() { // TODO: set default group name beforehand - if(this.get("model.product.groupName") === undefined) { - this.set("model.product.groupName", this.get("model.group.firstObject")); + if (this.get("model.product.groupName") === undefined) { + this.set( + "model.product.groupName", + this.get("model.group.firstObject") + ); } this.get("model.product") diff --git a/assets/javascripts/discourse/models/admin-product.js.es6 b/assets/javascripts/discourse/models/admin-product.js.es6 index 82a904b..c875c43 100644 --- a/assets/javascripts/discourse/models/admin-product.js.es6 +++ b/assets/javascripts/discourse/models/admin-product.js.es6 @@ -1,9 +1,9 @@ import { ajax } from "discourse/lib/ajax"; const AdminProduct = Discourse.Model.extend({ - active: true, - - destroy() {}, + destroy() { + return ajax(`/patrons/admin/products/${this.id}`, { method: "delete" }); + }, save() { const data = { @@ -17,7 +17,7 @@ const AdminProduct = Discourse.Model.extend({ }); AdminProduct.reopenClass({ - find() { + findAll() { return ajax("/patrons/admin/products", { method: "get" }).then(result => result.map(product => AdminProduct.create(product)) ); diff --git a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-products-index.js.es6 b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-products-index.js.es6 index 33f20eb..b814351 100644 --- a/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-products-index.js.es6 +++ b/assets/javascripts/discourse/routes/admin-plugins-discourse-patrons-products-index.js.es6 @@ -2,7 +2,7 @@ import AdminProduct from "discourse/plugins/discourse-patrons/discourse/models/a export default Discourse.Route.extend({ model() { - return AdminProduct.find(); + return AdminProduct.findAll(); }, actions: { @@ -13,9 +13,12 @@ export default Discourse.Route.extend({ I18n.t("yes_value"), confirmed => { if (confirmed) { - this.controllerFor("adminPluginsDiscoursePatronsProductsIndex") - .get("model") - .removeObject(product); + product.destroy().then(() => { + this.controllerFor("adminPluginsDiscoursePatronsProductsIndex") + .get("model") + .removeObject(product); + }) + .catch(data => bootbox.alert(data.jqXHR.responseJSON.errors.join("\n"))); } } ); diff --git a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs index 9279e76..fcf34aa 100644 --- a/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs +++ b/assets/javascripts/discourse/templates/admin/plugins-discourse-patrons-products-index.hbs @@ -9,7 +9,7 @@ {{#each model as |product|}} {{product.id}} - {{product.group}} + {{product.metadata.group_name}} {{product.active}} {{d-button diff --git a/config/routes.rb b/config/routes.rb index 9485ccc..29407a6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,7 +9,7 @@ DiscoursePatrons::Engine.routes.draw do namespace :admin do resources :plans resources :subscriptions, only: [:index] - resources :products, only: [:index, :create] + resources :products, only: [:index, :create, :destroy] end resources :customers, only: [:create] diff --git a/plugin.rb b/plugin.rb index a2007f8..97731f7 100644 --- a/plugin.rb +++ b/plugin.rb @@ -33,7 +33,7 @@ Discourse::Application.routes.append do end after_initialize do - ::Stripe.api_version = "2019-08-14" + ::Stripe.api_version = "2019-10-08" ::Stripe.set_app_info('Discourse Patrons', version: '1.2.3', url: 'https://github.com/rimian/discourse-patrons') [ diff --git a/spec/requests/admin/products_controller_spec.rb b/spec/requests/admin/products_controller_spec.rb index cebf5d0..00a4d03 100644 --- a/spec/requests/admin/products_controller_spec.rb +++ b/spec/requests/admin/products_controller_spec.rb @@ -10,17 +10,23 @@ module DiscoursePatrons end context 'unauthenticated' do - it "does nothing" do + it "does not list the products" do ::Stripe::Product.expects(:list).never get "/patrons/admin/products.json" expect(response.status).to eq(403) end - it "does nothing" do + it "does not create the products" do ::Stripe::Product.expects(:create).never post "/patrons/admin/products.json" expect(response.status).to eq(403) end + + it "does not delete the products" do + ::Stripe::Product.expects(:delete).never + delete "/patrons/admin/products/u2.json" + expect(response.status).to eq(403) + end end context 'authenticated' do @@ -47,15 +53,22 @@ module DiscoursePatrons end it 'has an active attribute' do - ::Stripe::Product.expects(:create).with(has_entry(active: false)) - post "/patrons/admin/products.json", params: { active: false } + ::Stripe::Product.expects(:create).with(has_entry(active: 'false')) + post "/patrons/admin/products.json", params: { active: 'false' } end it 'has a metadata' do - ::Stripe::Product.expects(:create).with(has_entry(:metadata, { group_name: 'discourse-user-group-name' })) + ::Stripe::Product.expects(:create).with(has_entry(metadata: { group_name: 'discourse-user-group-name' })) post "/patrons/admin/products.json", params: { group_name: 'discourse-user-group-name' } end end + + describe 'delete' do + it 'deletes the product' do + ::Stripe::Product.expects(:delete).with('prod_walterwhite') + delete "/patrons/admin/products/prod_walterwhite.json" + end + end end end end