Skip to content

Commit

Permalink
4175 add request type flag (#4827)
Browse files Browse the repository at this point in the history
* Adds type enum to request model.

* Adds request type enum to request model.

* Adds request_type to request.

* Adds request_type to request.

* Passes request_type to FamilyRequestCreateService.new

* Passes request_type to FamilyRequestCreateService.new

* Passes request_type to RequestCreateService.new

* Passes request_type to Request.new.

* Adds hidden field in order to pass request_type param on submit.

* Adds request type flag to index and show pages as well as the requests export. Adds filter by request type to index.

* Adds by_request_type scope for filtering by request type.

* Adds request type flag to index and show pages as well as the requests export. Adds filter by request type to index.

* Adds by_request_type filter.

* Updates flag names and removes flag styles based on feedback from ruby for good.

* Adds test for request_type enum.

* Removes unnecessary line.

* Adds Type to expected_headers.

* Adds request_type traits to request factory.

* Adds request_type to family_request_create_service_spec args.

* Adds request_type to args.

* Pass request_type directly.

* Remove request_type partial and enter text directly into reqest_row

* Revert changes to database.yml

* Revert changes to request_spec.rb

* Remove request_type

* Remove hidden request_type fields.

* Change request_type data_type to string, replace request_type string in view to display only the first letter, pull request_type from service instead of defining with an instance var.

* Remove request_type from tests

* Add request_type_label method

* Undo changes to versions

* Add request_type_label method

* Add request_type to initialize

* Fix request_type bug

* Fix linting

* Fix request_type_label method model spec

* Remove unused for_families attribute from RequestCreateService

* Remove leftover print debugging statement

* Fix/add specs for new request_type attribute

* Revert submit button color change for family requests

* Hover over request type abbreviation to see full text

* Show full request type when viewing individual request

* Fix linting

---------

Co-authored-by: nathan <[email protected]>
Co-authored-by: Nathan Thomas <[email protected]>
  • Loading branch information
3 people authored Dec 10, 2024
1 parent 56cb8cb commit b761496
Show file tree
Hide file tree
Showing 17 changed files with 146 additions and 10 deletions.
2 changes: 2 additions & 0 deletions app/controllers/partners/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def show

def create
create_service = Partners::RequestCreateService.new(
request_type: "quantity",
partner_user_id: current_user.id,
comments: partner_request_params[:comments],
item_requests_attributes: partner_request_params[:item_requests_attributes]&.values || []
Expand All @@ -43,6 +44,7 @@ def create

def validate
@partner_request = Partners::RequestCreateService.new(
request_type: "quantity",
partner_user_id: current_user.id,
comments: partner_request_params[:comments],
item_requests_attributes: partner_request_params[:item_requests_attributes]&.values || []
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def index
@partners = current_organization.partners.order(:name)
@statuses = Request.statuses.transform_keys(&:humanize)
@partner_users = User.where(id: @paginated_requests.pluck(:partner_user_id))
@request_types = Request.request_types.transform_keys(&:humanize)
@selected_request_type = filter_params[:by_request_type]
@selected_request_item = filter_params[:by_request_item_id]
@selected_partner = filter_params[:by_partner]
@selected_status = filter_params[:by_status]
Expand Down Expand Up @@ -71,6 +73,6 @@ def load_items
def filter_params
return {} unless params.key?(:filters)

params.require(:filters).permit(:by_request_item_id, :by_partner, :by_status)
params.require(:filters).permit(:by_request_item_id, :by_partner, :by_status, :by_request_type)
end
end
7 changes: 7 additions & 0 deletions app/models/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# discard_reason :text
# discarded_at :datetime
# request_items :jsonb
# request_type :string
# status :integer default("pending")
# created_at :datetime not null
# updated_at :datetime not null
Expand All @@ -31,6 +32,7 @@ class Request < ApplicationRecord
has_many :child_item_requests, through: :item_requests

enum status: { pending: 0, started: 1, fulfilled: 2, discarded: 3 }, _prefix: true
enum request_type: %w[quantity individual child].map { |v| [v, v] }.to_h

validates :distribution_id, uniqueness: true, allow_nil: true
validate :item_requests_uniqueness_by_item_id
Expand All @@ -45,6 +47,7 @@ class Request < ApplicationRecord
scope :by_partner, ->(partner_id) { where(partner_id: partner_id) }
# status scope to allow filtering by status
scope :by_status, ->(status) { where(status: status) }
scope :by_request_type, ->(request_type) { where(request_type: request_type) }
scope :during, ->(range) { where(created_at: range) }
scope :for_csv_export, ->(organization, *) {
where(organization: organization)
Expand All @@ -60,6 +63,10 @@ def user_email
partner_user_id ? User.find_by(id: partner_user_id).email : Partner.find_by(id: partner_id).email
end

def request_type_label
request_type&.first&.capitalize
end

private

def item_requests_uniqueness_by_item_id
Expand Down
3 changes: 3 additions & 0 deletions app/services/exports/export_request_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def base_table
"Requestor" => ->(request) {
request.partner.name
},
"Type" => ->(request) {
request.request_type&.humanize
},
"Status" => ->(request) {
request.status.humanize
}
Expand Down
8 changes: 6 additions & 2 deletions app/services/partners/family_request_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def call
request_create_svc = Partners::RequestCreateService.new(
partner_user_id: partner_user_id,
comments: comments,
for_families: @for_families,
request_type: request_type,
item_requests_attributes: item_requests_attributes
)

Expand All @@ -43,7 +43,7 @@ def initialize_only
Partners::RequestCreateService.new(
partner_user_id: partner_user_id,
comments: comments,
for_families: @for_families,
request_type: request_type,
item_requests_attributes: item_requests_attributes
).initialize_only
end
Expand Down Expand Up @@ -81,5 +81,9 @@ def convert_person_count_to_item_quantity(item_id:, person_count:)
def included_items_by_id
@included_items_by_id ||= Item.where(id: family_requests_attributes.pluck(:item_id)).index_by(&:id)
end

def request_type
@for_families ? "child" : "individual"
end
end
end
14 changes: 9 additions & 5 deletions app/services/partners/request_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ class RequestCreateService

attr_reader :partner_request

def initialize(partner_user_id:, comments: nil, for_families: false, item_requests_attributes: [], additional_attrs: {})
def initialize(request_type:, partner_user_id:, comments: nil, item_requests_attributes: [], additional_attrs: {})
@partner_user_id = partner_user_id
@comments = comments
@for_families = for_families
@request_type = request_type
@item_requests_attributes = item_requests_attributes
@additional_attrs = additional_attrs
end

def call
@partner_request = ::Request.new(partner_id: partner.id,
@partner_request = ::Request.new(
partner_id: partner.id,
organization_id: organization_id,
comments: comments,
partner_user_id: partner_user_id)
request_type: request_type,
partner_user_id: partner_user_id
)
@partner_request = populate_item_request(@partner_request)
@partner_request.assign_attributes(additional_attrs)

Expand Down Expand Up @@ -44,6 +47,7 @@ def initialize_only
partner_request = ::Request.new(partner_id: partner.id,
organization_id: organization_id,
comments: comments,
request_type: request_type,
partner_user_id: partner_user_id)
partner_request = populate_item_request(partner_request)
partner_request.assign_attributes(additional_attrs)
Expand All @@ -52,7 +56,7 @@ def initialize_only

private

attr_reader :partner_user_id, :comments, :item_requests_attributes, :additional_attrs
attr_reader :partner_user_id, :comments, :item_requests_attributes, :additional_attrs, :request_type

def populate_item_request(partner_request)
# Exclude any line item that is completely empty
Expand Down
5 changes: 5 additions & 0 deletions app/views/requests/_request_row.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<td>
<%= request_row.comments %>
</td>
<td>
<abbr class="text-decoration-none" title="<%= request_row.request_type&.humanize %>">
<%= request_row.request_type_label %>
</abbr>
</td>
<td>
<%= render partial: "status", locals: {status: request_row.status} %>
</td>
Expand Down
4 changes: 4 additions & 0 deletions app/views/requests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<%= filter_select(scope: :by_partner, collection: @partners, selected: @selected_partner) %>
</div>
<% end %>
<div class="form-group col-lg-3 col-md-3 col-sm-6 col-xs-12">
<%= filter_select(scope: :by_request_type, collection: @request_types, key: :last, value: :first, selected: @selected_request_type) %>
</div>
<div class="form-group col-lg-3 col-md-3 col-sm-6 col-xs-12">
<%= filter_select(scope: :by_status, collection: @statuses, key: :last, value: :first, selected: @selected_status) %>
</div>
Expand Down Expand Up @@ -98,6 +101,7 @@
<th>Request Sender</th>
<th># of Items (Request Limit)</th>
<th>Comments</th>
<th>Type</th>
<th>Status</th>
<th class="text-right">Actions</th>
</tr>
Expand Down
2 changes: 2 additions & 0 deletions app/views/requests/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<tr>
<th>Request was sent by:</th>
<th>Request Sender:</th>
<th>Request Type:</th>
<th>Request Status:</th>
<th>Comments:</th>
</tr>
Expand All @@ -45,6 +46,7 @@
<tr>
<td><%= @request.partner.name %></td>
<td><%= @request.partner_user&.formatted_email %></td>
<td><%= @request.request_type&.humanize %></td>
<td><%= render partial: "status", locals: {status: @request.status} %></td>
<td><%= @request.comments || 'None' %></td>
</tr>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240315190152_add_type_to_requests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddTypeToRequests < ActiveRecord::Migration[7.0]
def change
add_column :requests, :request_type, :string
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,7 @@
t.datetime "discarded_at", precision: nil
t.text "discard_reason"
t.integer "partner_user_id"
t.string "request_type"
t.index ["discarded_at"], name: "index_requests_on_discarded_at"
t.index ["distribution_id"], name: "index_requests_on_distribution_id", unique: true
t.index ["organization_id"], name: "index_requests_on_organization_id"
Expand Down
13 changes: 13 additions & 0 deletions spec/factories/requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# discard_reason :text
# discarded_at :datetime
# request_items :jsonb
# request_type :string
# status :integer default("pending")
# created_at :datetime not null
# updated_at :datetime not null
Expand Down Expand Up @@ -57,6 +58,18 @@ def random_request_items
status { 'fulfilled' }
end

trait :quantity do
request_type { 'quantity' }
end

trait :individual do
request_type { 'individual' }
end

trait :child do
request_type { 'child' }
end

trait :pending do
status { 'pending' }
end
Expand Down
9 changes: 9 additions & 0 deletions spec/models/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# discard_reason :text
# discarded_at :datetime
# request_items :jsonb
# request_type :string
# status :integer default("pending")
# created_at :datetime not null
# updated_at :datetime not null
Expand Down Expand Up @@ -127,6 +128,14 @@
end
end

describe "request_type_label" do
let(:request) { build(:request, request_type: "individual") }

it "returns the the first letter of the request_type capitalized" do
expect(request.request_type_label).to eq("I")
end
end

describe "versioning" do
it { is_expected.to be_versioned }
end
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/items_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@
it 'shows custom request units when flipper enabled' do
Flipper.enable(:enable_packs)
get item_path(id: item.id)
print(response.body)

expect(response.body).to include('Custom Units')
expect(response.body).to include("ITEM1; ITEM2")
end
Expand Down
Loading

0 comments on commit b761496

Please sign in to comment.