Skip to content

Commit

Permalink
Merge branch 'main' into 3571-update-invite-pwd-reset-emails
Browse files Browse the repository at this point in the history
  • Loading branch information
kiranbpatil authored Dec 10, 2023
2 parents 9029dd1 + e0b0c40 commit d3a31af
Show file tree
Hide file tree
Showing 18 changed files with 198 additions and 85 deletions.
3 changes: 3 additions & 0 deletions .cloud66/manifest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
rails:
configuration:
ruby_version: 3.2.2
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.2
3.2.2
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end

ruby "3.1.2"

###### BASIC FRAMEWORKS ######

# User management and login workflow.
Expand Down Expand Up @@ -202,7 +200,7 @@ group :test do
# More concise test ("should") matchers
gem 'shoulda-matchers', '~> 5.3'
# Selenium webdriver automatic installation and update.
gem 'selenium-webdriver', '~> 4.10'
gem 'selenium-webdriver', '~> 4.16'
# Mock HTTP requests and ensure they are not called during tests.
gem "webmock", "~> 3.19"
end
Expand Down
29 changes: 13 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ GEM
autoprefixer-rails (>= 9.1.0)
popper_js (>= 2.11.6, < 3)
sassc-rails (>= 2.0.0)
brakeman (6.0.1)
brakeman (6.1.0)
brow (0.4.1)
bugsnag (6.26.0)
concurrent-ruby (~> 1.0)
builder (3.2.4)
bullet (7.1.3)
bullet (7.1.4)
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
capybara (3.39.2)
Expand Down Expand Up @@ -262,7 +262,7 @@ GEM
multi_xml (>= 0.5.2)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
icalendar (2.10.0)
icalendar (2.10.1)
ice_cube (~> 0.16)
ice_cube (0.16.4)
ice_nine (0.11.2)
Expand All @@ -276,7 +276,7 @@ GEM
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.6.3)
json (2.7.1)
jwt (2.7.1)
kaminari (1.2.2)
activesupport (>= 4.1.0)
Expand All @@ -290,7 +290,7 @@ GEM
activerecord
kaminari-core (= 1.2.2)
kaminari-core (1.2.2)
knapsack_pro (5.7.0)
knapsack_pro (6.0.3)
rake
language_server-protocol (3.17.0.3)
launchy (2.5.2)
Expand Down Expand Up @@ -359,11 +359,11 @@ GEM
newrelic_rpm (9.6.0)
base64
nio4r (2.5.9)
nokogiri (1.15.4-arm64-darwin)
nokogiri (1.15.5-arm64-darwin)
racc (~> 1.4)
nokogiri (1.15.4-x86_64-darwin)
nokogiri (1.15.5-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.15.4-x86_64-linux)
nokogiri (1.15.5-x86_64-linux)
racc (~> 1.4)
notiffany (0.1.3)
nenv (~> 0.1)
Expand Down Expand Up @@ -486,7 +486,7 @@ GEM
redis-client (>= 0.17.0)
redis-client (0.18.0)
connection_pool
regexp_parser (2.8.2)
regexp_parser (2.8.3)
request_store (1.5.1)
rack (>= 1.4)
responders (3.1.1)
Expand Down Expand Up @@ -561,7 +561,7 @@ GEM
sprockets (> 3.0)
sprockets-rails
tilt
selenium-webdriver (4.15.0)
selenium-webdriver (4.16.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
Expand Down Expand Up @@ -593,7 +593,7 @@ GEM
actionpack (>= 5.2)
activesupport (>= 5.2)
sprockets (>= 3.0.0)
standard (1.32.0)
standard (1.32.1)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.57.2)
Expand All @@ -609,7 +609,7 @@ GEM
railties (>= 6.0.0)
strong_migrations (1.6.4)
activerecord (>= 5.2)
terser (1.1.19)
terser (1.1.20)
execjs (>= 0.3.0, < 3)
thor (1.3.0)
tilt (2.2.0)
Expand Down Expand Up @@ -729,7 +729,7 @@ DEPENDENCIES
rubocop-rails (~> 2.22.2)
rufus-scheduler
sass-rails
selenium-webdriver (~> 4.10)
selenium-webdriver (~> 4.16)
shoulda-matchers (~> 5.3)
simple_form
simplecov
Expand All @@ -742,8 +742,5 @@ DEPENDENCIES
web-console
webmock (~> 3.19)

RUBY VERSION
ruby 3.1.2p20

BUNDLED WITH
2.4.22
2 changes: 1 addition & 1 deletion app/events/adjustment_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.publish(adjustment)
create(
eventable: adjustment,
organization_id: adjustment.organization_id,
event_time: Time.zone.now,
event_time: adjustment.created_at,
data: EventTypes::InventoryPayload.new(
items: EventTypes::EventLineItem.from_line_items(adjustment.line_items, to: adjustment.storage_location_id)
)
Expand Down
2 changes: 1 addition & 1 deletion app/events/audit_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def self.publish(audit)
create(
eventable: audit,
organization_id: audit.organization_id,
event_time: Time.zone.now,
event_time: audit.updated_at,
data: EventTypes::AuditPayload.new(
storage_location_id: audit.storage_location_id,
items: EventTypes::EventLineItem.from_line_items(audit.line_items, to: audit.storage_location_id)
Expand Down
2 changes: 1 addition & 1 deletion app/events/distribution_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.publish(distribution)
create(
eventable: distribution,
organization_id: distribution.organization_id,
event_time: Time.zone.now,
event_time: distribution.created_at,
data: EventTypes::InventoryPayload.new(
items: EventTypes::EventLineItem.from_line_items(distribution.line_items, from: distribution.storage_location_id)
)
Expand Down
2 changes: 1 addition & 1 deletion app/events/donation_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.publish(donation)
create(
eventable: donation,
organization_id: donation.organization_id,
event_time: Time.zone.now,
event_time: donation.created_at,
data: EventTypes::InventoryPayload.new(
items: EventTypes::EventLineItem.from_line_items(donation.line_items, to: donation.storage_location_id)
)
Expand Down
13 changes: 13 additions & 0 deletions app/events/event_types/inventory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ def self.from(organization_id)
storage_locations: org.storage_locations.map { |s| [s.id, EventTypes::EventStorageLocation.from(s)] }.to_h)
end

# @param item_id [Integer]
# @param quantity [Integer]
# @param location [Integer]
def set_item_quantity(item_id:, quantity:, location:)
storage_locations[location] ||= EventTypes::EventStorageLocation.new(id: location, items: {})
storage_locations[location].set_inventory(item_id, quantity)
end

# @param item_id [Integer]
# @param quantity [Integer]
# @param from_location [Integer]
# @param to_location [Integer]
# @param validate [Boolean]
def move_item(item_id:, quantity:, from_location: nil, to_location: nil, validate: true)
if from_location
if storage_locations[from_location].nil? && validate
Expand Down
43 changes: 31 additions & 12 deletions app/events/inventory_aggregate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,37 @@ def on(*event_types, &block)
end
end

# @param organization_id
# @param organization_id [Integer]
# @param first_event [Integer]
# @param last_event [Integer]
# @param validate [Boolean]
# @return [EventTypes::Inventory]
def inventory_for(organization_id)
def inventory_for(organization_id, first_event: nil, last_event: nil, validate: false)
events = Event.for_organization(organization_id)
if first_event
events = events.where("id >= ?", first_event)
end
if last_event
events = events.where("id <= ?", last_event)
end
inventory = EventTypes::Inventory.from(organization_id)
events.group_by { |e| [e.eventable_type, e.eventable_id] }.each do |_, event_batch|
last_event = event_batch.max_by(&:event_time)
handle(last_event, inventory)
last_grouped_event = event_batch.max_by(&:updated_at)
handle(last_grouped_event, inventory, validate: validate)
end
inventory
end

# @param event [Event]
# @param inventory [Inventory]
def handle(event, inventory)
# @param validate [Boolean]
def handle(event, inventory, validate: false)
handler = @handlers[event.class]
if handler.nil?
Rails.logger.warn("No handler found for #{event.class}, skipping")
return
end
handler.call(event, inventory)
handler.call(event, inventory, validate: validate)
end

# @param payload [EventTypes::InventoryPayload]
Expand All @@ -43,21 +53,30 @@ def handle_inventory_event(payload, inventory, validate: true)
validate: validate)
end
end

# @param payload [EventTypes::InventoryPayload]
# @param inventory [EventTypes::Inventory]
def handle_audit_event(payload, inventory)
payload.items.each do |line_item|
inventory.set_item_quantity(item_id: line_item.item_id,
quantity: line_item.quantity,
location: line_item.to_storage_location)
end
end
end

on DonationEvent, DistributionEvent, AdjustmentEvent, PurchaseEvent,
TransferEvent, DistributionDestroyEvent, DonationDestroyEvent,
PurchaseDestroyEvent, TransferDestroyEvent,
KitAllocateEvent, KitDeallocateEvent do |event, inventory|
handle_inventory_event(event.data, inventory, validate: false)
KitAllocateEvent, KitDeallocateEvent do |event, inventory, validate: false|
handle_inventory_event(event.data, inventory, validate: validate)
end

on AuditEvent do |event, inventory|
inventory.storage_locations[event.data.storage_location_id].reset!
handle_inventory_event(event.data, inventory, validate: false)
on AuditEvent do |event, inventory, validate: false|
handle_audit_event(event.data, inventory)
end

on SnapshotEvent do |event, inventory|
on SnapshotEvent do |event, inventory, validate: false|
inventory.storage_locations.clear
inventory.storage_locations.merge!(event.data.storage_locations)
end
Expand Down
2 changes: 1 addition & 1 deletion app/events/purchase_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.publish(purchase)
create(
eventable: purchase,
organization_id: purchase.organization_id,
event_time: Time.zone.now,
event_time: purchase.created_at,
data: EventTypes::InventoryPayload.new(
items: EventTypes::EventLineItem.from_line_items(purchase.line_items, to: purchase.storage_location_id)
)
Expand Down
2 changes: 1 addition & 1 deletion app/events/transfer_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.publish(transfer)
create(
eventable: transfer,
organization_id: transfer.organization_id,
event_time: Time.zone.now,
event_time: transfer.created_at,
data: EventTypes::InventoryPayload.new(
items: EventTypes::EventLineItem.from_line_items(transfer.line_items,
from: transfer.from.id,
Expand Down
13 changes: 12 additions & 1 deletion app/models/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,17 @@ def invite_new_partner
end

def should_invite_because_email_changed?
email_changed? and (invited? or awaiting_review? or recertification_required? or approved?)
email_changed? &&
(
invited? ||
awaiting_review? ||
recertification_required? ||
approved?
) &&
!partner_user_with_same_email_exist?
end

def partner_user_with_same_email_exist?
User.exists?(email: email) && User.find_by(email: email).has_role?(Role::PARTNER, self)
end
end
2 changes: 2 additions & 0 deletions app/views/users/mailer/reset_password_instructions.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<p>Hello <%= @resource.email %>!</p>

<p>This is an automated email from the human essentials application. Please contact your diaper bank directly with any request-related questions.</p>

<p>Someone has requested a link to change your password. You can do this through the link below.</p>

<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20231201194409_fix_event_times.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class FixEventTimes < ActiveRecord::Migration[7.0]
def change
Event.where(type: %i(DistributionEvent DonationEvent PurchaseEvent)).find_each do |event|
next if event.eventable.nil?

event.update_attribute(:event_time, event.eventable.created_at)
end
end
end
2 changes: 1 addition & 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: 2023_11_17_141301) do
ActiveRecord::Schema[7.0].define(version: 2023_12_01_194409) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down
Loading

0 comments on commit d3a31af

Please sign in to comment.