Skip to content

Commit

Permalink
Move household event creation logic into the HouseholdEvent class
Browse files Browse the repository at this point in the history
  • Loading branch information
martha committed Feb 6, 2025
1 parent e6661b2 commit d8eb511
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 19 deletions.
37 changes: 18 additions & 19 deletions drivers/hmis/app/graphql/mutations/join_household.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,24 @@ def resolve(receiving_household_id:, joining_enrollment_inputs:)
donor_after_state = donor_household.snapshot_household_state
end

joining_event = Hmis::HouseholdEvent.new
joining_event.user = current_user
joining_event.household = receiving_household
joining_event.event_type = Hmis::HouseholdEvent::JOIN
joining_event.event_details = {
'donor_household_id': donor_household.household_id,
'before': receiving_before_state,
'after': receiving_after_state,
}

leaving_event = Hmis::HouseholdEvent.new
leaving_event.user = current_user
leaving_event.household = donor_household
leaving_event.event_type = Hmis::HouseholdEvent::SPLIT
leaving_event.event_details = {
'receiving_household_id': receiving_household_id,
'before': donor_before_state,
'after': donor_after_state,
}
# Create two events: One representing the JOIN *into* the receiving household...
joining_event = Hmis::HouseholdEvent.new_join_event(
user: current_user,
household: receiving_household,
donor_household_id: donor_household.household_id,
before_state: receiving_before_state,
after_state: receiving_after_state,
)

# ...and one representing the SPLIT *out from* the donor (remaining) household
leaving_event = Hmis::HouseholdEvent.new_split_event(
user: current_user,
household: donor_household,
receiving_household_id: receiving_household_id,
before_state: donor_before_state,
after_state: donor_after_state,
)

Hmis::HouseholdEvent.import!([joining_event, leaving_event])
end

Expand Down
26 changes: 26 additions & 0 deletions drivers/hmis/app/models/hmis/household_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,30 @@ class Hmis::HouseholdEvent < Hmis::HmisBase
validates :event_type, inclusion: { in: EVENT_TYPES, message: '%{value} is not a valid event type' }

alias_attribute :household_id, :HouseholdID

def self.new_join_event(user:, household:, donor_household_id:, before_state:, after_state:)
new(
event_type: Hmis::HouseholdEvent::JOIN,
user: user,
household: household,
event_details: {
'donor_household_id': donor_household_id,
'before': before_state,
'after': after_state,
},
)
end

def self.new_split_event(user:, household:, receiving_household_id:, before_state:, after_state:)
new(
event_type: Hmis::HouseholdEvent::SPLIT,
user: user,
household: household,
event_details: {
'receiving_household_id': receiving_household_id,
'before': before_state,
'after': after_state,
},
)
end
end

0 comments on commit d8eb511

Please sign in to comment.