Skip to content

Commit

Permalink
Eagerly de-duplicating items when submitting request
Browse files Browse the repository at this point in the history
  • Loading branch information
patelkrunal31 committed May 31, 2024
1 parent 1353ea1 commit 578d3d5
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
27 changes: 19 additions & 8 deletions app/services/partners/request_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,27 @@ def populate_item_request(partner_request)
attrs['item_id'].blank? && attrs['quantity'].blank?
end

item_requests = formatted_line_items.map do |ira|
Partners::ItemRequest.new(
item_id: ira['item_id'],
quantity: ira['quantity'],
children: ira['children'] || [], # will create ChildItemRequests if there are any
name: fetch_organization_item_name(ira['item_id']),
partner_key: fetch_organization_partner_key(ira['item_id'])
)
items = {}

formatted_line_items.each do |input_item|
pre_existing_entry = items[input_item['item_id']]
if pre_existing_entry
pre_existing_entry.quantity = (pre_existing_entry.quantity.to_i + input_item['quantity'].to_i).to_s
# TODO: Is the following the correct way to merge?
pre_existing_entry.children += (input_item['children'] || [])
else
items[input_item['item_id']] = Partners::ItemRequest.new(
item_id: input_item['item_id'],
quantity: input_item['quantity'],
children: input_item['children'] || [], # will create ChildItemRequests if there are any
name: fetch_organization_item_name(input_item['item_id']),
partner_key: fetch_organization_partner_key(input_item['item_id'])
)
end
end

item_requests = items.values

partner_request.item_requests << item_requests

partner_request.request_items = partner_request.item_requests.map do |ir|
Expand Down
35 changes: 35 additions & 0 deletions spec/services/partners/request_create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,41 @@
expect(NotifyPartnerJob).to have_received(:perform_now).with(Request.last.id)
end

it 'should have created item requests' do
subject
expect(Partners::ItemRequest.count).to eq(item_requests_attributes.count)
end

context 'when we have duplicate item as part of request' do
let(:duplicate_item) { FactoryBot.create(:item) }
let(:unique_item) { FactoryBot.create(:item) }
let(:item_requests_attributes) do
[
ActionController::Parameters.new(
item_id: duplicate_item.id,
quantity: 3
),
ActionController::Parameters.new(
item_id: unique_item.id,
quantity: 7
),
ActionController::Parameters.new(
item_id: duplicate_item.id,
quantity: 5
)
]
end
it 'should add the quantity of the duplicate item' do
subject
aggregate_failures {
expect(Partners::ItemRequest.count).to eq(2)
expect(Partners::ItemRequest.find_by(item_id: duplicate_item.id).quantity).to eq("8")
expect(Partners::ItemRequest.find_by(item_id: unique_item.id).quantity).to eq("7")
expect(Partners::ItemRequest.first.item_id).to eq(duplicate_item.id)
}
end
end

context 'but a unexpected error occured during the save' do
let(:error_message) { 'boom' }

Expand Down

0 comments on commit 578d3d5

Please sign in to comment.