mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Change controller action to 'notify_producers'.
Add flash message on completion. Simplify job variables. Improve mailer query. Spec for job.
This commit is contained in:
@@ -70,9 +70,12 @@ module Admin
|
||||
end
|
||||
|
||||
# Send notifications to all producers who are part of the order cycle
|
||||
def notifications
|
||||
def notify_producers
|
||||
@order_cycle = OrderCycle.find params[:id]
|
||||
Delayed::Job.enqueue OrderCycleNotificationJob.new(@order_cycle)
|
||||
|
||||
flash[:notice] = 'Emails to be sent to producers have been queued for sending.'
|
||||
format.html { redirect_to admin_order_cycles_path }
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
|
||||
OrderCycleNotificationJob = Struct.new(:order_cycle) do
|
||||
def perform
|
||||
@suppliers = order_cycle.suppliers
|
||||
@suppliers.each { |supplier| ProducerMailer.order_cycle_report(supplier, order_cycle).deliver }
|
||||
order_cycle.suppliers.each { |supplier| ProducerMailer.order_cycle_report(supplier, order_cycle).deliver }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -14,12 +14,11 @@ class ProducerMailer < Spree::BaseMailer
|
||||
# subject += " for #{@distribution_date}" if @distribution_date.present?
|
||||
# end
|
||||
|
||||
# TODO: what if producer handling orders into multiple order cycles?
|
||||
@orders = Spree::Order.complete.not_state(:canceled).managed_by(@producer.owner)
|
||||
# puts @orders.size
|
||||
@line_items = Spree::LineItem.
|
||||
joins(:order => :order_cycle, :variant => :product).
|
||||
where('order_cycles.id = ?', order_cycle).
|
||||
where('spree_products.supplier_id = ?', producer)
|
||||
|
||||
# Create a single flat list of all line items
|
||||
@line_items = @orders.map(&:line_items).flatten
|
||||
# Arrange the items in a hash to group quantities
|
||||
@line_items = @line_items.inject({}) do |lis, li|
|
||||
lis[li.variant] ||= {line_item: li, quantity: 0}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
= content_for :page_actions do
|
||||
%li
|
||||
= button_to "Notify producers", main_app.notifications_admin_order_cycle_path, :id => 'admin_notify_producers'
|
||||
= button_to "Notify producers", main_app.notify_producers_admin_order_cycle_path, :id => 'admin_notify_producers'
|
||||
|
||||
- ng_controller = order_cycles_simple_view ? 'AdminSimpleEditOrderCycleCtrl' : 'AdminEditOrderCycleCtrl'
|
||||
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
|
||||
Email sent.
|
||||
@@ -12,7 +12,7 @@ Orders summary
|
||||
Here is a summary of the orders for your products:
|
||||
\
|
||||
- @line_items.each_pair do |variant, data|
|
||||
#{variant.sku} #{raw(variant.product.supplier.name)} #{raw(variant.product.name)} #{raw(variant.options_text)} (QTY: #{data[:quantity]}) @ #{data[:line_item].single_money} = #{variant.display_amount}
|
||||
#{variant.sku} #{raw(variant.product.supplier.name)} #{raw(variant.product.name)} #{raw(variant.options_text)} (QTY: #{data[:quantity]}) @ #{data[:line_item].single_money} = #{data[:line_item].display_amount}
|
||||
|
||||
\
|
||||
Detailed orders breakdown
|
||||
|
||||
14
spec/jobs/order_cycle_notification_job_spec.rb
Normal file
14
spec/jobs/order_cycle_notification_job_spec.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe OrderCycleNotificationJob do
|
||||
let(:order_cycle) { create(:order_cycle) }
|
||||
|
||||
it 'sends a mail to each supplier' do
|
||||
mail = double()
|
||||
allow(mail).to receive(:deliver)
|
||||
mailer = double('ProducerMailer')
|
||||
expect(ProducerMailer).to receive(:order_cycle_report).twice.and_return(mail)
|
||||
job = OrderCycleNotificationJob.new(order_cycle)
|
||||
job.perform
|
||||
end
|
||||
end
|
||||
@@ -5,8 +5,8 @@ describe ProducerMailer do
|
||||
let(:s2) { create(:supplier_enterprise, address: create(:address)) }
|
||||
let(:d1) { create(:distributor_enterprise, address: create(:address)) }
|
||||
let(:d2) { create(:distributor_enterprise, address: create(:address)) }
|
||||
let(:p1) { create(:product, price: 12.34, distributors: [d1], supplier: s1) }
|
||||
let(:p2) { create(:product, price: 23.45, distributors: [d2], supplier: s2) }
|
||||
let(:p1) { create(:product, price: 12.34, supplier: s1) }
|
||||
let(:p2) { create(:product, price: 23.45, supplier: s2) }
|
||||
let(:order_cycle) { create(:simple_order_cycle) }
|
||||
let!(:order) do
|
||||
order = create(:order, distributor: d1, order_cycle: order_cycle, state: 'complete')
|
||||
|
||||
Reference in New Issue
Block a user