Skip to content

Commit

Permalink
CV2-6152: inherit status to confirmed items
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy committed Feb 12, 2025
1 parent 0e95c0e commit 7848322
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 40 deletions.
36 changes: 4 additions & 32 deletions app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,25 @@ def suggestion_accepted?
self.relationship_type_before_last_save.to_json == Relationship.suggested_type.to_json && self.is_confirmed?
end

def self.inherit_status_and_send_report(rid)
def self.smooch_send_report(rid)
relationship = Relationship.find_by_id(rid)
unless relationship.nil?
target = relationship.target
parent = relationship.source

# Always inherit status for confirmed matches
if ::Bot::Smooch.team_has_smooch_bot_installed(target) && relationship.is_confirmed?
s = target.annotations.where(annotation_type: 'verification_status').last&.load
status = parent.last_verification_status
if !s.nil? && s.status != status
s.status = status
s.bypass_status_publish_check = true
s.save
end

# A relationship created by the Smooch Bot or Alegre Bot is related to search results (unless it's a suggestion that was confirmed), so the user has already received the report as a search result... no need to send another report
# Only send a report for (1) Confirmed matches created manually OR (2) Suggestions accepted
created_by_bot = BotUser.where(login: ['alegre', 'smooch'], id: relationship.user_id).exists?
::Bot::Smooch.send_report_from_parent_to_child(parent.id, target.id) if !created_by_bot || relationship.confirmed_by
::Bot::Smooch.send_report_from_parent_to_child(relationship.source_id, relationship.target_id) if !created_by_bot || relationship.confirmed_by
end
end
end

after_create do
self.class.delay_for(2.seconds, { queue: 'smooch_priority'}).inherit_status_and_send_report(self.id)
self.class.delay_for(2.seconds, { queue: 'smooch_priority'}).smooch_send_report(self.id)
end

after_update do
self.class.delay_for(2.seconds, { queue: 'smooch_priority'}).inherit_status_and_send_report(self.id) if self.suggestion_accepted?
self.class.delay_for(2.seconds, { queue: 'smooch_priority'}).smooch_send_report(self.id) if self.suggestion_accepted?
end

after_destroy do
Expand Down Expand Up @@ -1049,23 +1038,6 @@ def self.send_report_from_parent_to_child(parent_id, target_id)
end
end

def self.replicate_status_to_children(pm_id, status, uid, tid)
pm = ProjectMedia.where(id: pm_id).last
return if pm.nil?
User.current = User.where(id: uid).last
Team.current = Team.where(id: tid).last
pm.source_relationships.confirmed.joins('INNER JOIN users ON users.id = relationships.user_id').where("users.type != 'BotUser' OR users.type IS NULL").find_each do |relationship|
target = relationship.target
s = target.annotations.where(annotation_type: 'verification_status').last&.load
next if s.nil? || s.status == status
s.status = status
s.bypass_status_publish_check = true
s.save!
end
User.current = nil
Team.current = nil
end

def self.refresh_smooch_slack_timeout(uid, slack_data = {})
time = Time.now.to_i
data = Rails.cache.read("smooch:slack:last_human_message:#{uid}") || {}
Expand Down
8 changes: 1 addition & 7 deletions app/models/concerns/smooch_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module SmoochStatus

module ClassMethods
::Workflow::VerificationStatus.class_eval do
check_workflow from: :any, to: :any, actions: [:replicate_status_to_children, :send_message]
check_workflow from: :any, to: :any, actions: [:send_message]
end

Team.class_eval do
Expand Down Expand Up @@ -35,12 +35,6 @@ def get_status_message_for_language(status, language)

protected

def replicate_status_to_children
pm = self.annotation.annotated
return unless Bot::Smooch.team_has_smooch_bot_installed(pm)
::Bot::Smooch.delay_for(1.second, { queue: 'smooch', retry: 0 }).replicate_status_to_children(self.annotation.annotated_id, self.value, User.current&.id, Team.current&.id)
end

def send_message
pm = self.annotation.annotated
return unless Bot::Smooch.team_has_smooch_bot_installed(pm)
Expand Down
17 changes: 17 additions & 0 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,23 @@ def self.create_unless_exists(source_id, target_id, relationship_type, options =
r
end

def self.replicate_status_to_children(pm_id, status, uid, tid)
pm = ProjectMedia.find_by_id(id: pm_id)
return if pm.nil?
User.current = User.where(id: uid).last
Team.current = Team.where(id: tid).last
pm.source_relationships.confirmed.find_each do |relationship|
target = relationship.target
s = target.annotations.where(annotation_type: 'verification_status').last&.load
next if s.nil? || s.status == status
s.status = status
s.bypass_status_publish_check = true
s.save!
end
User.current = nil
Team.current = nil
end

protected

def update_elasticsearch_parent(action = 'create_or_update')
Expand Down
6 changes: 5 additions & 1 deletion app/models/workflow/verification_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Workflow::VerificationStatus < Workflow::Base

check_default_project_media_workflow

check_workflow from: :any, to: :any, actions: [:check_if_item_is_published, :apply_rules, :update_report_design_if_needed]
check_workflow from: :any, to: :any, actions: [:check_if_item_is_published, :apply_rules, :update_report_design_if_needed, :replicate_status_to_children]
check_workflow on: :create, actions: :index_on_es_background
check_workflow on: :update, actions: :index_on_es_foreground

Expand Down Expand Up @@ -119,6 +119,10 @@ def update_report_design_if_needed
end
end
end

def replicate_status_to_children
Relationship.delay_for(1.second, { queue: 'smooch', retry: 0 }).replicate_status_to_children(self.annotation.annotated_id, self.value, User.current&.id, Team.current&.id)
end
end

Team.class_eval do
Expand Down

0 comments on commit 7848322

Please sign in to comment.