destroy products

This commit is contained in:
Rimian Perkins 2019-10-15 23:14:04 +11:00
parent 23cb6ef93e
commit 1bedc1ba2f
9 changed files with 49 additions and 27 deletions

View File

@ -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

View File

@ -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}`

View File

@ -5,7 +5,10 @@ export default Ember.Controller.extend({
createProduct() {
// TODO: set default group name beforehand
if (this.get("model.product.groupName") === undefined) {
this.set("model.product.groupName", this.get("model.group.firstObject"));
this.set(
"model.product.groupName",
this.get("model.group.firstObject")
);
}
this.get("model.product")

View File

@ -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))
);

View File

@ -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) {
product.destroy().then(() => {
this.controllerFor("adminPluginsDiscoursePatronsProductsIndex")
.get("model")
.removeObject(product);
})
.catch(data => bootbox.alert(data.jqXHR.responseJSON.errors.join("\n")));
}
}
);

View File

@ -9,7 +9,7 @@
{{#each model as |product|}}
<tr>
<td>{{product.id}}</td>
<td>{{product.group}}</td>
<td>{{product.metadata.group_name}}</td>
<td>{{product.active}}</td>
<td class="td-right">
{{d-button

View File

@ -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]

View File

@ -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')
[

View File

@ -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