Skip to content
This repository was archived by the owner on Aug 3, 2025. It is now read-only.

Commit b7ba509

Browse files
Update transaction to reflect items with no errors from Pi
1 parent f192d15 commit b7ba509

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

models/transaction.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ def vend
4040
result = JSON.parse(response.body)
4141
errors = ""
4242
result["items"].each do |item_json|
43+
item_idx = items_locations.index(item_json['location'])
4344
if item_json['error']
4445
errors += "#{item_json['location']}: #{item_json['error']}\n"
46+
# Remove item from transaction
47+
items_except_error = self.items.reject { |i| i.vend_location.pretty_location == item_json['location'] }
48+
self.update(items: items_except_error)
4549
else
46-
item_idx = items_locations.index(item_json['location'])
4750
self.items[item_idx].location.quantity -= 1
4851
self.items[item_idx].location.save
4952
end

routes/transaction.rb

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,25 @@ def self.registered(app)
4040
)
4141

4242
begin
43+
# Vending a transaction will remove items that could not be vended so that user
44+
# could be billed for correct amount according to what was actually bought
4345
error_message = transaction.vend
44-
old_balance = user.balance
4546

46-
unless error_message
47-
user.set_balance(user.balance - total_credits_needed, "Merch Transaction: #{items.map(&:name).join(", ")}")
48-
49-
if user.balance == old_balance # Transaction failed (quietly)
50-
transaction.destroy
51-
halt 500, Errors::BALANCE_ERROR
52-
end
47+
old_balance = user.balance
48+
transaction_name = "Merch Transaction: #{items.map(&:name).join(", ")}"
49+
user.set_balance(user.balance - transaction.amount, transaction_name)
5350

54-
ResponseFormat.data(transaction)
55-
else
56-
halt 500, ResponseFormat.error(error_message)
51+
# Transaction failed (quietly)
52+
if user.balance == old_balance
53+
transaction.destroy
54+
halt 500, Errors::BALANCE_ERROR
5755
end
56+
57+
# Attach transaction which contains successful items only + error_message for failed items
58+
response = ResponseFormat.data(transaction)
59+
JSON.parse(response)['error'] = error_message
60+
61+
response
5862
rescue Exception => e
5963
halt 500, ResponseFormat.error(e.message)
6064
end

0 commit comments

Comments
 (0)