Skip to content

Commit

Permalink
Merge pull request #23 from caiena/master_update_proposal_failure_flow
Browse files Browse the repository at this point in the history
Ajuste no fluxo de negação de propostas pelo Revisor
  • Loading branch information
yusser authored Mar 4, 2020
2 parents 8e5b049 + d29fbbc commit a4430c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
9 changes: 6 additions & 3 deletions app/services/proposal_service/fail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ def fail
def change_proposals_statuses!
bidding&.proposals&.not_failure.not_draft_or_abandoned&.map(&:sent!)
bidding&.proposals&.sent&.lower&.triage!
bidding&.proposals&.lower&.reload
bidding&.proposals&.map(&:reload)
end

def update_proposal_at_blockchain!
response = Blockchain::Proposal::Update.call(bidding&.proposals&.lower)
raise BlockchainError unless response.success?
proposal.reload

bidding&.proposals&.not_failure.not_draft_or_abandoned&.each do |current|
response = Blockchain::Proposal::Update.call(current)
raise BlockchainError unless response.success?
end
end

def notify
Expand Down
33 changes: 30 additions & 3 deletions spec/services/proposal_service/fail_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,43 @@
let(:covenant) { create(:covenant) }
let(:user) { create(:admin) }
let(:lots) { create_list(:lot, 2, status: :accepted) }

let(:bidding) do
create(:bidding, covenant: covenant, build_lot: false, lots: lots)
end

let(:lot) { lots.first }

let(:proposal) do
create(:proposal, bidding: bidding, status: proposal_status,
price_total: 100)
end

let(:proposal_status) { :coop_accepted }
let(:lot_proposal) { create(:lot_proposal, lot: lot, proposal: proposal) }
let(:comment) { 'a comment' }
let(:params) { { proposal: proposal, creator: user, comment: comment } }

let(:event_service_params) do
{ proposal: proposal, comment: comment, creator: user }
end

let(:accepted_event) do
create(:event_cancel_proposal_accepted, eventable: proposal, creator: user)
end

let(:refused_event) do
create(:event_cancel_proposal_refused, eventable: proposal, creator: user)
end

let(:accepted_event_response) do
double('event_response', call!: true, event: accepted_event)
end

let(:refused_event_response) do
double('event_response', call!: true, event: refused_event)
end

let(:bc_response) { double('bc_response', success?: true) }

before do
Expand Down Expand Up @@ -62,16 +72,17 @@

describe '.call' do
let(:status) do
bidding&.proposals&.where.not(status: :draft)&.map(&:status)
bidding&.proposals&.not_failure.not_draft_or_abandoned&.map(&:status)
end
let(:lower) { bidding&.proposals&.lower }

subject { described_class.call(params) }

context 'when success' do
before { subject }

context 'and coop_accepted' do
before { subject }

it { is_expected.to be_truthy }
it { expect(status).to match_array ['triage'] }
it { expect(lower.status).to eq 'triage' }
Expand All @@ -92,6 +103,8 @@
end

context 'and coop_refused' do
before { subject }

let(:proposal_status) { :coop_refused }

it { is_expected.to be_truthy }
Expand All @@ -114,26 +127,40 @@
end

context 'and have 2 proposals' do
before { proposal_2; proposal_3; subject }

let!(:proposal_2) do
create(:proposal, bidding: bidding, status: :sent, price_total: 200)
end

let!(:lot_proposal_2) do
create(:lot_proposal, lot: lot, proposal: proposal_2)
end

let!(:proposal_3) do
create(:proposal, bidding: bidding, status: :draft, price_total: 200)
end

let!(:lot_proposal_3) do
create(:lot_proposal, lot: lot, proposal: proposal_3)
end

it { is_expected.to be_truthy }
it { expect(status).to match_array ['sent', 'triage'] }
it { expect(lower.status).to eq 'triage' }
it { expect(lots.map(&:status)).not_to include('triage') }
it { expect(Blockchain::Proposal::Update).to have_received(:call).twice }

it do
expect(EventServices::Proposal::CancelProposal::Accepted).
to have_received(:new).with(event_service_params)
end

it do
expect(EventServices::Proposal::CancelProposal::Refused).
not_to have_received(:new).with(event_service_params)
end
it { expect(Blockchain::Proposal::Update).to have_received(:call) }

it do
expect(Notifications::Proposals::Fail).
to have_received(:call).with(proposal, anything)
Expand Down

0 comments on commit a4430c6

Please sign in to comment.