-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor update owner audit activity to store metadata, and remove ca…
…llbacks and dependency on User.current
- Loading branch information
Showing
36 changed files
with
281 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class ChangeCaseOwnerForm | ||
include ActiveModel::Model | ||
include ActiveModel::Attributes | ||
|
||
attribute :owner_id | ||
attribute :owner_rationale | ||
|
||
validates_presence_of :owner_id | ||
validate :new_owner_must_be_active_user_or_team | ||
|
||
def owner | ||
user || team | ||
end | ||
|
||
private | ||
|
||
def new_owner_must_be_active_user_or_team | ||
errors.add(:owner_id, :not_found) unless owner | ||
end | ||
|
||
def user | ||
@user ||= User.active.find_by(id: owner_id) | ||
end | ||
|
||
def team | ||
@team ||= Team.find_by(id: owner_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 16 additions & 16 deletions
32
psd-web/app/models/audit_activity/investigation/automatically_update_owner.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,31 @@ | ||
class AuditActivity::Investigation::AutomaticallyUpdateOwner < AuditActivity::Investigation::Base | ||
def self.from(investigation) | ||
title = investigation.owner.id.to_s | ||
super(investigation, title) | ||
def self.from(*) | ||
raise "Deprecated - use DeleteUser.call instead" | ||
end | ||
|
||
def owner_id | ||
# We store owner_id in title field, this is getting it back | ||
# Using alias for accessing parent method causes errors elsewhere :( | ||
AuditActivity::Investigation::Base.instance_method(:title).bind(self).call | ||
def self.build_metadata(owner) | ||
{ | ||
owner_id: owner.id | ||
} | ||
end | ||
|
||
# We store owner_id in title field, this is computing title based on that | ||
def title | ||
def title(user) | ||
type = investigation.case_type.capitalize | ||
new_owner = (User.find_by(id: owner_id) || Team.find_by(id: owner_id))&.decorate&.display_name | ||
new_owner = owner.decorate.display_name(viewer: user) | ||
"Case owner automatically changed on #{type} to #{new_owner}" | ||
end | ||
|
||
def subtitle | ||
def subtitle(_user) | ||
"Case owner automatically changed, #{pretty_date_stamp}" | ||
end | ||
|
||
def entities_to_notify | ||
[] | ||
end | ||
private | ||
|
||
def email_subject_text; end | ||
def owner | ||
User.find_by(id: metadata["owner_id"]) || Team.find_by(id: metadata["owner_id"]) | ||
end | ||
|
||
def email_update_text(viewer = nil); end | ||
# Do not send investigation_updated mail when case added. This overrides | ||
# inherited functionality in the Activity model :( | ||
def notify_relevant_users; end | ||
end |
4 changes: 2 additions & 2 deletions
4
psd-web/app/models/audit_activity/investigation/team_added.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
psd-web/app/models/audit_activity/investigation/team_deleted.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 18 additions & 50 deletions
68
psd-web/app/models/audit_activity/investigation/update_owner.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,34 @@ | ||
class AuditActivity::Investigation::UpdateOwner < AuditActivity::Investigation::Base | ||
include NotifyHelper | ||
|
||
def self.from(investigation) | ||
title = investigation.owner.id.to_s | ||
body = investigation.owner_rationale | ||
super(investigation, title, sanitize_text(body)) | ||
end | ||
|
||
def owner_id | ||
# We store owner_id in title field, this is getting it back | ||
# Using alias for accessing parent method causes errors elsewhere :( | ||
AuditActivity::Investigation::Base.instance_method(:title).bind(self).call | ||
def self.from(*) | ||
raise "Deprecated - use ChangeCaseOwner.call instead" | ||
end | ||
|
||
def title | ||
# We store owner_id in title field, this is computing title based on that | ||
"Case owner changed to #{(User.find_by(id: owner_id) || Team.find_by(id: owner_id))&.decorate&.display_name}" | ||
def self.build_metadata(owner, rationale) | ||
{ | ||
owner_id: owner.id, | ||
rationale: rationale | ||
} | ||
end | ||
|
||
def email_update_text(viewer = nil) | ||
body = [] | ||
body << "Case owner changed on #{investigation.case_type} to #{investigation.owner.decorate.display_name(viewer: viewer)} by #{source&.show(viewer)}." | ||
|
||
if investigation.owner_rationale.present? | ||
body << "Message from #{source&.show(viewer)}:" | ||
body << inset_text_for_notify(investigation.owner_rationale) | ||
end | ||
|
||
body.join("\n\n") | ||
def title(user) | ||
"Case owner changed to #{owner.decorate.display_name(viewer: user)}" | ||
end | ||
|
||
def email_subject_text | ||
"Case owner changed for #{investigation.case_type}" | ||
def body | ||
metadata["rationale"] | ||
end | ||
|
||
private | ||
|
||
def subtitle_slug | ||
"Changed" | ||
def owner | ||
User.find_by(id: metadata["owner_id"]) || Team.find_by(id: metadata["owner_id"]) | ||
end | ||
|
||
def users_to_notify | ||
compute_relevant_entities(model: User, compute_users_from_entity: proc { |user| [user] }) | ||
end | ||
|
||
def teams_to_notify | ||
compute_relevant_entities(model: Team, compute_users_from_entity: proc { |team| team.users }) | ||
def subtitle_slug | ||
"Changed" | ||
end | ||
|
||
def compute_relevant_entities(model:, compute_users_from_entity:) | ||
return [] unless investigation.saved_changes.key?("owner_id") | ||
|
||
previous_owner_id = investigation.saved_changes["owner_id"][0] | ||
previous_owner = model.find_by(id: previous_owner_id) | ||
new_owner = investigation.owner | ||
owner_changed_by = source.user | ||
|
||
old_users = previous_owner.present? ? compute_users_from_entity.call(previous_owner) : [] | ||
old_entities = previous_owner.present? ? [previous_owner] : [] | ||
new_entities = new_owner.is_a?(model) ? [new_owner] : [] | ||
return new_entities if old_users.include? owner_changed_by | ||
|
||
(new_entities + old_entities).uniq | ||
end | ||
# Do not send investigation_updated mail when case added. This overrides | ||
# inherited functionality in the Activity model :( | ||
def notify_relevant_users; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.