-
-
Notifications
You must be signed in to change notification settings - Fork 572
5223 error message on nonvisible item request #5405
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
base: main
Are you sure you want to change the base?
Changes from 3 commits
0024454
86d8d5e
7e0e23b
adb953a
e321897
1af5323
b7c0550
13beeb4
5cc93fb
6561406
5d4f6e2
b4fab28
a55061a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,12 @@ def new | |
| end | ||
|
|
||
| def create | ||
| family_requests_attributes = build_family_requests_attributes(params) | ||
| begin | ||
| family_requests_attributes = build_family_requests_attributes(params) | ||
| rescue ArgumentError => e | ||
| redirect_to new_partners_family_request_path, error: e | ||
| return | ||
| end | ||
|
|
||
| create_service = Partners::FamilyRequestCreateService.new( | ||
| partner_user_id: current_user.id, | ||
|
|
@@ -28,7 +33,7 @@ def create | |
| if create_service.errors.none? | ||
| redirect_to partners_request_path(create_service.partner_request), notice: "Requested items successfully!" | ||
| else | ||
| redirect_to new_partners_family_request_path, error: "Request failed! #{create_service.errors.map { |error| error.message.to_s }}}" | ||
| redirect_to new_partners_family_request_path, error: "Request failed! #{create_service.errors.map { |error| error.message.to_s }}" | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -55,21 +60,55 @@ def validate | |
| private | ||
|
|
||
| def build_family_requests_attributes(params) | ||
| children_ids = [] | ||
| child_ids = params.keys.grep(/^child-/).map { |key| key.split('-').last } | ||
|
|
||
| params.each do |key, _| | ||
| is_child, id = key.split('-') | ||
| if is_child == 'child' | ||
| children_ids << id | ||
| end | ||
| end | ||
| validate_visible_items!(child_ids) | ||
|
|
||
| children = current_partner.children.where(id: children_ids).joins(:requested_items).select('children.*', :item_id) | ||
| children_grouped_by_item_id = current_partner | ||
| .children | ||
| .where(id: child_ids) | ||
| .joins(:requested_items) | ||
| .select('children.*', 'items.id as item_id', | ||
| 'items.name as item_name', | ||
| 'items.visible_to_partners') | ||
| .group_by(&:item_id) | ||
|
|
||
| children_grouped_by_item_id = children.group_by(&:item_id) | ||
| children_grouped_by_item_id.map do |item_id, item_requested_children| | ||
| { item_id: item_id, person_count: item_requested_children.size, children: item_requested_children } | ||
| { | ||
| item_id: item_id, | ||
| person_count: item_requested_children.size, | ||
| children: item_requested_children | ||
| } | ||
| end | ||
| end | ||
|
|
||
| def validate_visible_items!(child_ids) | ||
| invisible_item_requests = current_partner | ||
|
||
| .children | ||
| .where(id: child_ids) | ||
| .joins(:requested_items) | ||
| .where(items: { visible_to_partners: false }) | ||
| .select( | ||
| 'children.first_name', | ||
| 'children.last_name', | ||
| 'items.id as item_id', | ||
| 'items.name as item_name', | ||
| 'items.visible_to_partners' | ||
| ) | ||
| .group_by(&:item_id) | ||
|
|
||
| return if invisible_item_requests.empty? | ||
|
|
||
| # raise an exception if there are requests | ||
| # to items that aren't visible to partners | ||
| item_errors = invisible_item_requests.map do |_, children| | ||
| children_names = children.map { |c| "#{c.first_name} #{c.last_name}" }.join(", ") | ||
| item_name = children.first.item_name | ||
|
|
||
| "\"#{item_name}\" requested for #{children_names} is not currently available for request." | ||
| end | ||
|
|
||
| raise ArgumentError.new item_errors.join(", ") | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ArgumentErroris a built-in Ruby exception that shouldn't be used for business logic validation. Probably best to create your own error class.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Where would be the best place for the error class?
The file that looks most like is
app/events/inventory_error.rb, so something likeapp/events/item_visibility_error.rb?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're moving this to the service class, you can define the error inside the class itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After looking at the service class, I felt it made more sense to just use the error handling that was already present. If you'd like me to create a custom error class instead just let me know