Merge pull request #2058 from coopdevs/improve-order-cycle-notification

Improve code style and docs of job
This commit is contained in:
Pau Pérez Fabregat
2018-02-09 13:19:32 +01:00
committed by GitHub
4 changed files with 22 additions and 14 deletions

View File

@@ -1,6 +1,9 @@
# Delivers an email with a report of the order cycle to each of its suppliers
OrderCycleNotificationJob = Struct.new(:order_cycle_id) do
def perform
order_cycle = OrderCycle.find order_cycle_id
order_cycle.suppliers.each { |supplier| ProducerMailer.order_cycle_report(supplier, order_cycle).deliver }
order_cycle = OrderCycle.find(order_cycle_id)
order_cycle.suppliers.each do |supplier|
ProducerMailer.order_cycle_report(supplier, order_cycle).deliver
end
end
end

View File

@@ -12,12 +12,14 @@ class ProducerMailer < Spree::BaseMailer
subject = "[#{Spree::Config.site_name}] #{I18n.t('producer_mailer.order_cycle.subject', producer: producer.name)}"
if has_orders? order_cycle, producer
mail(to: @producer.contact.email,
from: from_address,
subject: subject,
reply_to: @coordinator.contact.email,
cc: @coordinator.contact.email)
if has_orders?(order_cycle, producer)
mail(
to: @producer.contact.email,
from: from_address,
subject: subject,
reply_to: @coordinator.contact.email,
cc: @coordinator.contact.email
)
end
end
@@ -30,7 +32,7 @@ class ProducerMailer < Spree::BaseMailer
def line_items_from(order_cycle, producer)
Spree::LineItem.
joins(:order => :order_cycle, :variant => :product).
joins(order: :order_cycle, variant: :product).
where('order_cycles.id = ?', order_cycle).
merge(Spree::Product.in_supplier(producer)).
merge(Spree::Order.by_state('complete'))

View File

@@ -76,7 +76,7 @@ class OrderCycle < ActiveRecord::Base
enterprises = Enterprise.managed_by(user)
# Order cycles where I managed an enterprise at either end of an outgoing exchange
# ie. coordinator or distibutor
# ie. coordinator or distributor
joins(:exchanges).merge(Exchange.outgoing).
where('exchanges.receiver_id IN (?) OR exchanges.sender_id IN (?)', enterprises, enterprises).
select('DISTINCT order_cycles.*')

View File

@@ -2,11 +2,14 @@ require 'spec_helper'
describe OrderCycleNotificationJob do
let(:order_cycle) { create(:order_cycle) }
let(:mail) { double(:mail, deliver: true) }
it "sends a mail to each supplier" do
mail = double(:mail)
allow(mail).to receive(:deliver)
expect(ProducerMailer).to receive(:order_cycle_report).twice.and_return(mail)
before do
allow(ProducerMailer).to receive(:order_cycle_report).twice.and_return(mail)
end
it 'sends a mail to each supplier' do
run_job OrderCycleNotificationJob.new(order_cycle.id)
expect(ProducerMailer).to have_received(:order_cycle_report).twice
end
end