Skip to content

Commit d201bd4

Browse files
Merge pull request #5405 from Budmin/5223_error_message_on_nonvisible_item_request
5223 error message on nonvisible item request
2 parents 67da1ea + dc6556f commit d201bd4

File tree

4 files changed

+84
-13
lines changed

4 files changed

+84
-13
lines changed

app/controllers/partners/family_requests_controller.rb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def create
2929
if create_service.errors.none?
3030
redirect_to partners_request_path(create_service.partner_request), notice: "Requested items successfully!"
3131
else
32-
redirect_to new_partners_family_request_path, error: "Request failed! #{create_service.errors.map { |error| error.message.to_s }}}"
32+
redirect_to new_partners_family_request_path, error: create_service.errors.map { |error| error.message.to_s }.join(",").to_s
3333
end
3434
end
3535

@@ -56,20 +56,23 @@ def validate
5656
private
5757

5858
def build_family_requests_attributes(params)
59-
children_ids = []
59+
child_ids = params.keys.grep(/^child-/).map { |key| key.split('-').last }
6060

61-
params.each do |key, _|
62-
is_child, id = key.split('-')
63-
if is_child == 'child'
64-
children_ids << id
65-
end
66-
end
67-
68-
children = current_partner.children.where(id: children_ids).joins(:requested_items).select('children.*', :item_id)
61+
children_grouped_by_item_id = current_partner
62+
.children
63+
.where(id: child_ids)
64+
.joins(:requested_items)
65+
.select('children.*', 'items.id as item_id',
66+
'items.name as item_name',
67+
'items.visible_to_partners')
68+
.group_by(&:item_id)
6969

70-
children_grouped_by_item_id = children.group_by(&:item_id)
7170
children_grouped_by_item_id.map do |item_id, item_requested_children|
72-
{ item_id: item_id, person_count: item_requested_children.size, children: item_requested_children }
71+
{
72+
item_id: item_id,
73+
person_count: item_requested_children.size,
74+
children: item_requested_children
75+
}
7376
end
7477
end
7578
end

app/services/partners/family_request_create_service.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ def valid?
5757
errors.add(:base, 'detected a unknown item_id')
5858
end
5959

60+
check_for_item_visibility
61+
6062
errors.none?
6163
end
6264

@@ -71,6 +73,37 @@ def item_requests_attributes
7173
end
7274
end
7375

76+
# If requested item(s) isn't visible to partners,
77+
# an error specifying which item is not available is raised
78+
def check_for_item_visibility
79+
invisible_items = item_requests_attributes.select { |attr|
80+
!included_items_by_id[attr[:item_id]].nil? &&
81+
!included_items_by_id[attr[:item_id]].visible_to_partners
82+
}
83+
84+
unless invisible_items.empty?
85+
86+
item_errors = invisible_items.map do |item|
87+
item_name = included_items_by_id[item[:item_id]].name
88+
89+
children_names = item[:children].map { |c| "#{c.first_name} #{c.last_name}" }.join(", ")
90+
91+
"\"#{item_name}\" requested for #{children_names} is not currently available for request."
92+
end
93+
94+
joined_errors = item_errors.join(", ")
95+
96+
# don't want to show a memflash error
97+
if joined_errors.length >= Memflash.threshold
98+
truncated_errors = joined_errors[0...(Memflash.threshold - 10)]
99+
errors.add(:base, "#{truncated_errors}...")
100+
else
101+
errors.add(:base, joined_errors)
102+
end
103+
104+
end
105+
end
106+
74107
def convert_person_count_to_item_quantity(item_id:, person_count:)
75108
item = included_items_by_id[item_id.to_i]
76109

app/views/layouts/partners/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
<div class="content-wrapper">
6767
<% flash.each do |key, value| %>
6868
<div class="<%= flash_class(key) %> alert-dismissible fade show" role="alert">
69-
<a href="#" class="close btn-close" data-bs-dismiss="alert" aria-label="Close" style="text-decoration: none;"><%= fa_icon('times') %></a>
69+
<a href="#" class="close btn-close" data-bs-dismiss="alert" aria-label="Close" style="text-decoration: none;"></a>
7070
<%= sanitize(value, tags: %w(ul li)) %>
7171
</div>
7272
<% end %>

spec/requests/partners/family_requests_requests_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,41 @@
4848
expect(subject).to redirect_to(partners_requests_path)
4949
end
5050

51+
it "does not allow requesting non-visible items" do
52+
partner.update!(status: :approved)
53+
54+
# update child item to not be visible to partners
55+
i = Item.first
56+
i.update!(visible_to_partners: false)
57+
58+
subject
59+
60+
child_with_unavailable_item = children[0]
61+
expected = "\"#{i.name}\" requested for #{child_with_unavailable_item.first_name} #{child_with_unavailable_item.last_name} is not currently available for request."
62+
63+
expect(response.request.flash[:error]).to eql expected
64+
end
65+
66+
it "does not allow requesting non-visible items for multiple children" do
67+
partner.update!(status: :approved)
68+
69+
# update child item to not be visible to partners
70+
i = Item.first
71+
i.update!(visible_to_partners: false)
72+
73+
children[1].update(requested_item_ids: [i.id])
74+
75+
subject
76+
77+
children_with_unavailable_item = children[0..1]
78+
79+
child_formatting = children_with_unavailable_item.map { |c| "#{c.first_name} #{c.last_name}" }.join(", ")
80+
81+
expected = "\"#{i.name}\" requested for #{child_formatting} is not currently available for request."
82+
83+
expect(response.request.flash[:error]).to eql expected
84+
end
85+
5186
it "submits the request" do
5287
partner.update!(status: :approved)
5388

0 commit comments

Comments
 (0)