send-shipment-email-optionally

This commit is contained in:
binarygit
2023-11-05 14:34:58 +05:45
parent 0a5982bb8f
commit 95a51159d7
7 changed files with 47 additions and 7 deletions

View File

@@ -27,7 +27,8 @@ module Spree
go_to_state :complete
end
attr_accessor :use_billing, :checkout_processing, :save_bill_address, :save_ship_address
attr_accessor :use_billing, :checkout_processing, :save_bill_address,
:save_ship_address, :send_shipment_email
token_resource

View File

@@ -346,7 +346,7 @@ module Spree
def after_ship
inventory_units.each(&:ship!)
fee_adjustment.finalize!
send_shipped_email
send_shipped_email if order.send_shipment_email
touch :shipped_at
update_order_shipment_state
end

View File

@@ -17,6 +17,7 @@ module Admin
end
def ship
@order.send_shipment_email = true if params[:send_shipment_email]
if @order.ship
morph dom_id(@order), render(partial: "spree/admin/orders/table_row",
locals: { order: @order.reload, success: true })
@@ -83,7 +84,8 @@ module Admin
private
def authorize_order
@order = Spree::Order.find_by(id: element.dataset[:id])
id = element.dataset[:id] || params[:id]
@order = Spree::Order.find_by(id:)
authorize! :admin, @order
end

View File

@@ -1,5 +1,8 @@
%div{ "data-controller": "tooltip" }
%button{class: button_class, "data-reflex": button_reflex, "data-id": reflex_data_id, "data-tooltip-target": "element" }
- if local_assigns[:shipment]
%button{class: button_class,"data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "ship_order", "data-id": reflex_data_id, "data-tooltip-target": "element" }
- else
%button{class: button_class, "data-reflex": button_reflex, "data-id": reflex_data_id, "data-tooltip-target": "element" }
.tooltip-container
.tooltip{"data-tooltip-target": "tooltip"}
= sanitize tooltip_text

View File

@@ -47,6 +47,16 @@
%i.success.icon-ok-sign{"data-controller": "ephemeral"}
= render partial: 'admin/shared/tooltip', locals: {link_class: "icon_link with-tip icon-edit no-text" ,link: edit_admin_order_path(order), link_text: "", tooltip_text: t('spree.admin.orders.index.edit')}
- if order.ready_to_ship?
= render partial: 'admin/shared/tooltip_button', locals: {button_class: "icon-road icon_link with-tip no-text", button_reflex: "click->Admin::OrdersReflex#ship", reflex_data_id: order.id.to_s, tooltip_text: t('spree.admin.orders.index.ship')}
%form
= render ConfirmModalComponent.new(id: "ship_order", confirm_reflexes: "click->Admin::OrdersReflex#ship", controller: "orders", reflex: "Admin::Orders#ship") do
%div{class: "margin-bottom-30"}
%p This will mark the order as Shipped
%div{class: "margin-bottom-30"}
= hidden_field_tag :id, order.id
= check_box_tag :send_shipment_email
= label_tag :send_shipment_email, "Send email confirmation to customer"
= render partial: 'admin/shared/tooltip_button', locals: {button_class: "icon-road icon_link with-tip no-text", reflex_data_id: order.id.to_s, tooltip_text: t('spree.admin.orders.index.ship'), shipment: true}
- if order.payment_required? && order.pending_payments.reject(&:requires_authorization?).any?
= render partial: 'admin/shared/tooltip_button', locals: {button_class: "icon-capture icon_link no-text", button_reflex: "click->Admin::OrdersReflex#capture", reflex_data_id: order.id.to_s, tooltip_text: t('spree.admin.orders.index.capture')}

View File

@@ -353,7 +353,8 @@ describe Spree::Shipment do
expect(shipment.shipped_at).to_not be_nil
end
it "should send a shipment email" do
it "should send a shipment email if order.send_shipment_email is true" do
shipment.order.send_shipment_email = true
mail_message = double 'Mail::Message'
shipment_id = nil
expect(Spree::ShipmentMailer).to receive(:shipped_email) { |*args|

View File

@@ -746,13 +746,36 @@ describe '
expect(page).to have_current_path spree.admin_orders_path
end
it "ship order from the orders index page" do
it "ship order from the orders index page and send email" do
order.payments.first.capture!
login_as_admin
visit spree.admin_orders_path
page.find("button.icon-road").click
within ".reveal-modal" do
check 'Send email confirmation to customer'
expect {
find_button("Confirm").click
}.to enqueue_job(ActionMailer::MailDeliveryJob).exactly(:once)
end
expect(page).to have_css "i.success"
expect(order.reload.shipments.any?(&:shipped?)).to be true
expect(order.shipment_state).to eq("shipped")
end
it "ship order from the orders index page and do not send email" do
order.payments.first.capture!
login_as_admin
visit spree.admin_orders_path
page.find("button.icon-road").click
within ".reveal-modal" do
expect {
find_button("Confirm").click
}.not_to enqueue_job(ActionMailer::MailDeliveryJob)
end
expect(page).to have_css "i.success"
expect(order.reload.shipments.any?(&:shipped?)).to be true
expect(order.shipment_state).to eq("shipped")