Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Merge 4dafb66 into e857a18
Browse files Browse the repository at this point in the history
  • Loading branch information
adelevie committed Jul 1, 2016
2 parents e857a18 + 4dafb66 commit f9d8fab
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 12 deletions.
7 changes: 5 additions & 2 deletions app/controllers/admin/auctions_controller.rb
Expand Up @@ -45,11 +45,14 @@ def update
auction = Auction.find(params[:id])
update_auction = UpdateAuction.new(auction: auction, params: params, current_user: current_user)

# display errors even on success, to notify admin of important information
# such as an email being sent to the vendor asking for a credit card form url
error_messages = auction.errors.full_messages.to_sentence
flash[:error] = error_messages

if update_auction.perform
return_to_stored(default: admin_auction_path(auction))
else
error_messages = auction.errors.full_messages.to_sentence
flash[:error] = error_messages
@view_model = Admin::EditAuctionViewModel.new(auction)
render :edit
end
Expand Down
88 changes: 88 additions & 0 deletions app/models/auction_accepted.rb
@@ -0,0 +1,88 @@
class AuctionAccepted
def initialize(auction)
@auction = auction
end

def perform
if vendor_ineligible?
auction.errors.add(:base, 'The vendor is ineligible from being paid.')
else
request_credit_card_form_url
create_purchase_request
end
end

private

attr_reader :auction

def request_credit_card_form_url

if should_request_credit_card_form_url?
RequestCreditCardFormUrlEmailSender.new(auction).perform
auction.errors.add(:base, 'The vendor is missing their credit card. An email was sent to them requesting a credit card form URL.')
end
end

def should_request_credit_card_form_url?
winning_bidder_lacks_credit_card_form_url?
end

def create_purchase_request
if should_create_cap_proposal?
CreateCapProposalJob.perform_later(auction.id)
end
end

def should_create_cap_proposal?
cap_proposal_is_blank? &&
auction.purchase_card == "default" &&
winning_bidder_has_credit_card_form_url?
end

def vendor_ineligible?

!winning_bidder_is_eligible_to_be_paid?
end

def cap_proposal_is_blank?
auction.cap_proposal_url.blank?
end

def winning_bidder_is_eligible_to_be_paid?
if auction_is_small_business?
reckoner = SamAccountReckoner.new(winning_bidder)
reckoner.set!
winning_bidder.reload

user_is_eligible_to_bid?
else
true
end
end

def winning_bidder_has_credit_card_form_url?
!winning_bidder_lacks_credit_card_form_url?
end

def winning_bidder_lacks_credit_card_form_url?
winning_bidder.credit_card_form_url.nil? ||
winning_bidder.credit_card_form_url.empty?
end

def user_is_eligible_to_bid?
auction_rules.user_is_eligible_to_bid?(winning_bidder)
end

def auction_rules
RulesFactory.new(auction).create
end

def auction_is_small_business?
AuctionThreshold.new(auction).small_business?
end

def winning_bidder
WinningBid.new(auction).find.bidder
end
end
8 changes: 8 additions & 0 deletions app/services/request_credit_card_form_url_email_sender.rb
@@ -0,0 +1,8 @@
class RequestCreditCardFormUrlEmailSender
def initialize(auction)
@auction = auction
end

def perform
end
end
7 changes: 6 additions & 1 deletion app/services/update_auction.rb
Expand Up @@ -7,7 +7,6 @@ def initialize(auction:, params:, current_user:)

def perform
assign_attributes

update_auction_ended_job

if vendor_ineligible?
Expand All @@ -17,6 +16,8 @@ def perform
perform_approved_auction_tasks
auction.save
end

auction.persisted?
end

private
Expand Down Expand Up @@ -104,6 +105,10 @@ def winning_bidder
WinningBid.new(auction).find.bidder
end

def auction_accepted?
attributes[:result] == 'accepted'
end

def attributes
@_attributes ||= AuctionParser.new(params, user).attributes
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/auctions/_data.html.erb
@@ -1,4 +1,4 @@
<div>
<h6><%= label %></h6>
<p class="auction-label-info"><%= data %></p>
<p class="auction-label-info" id="auction-label-info-<%= label.split.join('-') %>"><%= data %></p>
</div>
12 changes: 11 additions & 1 deletion features/admin_accepts_delivery.feature
Expand Up @@ -22,4 +22,14 @@ Feature: Admin accepts delivery of a project
And I select the result as accepted
And I click on the "Update" button
Then I should see that the auction does not have a CAP Proposal URL
And I should see an error that "The vendor cannot be paid"
And I should see an error that "The vendor is ineligible from being paid"

Scenario: Marking an auction as accepted where the vendor is missing a payment method
Given I am an administrator
And there is an auction where the winning vendor is missing a payment method
And I sign in
When I visit the admin form for that auction
And I select the result as accepted
And I click on the "Update" button
Then I should see that the auction does not have a CAP Proposal URL
And I should see an error that "The vendor is missing their credit card. An email was sent to them requesting a credit card form URL."
6 changes: 3 additions & 3 deletions features/step_definitions/admin_auction_form_steps.rb
Expand Up @@ -17,8 +17,8 @@
end

Then(/^I should see that the auction does not have a CAP Proposal URL$/) do
field = find_field(I18n.t('simple_form.labels.auction.cap_proposal_url'))
expect(field.value).to eq('')
element = find(:id, 'auction-label-info-CAP-proposal-URL')
expect(element.value).to eq(nil)
end

Then(/^I expect my auction changes to have been saved$/) do
Expand Down Expand Up @@ -121,7 +121,7 @@
select('5', from: 'auction_ended_at_1i')
select('30', from: 'auction_ended_at_2i')
select('PM', from: 'auction_ended_at_3i')
@end_time = DcTimePresenter.time_zone.parse("#{@end_day.strftime('%Y-%m-%d')} 5:30 PM")
@end_time = DcTimePresenter.time_zone.parse("#{@end_day.strftime('%Y-%m-%d')} 5:30 PM")

@deadline_day = DcTimePresenter.convert(Time.now + 5.days)
fill_in "auction_delivery_due_at", with: @deadline_day.strftime('%Y-%m-%d')
Expand Down
11 changes: 11 additions & 0 deletions features/step_definitions/auction_create_steps.rb
Expand Up @@ -100,6 +100,17 @@
)
end

Given(/^there is an auction where the winning vendor is missing a payment method$/) do
@auction = FactoryGirl.create(
:auction,
:between_micropurchase_and_sat_threshold,
:winning_vendor_is_non_small_business,
:evaluation_needed
)
winning_bidder = WinningBid.new(@auction).find.bidder
winning_bidder.update(credit_card_form_url: nil)
end

Given(/^there is a paid auction$/) do
@auction = FactoryGirl.create(:auction, :closed, :paid)
end
122 changes: 122 additions & 0 deletions spec/models/auction_accepted_spec.rb
@@ -0,0 +1,122 @@
require 'rails_helper'

describe AuctionAccepted do
context 'the winning bidder lacks a credit card URL' do
it 'does not call CreateCapProposalJob' do
auction = create(
:auction,
:below_micropurchase_threshold,
:winning_vendor_is_small_business,
:delivery_due_at_expired
)
winning_bidder = WinningBid.new(auction).find.bidder
winning_bidder.update(credit_card_form_url: "")

allow(CreateCapProposalJob).to receive(:perform_later)
.with(auction.id)
.and_return(nil)

AuctionAccepted.new(auction).perform

expect(CreateCapProposalJob).not_to have_received(:perform_later)
end

it 'calls RequestCreditCardFormUrlEmailSender#perform' do
auction = create(
:auction,
:below_micropurchase_threshold,
:winning_vendor_is_small_business,
:delivery_due_at_expired
)

winning_bidder = WinningBid.new(auction).find.bidder
winning_bidder.update(credit_card_form_url: "")

request_cc_form_url_double = instance_double(
'RequestCreditCardFormUrlEmailSender'
)
allow(request_cc_form_url_double).to receive(:perform)
allow(RequestCreditCardFormUrlEmailSender).to receive(:new)
.with(auction)
.and_return(request_cc_form_url_double)

AuctionAccepted.new(auction).perform

expect(request_cc_form_url_double).to have_received(:perform)
end
end

context 'auction is below the micropurchase threshold' do
it 'calls the CreateCapProposalJob' do
auction = create(
:auction,
:below_micropurchase_threshold,
:winning_vendor_is_small_business,
:delivery_due_at_expired
)
allow(CreateCapProposalJob).to receive(:perform_later)
.with(auction.id)
.and_return(nil)

AuctionAccepted.new(auction).perform

expect(CreateCapProposalJob).to have_received(:perform_later).with(auction.id)
end
end

context 'auction is between micropurchase and SAT threshold' do
context 'winning vendor is a small business' do
it 'calls the CreateCapProposalJob' do
auction = create(
:auction,
:between_micropurchase_and_sat_threshold,
:winning_vendor_is_small_business,
:delivery_due_at_expired
)
allow(CreateCapProposalJob).to receive(:perform_later)
.with(auction.id)
.and_return(nil)

AuctionAccepted.new(auction).perform

expect(CreateCapProposalJob).to have_received(:perform_later).with(auction.id)
end
end

context 'winning vendor is not a small business' do
it 'does not call the CreateCapProposalJob' do
auction = create(
:auction,
:between_micropurchase_and_sat_threshold,
:winning_vendor_is_non_small_business,
:delivery_due_at_expired
)
allow(CreateCapProposalJob).to receive(:perform_later)
.with(auction.id)
.and_return(nil)

AuctionAccepted.new(auction).perform

expect(CreateCapProposalJob).to_not have_received(:perform_later).with(auction.id)
end
end
end

context 'auction is for another purchase card' do
it 'does not call CreateCapProposalJob' do
auction = create(
:auction,
:below_micropurchase_threshold,
:winning_vendor_is_small_business,
:delivery_due_at_expired,
purchase_card: :other
)
allow(CreateCapProposalJob).to receive(:perform_later)
.with(auction.id)

AuctionAccepted.new(auction).perform

expect(CreateCapProposalJob).not_to have_received(:perform_later)
end
end
end
35 changes: 31 additions & 4 deletions spec/services/save_auction_spec.rb
Expand Up @@ -16,6 +16,20 @@

expect(saved).to eq true
end

it 'queues and schedules AuctionEndedJob' do
auction = build(:auction, ended_at: 2.days.from_now)

expect { SaveAuction.new(auction).perform }
.to change { Delayed::Job.count }.by(1)

job = Delayed::Job
.where(queue: 'auction_ended')
.where(auction_id: auction.id)
.first

expect(job.run_at).to eq(auction.ended_at)
end
end

context 'given an invalid auction' do
Expand All @@ -34,11 +48,24 @@
end
end

it 'queues and schedules AuctionEndedJob' do
auction = build(:auction, ended_at: 2.days.from_now)
context 'setting auction.ended_at' do
it 'queues and schedules AuctionEndedJob#perform' do
auction = build(:auction, ended_at: 2.days.from_now)

expect { SaveAuction.new(auction).perform }
.to change { Delayed::Job.count }.by(1)

SaveAuction.new(auction).perform
end
end

context 'not setting auction.ended_at' do
it 'does not queue and schedule AuctionEndedJob#perform' do
auction = build(:auction, ended_at: nil)

expect { SaveAuction.new(auction).perform }
.to change { Delayed::Job.count }.by(1)
expect { SaveAuction.new(auction).perform }
.not_to change { Delayed::Job.count }
end
end
end
end

0 comments on commit f9d8fab

Please sign in to comment.