destroy products
This commit is contained in:
parent
23cb6ef93e
commit
1bedc1ba2f
|
@ -19,7 +19,7 @@ module DiscoursePatrons
|
||||||
name: params[:name],
|
name: params[:name],
|
||||||
active: params[:active],
|
active: params[:active],
|
||||||
metadata: {
|
metadata: {
|
||||||
group_name: params[:groupName]
|
group_name: params[:group_name]
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,6 +29,17 @@ module DiscoursePatrons
|
||||||
return render_json_error e.message
|
return render_json_error e.message
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,14 +2,6 @@ import DiscourseURL from "discourse/lib/url";
|
||||||
|
|
||||||
export default Ember.Controller.extend({
|
export default Ember.Controller.extend({
|
||||||
actions: {
|
actions: {
|
||||||
destroyProduct(product) {
|
|
||||||
product.destroy().then(() =>
|
|
||||||
this.controllerFor("adminPluginsDiscoursePatronsProductsIndex")
|
|
||||||
.get("model")
|
|
||||||
.removeObject(product)
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
editProduct(id) {
|
editProduct(id) {
|
||||||
return DiscourseURL.redirectTo(
|
return DiscourseURL.redirectTo(
|
||||||
`/admin/plugins/discourse-patrons/products/${id}`
|
`/admin/plugins/discourse-patrons/products/${id}`
|
||||||
|
|
|
@ -5,7 +5,10 @@ export default Ember.Controller.extend({
|
||||||
createProduct() {
|
createProduct() {
|
||||||
// TODO: set default group name beforehand
|
// TODO: set default group name beforehand
|
||||||
if (this.get("model.product.groupName") === undefined) {
|
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")
|
this.get("model.product")
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
|
|
||||||
const AdminProduct = Discourse.Model.extend({
|
const AdminProduct = Discourse.Model.extend({
|
||||||
active: true,
|
destroy() {
|
||||||
|
return ajax(`/patrons/admin/products/${this.id}`, { method: "delete" });
|
||||||
destroy() {},
|
},
|
||||||
|
|
||||||
save() {
|
save() {
|
||||||
const data = {
|
const data = {
|
||||||
|
@ -17,7 +17,7 @@ const AdminProduct = Discourse.Model.extend({
|
||||||
});
|
});
|
||||||
|
|
||||||
AdminProduct.reopenClass({
|
AdminProduct.reopenClass({
|
||||||
find() {
|
findAll() {
|
||||||
return ajax("/patrons/admin/products", { method: "get" }).then(result =>
|
return ajax("/patrons/admin/products", { method: "get" }).then(result =>
|
||||||
result.map(product => AdminProduct.create(product))
|
result.map(product => AdminProduct.create(product))
|
||||||
);
|
);
|
||||||
|
|
|
@ -2,7 +2,7 @@ import AdminProduct from "discourse/plugins/discourse-patrons/discourse/models/a
|
||||||
|
|
||||||
export default Discourse.Route.extend({
|
export default Discourse.Route.extend({
|
||||||
model() {
|
model() {
|
||||||
return AdminProduct.find();
|
return AdminProduct.findAll();
|
||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
|
@ -13,9 +13,12 @@ export default Discourse.Route.extend({
|
||||||
I18n.t("yes_value"),
|
I18n.t("yes_value"),
|
||||||
confirmed => {
|
confirmed => {
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
|
product.destroy().then(() => {
|
||||||
this.controllerFor("adminPluginsDiscoursePatronsProductsIndex")
|
this.controllerFor("adminPluginsDiscoursePatronsProductsIndex")
|
||||||
.get("model")
|
.get("model")
|
||||||
.removeObject(product);
|
.removeObject(product);
|
||||||
|
})
|
||||||
|
.catch(data => bootbox.alert(data.jqXHR.responseJSON.errors.join("\n")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
{{#each model as |product|}}
|
{{#each model as |product|}}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{product.id}}</td>
|
<td>{{product.id}}</td>
|
||||||
<td>{{product.group}}</td>
|
<td>{{product.metadata.group_name}}</td>
|
||||||
<td>{{product.active}}</td>
|
<td>{{product.active}}</td>
|
||||||
<td class="td-right">
|
<td class="td-right">
|
||||||
{{d-button
|
{{d-button
|
||||||
|
|
|
@ -9,7 +9,7 @@ DiscoursePatrons::Engine.routes.draw do
|
||||||
namespace :admin do
|
namespace :admin do
|
||||||
resources :plans
|
resources :plans
|
||||||
resources :subscriptions, only: [:index]
|
resources :subscriptions, only: [:index]
|
||||||
resources :products, only: [:index, :create]
|
resources :products, only: [:index, :create, :destroy]
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :customers, only: [:create]
|
resources :customers, only: [:create]
|
||||||
|
|
|
@ -33,7 +33,7 @@ Discourse::Application.routes.append do
|
||||||
end
|
end
|
||||||
|
|
||||||
after_initialize do
|
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')
|
::Stripe.set_app_info('Discourse Patrons', version: '1.2.3', url: 'https://github.com/rimian/discourse-patrons')
|
||||||
|
|
||||||
[
|
[
|
||||||
|
|
|
@ -10,17 +10,23 @@ module DiscoursePatrons
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'unauthenticated' do
|
context 'unauthenticated' do
|
||||||
it "does nothing" do
|
it "does not list the products" do
|
||||||
::Stripe::Product.expects(:list).never
|
::Stripe::Product.expects(:list).never
|
||||||
get "/patrons/admin/products.json"
|
get "/patrons/admin/products.json"
|
||||||
expect(response.status).to eq(403)
|
expect(response.status).to eq(403)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does nothing" do
|
it "does not create the products" do
|
||||||
::Stripe::Product.expects(:create).never
|
::Stripe::Product.expects(:create).never
|
||||||
post "/patrons/admin/products.json"
|
post "/patrons/admin/products.json"
|
||||||
expect(response.status).to eq(403)
|
expect(response.status).to eq(403)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context 'authenticated' do
|
context 'authenticated' do
|
||||||
|
@ -47,15 +53,22 @@ module DiscoursePatrons
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'has an active attribute' do
|
it 'has an active attribute' do
|
||||||
::Stripe::Product.expects(:create).with(has_entry(active: false))
|
::Stripe::Product.expects(:create).with(has_entry(active: 'false'))
|
||||||
post "/patrons/admin/products.json", params: { active: false }
|
post "/patrons/admin/products.json", params: { active: 'false' }
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'has a metadata' do
|
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' }
|
post "/patrons/admin/products.json", params: { group_name: 'discourse-user-group-name' }
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue