Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Add Quantity Column to Inventory and Improve Inventory Check Logic #633

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions ruby-on-rails/app/controllers/api/v1/checkout_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,33 @@ def index
products_in_inventory = Inventory.all()
span_inventory_call.finish


span_logic = transaction.start_child(op: "custom.inventory_vs_cart_logic")

products_in_inventory.each_with_index { |inv_objs, i|
if !enough_inventory?(cart_contents)
begin
inventory_check_result = enough_inventory?(cart_contents)

if !inventory_check_result[:success]
begin
raise Exception.new "Not enough inventory for product"
STDERR.puts "Not enough inventory for productid " + inv_objs["productid"].to_s
Sentry.capture_exception(Exception)
logged = "Error: Not enough inventory"
error = StandardError.new(inventory_check_result[:message])
Sentry.capture_exception(error)
logged = "Error: #{inventory_check_result[:message]}"
render json: {"message": logged}, status: 500
break # breaks on first error. might be more inventory errors.
return
end
end
}
rescue => e
Sentry.capture_exception(e)
logged = "Error: An unexpected error occurred while checking inventory"
render json: {"message": logged}, status: 500
return
end

span_logic.finish

render json: {"message": logged}, status: 200

end


def enough_inventory?(cart_contents)
return false
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddQuantityToInventory < ActiveRecord::Migration[6.1]
def change
# Add quantity column if it doesn't exist
unless column_exists?(:inventory, :quantity)
add_column :inventory, :quantity, :integer, null: false, default: 0
end

# Add index for faster lookups
unless index_exists?(:inventory, :product_id)
add_index :inventory, :product_id
end
end
end