From 501a57d6f4f18f7b8d05b52ec148916b85ebddc7 Mon Sep 17 00:00:00 2001 From: John Manuel Derecho Date: Fri, 12 Jul 2024 08:38:57 +0800 Subject: [PATCH] smart actions active status --- .../v1/accounts/conversations/messages_controller.rb | 10 ++++++++++ .../accounts/conversations/smart_actions_controller.rb | 4 ++-- app/models/smart_action.rb | 5 ++++- ...20240711235814_add_active_status_on_smart_action.rb | 6 ++++++ db/schema.rb | 3 ++- 5 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20240711235814_add_active_status_on_smart_action.rb diff --git a/app/controllers/api/v1/accounts/conversations/messages_controller.rb b/app/controllers/api/v1/accounts/conversations/messages_controller.rb index d34561e36589f..075ae3679ff38 100644 --- a/app/controllers/api/v1/accounts/conversations/messages_controller.rb +++ b/app/controllers/api/v1/accounts/conversations/messages_controller.rb @@ -6,6 +6,7 @@ def index def create user = Current.user || @resource mb = Messages::MessageBuilder.new(user, @conversation, params) + deactivate_smart_actions @message = mb.perform rescue StandardError => e render_could_not_create_error(e.message) @@ -62,4 +63,13 @@ def permitted_params def already_translated_content_available? message.translations.present? && message.translations[permitted_params[:target_language]].present? end + + def deactivate_smart_actions + return unless Current.account.feature_enabled?('smart_actions') + return unless (copilot_draft = @conversation.smart_actions.ask_copilot.first&.content).present? + + if params[:content].to_s.include? copilot_draft + @conversation.smart_actions.update_all(active: false) + end + end end diff --git a/app/controllers/api/v1/accounts/conversations/smart_actions_controller.rb b/app/controllers/api/v1/accounts/conversations/smart_actions_controller.rb index c5917a6af55c9..ebf4e8c84b2ab 100644 --- a/app/controllers/api/v1/accounts/conversations/smart_actions_controller.rb +++ b/app/controllers/api/v1/accounts/conversations/smart_actions_controller.rb @@ -1,6 +1,6 @@ class Api::V1::Accounts::Conversations::SmartActionsController < Api::V1::Accounts::Conversations::BaseController def index - @smart_actions = @conversation.smart_actions + @smart_actions = @conversation.smart_actions.active end def create @@ -17,7 +17,7 @@ def event_data # TODO: move to service action case params[:event] when 'ask_copilot' - event = @conversation.smart_actions.ask_copilot.last + event = @conversation.smart_actions.ask_copilot.active.last render json: event.present? ? event.event_data : {} else render json: { success: false } diff --git a/app/models/smart_action.rb b/app/models/smart_action.rb index 5e2920bb3bd44..a2f21ee0bcb6c 100644 --- a/app/models/smart_action.rb +++ b/app/models/smart_action.rb @@ -3,6 +3,7 @@ # Table name: smart_actions # # id :bigint not null, primary key +# active :boolean default(TRUE) # custom_attributes :jsonb # description :string # event :string @@ -29,7 +30,7 @@ class SmartAction < ApplicationRecord validates :conversation_id, presence: true scope :ask_copilot, -> { where(event: 'ask_copilot') } - + scope :active, -> { where(active: true) } delegate :account, to: :conversation after_create_commit :execute_after_create_commit_callbacks @@ -55,6 +56,8 @@ def execute_after_create_commit_callbacks end def dispatch_create_events + return unless active? + Rails.configuration.dispatcher.dispatch(SMART_ACTION_CREATED, Time.zone.now, smart_action: self, performed_by: Current.executed_by) end end diff --git a/db/migrate/20240711235814_add_active_status_on_smart_action.rb b/db/migrate/20240711235814_add_active_status_on_smart_action.rb new file mode 100644 index 0000000000000..7974691b9d1fa --- /dev/null +++ b/db/migrate/20240711235814_add_active_status_on_smart_action.rb @@ -0,0 +1,6 @@ +class AddActiveStatusOnSmartAction < ActiveRecord::Migration[7.0] + def change + print ENV.to_json + add_column :smart_actions, :active, :boolean, default: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 4e583cdd364d0..f9d8a0c61a869 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_06_26_105938) do +ActiveRecord::Schema[7.0].define(version: 2024_07_11_235814) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "pg_trgm" @@ -926,6 +926,7 @@ t.jsonb "custom_attributes", default: {} t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "active", default: true t.index ["conversation_id"], name: "index_smart_actions_on_conversation_id" t.index ["message_id"], name: "index_smart_actions_on_message_id" end