Refactored order finishing. Delay user notifications.

This commit is contained in:
benni 2012-08-24 14:24:36 +02:00
parent b0c9580b53
commit af3875d46b
4 changed files with 38 additions and 42 deletions

View file

@ -97,9 +97,7 @@ class OrdersController < ApplicationController
def finish def finish
order = Order.find(params[:id]) order = Order.find(params[:id])
order.finish!(@current_user) order.finish!(@current_user)
call_rake "foodsoft:finished_order_tasks", :order_id => order.id redirect_to order, notice: "Die Bestellung wurde beendet."
flash[:notice] = "Die Bestellung wurde beendet."
redirect_to order
end end
# Renders the groups-orderd PDF. # Renders the groups-orderd PDF.

View file

@ -156,15 +156,30 @@ class Order < ActiveRecord::Base
def finish!(user) def finish!(user)
unless finished? unless finished?
Order.transaction do Order.transaction do
# Update order_articles. Save the current article_price to keep price consistency
# Also save results for each group_order_result
order_articles.all(:include => :article).each do |oa|
oa.update_attribute(:article_price, oa.article.article_prices.first)
oa.group_order_articles.each { |goa| goa.save_results! }
end
# set new order state (needed by notify_order_finished) # set new order state (needed by notify_order_finished)
update_attributes(:state => 'finished', :ends => Time.now, :updated_by => user) update_attributes(:state => 'finished', :ends => Time.now, :updated_by => user)
# Update order_articles. Save the current article_price to keep price consistency
# Also save results for each group_order_result
# Clean up
order_articles.all(:include => :article).each do |oa|
oa.update_attribute(:article_price, oa.article.article_prices.first)
oa.group_order_articles.each do |goa|
goa.save_results!
# Delete no longer required order-history (group_order_article_quantities) and
# TODO: Do we need articles, which aren't ordered? (units_to_order == 0 ?)
goa.group_order_article_quantities.clear
end
end
# Update GroupOrder prices
group_orders.each { |go| go.update_price! }
# Stats
ordergroups.each { |o| o.update_stats! }
# Notifications
UserNotifier.delay.finished_order(self.id)
end end
end end
end end

View file

@ -0,0 +1,15 @@
# This plain ruby class should handle all user notifications, called by various models
class UserNotifier
def self.finished_order(order_id)
Order.find(order_id).group_orders.each do |group_order|
group_order.ordergroup.users.each do |user|
begin
Mailer.order_result(user, group_order).deliver if user.settings["notify.orderFinished"] == '1'
rescue
Rails.logger.warn "Can't deliver mail to #{user.email}"
end
end
end
end
end

View file

@ -52,36 +52,4 @@ namespace :foodsoft do
end end
end end
end end
desc "finished order tasks, cleanup, notifications, stats ..."
task :finished_order_tasks => :environment do
puts "Start: #{Time.now}"
order = Order.find(ENV["ORDER_ID"])
# Update GroupOrder prices
order.group_orders.each { |go| go.update_price! }
# Clean up
# Delete no longer required order-history (group_order_article_quantities) and
# TODO: Do we need articles, which aren't ordered? (units_to_order == 0 ?)
order.order_articles.each do |oa|
oa.group_order_articles.each { |goa| goa.group_order_article_quantities.clear }
end
# Notifications
for group_order in order.group_orders
for user in group_order.ordergroup.users
begin
Mailer.order_result(user, group_order).deliver if user.settings["notify.orderFinished"] == '1'
rescue
puts "deliver aborted for #{user.email}.."
end
end
end
# Stats
order.ordergroups.each { |o| o.update_stats! }
puts "End: #{Time.now}"
end
end end