Skip to content

Commit 8f1fc0a

Browse files
daynewedwinthinks
andauthored
Add "Pick up" vs "Delivery" for Distributions (#1818)
* Adding 'Delivery method' for Distributions * Updating database seed with new 'delivery_method' field * Updating the delivery_method of a distribution will trigger a notification * Updated notification messages to indicate 'delivery_method' change * Updated 'partner_mailer' to specify delivery or pickup * Updated reminder_email to specify 'pick up' or 'delivery' * Changing 'pick up' usage to be more general * Fixing the distribution changed email and various unit tests * Make the Delivery Method a radio select * Adding default 'delivery_method' to be pick_up in order to backfill existing Distributions * Making helper methods private * Fixing unit tests to reflect UI changes for delivery_method * Update app/controllers/distributions_controller.rb * Update spec/requests/distributions_requests_spec.rb Co-authored-by: Edwin Mak <[email protected]>
1 parent 8b1ad9b commit 8f1fc0a

26 files changed

+118
-30
lines changed

app/controllers/distributions_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def update
109109

110110
if result.success?
111111
if result.resend_notification? && @distribution.partner&.send_reminders
112-
send_notification(current_organization.id, @distribution.id, subject: "Your Distribution New Schedule Date is #{@distribution.issued_at}")
112+
send_notification(current_organization.id, @distribution.id, subject: "Your Distribution Date Has Changed")
113113
end
114114
schedule_reminder_email(@distribution)
115115

@@ -133,9 +133,9 @@ def picked_up
133133
distribution = current_organization.distributions.find(params[:id])
134134

135135
if !distribution.complete? && distribution.complete!
136-
flash[:notice] = 'This distribution has been marked as being picked up!'
136+
flash[:notice] = 'This distribution has been marked as being completed!'
137137
else
138-
flash[:error] = 'Sorry, we encountered an error when trying to mark this distribution as being picked up'
138+
flash[:error] = 'Sorry, we encountered an error when trying to mark this distribution as being completed'
139139
end
140140

141141
redirect_back(fallback_location: distribution_path)
@@ -164,7 +164,7 @@ def schedule_reminder_email(distribution)
164164
end
165165

166166
def distribution_params
167-
params.require(:distribution).permit(:comment, :agency_rep, :issued_at, :partner_id, :storage_location_id, :reminder_email_enabled, line_items_attributes: %i(item_id quantity _destroy))
167+
params.require(:distribution).permit(:comment, :agency_rep, :issued_at, :partner_id, :storage_location_id, :reminder_email_enabled, :delivery_method, line_items_attributes: %i(item_id quantity _destroy))
168168
end
169169

170170
def request_id

app/models/distribution.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# id :integer not null, primary key
77
# agency_rep :string
88
# comment :text
9+
# delivery_method :integer default("pick_up"), not null
910
# issued_at :datetime
1011
# reminder_email_enabled :boolean default(FALSE), not null
1112
# state :integer default("started"), not null
@@ -31,7 +32,7 @@ class Distribution < ApplicationRecord
3132
has_one :request, dependent: :nullify
3233
accepts_nested_attributes_for :request
3334

34-
validates :storage_location, :partner, :organization, presence: true
35+
validates :storage_location, :partner, :organization, :delivery_method, presence: true
3536
validate :line_item_items_exist_in_inventory
3637

3738
include IssuedAt
@@ -40,6 +41,8 @@ class Distribution < ApplicationRecord
4041

4142
enum state: { started: 0, scheduled: 5, complete: 10 }
4243

44+
enum delivery_method: { pick_up: 0, delivery: 1 }
45+
4346
include Filterable
4447
# add item_id scope to allow filtering distributions by item
4548
scope :by_item_id, ->(item_id) { joins(:items).where(items: { id: item_id }) }

app/services/distribution_update_service.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def initialize(old_distribution, new_distribution_params)
88
def call
99
perform_distribution_service do
1010
@old_issued_at = distribution.issued_at
11+
@old_delivery_method = distribution.delivery_method
1112
distribution.storage_location.increase_inventory(distribution.to_a)
1213
# Delete the line items -- they'll be replaced later
1314
distribution.line_items.each(&:destroy!)
@@ -16,12 +17,23 @@ def call
1617
distribution.update! @params
1718
distribution.reload
1819
@new_issued_at = distribution.issued_at
20+
@new_delivery_method = distribution.delivery_method
1921
# Apply the new changes to the storage location inventory
2022
distribution.storage_location.decrease_inventory(distribution.to_a)
2123
end
2224
end
2325

2426
def resend_notification?
27+
issued_at_changed? || delivery_method_changed?
28+
end
29+
30+
private
31+
32+
def issued_at_changed?
2533
@old_issued_at.to_date != @new_issued_at.to_date
2634
end
35+
36+
def delivery_method_changed?
37+
@old_delivery_method != @new_delivery_method
38+
end
2739
end

app/views/distribution_mailer/partner_mailer.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,10 @@
341341
<table role="presentation" border="0" cellpadding="0" cellspacing="0">
342342
<tr>
343343
<td>
344+
<% delivery_method = (@distribution.delivery?) ? 'delivered' : 'picked up' %>
344345
<p><%= @partner.name %>,</p>
345346
<p>Your diaper request has been approved and you can find attached to this email a copy of the distribution you will be receiving.</p>
346-
<p>Your distribution has been set to be picked up on <%= @distribution.issued_at.strftime("%m/%d/%Y") %>.</p>
347+
<p>Your distribution has been set to be <b><%= delivery_method %></b> on <b><%= @distribution.issued_at.strftime("%m/%d/%Y") %></b>.</p>
347348
<p>See you soon!</p>
348349
</td>
349350
<tr>

app/views/distribution_mailer/reminder_email.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
55
</head>
66
<body>
7+
<% delivery_method = (@distribution.delivery?) ? "delivery" : "pick up"%>
78
<p>Hello <%= @partner.name %>,</p>
8-
<p>This is a friendly reminder that tomorrow, <%= @distribution.issued_at.strftime("%A, %B #{@distribution.issued_at.day.ordinalize} %Y at %I:%M%p") %>, is your distribution pick up date. </p>
9+
<p>This is a friendly reminder that tomorrow, <b><%= @distribution.issued_at.strftime("%A, %B #{@distribution.issued_at.day.ordinalize} %Y at %I:%M%p") %></b>, is your distribution <b><%= delivery_method %></b> date. </p>
910
</body>
1011
</html>

app/views/distributions/_distribution_row.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
<td><%= distribution_row.storage_location.name %></td>
55
<td class="numeric"><%= distribution_row.line_items.total %></td>
66
<td class="numeric"><%= dollar_value(distribution_row.value_per_itemizable) %></td>
7-
<td><%= distribution_row.state&.humanize %>
7+
<td><%= distribution_row.delivery_method.humanize %></td>
8+
<td><%= distribution_row.state&.humanize %></td>
89

910
<td class="text-right">
1011
<%= view_button_to distribution_path(distribution_row) %>

app/views/distributions/_pickup_day_row.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
<td><%= pickup_day_row.line_items.total %></td>
66
<td>
77
<% if pickup_day_row.complete? %>
8-
Picked up
8+
Complete
99
<% else %>
1010
Scheduled
1111
<% end %>
1212
</td>
1313
<td class="text-right">
14-
<%= update_button_to picked_up_distribution_path(pickup_day_row), {text: "Pick up Complete", size: "xs"} if !pickup_day_row.complete? %>
14+
<%= update_button_to picked_up_distribution_path(pickup_day_row), {text: "Distribution Complete", size: "xs"} if !pickup_day_row.complete? %>
1515
<%= view_button_to distribution_path(pickup_day_row) %>
1616
</td>
1717
</tr>

app/views/distributions/edit.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
<% unless @distribution.future? %>
3030
<div class="alert alert-warning" , role="alert">
31-
The current date is past the date this distribution was picked up.
31+
The current date is past the date this distribution was scheduled for.
3232
Please be very careful when editing this record;
3333
the contents should match what was given to the recipient.
3434
</div>
@@ -60,6 +60,7 @@
6060
<%= f.input :issued_at, as: :datetime, ampm: true, minute_step: 15, label: "Distribution date" %>
6161
<%= f.input :reminder_email_enabled, as: :boolean, checked_value: true, unchecked_value: false, label: "Send email reminder the day before?" %>
6262
<%= f.input :agency_rep, label: "Agency representative" %>
63+
<%= f.input :delivery_method, as: :radio_buttons, collection: Distribution.delivery_methods.keys, label_method: :humanize, label: "Delivery method" %>
6364

6465
<%= render partial: "storage_locations/source", object: f %>
6566

app/views/distributions/index.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<th>Source Inventory</th>
8585
<th class="numeric">Total items</th>
8686
<th class="numeric">Total value</th>
87+
<th>Delivery method</th>
8788
<th>State</th>
8889
<th class="text-right" style="width: 400px">Actions</th>
8990
</tr>

app/views/distributions/new.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<%= f.input :issued_at, as: :datetime, ampm: true, minute_step: 15, label: "Distribution date" %>
5151
<%= f.input :reminder_email_enabled, as: :boolean, checked_value: true, unchecked_value: false, label: "Send email reminder the day before?" %>
5252
<%= f.input :agency_rep, label: "Agency representative" %>
53+
<%= f.input :delivery_method, as: :radio_buttons, collection: Distribution.delivery_methods.keys, label_method: :humanize, label: "Delivery method" %>
5354

5455
<%= render partial: "storage_locations/source", object: f %>
5556

0 commit comments

Comments
 (0)