Class: Shipping::InsurancePurchaseHandler
- Inherits:
-
ApplicationJob
- Object
- ActiveJob::Base
- ApplicationJob
- Shipping::InsurancePurchaseHandler
- Includes:
- RailsEventStore::AsyncHandler
- Defined in:
- app/subscribers/shipping/insurance_purchase_handler.rb
Overview
Purchases shipping insurance for qualifying deliveries after invoicing.
Subscribes to: Events::DeliveryInvoiced
WHY async: Insurance purchase costs money and calls an external service.
Failure must not roll back the invoice. Running as an independent subscriber
allows retries without re-running any other post-invoicing work.
WHY after invoicing: We only buy insurance once we have committed to the sale
(invoice created). Purchasing before invoicing would require voiding insurance
if the invoice creation failed.
Instance Method Summary collapse
Instance Method Details
#perform(event) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/subscribers/shipping/insurance_purchase_handler.rb', line 18 def perform(event) delivery = Delivery.find_by(id: event.data[:delivery_id]) return unless delivery return unless qualifies?(delivery) insurance_service = Shipping::ShippingInsurance.new result = insurance_service.process(delivery) return if result&.status == :ok ErrorReporting.error( 'Could not ship insure delivery ship labeled via heatwave', order_reference_number: delivery.order&.reference_number, delivery_id: delivery.id, status_message: result&. ) rescue StandardError => e ErrorReporting.error(e) raise end |