aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/info_request_batch.rb8
-rw-r--r--app/models/outgoing_message.rb70
2 files changed, 41 insertions, 37 deletions
diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb
index d7c5eb9af..8a5ebeaba 100644
--- a/app/models/info_request_batch.rb
+++ b/app/models/info_request_batch.rb
@@ -46,7 +46,13 @@ class InfoRequestBatch < ActiveRecord::Base
self.sent_at = Time.now
self.save!
end
- created.each{ |info_request| info_request.outgoing_messages.first.send_message }
+ created.each do |info_request|
+ outgoing_message = info_request.outgoing_messages.first
+
+ outgoing_message.sendable?
+ mail_message = OutgoingMailer.initial_request(outgoing_message.info_request, outgoing_message).deliver
+ outgoing_message.record_email_delivery(mail_message.to_addrs.join(', '), mail_message.message_id)
+ end
return unrequestable
end
diff --git a/app/models/outgoing_message.rb b/app/models/outgoing_message.rb
index 9424113fc..fa83c7381 100644
--- a/app/models/outgoing_message.rb
+++ b/app/models/outgoing_message.rb
@@ -171,57 +171,42 @@ class OutgoingMessage < ActiveRecord::Base
MySociety::Validate.contains_postcode?(body)
end
- # Deliver outgoing message
- # Note: You can test this from script/console with, say:
- # InfoRequest.find(1).outgoing_messages[0].send_message
- def send_message(log_event_type = 'sent')
+ def record_email_delivery(to_addrs, message_id, log_event_type = 'sent')
+ self.last_sent_at = Time.now
+ self.status = 'sent'
+ save!
+
+ log_event_type = "followup_#{ log_event_type }" if message_type == 'followup'
+
+ info_request.log_event(log_event_type, { :email => to_addrs,
+ :outgoing_message_id => id,
+ :smtp_message_id => message_id })
+ set_info_request_described_state
+ end
+
+ def sendable?
if status == 'ready'
if message_type == 'initial_request'
- self.last_sent_at = Time.now
- self.status = 'sent'
- self.save!
-
- mail_message = OutgoingMailer.initial_request(info_request, self).deliver
- self.info_request.log_event(log_event_type, {
- :email => mail_message.to_addrs.join(", "),
- :outgoing_message_id => self.id,
- :smtp_message_id => mail_message.message_id
- })
- self.info_request.set_described_state('waiting_response')
+ return true
elsif message_type == 'followup'
- self.last_sent_at = Time.now
- self.status = 'sent'
- self.save!
-
- mail_message = OutgoingMailer.followup(info_request, self, incoming_message_followup).deliver
- self.info_request.log_event('followup_' + log_event_type, {
- :email => mail_message.to_addrs.join(", "),
- :outgoing_message_id => self.id,
- :smtp_message_id => mail_message.message_id
- })
- if info_request.described_state == 'waiting_clarification'
- self.info_request.set_described_state('waiting_response')
- end
- if what_doing == 'internal_review'
- self.info_request.set_described_state('internal_review')
- end
+ return true
else
- raise "Message id #{id} has type '#{message_type}' which send_message can't handle"
+ raise "Message id #{id} has type '#{message_type}' which cannot be sent"
end
elsif status == 'sent'
raise "Message id #{id} has already been sent"
else
- raise "Message id #{id} not in state for send_message"
+ raise "Message id #{id} not in state for sending"
end
end
# An admin function
- def resend_message
+ def prepare_message_for_resend
if ['initial_request', 'followup'].include?(message_type) and status == 'sent'
self.status = 'ready'
- send_message('resent')
else
- raise "Message id #{id} has type '#{message_type}' status '#{status}' which resend_message can't handle"
+ raise "Message id #{id} has type '#{message_type}' status " \
+ "'#{status}' which prepare_message_for_resend can't handle"
end
end
@@ -303,6 +288,19 @@ class OutgoingMessage < ActiveRecord::Base
private
+ def set_info_request_described_state
+ if message_type == 'initial_request'
+ info_request.set_described_state('waiting_response')
+ elsif message_type == 'followup'
+ if info_request.described_state == 'waiting_clarification'
+ info_request.set_described_state('waiting_response')
+ end
+ if what_doing == 'internal_review'
+ info_request.set_described_state('internal_review')
+ end
+ end
+ end
+
def set_default_letter
self.body = get_default_message if body.nil?
end