Merge branch 'develop' into rethinking-copilot
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
class Api::V1::Accounts::CustomFiltersController < Api::V1::Accounts::BaseController
|
||||
before_action :check_authorization
|
||||
before_action :fetch_custom_filters, except: [:create]
|
||||
before_action :fetch_custom_filters, only: [:index]
|
||||
before_action :fetch_custom_filter, only: [:show, :update, :destroy]
|
||||
DEFAULT_FILTER_TYPE = 'conversation'.freeze
|
||||
|
||||
@@ -9,8 +9,8 @@ class Api::V1::Accounts::CustomFiltersController < Api::V1::Accounts::BaseContro
|
||||
def show; end
|
||||
|
||||
def create
|
||||
@custom_filter = current_user.custom_filters.create!(
|
||||
permitted_payload.merge(account_id: Current.account.id)
|
||||
@custom_filter = Current.account.custom_filters.create!(
|
||||
permitted_payload.merge(user: Current.user)
|
||||
)
|
||||
render json: { error: @custom_filter.errors.messages }, status: :unprocessable_entity and return unless @custom_filter.valid?
|
||||
end
|
||||
@@ -27,14 +27,16 @@ class Api::V1::Accounts::CustomFiltersController < Api::V1::Accounts::BaseContro
|
||||
private
|
||||
|
||||
def fetch_custom_filters
|
||||
@custom_filters = current_user.custom_filters.where(
|
||||
account_id: Current.account.id,
|
||||
@custom_filters = Current.account.custom_filters.where(
|
||||
user: Current.user,
|
||||
filter_type: permitted_params[:filter_type] || DEFAULT_FILTER_TYPE
|
||||
)
|
||||
end
|
||||
|
||||
def fetch_custom_filter
|
||||
@custom_filter = @custom_filters.find(permitted_params[:id])
|
||||
@custom_filter = Current.account.custom_filters.where(
|
||||
user: Current.user
|
||||
).find(permitted_params[:id])
|
||||
end
|
||||
|
||||
def permitted_payload
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
module AccessTokenAuthHelper
|
||||
BOT_ACCESSIBLE_ENDPOINTS = {
|
||||
'api/v1/accounts/conversations' => %w[toggle_status toggle_priority create update],
|
||||
'api/v1/accounts/conversations' => %w[toggle_status toggle_priority create update custom_attributes],
|
||||
'api/v1/accounts/conversations/messages' => ['create'],
|
||||
'api/v1/accounts/conversations/assignments' => ['create']
|
||||
}.freeze
|
||||
|
||||
@@ -70,7 +70,7 @@
|
||||
},
|
||||
"AUTO_RESOLVE_IGNORE_WAITING": {
|
||||
"LABEL": "Exclude unattended conversations",
|
||||
"HELP": "If toggled, the system will not resolve conversations that have been waiting for an agent reply."
|
||||
"HELP": "When enabled, the system will skip resolving conversations that are still waiting for an agent’s reply."
|
||||
},
|
||||
"AUTO_RESOLVE_DURATION": {
|
||||
"LABEL": "Inactivity duration for resolution",
|
||||
|
||||
@@ -94,8 +94,8 @@ export default {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="hidden lg:block flex-1 py-6 scroll-mt-24 pl-4">
|
||||
<div v-if="rows.length > 0" class="sticky top-24 py-2 overflow-auto">
|
||||
<div class="hidden lg:block flex-1 py-6 scroll-mt-24 pl-4 sticky top-24">
|
||||
<div v-if="rows.length > 0" class="py-2 overflow-auto">
|
||||
<nav class="max-w-2xl">
|
||||
<ol
|
||||
role="list"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# housekeeping
|
||||
# remove stale contacts for all accounts
|
||||
# remove stale contacts for subset of accounts each day
|
||||
# - have no identification (email, phone_number, and identifier are NULL)
|
||||
# - have no conversations
|
||||
# - are older than 30 days
|
||||
@@ -7,14 +7,33 @@
|
||||
class Internal::ProcessStaleContactsJob < ApplicationJob
|
||||
queue_as :housekeeping
|
||||
|
||||
# Number of day-based groups to split accounts into
|
||||
DISTRIBUTION_GROUPS = 5
|
||||
# Max accounts to process in one batch
|
||||
MAX_ACCOUNTS_PER_BATCH = 20
|
||||
|
||||
# Process only a subset of accounts per day to avoid flooding the queue
|
||||
def perform
|
||||
return unless ChatwootApp.chatwoot_cloud?
|
||||
|
||||
Account.find_in_batches(batch_size: 100) do |accounts|
|
||||
accounts.each do |account|
|
||||
Rails.logger.info "Enqueuing RemoveStaleContactsJob for account #{account.id}"
|
||||
Internal::RemoveStaleContactsJob.perform_later(account)
|
||||
end
|
||||
# Use the day of the month to determine which accounts to process
|
||||
day_of_month = Date.current.day
|
||||
remainder = day_of_month % DISTRIBUTION_GROUPS
|
||||
|
||||
# Count total accounts for logging
|
||||
total_accounts = Account.count
|
||||
log_message = "ProcessStaleContactsJob: Processing accounts with ID % #{DISTRIBUTION_GROUPS} = "
|
||||
log_message += "#{remainder} (out of #{total_accounts} total accounts)"
|
||||
Rails.logger.info log_message
|
||||
|
||||
# Process only accounts where ID % 5 = remainder for today
|
||||
# This ensures each account is processed approximately once every 5 days
|
||||
Account.where("id % #{DISTRIBUTION_GROUPS} = ?", remainder).find_each(batch_size: MAX_ACCOUNTS_PER_BATCH) do |account|
|
||||
Rails.logger.info "Enqueuing RemoveStaleContactsJob for account #{account.id}"
|
||||
|
||||
# Add a small delay between jobs to further reduce queue pressure
|
||||
delay = rand(1..10).minutes
|
||||
Internal::RemoveStaleContactsJob.set(wait: delay).perform_later(account)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -843,7 +843,7 @@ RSpec.describe 'Conversations API', type: :request do
|
||||
create(:inbox_member, user: agent, inbox: conversation.inbox)
|
||||
end
|
||||
|
||||
it 'updates last seen' do
|
||||
it 'updates custom attributes' do
|
||||
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/custom_attributes",
|
||||
headers: agent.create_new_auth_token,
|
||||
params: valid_params,
|
||||
@@ -854,6 +854,27 @@ RSpec.describe 'Conversations API', type: :request do
|
||||
expect(conversation.reload.custom_attributes.count).to eq 3
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is a bot' do
|
||||
let(:agent_bot) { create(:agent_bot, account: account) }
|
||||
let(:custom_attributes) { { bot_id: 1001, flow_name: 'support_flow', step: 'greeting' } }
|
||||
let(:valid_params) { { custom_attributes: custom_attributes } }
|
||||
|
||||
before do
|
||||
create(:agent_bot_inbox, agent_bot: agent_bot, inbox: conversation.inbox)
|
||||
end
|
||||
|
||||
it 'updates custom attributes' do
|
||||
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/custom_attributes",
|
||||
headers: { api_access_token: agent_bot.access_token.token },
|
||||
params: valid_params,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(conversation.reload.custom_attributes).not_to be_nil
|
||||
expect(conversation.reload.custom_attributes.count).to eq 3
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/conversations/:id/attachments' do
|
||||
|
||||
@@ -3,44 +3,62 @@ require 'rails_helper'
|
||||
RSpec.describe Internal::ProcessStaleContactsJob do
|
||||
subject(:job) { described_class.perform_later }
|
||||
|
||||
it 'enqueues the job' do
|
||||
allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(true)
|
||||
expect { job }.to have_enqueued_job(described_class)
|
||||
.on_queue('housekeeping')
|
||||
context 'when in cloud environment' do
|
||||
before do
|
||||
allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(true)
|
||||
end
|
||||
|
||||
it 'processes accounts based on the day of month' do
|
||||
# Set a fixed day for testing
|
||||
day_of_month = 16
|
||||
remainder = day_of_month % described_class::DISTRIBUTION_GROUPS
|
||||
allow(Date).to receive(:current).and_return(Date.new(2025, 5, day_of_month))
|
||||
|
||||
# Create an account and set its ID to match today's pattern
|
||||
account = create(:account)
|
||||
allow(account).to receive(:id).and_return(remainder)
|
||||
|
||||
# Mock the Account.where to return our filtered accounts
|
||||
account_relation = double
|
||||
allow(Account).to receive(:where).with("id % #{described_class::DISTRIBUTION_GROUPS} = ?", remainder).and_return(account_relation)
|
||||
allow(account_relation).to receive(:find_each).and_yield(account)
|
||||
|
||||
# Mock the delay setting
|
||||
allow(Internal::RemoveStaleContactsJob).to receive(:set).and_return(Internal::RemoveStaleContactsJob)
|
||||
expect(Internal::RemoveStaleContactsJob).to receive(:perform_later).with(account)
|
||||
|
||||
described_class.perform_now
|
||||
end
|
||||
|
||||
it 'adds a delay between jobs' do
|
||||
day_of_month = 15
|
||||
remainder = day_of_month % described_class::DISTRIBUTION_GROUPS
|
||||
allow(Date).to receive(:current).and_return(Date.new(2025, 5, day_of_month))
|
||||
|
||||
account = create(:account)
|
||||
|
||||
account_relation = double
|
||||
allow(Account).to receive(:where).with("id % #{described_class::DISTRIBUTION_GROUPS} = ?", remainder).and_return(account_relation)
|
||||
allow(account_relation).to receive(:find_each).and_yield(account)
|
||||
|
||||
expect(Internal::RemoveStaleContactsJob).to receive(:set) do |args|
|
||||
expect(args[:wait]).to be_between(1.minute, 10.minutes)
|
||||
Internal::RemoveStaleContactsJob
|
||||
end
|
||||
expect(Internal::RemoveStaleContactsJob).to receive(:perform_later).with(account)
|
||||
|
||||
described_class.perform_now
|
||||
end
|
||||
end
|
||||
|
||||
it 'enqueues RemoveStaleContactsJob for each account' do
|
||||
allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(true)
|
||||
account1 = create(:account)
|
||||
account2 = create(:account)
|
||||
account3 = create(:account)
|
||||
context 'when not in cloud environment' do
|
||||
it 'does not process any accounts' do
|
||||
allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(false)
|
||||
|
||||
expect { described_class.perform_now }.to have_enqueued_job(Internal::RemoveStaleContactsJob)
|
||||
.with(account1)
|
||||
.on_queue('housekeeping')
|
||||
expect { described_class.perform_now }.to have_enqueued_job(Internal::RemoveStaleContactsJob)
|
||||
.with(account2)
|
||||
.on_queue('housekeeping')
|
||||
expect { described_class.perform_now }.to have_enqueued_job(Internal::RemoveStaleContactsJob)
|
||||
.with(account3)
|
||||
.on_queue('housekeeping')
|
||||
end
|
||||
expect(Account).not_to receive(:where)
|
||||
expect(Internal::RemoveStaleContactsJob).not_to receive(:perform_later)
|
||||
|
||||
it 'processes accounts in batches' do
|
||||
allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(true)
|
||||
account = create(:account)
|
||||
allow(Account).to receive(:find_in_batches).with(batch_size: 100).and_yield([account])
|
||||
|
||||
expect(Internal::RemoveStaleContactsJob).to receive(:perform_later).with(account)
|
||||
described_class.perform_now
|
||||
end
|
||||
|
||||
it 'does not process accounts when not in cloud environment' do
|
||||
allow(ChatwootApp).to receive(:chatwoot_cloud?).and_return(false)
|
||||
create(:account)
|
||||
|
||||
expect(Account).not_to receive(:find_in_batches)
|
||||
expect(Internal::RemoveStaleContactsJob).not_to receive(:perform_later)
|
||||
described_class.perform_now
|
||||
described_class.perform_now
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user