allows unfeasible emails only to be send out once

This commit is contained in:
rgarcia
2016-03-28 18:06:43 +02:00
parent 8a8f062398
commit 467c06c3ca
6 changed files with 45 additions and 14 deletions

View File

@@ -20,7 +20,7 @@ class Valuation::SpendingProposalsController < Valuation::BaseController
if valid_price_params? && @spending_proposal.update(valuation_params) if valid_price_params? && @spending_proposal.update(valuation_params)
if @spending_proposal.marked_as_unfeasible? if @spending_proposal.marked_as_unfeasible?
Mailer.unfeasible_spending_proposal(@spending_proposal).deliver_later @spending_proposal.send_unfeasible_email
end end
redirect_to valuation_spending_proposal_path(@spending_proposal), notice: t('valuation.spending_proposals.notice.valuate') redirect_to valuation_spending_proposal_path(@spending_proposal), notice: t('valuation.spending_proposals.notice.valuate')

View File

@@ -69,15 +69,24 @@ class SpendingProposal < ActiveRecord::Base
end end
def marked_as_unfeasible? def marked_as_unfeasible?
previous_changes.has_key?("feasible") && unfeasible? unfeasible_email_sent_at.blank? && unfeasible? && valuation_finished?
end end
def unfeasible? def unfeasible?
not feasible? not feasible?
end end
def valuation_finished?
valuation_finished
end
def code def code
"#{id}" + (administrator.present? ? "-A#{administrator.id}" : "") "#{id}" + (administrator.present? ? "-A#{administrator.id}" : "")
end end
def send_unfeasible_email
Mailer.unfeasible_spending_proposal(self).deliver_later
update(unfeasible_email_sent_at: Time.now)
end
end end

View File

@@ -0,0 +1,5 @@
class AddUnfeasibleEmailSentAtToSpendingProposals < ActiveRecord::Migration
def change
add_column :spending_proposals, :unfeasible_email_sent_at, :datetime, default: nil
end
end

View File

@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160315092854) do ActiveRecord::Schema.define(version: 20160328152843) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@@ -311,6 +311,7 @@ ActiveRecord::Schema.define(version: 20160315092854) do
t.integer "valuation_assignments_count", default: 0 t.integer "valuation_assignments_count", default: 0
t.integer "price_first_year", limit: 8 t.integer "price_first_year", limit: 8
t.string "time_scope" t.string "time_scope"
t.datetime "unfeasible_email_sent_at"
end end
add_index "spending_proposals", ["author_id"], name: "index_spending_proposals_on_author_id", using: :btree add_index "spending_proposals", ["author_id"], name: "index_spending_proposals_on_author_id", using: :btree

View File

@@ -134,6 +134,7 @@ feature 'Emails' do
choose 'spending_proposal_feasible_false' choose 'spending_proposal_feasible_false'
fill_in 'spending_proposal_feasible_explanation', with: 'This is not legal as stated in Article 34.9' fill_in 'spending_proposal_feasible_explanation', with: 'This is not legal as stated in Article 34.9'
check 'spending_proposal_valuation_finished'
click_button 'Save changes' click_button 'Save changes'
expect(page).to have_content "Dossier updated" expect(page).to have_content "Dossier updated"

View File

@@ -75,24 +75,39 @@ describe SpendingProposal do
describe "#marked_as_unfeasible?" do describe "#marked_as_unfeasible?" do
let(:spending_proposal) { create(:spending_proposal) } let(:spending_proposal) { create(:spending_proposal) }
it "returns true when feasibility has changed and it is false" do it "returns true when marked as unfeasibable and valuation_finished" do
spending_proposal.update(feasible: false) spending_proposal.update(feasible: false, valuation_finished: true)
expect(spending_proposal.marked_as_unfeasible?).to eq true expect(spending_proposal.marked_as_unfeasible?).to eq true
end end
it "returns false when feasibility has not changed" do it "returns false when marked as feasible" do
spending_proposal.update(price: 1000000)
expect(spending_proposal.marked_as_unfeasible?).to eq false
end
it "returns false when it is feasible" do
spending_proposal.update(feasible: true) spending_proposal.update(feasible: true)
expect(spending_proposal.marked_as_unfeasible?).to eq false expect(spending_proposal.marked_as_unfeasible?).to eq false
end end
xit "when marked as unfeasible but there is no admin associated... it "returns false when marked as feasable and valuation_finished" do
an exception occurs when sending the unfeasible email, spending_proposal.update(feasible: true, valuation_finished: true)
because spending_proposal.administrator.id is nil.." expect(spending_proposal.marked_as_unfeasible?).to eq false
end
it "returns false when unfeasible email already sent" do
spending_proposal.update(unfeasible_email_sent_at: 1.day.ago)
expect(spending_proposal.marked_as_unfeasible?).to eq false
end
end
describe "#send_unfeasible_email" do
let(:spending_proposal) { create(:spending_proposal) }
it "sets the time when the unfeasible email was sent" do
expect(spending_proposal.unfeasible_email_sent_at).to_not be
spending_proposal.send_unfeasible_email
expect(spending_proposal.unfeasible_email_sent_at).to be
end
it "send an email" do
expect {spending_proposal.send_unfeasible_email}.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end end
describe "#code" do describe "#code" do