Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Let a wrong label be removed after the parcel has shipped

Damian Legawiec0a1d51aed574
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders/labels_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders/labels_controller.rb
index d51022eb60..72a2a198dc 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders/labels_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders/labels_controller.rb
@@ -79,19 +79,11 @@ module Spree
end
with_order_lock do
- # The label's own consignment goes with it, but only while the
- # parcel never moved: a journey that happened is a fact, and
- # Deliveries::Destroy is what knows that and rolls the
- # fulfillment up afterwards.
- delivery = @resource.delivery
- if delivery && [email protected]_unmoved_delivery
- return render_error(
- code: ERROR_CODES[:validation_error],
- message: Spree.t('shipping_labels.errors.delivery_has_moved'),
- status: :unprocessable_content
- )
- end
-
+ # The consignment goes with the label only while the parcel
+ # never moved — a journey that happened is a fact. Once it has,
+ # the delivery stays and is simply unlinked, so a wrong PDF can
+ # still be removed and replaced.
+ @resource.release_unmoved_delivery
@resource.destroy!
head :no_content
end
diff --git a/spree/api/app/controllers/spree/api/v3/seller/orders/labels_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/orders/labels_controller.rb
index 6bf71841bf..b940e19415 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/orders/labels_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/orders/labels_controller.rb
@@ -87,16 +87,10 @@ module Spree
end
with_order_lock do
- # The label's own consignment goes with it, but only while the
- # parcel never moved: a journey that happened is a fact.
- if @label.delivery && [email protected]_unmoved_delivery
- return render_error(
- code: ERROR_CODES[:validation_error],
- message: Spree.t('shipping_labels.errors.delivery_has_moved'),
- status: :unprocessable_content
- )
- end
-
+ # The consignment goes with the label only while the parcel
+ # never moved — a journey that happened is a fact. Once it has,
+ # the delivery stays and is simply unlinked.
+ @label.release_unmoved_delivery
@label.destroy!
head :no_content
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders/labels_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders/labels_controller_spec.rb
index 2d4462f994..6b14756398 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/orders/labels_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders/labels_controller_spec.rb
@@ -148,9 +148,9 @@ RSpec.describe Spree::Api::V3::Admin::Orders::LabelsController, type: :controlle
expect(response).to have_http_status(:unprocessable_content)
end
- # A journey that happened is a fact: deleting the label a parcel shipped
- # under must not erase where that parcel went.
- it 'keeps the consignment once the parcel has shipped' do
+ # A journey that happened is a fact, so the consignment survives — but the
+ # wrong PDF still has to be removable, or a merchant is stuck with it.
+ it 'keeps the consignment once the parcel has shipped, and still deletes the label' do
label = create(:shipping_label, :uploaded, :with_delivery, owner: fulfillment)
fulfillment.update!(status: 'fulfilled', fulfilled_at: Time.current)
@@ -158,8 +158,8 @@ RSpec.describe Spree::Api::V3::Admin::Orders::LabelsController, type: :controlle
order_id: order.prefixed_id, fulfillment_id: fulfillment.prefixed_id, id: label.prefixed_id
}, as: :json
- expect(response).to have_http_status(:unprocessable_content)
- expect(Spree::ShippingLabel.exists?(label.id)).to be(true)
+ expect(response).to have_http_status(:no_content)
+ expect(Spree::ShippingLabel.exists?(label.id)).to be(false)
expect(fulfillment.reload.deliveries).to be_present
end
end
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index f2500b0f1b..992e807492 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -1730,7 +1730,6 @@ en:
shipping_labels:
errors:
already_purchased: This parcel already has an active shipping label
- delivery_has_moved: This label's parcel is already on its way, so its tracking cannot be removed with the label.
file_fetch_failed: The label file could not be fetched from the carrier (HTTP %{status})
file_required: An uploaded label needs a file
no_file_url: The carrier did not provide a file for this label