diff options
-rw-r--r-- | app/models/info_request_batch.rb | 9 | ||||
-rw-r--r-- | config/crontab-example | 1 | ||||
-rwxr-xr-x | script/send-batch-requests | 4 | ||||
-rw-r--r-- | spec/models/info_request_batch_spec.rb | 40 |
4 files changed, 54 insertions, 0 deletions
diff --git a/app/models/info_request_batch.rb b/app/models/info_request_batch.rb index 7e7390736..498ab4951 100644 --- a/app/models/info_request_batch.rb +++ b/app/models/info_request_batch.rb @@ -61,4 +61,13 @@ class InfoRequestBatch < ActiveRecord::Base info_request.save! info_request end + + def InfoRequestBatch.send_batches() + find_each(:conditions => "sent_at IS NULL") do |info_request_batch| + unrequestable = info_request_batch.create_batch! + mail_message = InfoRequestBatchMailer.batch_sent(info_request_batch, + unrequestable, + info_request_batch.user).deliver + end + end end diff --git a/config/crontab-example b/config/crontab-example index 366624998..28ee616ac 100644 --- a/config/crontab-example +++ b/config/crontab-example @@ -12,6 +12,7 @@ MAILTO=cron-!!(*= $site *)!!@mysociety.org # Every 10 minutes 5,15,25,35,45,55 * * * * !!(*= $user *)!! /etc/init.d/foi-alert-tracks check 5,15,25,35,45,55 * * * * !!(*= $user *)!! /etc/init.d/foi-purge-varnish check +0,10,20,30,40,50 * * * * !(*= $user *)!! run-with-lockfile -n /data/vhost/!!(*= $vhost *)!!/send-batch-requests.lock /data/vhost/!!(*= $vhost *)!!/!!(*= $vcspath *)!!/script/send-batch-requests || echo "stalled?" # Once an hour 09 * * * * !!(*= $user *)!! run-with-lockfile -n !!(*= $vhost_dir *)!!/alert-comment-on-request.lock !!(*= $vhost_dir *)!!/!!(*= $vcspath *)!!/script/alert-comment-on-request || echo "stalled?" diff --git a/script/send-batch-requests b/script/send-batch-requests new file mode 100755 index 000000000..794bab11f --- /dev/null +++ b/script/send-batch-requests @@ -0,0 +1,4 @@ +#!/bin/bash +TOP_DIR="$(dirname "$BASH_SOURCE")/.." +cd "$TOP_DIR" +bundle exec rails runner 'InfoRequestBatch.send_batches' diff --git a/spec/models/info_request_batch_spec.rb b/spec/models/info_request_batch_spec.rb index 8914da3fa..53158ebe2 100644 --- a/spec/models/info_request_batch_spec.rb +++ b/spec/models/info_request_batch_spec.rb @@ -108,3 +108,43 @@ describe InfoRequestBatch, "when creating a batch", :focus => true do end end + +describe InfoRequestBatch, "when sending batches" do + + before do + @title = 'A test title' + @body = "Dear [Authority name],\nA message\nYours faithfully,\nRequester" + @first_public_body = FactoryGirl.create(:public_body) + @second_public_body = FactoryGirl.create(:public_body) + @user = FactoryGirl.create(:user) + @info_request_batch = InfoRequestBatch.create!({:title => @title, + :body => @body, + :public_bodies => [@first_public_body, + @second_public_body], + :user => @user}) + @sent_batch = InfoRequestBatch.create!({:title => @title, + :body => @body, + :public_bodies => [@first_public_body, + @second_public_body], + :user => @user, + :sent_at => Time.now}) + end + + it 'should send requests and notifications for only unsent batch requests' do + InfoRequestBatch.send_batches + ActionMailer::Base.deliveries.size.should == 3 + first_email = ActionMailer::Base.deliveries.first + first_email.to.should == [@first_public_body.request_email] + first_email.subject.should == 'Freedom of Information request - A test title' + + second_email = ActionMailer::Base.deliveries.second + second_email.to.should == [@second_public_body.request_email] + second_email.subject.should == 'Freedom of Information request - A test title' + + third_email = ActionMailer::Base.deliveries.third + third_email.to.should == [@user.email] + third_email.subject.should == 'Your batch request "A test title" has been sent' + end + +end + |