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

4175 add request type flag #4271

Closed
Closed
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
35c4e27
Adds type enum to request model.
nathangthomas Mar 15, 2024
d1911fe
Adds request type enum to request model.
nathangthomas Mar 19, 2024
3007e64
Adds request_type to request.
nathangthomas Mar 19, 2024
7ae36b4
Adds request_type to request.
nathangthomas Mar 19, 2024
a2c79df
Passes request_type to FamilyRequestCreateService.new
nathangthomas Mar 19, 2024
7e17565
Passes request_type to FamilyRequestCreateService.new
nathangthomas Mar 19, 2024
9b5977e
Passes request_type to RequestCreateService.new
nathangthomas Mar 19, 2024
854cd39
Passes request_type to Request.new.
nathangthomas Mar 19, 2024
8435518
Adds hidden field in order to pass request_type param on submit.
nathangthomas Mar 19, 2024
45a9495
Adds request type flag to index and show pages as well as the request…
nathangthomas Mar 19, 2024
4791021
Adds by_request_type scope for filtering by request type.
nathangthomas Mar 19, 2024
3866a8f
Adds request type flag to index and show pages as well as the request…
nathangthomas Mar 19, 2024
8a775ec
Adds by_request_type filter.
nathangthomas Mar 19, 2024
a037b1c
Updates flag names and removes flag styles based on feedback from rub…
nathangthomas Apr 2, 2024
26afeb4
Adds test for request_type enum.
nathangthomas Apr 3, 2024
5c2997b
Removes unnecessary line.
nathangthomas Apr 8, 2024
0cd4451
Adds Type to expected_headers.
nathangthomas Apr 8, 2024
94a1be4
Adds request_type traits to request factory.
nathangthomas Apr 8, 2024
42f1f84
Adds request_type to family_request_create_service_spec args.
nathangthomas Apr 8, 2024
c6e45c8
Adds request_type to args.
nathangthomas Apr 8, 2024
0cfddca
Merge branch 'main' into 4175-add-request-type-flag
nathangthomas Apr 12, 2024
67e9d71
Pass request_type directly.
nathangthomas Apr 22, 2024
d8e9806
Remove request_type partial and enter text directly into reqest_row
nathangthomas Apr 22, 2024
1e19d30
Revert changes to database.yml
nathangthomas Apr 22, 2024
43f7365
Revert changes to request_spec.rb
nathangthomas Apr 22, 2024
c1792ac
Remove request_type
nathangthomas Apr 22, 2024
6441532
Remove hidden request_type fields.
nathangthomas Apr 22, 2024
a27b4e6
Fix merge conflicts / merge remote.
nathangthomas Apr 23, 2024
d53785c
Change request_type data_type to string, replace request_type string …
nathangthomas May 15, 2024
56b7c04
Merge branch 'main' of github.com:unabridged/human-essentials into 41…
nathangthomas May 15, 2024
36cb9b2
Merge branch 'main' into 4175-add-request-type-flag
nathangthomas May 15, 2024
41a0429
Remove request_type from tests
nathangthomas May 15, 2024
c0af65c
Merge branch '4175-add-request-type-flag' of github.com:unabridged/hu…
nathangthomas May 15, 2024
f46736e
Add request_type_label method
nathangthomas May 16, 2024
656e712
Undo changes to versions
nathangthomas May 16, 2024
3a0e841
Add request_type_label method
nathangthomas May 16, 2024
389e6d9
Add request_type to initialize
nathangthomas May 17, 2024
c1acb98
Merge branch 'main' into 4175-add-request-type-flag
nathangthomas May 17, 2024
15ecc79
Fix request_type bug
nathangthomas May 21, 2024
9c0572b
Merge branch '4175-add-request-type-flag' of github.com:unabridged/hu…
nathangthomas May 21, 2024
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
1 change: 1 addition & 0 deletions app/controllers/partners/family_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def create
end

create_service = Partners::FamilyRequestCreateService.new(
request_type: params[:request_type],
nathangthomas marked this conversation as resolved.
Show resolved Hide resolved
partner_user_id: current_user.id,
family_requests_attributes: family_requests_attributes,
for_families: true
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/partners/individuals_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def new

def create
create_service = Partners::FamilyRequestCreateService.new(
request_type: params[:request_type],
nathangthomas marked this conversation as resolved.
Show resolved Hide resolved
partner_user_id: current_user.id,
comments: individuals_request_params[:comments],
family_requests_attributes: individuals_request_params[:items_attributes]&.values
Expand All @@ -36,7 +37,7 @@ def create

def individuals_request_params
params.require(:partners_family_request)
.permit(:comments, items_attributes: %i[item_id person_count])
.permit(:request_type, :comments, items_attributes: %i[item_id person_count])
end
end
end
3 changes: 2 additions & 1 deletion 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: params[:request_type],
partner_user_id: current_user.id,
comments: partner_request_params[:comments],
item_requests_attributes: partner_request_params[:item_requests_attributes]&.values || []
Expand All @@ -45,7 +46,7 @@ def create
private

def partner_request_params
params.require(:request).permit(:comments, item_requests_attributes: [:item_id, :quantity])
params.require(:request).permit(:request_type, :comments, item_requests_attributes: [:item_id, :quantity])
end
end
end
8 changes: 5 additions & 3 deletions app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ def index
@calculate_product_totals = RequestsTotalItemsService.new(requests: @requests).calculate
@items = current_organization.items.alphabetized
@partners = current_organization.partners.order(:name)
@statuses = Request.statuses.transform_keys(&:humanize)
@selected_partner = filter_params[:by_partner]
@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]
@statuses = Request.statuses.transform_keys(&:humanize)
@selected_status = filter_params[:by_status]

respond_to do |format|
Expand Down Expand Up @@ -57,6 +59,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
3 changes: 3 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 :integer
# 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: { quantity: 0, individual: 1, child: 2 }, _prefix: true

validates :distribution_id, uniqueness: true, allow_nil: true
before_save :sanitize_items_data
Expand All @@ -42,6 +44,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 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
4 changes: 3 additions & 1 deletion app/services/partners/family_request_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class FamilyRequestCreateService

attr_reader :partner_user_id, :comments, :family_requests_attributes, :partner_request

def initialize(partner_user_id:, family_requests_attributes:, comments: nil, for_families: false)
def initialize(request_type:, partner_user_id:, family_requests_attributes:, comments: nil, for_families: false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, why was this put back? We agreed that it wouldn't be needed because the service itself knows what type it is just by the current parameters being passed in.

@request_type = request_type
@partner_user_id = partner_user_id
@comments = comments
@family_requests_attributes = family_requests_attributes.presence || []
Expand All @@ -19,6 +20,7 @@ def call
return self unless valid?

request_create_svc = Partners::RequestCreateService.new(
request_type: @request_type.to_i,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This service should already know the request type just by whether for_families is true or false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that defining request_type in this way is more transparent. Would you prefer to add logic in the controller to determine request_type rather than using the instance var?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer putting the business logic as far back as possible. The controller is calling a service, which already has to know what kind of request it's creating. We can rely on the service to set the request type.

partner_user_id: partner_user_id,
comments: comments,
for_families: @for_families,
Expand Down
7 changes: 5 additions & 2 deletions app/services/partners/request_create_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ 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, for_families: false, item_requests_attributes: [], additional_attrs: {})
@request_type = request_type
@partner_user_id = partner_user_id
@comments = comments
@for_families = for_families
Expand All @@ -13,7 +14,9 @@ def initialize(partner_user_id:, comments: nil, for_families: false, item_reques
end

def call
@partner_request = ::Request.new(partner_id: partner.id,
@partner_request = ::Request.new(
request_type: @request_type.to_i,
partner_id: partner.id,
organization_id: organization_id,
comments: comments,
partner_user_id: partner_user_id)
Expand Down
1 change: 1 addition & 0 deletions app/views/partners/family_requests/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<hr>

<div class="card-footer">
<%= hidden_field_tag :request_type, 2 %>
<%= submit_tag("Submit Essentials Request", class: "btn btn-success") %>
<%= link_to "Cancel Request", partners_requests_path, class: "btn btn-danger" %>
</div>
Expand Down
1 change: 1 addition & 0 deletions app/views/partners/individuals_requests/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<hr>

<div class="card-footer">
<%= hidden_field_tag :request_type, 1 %>
<!-- TODO(chaserx): we should add some js to prevent submission if the items selected are the blank option or any item has an empty quantity -->
<%= form.submit("Submit Essentials Request", class: "btn btn-success") %>
<%= link_to "Cancel Request", partners_requests_path, class: "btn btn-danger" %>
Expand Down
1 change: 1 addition & 0 deletions app/views/partners/requests/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

</div>
<div class="card-footer">
<%= hidden_field_tag :request_type, 0 %>
<!-- TODO(chaserx): we should add some js to prevent submission if the items selected are the blank option or any item has an empty quantity -->
<%= form.submit("Submit Essentials Request", class: "btn btn-success") %> <%= link_to "Cancel Request", partners_requests_path, class: "btn btn-danger" %>
</div>
Expand Down
3 changes: 3 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,9 @@
<td>
<%= request_row.comments %>
</td>
<td>
<%= render partial: "request_type", locals: {request_type: request_row.request_type} %>
</td>
<td>
<%= render partial: "status", locals: {status: request_row.status} %>
</td>
Expand Down
8 changes: 8 additions & 0 deletions app/views/requests/_request_type.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<% case request_type %>
nathangthomas marked this conversation as resolved.
Show resolved Hide resolved
<% when "quantity" %>
<div>Quantity<div>
<% when "individual" %>
<div>Individual<div>
<% when "child" %>
<div>Child</div>
<% end %>
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 @@ -92,6 +95,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><%= render partial: "request_type", locals: {request_type: @request.request_type} %></td>
<td><%= render partial: "status", locals: {status: @request.status} %></td>
<td><%= @request.comments || 'None' %></td>
</tr>
Expand Down
3 changes: 2 additions & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
default: &default
adapter: postgresql
encoding: unicode
host: <%= ENV.fetch("PG_HOST", 'localhost') %>
host: <%= ENV['DB_HOST'] || 'localhost' %>
nathangthomas marked this conversation as resolved.
Show resolved Hide resolved

database: diaper_dev
username: <%= ENV.fetch("PG_USERNAME", 'postgres') %>
password: <%= ENV.fetch("PG_PASSWORD", nil) %>
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, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2024_04_02_230156) do
ActiveRecord::Schema[7.0].define(version: 2024_03_15_190152) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -735,6 +735,7 @@
t.datetime "discarded_at", precision: nil
t.text "discard_reason"
t.integer "partner_user_id"
t.integer "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 :integer
# status :integer default("pending")
# created_at :datetime not null
# updated_at :datetime not null
Expand Down Expand Up @@ -42,6 +43,18 @@ def random_request_items
status { 'pending' }
end

trait :quantity do
request_type { 'quantity' }
end

trait :individual do
request_type { 'individual' }
end

trait :child do
request_type { 'child' }
end

trait :with_duplicates do
request_items {
# get 3 unique item ids
Expand Down
18 changes: 17 additions & 1 deletion 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 :integer
# status :integer default("pending")
# created_at :datetime not null
# updated_at :datetime not null
Expand All @@ -23,12 +24,27 @@
let!(:request_started) { create(:request, :started) }
let!(:request_fulfilled) { create(:request, :fulfilled) }

it "scopes" do
it "scopes by status" do
expect(Request.status_pending).to eq([request_pending])
expect(Request.status_started).to eq([request_started])
expect(Request.status_fulfilled).to eq([request_fulfilled])
end
end

describe "#request_type" do
it "scopes by request_type" do
nathangthomas marked this conversation as resolved.
Show resolved Hide resolved
request = Request.new

request.request_type = :quantity
assert_equal "quantity", request.request_type

request.request_type = :individual
assert_equal "individual", request.request_type

request.request_type = :child
assert_equal "child", request.request_type
end
end
end

describe "item data" do
Expand Down
2 changes: 1 addition & 1 deletion spec/services/exports/export_request_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
let(:expected_headers) do
expected_headers_item_headers = [item_2t, item_3t].map(&:name).sort
expected_headers_item_headers << Exports::ExportRequestService::DELETED_ITEMS_COLUMN_HEADER
%w(Date Requestor Status) + expected_headers_item_headers
%w(Date Requestor Type Status) + expected_headers_item_headers
end

it "includes headers as the first row with ordered item names alphabetically with deleted item included at the end" do
Expand Down
3 changes: 2 additions & 1 deletion spec/services/partners/family_request_create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
subject { described_class.new(**args).call }
let(:args) do
{
request_type: 0,
partner_user_id: partner_user.id,
comments: comments,
for_families: for_families,
Expand Down Expand Up @@ -108,7 +109,7 @@
let(:fake_request_create_service) { instance_double(Partners::RequestCreateService, call: -> {}, errors: [], partner_request: -> {}) }

before do
allow(Partners::RequestCreateService).to receive(:new).with(partner_user_id: partner_user.id, comments: comments, for_families: false, item_requests_attributes: contain_exactly(*expected_item_request_attributes)).and_return(fake_request_create_service)
allow(Partners::RequestCreateService).to receive(:new).with(request_type: 0, partner_user_id: partner_user.id, comments: comments, for_families: false, item_requests_attributes: contain_exactly(*expected_item_request_attributes)).and_return(fake_request_create_service)
end

it 'should send the correct request payload to the Partners::RequestCreateService and call it' do
Expand Down
1 change: 1 addition & 0 deletions spec/services/partners/request_create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
subject { described_class.new(**args).call }
let(:args) do
{
request_type: 0,
partner_user_id: partner_user.id,
comments: comments,
item_requests_attributes: item_requests_attributes
Expand Down