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:
Paul Mackay
2015-04-11 13:19:48 +01:00
parent 717b3b6494
commit 99709b53ed
8 changed files with 27 additions and 14 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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}

View File

@@ -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'

View File

@@ -1,2 +0,0 @@
Email sent.

View File

@@ -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

View 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

View File

@@ -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')