Class: Order::BackOrderClientNotification
- Inherits:
-
BaseService
- Object
- BaseService
- Order::BackOrderClientNotification
- Defined in:
- app/services/order/back_order_client_notification.rb
Overview
Service object: back order client notification.
Constant Summary collapse
- EMAIL_TEMPLATE_CODE =
Email template code.
'ORDER_BO_NOTIFY'.freeze
Instance Attribute Summary
Attributes inherited from BaseService
Class Method Summary collapse
Instance Method Summary collapse
- #generate_builder_options(order) ⇒ Object
-
#load_orders(options = {}) ⇒ ActiveRecord::Relation<Order>
The back-ordered orders to notify about.
-
#process(options = {}) ⇒ Array
Processed notification results.
- #process_order(order) ⇒ Object
- #process_orders(orders) ⇒ Object
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Class Method Details
.build_or_create_email_template ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'app/services/order/back_order_client_notification.rb', line 91 def self.build_or_create_email_template # This is for helping with migration email_template = EmailTemplate.where(system_code: EMAIL_TEMPLATE_CODE).first_or_initialize email_template.update!( category: 'transactional', description: 'Back Order Notification to Customer Message', subject: 'WarmlyYours: Backorder notification for order {{order.reference_number}}', stylesheet: 'default', template: 'email', body: %{ <p>{{salutation}}</p> <p>Thank you for your order {{order.reference_number}}, we wanted to inform you that the following item(s) are currently on backorder.</p> <ul> {% for line in order.lines_on_backorder %} <li>{{ line }}</li> {% endfor %} </ul> <p> If you need to make a change on your order or explore possible substitutions, please contact your account manager directly, otherwise we will notify you when your items are being restocked and released! </p> <p>Sincerely,</p> {{signature}} } ) end |
Instance Method Details
#generate_builder_options(order) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/services/order/back_order_client_notification.rb', line 71 def (order) sender = order.customer.primary_sales_rep party_for_order_confirmation = order.party_for_order_confirmation email_for_order_confirmation = order.email_for_order_confirmation order_drop = order.to_liquid { resource: order, sender_party: sender, sender: sender&.email || INFO_EMAIL, recipient_party: party_for_order_confirmation, emails: email_for_order_confirmation, template_system_code: EMAIL_TEMPLATE_CODE, bcc: sender&.email, # NOTE: lines_on_backorder is also available via order.lines_on_backorder in templates, # but we pass it explicitly for backward compatibility with existing template merge_options: { order: order_drop, lines_on_backorder: order_drop.lines_on_backorder } } end |
#load_orders(options = {}) ⇒ ActiveRecord::Relation<Order>
Returns the back-ordered orders to notify about.
24 25 26 27 |
# File 'app/services/order/back_order_client_notification.rb', line 24 def load_orders( = {}) lookback_period = [:lookback_period] || 7.days.ago Order.sales_orders.back_order.where('bo_notification_sent_at IS NULL or bo_notification_sent_at < ?', lookback_period) end |
#process(options = {}) ⇒ Array
Returns processed notification results.
10 11 12 13 14 15 16 17 18 19 |
# File 'app/services/order/back_order_client_notification.rb', line 10 def process( = {}) res = [] orders = load_orders() PaperTrail.request(whodunnit: 'Order::BackOrderClientNotification') do logger.tagged 'Order::BackOrderClientNotification' do res = process_orders(orders) end end res end |
#process_order(order) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/services/order/back_order_client_notification.rb', line 42 def process_order(order) res = { order_id: order.id } = order.customer.is_direct_pro? || order.customer.is_homeowner? logger.tagged "Order ID:#{order.id}" do if res[:builder_options] = (order) # Skip sending if there are no items actually on backorder # This can happen if inventory changed since order was marked back_order if res[:builder_options][:merge_options][:lines_on_backorder].blank? res.merge!({ status_code: :skipped, status_message: 'No items currently on backorder' }) else logger.debug("Creating communication") co = CommunicationBuilder.new(res[:builder_options]).create res[:communication_id] = co.id if co.draft? logger.error co.errors_to_s res.merge!({ status_code: :error, status_message: co.errors_to_s }) else order.update_column(:bo_notification_sent_at, Time.current) res.merge!({ status_code: :ok }) end end else res.merge!({ status_code: :skipped, status_message: 'Notifications are only for homeowners and trade pros' }) end end res end |
#process_orders(orders) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/services/order/back_order_client_notification.rb', line 29 def process_orders(orders) res = [] orders.find_each do |order| res << process_order(order) rescue StandardError => e ErrorReporting.error e, order_id: order.id logger.error e ap e.backtrace res << { order_id: order.id, status_code: :error, status_message: "Exception: #{e.inspect}" } end res end |