Save deleted_by user when deleting products

This commit is contained in:
David Cook
2024-05-02 12:05:40 +10:00
parent 20b1a40fca
commit a38f22fbee
6 changed files with 20 additions and 2 deletions

View File

@@ -42,7 +42,7 @@ module Api
authorize! :delete, Spree::Product
@product = product_finder.find_product
authorize! :delete, @product
@product.destroy
@product.destroy(deleted_by: current_api_user)
render json: @product, serializer: Api::Admin::ProductSerializer, status: :no_content
end

View File

@@ -33,6 +33,7 @@ module Spree
searchable_scopes :active, :with_properties
belongs_to :supplier, class_name: 'Enterprise', optional: false, touch: true
belongs_to :deleted_by, class_name: "Spree::User", optional: true
has_one :image, class_name: "Spree::Image", as: :viewable, dependent: :destroy
@@ -260,6 +261,11 @@ module Spree
variants.map(&:import_date).compact.max
end
def destroy(deleted_by: nil)
update_attribute(:deleted_by, deleted_by) if deleted_by.present?
super()
end
def destruction
transaction do
touch_distributors

View File

@@ -26,7 +26,7 @@ class ProductsReflex < ApplicationReflex
product = product_finder(id).find_product
authorize! :delete, product
if product.destroy
if product.destroy(deleted_by: current_user)
flash[:success] = I18n.t('admin.products_v3.delete_product.success')
else
flash[:error] = I18n.t('admin.products_v3.delete_product.error')

View File

@@ -81,6 +81,7 @@ describe Api::V0::ProductsController, type: :controller do
expect(response.status).to eq(204)
expect { product.reload }.not_to raise_error
expect(product.deleted_at).not_to be_nil
expect(product.deleted_by).to eq current_api_user
end
it "is denied access to deleting another enterprises' product" do

View File

@@ -28,6 +28,12 @@ module Spree
expect(product.deleted_at).not_to be_nil
expect(product.variants.all? { |v| !v.deleted_at.nil? }).to be_truthy
end
it "should set deleted_by user if provided" do
user = create(:user)
product.destroy(deleted_by: user)
expect(product.reload.deleted_by).to eq user
end
end
end
@@ -340,6 +346,10 @@ module Spree
it "removes variants from order cycles" do
expect { product.destroy }.to change { ExchangeVariant.count }
end
#todo: it "aborts if exchange variants failed to delete"
# expect(product.deleted_at).to be_nil
# expect(product.deleted_by).to be_nil
end
it "updates units when saved change to variant unit" do

View File

@@ -29,6 +29,7 @@ describe ProductsReflex, type: :reflex, feature: :admin_style_v3 do
subject.run(action_name)
product.reload
expect(product.deleted_at).not_to be_nil
expect(product.deleted_by).to eq current_user
expect(flash[:success]).to eq('Successfully deleted the product')
end