Skip to content

Commit

Permalink
Don't crash on donation/purchase creation
Browse files Browse the repository at this point in the history
  • Loading branch information
dorner committed Feb 25, 2024
1 parent 4a7e8cc commit 44446c6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
13 changes: 7 additions & 6 deletions app/controllers/donations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ def index
def create
@donation = current_organization.donations.new(donation_params)

if DonationCreateService.call(@donation)
flash[:notice] = "Donation created and logged!"
redirect_to donations_path
else
begin
DonationCreateService.call(@donation)
flash[:notice] = "Donation created and logged!"
redirect_to donations_path
rescue => e
load_form_collections
@donation.line_items.build if @donation.line_items.count.zero?
flash[:error] = "There was an error starting this donation, try again?"
Rails.logger.error "[!] DonationsController#create Error: #{@donation.errors}"
flash[:error] = "There was an error starting this donation: #{e.message}"
Rails.logger.error "[!] DonationsController#create Error: #{e.message}"
render action: :new
end
end
Expand Down
9 changes: 5 additions & 4 deletions app/controllers/purchases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ def index

def create
@purchase = current_organization.purchases.new(purchase_params)
if PurchaseCreateService.call(@purchase)
begin
PurchaseCreateService.call(@purchase)
flash[:notice] = "New Purchase logged!"
redirect_to purchases_path
else
rescue => e
load_form_collections
@purchase.line_items.build if @purchase.line_items.count.zero?
flash[:error] = "Failed to create purchase due to: #{@purchase.errors.full_messages}"
Rails.logger.error "[!] PurchasesController#create ERROR: #{@purchase.errors.full_messages}"
flash[:error] = "Failed to create purchase due to: #{e.message}"
Rails.logger.error "[!] PurchasesController#create ERROR: #{e.message}"
render action: :new
end
end
Expand Down
7 changes: 4 additions & 3 deletions app/services/donation_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ module DonationCreateService
class << self
def call(donation)
Donation.transaction do
if donation.save
donation.storage_location.increase_inventory(donation.line_item_values)
DonationEvent.publish(donation)
unless donation.save
raise donation.errors.full_messages.join("\n")
end
donation.storage_location.increase_inventory(donation.line_item_values)
DonationEvent.publish(donation)
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions app/services/purchase_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ class PurchaseCreateService
class << self
def call(purchase)
Purchase.transaction do
if purchase.save
purchase.storage_location.increase_inventory(purchase.line_item_values)
PurchaseEvent.publish(purchase)
unless purchase.save
raise purchase.errors.full_messages.join("\n")
end
purchase.storage_location.increase_inventory(purchase.line_item_values)
PurchaseEvent.publish(purchase)
end
end
end
Expand Down

0 comments on commit 44446c6

Please sign in to comment.