From 95a51159d7305653589cba59e76cbe5693c3b9de Mon Sep 17 00:00:00 2001 From: binarygit Date: Sun, 5 Nov 2023 14:34:58 +0545 Subject: [PATCH] send-shipment-email-optionally --- app/models/spree/order.rb | 3 ++- app/models/spree/shipment.rb | 2 +- app/reflexes/admin/orders_reflex.rb | 4 ++- .../admin/shared/_tooltip_button.html.haml | 5 +++- .../spree/admin/orders/_table_row.html.haml | 12 ++++++++- spec/models/spree/shipment_spec.rb | 3 ++- spec/system/admin/orders_spec.rb | 25 ++++++++++++++++++- 7 files changed, 47 insertions(+), 7 deletions(-) diff --git a/app/models/spree/order.rb b/app/models/spree/order.rb index 238e935e5d..f4dc90b575 100644 --- a/app/models/spree/order.rb +++ b/app/models/spree/order.rb @@ -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 diff --git a/app/models/spree/shipment.rb b/app/models/spree/shipment.rb index 476658b13a..0b060c727c 100644 --- a/app/models/spree/shipment.rb +++ b/app/models/spree/shipment.rb @@ -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 diff --git a/app/reflexes/admin/orders_reflex.rb b/app/reflexes/admin/orders_reflex.rb index 765438d7cc..540ea892e4 100644 --- a/app/reflexes/admin/orders_reflex.rb +++ b/app/reflexes/admin/orders_reflex.rb @@ -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 diff --git a/app/views/admin/shared/_tooltip_button.html.haml b/app/views/admin/shared/_tooltip_button.html.haml index 06b37688cd..9d7d0979bb 100644 --- a/app/views/admin/shared/_tooltip_button.html.haml +++ b/app/views/admin/shared/_tooltip_button.html.haml @@ -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 diff --git a/app/views/spree/admin/orders/_table_row.html.haml b/app/views/spree/admin/orders/_table_row.html.haml index 9c0ffb5e17..5ee873bda5 100644 --- a/app/views/spree/admin/orders/_table_row.html.haml +++ b/app/views/spree/admin/orders/_table_row.html.haml @@ -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')} diff --git a/spec/models/spree/shipment_spec.rb b/spec/models/spree/shipment_spec.rb index 114fb6d8ca..8a3d4bd195 100644 --- a/spec/models/spree/shipment_spec.rb +++ b/spec/models/spree/shipment_spec.rb @@ -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| diff --git a/spec/system/admin/orders_spec.rb b/spec/system/admin/orders_spec.rb index 9883469106..fa2014e185 100644 --- a/spec/system/admin/orders_spec.rb +++ b/spec/system/admin/orders_spec.rb @@ -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")