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

@ -156,15 +156,30 @@ class Order < ActiveRecord::Base
def finish!(user)
unless finished?
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)
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

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