Skip to content

Commit

Permalink
smart actions active status
Browse files Browse the repository at this point in the history
  • Loading branch information
jderecho committed Jul 12, 2024
1 parent f1fd475 commit 501a57d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 }
Expand Down
5 changes: 4 additions & 1 deletion app/models/smart_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 501a57d

Please sign in to comment.