Skip to content

Commit

Permalink
Ticket CV2-765: Change how tipline search result feedback is stored
Browse files Browse the repository at this point in the history
  • Loading branch information
caiosba committed Oct 24, 2023
1 parent df05a19 commit b01baa8
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 96 deletions.
2 changes: 2 additions & 0 deletions app/models/bot/smooch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def suggestion_accepted?

def self.inherit_status_and_send_report(rid)
relationship = Relationship.find_by_id(rid)
# A relationship created by the Smooch Bot is related to search results, so the user has already received the report as a search result
return if relationship&.user && relationship.user == BotUser.smooch_user
unless relationship.nil?
target = relationship.target
parent = relationship.source
Expand Down
8 changes: 7 additions & 1 deletion app/models/concerns/smooch_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,14 @@ def save_message(message_json, app_id, author = nil, request_type = 'default_req
if ['default_requests', 'timeout_requests', 'irrelevant_search_result_requests'].include?(request_type)
message['archived'] = ['default_requests', 'irrelevant_search_result_requests'].include?(request_type) ? self.default_archived_flag : CheckArchivedFlags::FlagCodes::UNCONFIRMED
annotated = self.create_project_media_from_message(message)
elsif ['menu_options_requests', 'relevant_search_result_requests', 'timeout_search_requests', 'resource_requests'].include?(request_type)
elsif ['menu_options_requests', 'resource_requests'].include?(request_type)
annotated = annotated_obj
elsif ['relevant_search_result_requests', 'timeout_search_requests'].include?(request_type)
message['archived'] = (request_type == 'relevant_search_result_requests' ? self.default_archived_flag : CheckArchivedFlags::FlagCodes::UNCONFIRMED)
annotated = self.create_project_media_from_message(message)
if annotated != annotated_obj && annotated.is_a?(ProjectMedia)
Relationship.create!(relationship_type: Relationship.suggested_type, source: annotated_obj, target: annotated, user: BotUser.smooch_user)
end
end

return if annotated.nil?
Expand Down
23 changes: 17 additions & 6 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ class Relationship < ApplicationRecord
before_validation :set_confirmed, if: :is_being_confirmed?, on: :update
before_validation :set_cluster, if: :is_being_confirmed?, on: :update
validate :relationship_type_is_valid, :items_are_from_the_same_team
validate :target_not_pulished_report, on: :create
validate :target_not_published_report, on: :create
validate :similar_item_exists, on: :create, if: proc { |r| r.is_suggested? }
validate :cant_be_related_to_itself
validates :relationship_type, uniqueness: { scope: [:source_id, :target_id], message: :already_exists }, on: :create

before_create :destroy_suggest_item, if: proc { |r| r.is_confirmed? }
before_create :destroy_same_suggested_item, if: proc { |r| r.is_confirmed? }
before_update :destroy_other_suggested_items, if: proc { |r| r.is_confirmed? }
after_create :move_to_same_project_as_main, prepend: true
after_create :point_targets_to_new_source, :update_counters, prepend: true
after_update :reset_counters, prepend: true
Expand Down Expand Up @@ -242,7 +243,7 @@ def items_are_from_the_same_team
end
end

def target_not_pulished_report
def target_not_published_report
unless self.target.nil?
state = self.target.get_annotations('report_design').last&.load&.get_field_value('state')
errors.add(:base, I18n.t(:target_is_published)) if state == 'published'
Expand Down Expand Up @@ -323,10 +324,20 @@ def move_to_same_project_as_main
end
end

def destroy_suggest_item
# Check if same item already exists as a suggested item
def destroy_same_suggested_item
# Check if same item already exists as a suggested item by a bot
Relationship.where(source_id: self.source_id, target_id: self.target_id)
.where('relationship_type = ?', Relationship.suggested_type.to_yaml).destroy_all
.joins(:user).where('users.type' => 'BotUser')
.where('relationship_type = ?', Relationship.suggested_type.to_yaml)
.destroy_all
end

def destroy_other_suggested_items
# If created by Smooch Bot, destroy other suggestions to the same media
Relationship.where(target_id: self.target_id, user: BotUser.smooch_user)
.where('relationship_type = ?', Relationship.suggested_type.to_yaml)
.where('id != ?', self.id)
.destroy_all
end

def cant_be_related_to_itself
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class UpdateRelationshipTargetIdIndex < ActiveRecord::Migration[6.1]
def change
remove_index :relationships, name: 'index_relationships_on_target_id'
add_index :relationships, :target_id
add_index :relationships, :source_id
add_index :relationships, [:target_id, :relationship_type]
add_index :relationships, [:source_id, :relationship_type]
end
end
Loading

0 comments on commit b01baa8

Please sign in to comment.