# Pull Request Template ## Description Better scheduling and queueing mechanics for document auto-sync - add jitter plan wise for document sync - move auto-sync documents to purgeable queue ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) ## How Has This Been Tested? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. locally tested and with specs ## Checklist: - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my code - [x] I have commented on my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [x] Any dependent changes have been merged and published in downstream modules --------- Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: iamsivin <iamsivin@gmail.com> Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com> Co-authored-by: Sony Mathew <sony@chatwoot.com> Co-authored-by: Vishnu Narayanan <iamwishnu@gmail.com>
133 lines
4.1 KiB
Ruby
133 lines
4.1 KiB
Ruby
class Api::V1::Accounts::Captain::DocumentsController < Api::V1::Accounts::BaseController
|
|
before_action :current_account
|
|
before_action -> { check_authorization(Captain::Assistant) }
|
|
|
|
before_action :set_current_page, only: [:index]
|
|
before_action :set_documents, except: [:create]
|
|
before_action :set_document, only: [:show, :destroy, :sync]
|
|
before_action :set_assistant, only: [:create]
|
|
RESULTS_PER_PAGE = 25
|
|
|
|
def index
|
|
base_query = @documents
|
|
base_query = base_query.where(assistant_id: permitted_params[:assistant_id]) if permitted_params[:assistant_id].present?
|
|
base_query = apply_source_filter(base_query, permitted_params[:source])
|
|
base_query = apply_filter(base_query, permitted_params[:filter])
|
|
base_query = apply_search(base_query, permitted_params[:search_key])
|
|
base_query = apply_sort(base_query, permitted_params[:sort])
|
|
|
|
@documents_count = base_query.count
|
|
@sync_interval_hours = current_sync_interval&.in_hours&.to_i
|
|
@documents = base_query.page(@current_page).per(RESULTS_PER_PAGE)
|
|
end
|
|
|
|
def show; end
|
|
|
|
def create
|
|
return render_could_not_create_error('Missing Assistant') if @assistant.nil?
|
|
|
|
@document = @assistant.documents.build(document_params)
|
|
@document.save!
|
|
rescue Captain::Document::LimitExceededError => e
|
|
render_could_not_create_error(e.message)
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
render_could_not_create_error(e.record.errors.full_messages.join(', '))
|
|
end
|
|
|
|
def sync
|
|
return render_could_not_create_error(I18n.t('captain.documents.sync_not_supported_for_pdf')) unless @document.syncable?
|
|
return render_could_not_create_error(I18n.t('captain.documents.sync_only_available_documents')) unless @document.available?
|
|
|
|
@document.update!(
|
|
sync_status: :syncing,
|
|
sync_step: nil,
|
|
last_sync_error_code: nil,
|
|
last_sync_attempted_at: Time.current
|
|
)
|
|
Captain::Documents::PerformSyncJob.perform_later(@document)
|
|
head :accepted
|
|
end
|
|
|
|
def destroy
|
|
@document.destroy
|
|
head :no_content
|
|
end
|
|
|
|
private
|
|
|
|
def set_documents
|
|
@documents = Current.account.captain_documents.with_attached_pdf_file.includes(:assistant)
|
|
end
|
|
|
|
def set_document
|
|
@document = @documents.find(permitted_params[:id])
|
|
end
|
|
|
|
def set_assistant
|
|
@assistant = Current.account.captain_assistants.find_by(id: document_params[:assistant_id])
|
|
end
|
|
|
|
def set_current_page
|
|
@current_page = permitted_params[:page] || 1
|
|
end
|
|
|
|
def permitted_params
|
|
params.permit(:assistant_id, :page, :id, :account_id, :filter, :source, :sort, :search_key)
|
|
end
|
|
|
|
def apply_source_filter(scope, source)
|
|
case source
|
|
when 'web' then scope.syncable
|
|
when 'pdf' then scope.pdf_documents
|
|
else scope
|
|
end
|
|
end
|
|
|
|
def apply_filter(scope, filter)
|
|
case filter
|
|
when 'stale' then stale_documents(scope.syncable)
|
|
when 'synced' then up_to_date_documents(scope.syncable)
|
|
when 'syncing' then scope.syncable.sync_in_progress
|
|
when 'failed' then scope.syncable.sync_failed
|
|
else scope
|
|
end
|
|
end
|
|
|
|
def apply_search(scope, search_key)
|
|
return scope if search_key.blank?
|
|
|
|
query = "%#{ActiveRecord::Base.sanitize_sql_like(search_key)}%"
|
|
scope.where('captain_documents.name ILIKE :query OR captain_documents.external_link ILIKE :query', query: query)
|
|
end
|
|
|
|
def apply_sort(scope, sort)
|
|
case sort
|
|
when 'recently_created' then scope.order(created_at: :desc)
|
|
else scope.order(updated_at: :desc)
|
|
end
|
|
end
|
|
|
|
def stale_documents(scope)
|
|
return scope.none unless current_sync_interval
|
|
|
|
scope.sync_synced.where(Captain::Document.arel_table[:last_synced_at].lt(current_sync_interval.ago))
|
|
end
|
|
|
|
def up_to_date_documents(scope)
|
|
documents = scope.sync_synced
|
|
return documents unless current_sync_interval
|
|
|
|
documents.where(Captain::Document.arel_table[:last_synced_at].gteq(current_sync_interval.ago))
|
|
end
|
|
|
|
def current_sync_interval
|
|
return @current_sync_interval if defined?(@current_sync_interval)
|
|
|
|
@current_sync_interval = Current.account.captain_document_sync_interval
|
|
end
|
|
|
|
def document_params
|
|
params.require(:document).permit(:name, :external_link, :assistant_id, :pdf_file)
|
|
end
|
|
end
|