Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rubocop #39

Open
wants to merge 3 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Metrics/ModuleLength:
Exclude:
- lib/seeders/message_seeder.rb
- spec/support/slack_stubs.rb
- app/models/concerns/activity_message_handler.rb
- app/mailboxes/mailbox_helper.rb
Rails/ApplicationController:
Exclude:
- 'app/controllers/api/v1/widget/messages_controller.rb'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def create
end

def event_data
# todo move to service action
# TODO: move to service action
case params[:event]
when 'ask_copilot'
event = @conversation.smart_actions.ask_copilot.last
render json: event.present? ? event.event_data : {}
else
render json: {success: false}
render json: { success: false }
end
end
end
33 changes: 33 additions & 0 deletions app/helpers/custom_report_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module CustomReportHelper
private

def custom_filter(collection)
collection.filter_by_label(selected_label)
.filter_by_team(selected_team)
.filter_by_inbox(selected_inbox)
.filter_by_rating(selected_rating)
end

def get_filter(key)
filter = params.dig(:custom_filter, key)
return [] if filter.blank?

filter.to_unsafe_h.values
end

def selected_label
get_filter(:selected_label)
end

def selected_team
get_filter(:selected_team)
end

def selected_inbox
get_filter(:selected_inbox)
end

def selected_rating
get_filter(:selected_rating)
end
end
40 changes: 6 additions & 34 deletions app/helpers/report_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module ReportHelper
include CustomReportHelper

private

def scope
Expand All @@ -16,36 +18,6 @@ def scope
end
end

def custom_filter(collection)
collection.filter_by_label(selected_label)
.filter_by_team(selected_team)
.filter_by_inbox(selected_inbox)
.filter_by_rating(selected_rating)
end

def get_filter(key)
filter = params.dig(:custom_filter, key)
return [] if filter.blank?

filter.to_unsafe_h.values
end

def selected_label
get_filter(:selected_label)
end

def selected_team
get_filter(:selected_team)
end

def selected_inbox
get_filter(:selected_inbox)
end

def selected_rating
get_filter(:selected_rating)
end

def conversations_count
(get_grouped_values conversations).count
end
Expand Down Expand Up @@ -83,13 +55,13 @@ def outgoing_messages
end

def resolutions
custom_filter(scope.reporting_events).joins(:conversation).select(:conversation_id).where(account_id: account.id, name: :conversation_resolved,
conversations: { status: :resolved }, created_at: range).distinct
custom_filter(scope.reporting_events).joins(:conversation).select(:conversation_id).where(conversations: { status: :resolved })
.where(account_id: account.id, name: :conversation_resolved, created_at: range).distinct
end

def bot_resolutions
custom_filter(scope.reporting_events).joins(:conversation).select(:conversation_id).where(account_id: account.id, name: :conversation_bot_resolved,
conversations: { status: :resolved }, created_at: range).distinct
custom_filter(scope.reporting_events).joins(:conversation).select(:conversation_id).where(conversations: { status: :resolved })
.where(account_id: account.id, name: :conversation_bot_resolved, created_at: range).distinct
end

def bot_handoffs
Expand Down
10 changes: 4 additions & 6 deletions app/jobs/conversations/activity_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ class Conversations::ActivityMessageJob < ApplicationJob
queue_as :high

def perform(conversation, message_params)
begin
conversation.messages.create!(message_params)
rescue StandardError => e
Rails.logger.error e.message
Rails.logger.error e.backtrace.first
end
conversation.messages.create!(message_params)
rescue StandardError => e
Rails.logger.error e.message
Rails.logger.error e.backtrace.first
end
end
1 change: 0 additions & 1 deletion app/jobs/migration/clear_conversation_draft_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def perform
counter = 0
if account.present?
account.conversations.each do |convo|
key = format(Redis::Alfred::CONVERSATION_DRAFT_MESSAGE, conversation_id: convo.id, account_id: account.id)
convo.clear_draft_message
counter += 1
Rails.logger.debug '.'
Expand Down
2 changes: 1 addition & 1 deletion app/mailboxes/mailbox_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module MailboxHelper
private

def create_message
Rails.logger.info "[MailboxHelper] Creating message #{processed_mail.message_id}"
# Rails.logger.info "[MailboxHelper] Creating message #{processed_mail.message_id}"
return if @conversation.messages.find_by(source_id: processed_mail.message_id).present?

@message = @conversation.messages.create!(
Expand Down
9 changes: 5 additions & 4 deletions app/mailers/conversation_reply_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,13 @@ def inbox_from_email_address
end

def custom_message_id
last_message = @message || @messages&.reject(&:customized)&.last

return if last_message.blank?
return last_message.source_id if last_message&.source_id.present?

"<conversation/#{@conversation.uuid}/messages/#{last_message&.id}@#{channel_email_domain}>"
last_message.source_id.presence || "<conversation/#{@conversation.uuid}/messages/#{last_message&.id}@#{channel_email_domain}>"
end

def last_message
@message || @messages&.reject(&:customized)&.last
end

def in_reply_to_email
Expand Down
15 changes: 9 additions & 6 deletions app/mailers/conversation_reply_mailer_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ def prepare_mail(cc_bcc_enabled)
end
ms_smtp_settings
set_delivery_method
set_attachments

Rails.logger.info("Email sent from #{email_from} to #{to_emails} with subject #{mail_subject}")

if @attachments.present?
@attachments.each do |attachment|
attachments[attachment.file.filename.to_s] = attachment.file.download
end
end

mail(@options)
end

private

def set_attachments
return if @attachments.blank?

@attachments.each do |attachment|
attachments[attachment.file.filename.to_s] = attachment.file.download
end
end

def ms_smtp_settings
return unless @inbox.email? && @channel.imap_enabled && @inbox.channel.provider == 'microsoft'

Expand Down
2 changes: 2 additions & 0 deletions app/models/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ def auto_assign_to_latest_agent
return if assignee_id.present?
return if latest_agent.blank?

# rubocop:disable Rails/SkipsModelValidations
update_column(:assignee_id, latest_agent.id)
# rubocop:enable Rails/SkipsModelValidations
end

def latest_agent
Expand Down
13 changes: 7 additions & 6 deletions app/services/message_templates/hook_execution_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def contact_has_email?
contact.email
end

# rubocop:disable Metrics/CyclomaticComplexity
def csat_enabled_conversation?
return false unless conversation.resolved? || ((message.outgoing? || message.input_csat?) && inbox.send_csat_on_all_reply?)

Expand All @@ -64,23 +65,23 @@ def csat_enabled_conversation?

true
end
# rubocop:enable Metrics/CyclomaticComplexity

def should_send_csat_survey?
return unless csat_enabled_conversation?

if inbox.csat_template_enabled?
last_csat_reached = conversation.messages.csat.count >= csat_template.questions_count
if inbox.email?
return if last_csat_reached
elsif last_csat_reached || conversation.messages.unanswered_csat.exists?
return
end
return if last_csat_reached? || (!inbox.email? && conversation.messages.unanswered_csat.exists?)
elsif conversation.messages.csat.present?
return
# only send CSAT once in a conversation
end

true
end

def last_csat_reached?
conversation.messages.csat.count >= csat_template.questions_count
end
end
MessageTemplates::HookExecutionService.prepend_mod_with('MessageTemplates::HookExecutionService')
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:disable Metrics/BlockLength
json.array! @conversations do |conversation|
json.meta do
json.sender do
Expand Down Expand Up @@ -47,5 +48,6 @@ json.array! @conversations do |conversation|
json.last_non_activity_message conversation.messages.where(account_id: conversation.account_id).non_activity_messages.first.try(:push_event_data)
json.last_activity_at conversation.last_activity_at.to_i
json.priority conversation.priority
json.waiting_since conversation.waiting_since.to_i.to_i
json.waiting_since conversation.waiting_since.to_i
end
# rubocop:enable Metrics/BlockLength
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddInboxNotificationEnabled < ActiveRecord::Migration[7.0]
def change
add_column :inboxes, :push_notification_enabled, :boolean, default: true
add_column :inboxes, :push_notification_enabled, :boolean, default: true, null: false
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddAudioNotificationEnableToInbox < ActiveRecord::Migration[7.0]
def change
add_column :inboxes, :audio_notification_enabled, :boolean, default: true
add_column :inboxes, :audio_notification_enabled, :boolean, default: true, null: false
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class TransferCsatSettingsToInbox < ActiveRecord::Migration[7.0]
def change
add_column :inboxes, :csat_trigger, :string
remove_column :accounts, :csat_trigger
remove_column :accounts, :csat_trigger, :string
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class RemoveCsatTemplateEnabledForAccount < ActiveRecord::Migration[7.0]
def change
remove_column :accounts, :csat_template_enabled
remove_column :accounts, :csat_template_enabled, :boolean
end
end
2 changes: 2 additions & 0 deletions db/migrate/20240119112918_fix_old_dt_url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ def change

new_content = new_content.gsub(old_url, new_url)
Rails.logger.debug new_content
# rubocop:disable Rails/SkipsModelValidations
message.update_column(:content, new_content)
# rubocop:enable Rails/SkipsModelValidations
end
end
end
2 changes: 1 addition & 1 deletion db/migrate/20240122104127_add_webhook_enabled.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddWebhookEnabled < ActiveRecord::Migration[7.0]
def change
add_column :webhooks, :enabled, :boolean, default: true
add_column :webhooks, :enabled, :boolean, default: true, null: false
end
end
4 changes: 4 additions & 0 deletions db/migrate/20240122135344_clear_chatwoot_labels.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ClearChatwootLabels < ActiveRecord::Migration[7.0]
# rubocop:disable Metrics/MethodLength
def change
Label.destroy_all

Expand Down Expand Up @@ -64,6 +65,9 @@ def change
objects << { account_id: 1, title: label, show_on_sidebar: true, description: label, color: Faker::Color.hex_color }
end

# rubocop:disable Rails/SkipsModelValidations
Label.insert_all(objects)
# rubocop:enable Rails/SkipsModelValidations
end
# rubocop:enable Metrics/MethodLength
end
2 changes: 1 addition & 1 deletion db/migrate/20240128123047_inbox_required_label.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class InboxRequiredLabel < ActiveRecord::Migration[7.0]
def change
add_column :inboxes, :label_required, :boolean, default: false
add_column :inboxes, :label_required, :boolean, default: false, null: false
end
end
2 changes: 1 addition & 1 deletion db/migrate/20240131060216_add_auto_reply_on_message.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddAutoReplyOnMessage < ActiveRecord::Migration[7.0]
def change
add_column :messages, :auto_reply, :boolean, default: false
add_column :messages, :auto_reply, :boolean, default: false, null: false
end
end
2 changes: 1 addition & 1 deletion db/migrate/20240308131440_add_archived_on_conversations.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddArchivedOnConversations < ActiveRecord::Migration[7.0]
def change
add_column :conversations, :closed, :boolean, default: false
add_column :conversations, :closed, :boolean, default: false, null: false
end
end
2 changes: 1 addition & 1 deletion db/migrate/20240403124715_add_custom_message.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class AddCustomMessage < ActiveRecord::Migration[7.0]
def change
add_column :messages, :customized, :boolean, default: false
add_column :messages, :customized, :boolean, default: false, null: false
end
end
14 changes: 7 additions & 7 deletions db/migrate/20240517140740_log_conversation.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class LogConversation < ActiveRecord::Migration[7.0]
def change
conversation = Conversation.find_by(display_id: 33053)
conversation = Conversation.find_by(display_id: 33_053)

if conversation.present?
puts 'logging conversation'
puts conversation.attributes
conversation.messages.each do |msg|
puts msg.attributes
end
return if conversation.blank?

Rails.logger.warn 'logging conversation'
Rails.logger.warn conversation.attributes
conversation.messages.each do |msg|
Rails.logger.warn msg.attributes
end
end
end
2 changes: 1 addition & 1 deletion lib/digitaltolk/add_conversation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def inbox
end

def find_or_create_contact
@contact = inbox.contacts.find_by(email: email_address)
@contact = inbox.contacts.from_email(email_address)

if @contact.present?
@contact_inbox = ContactInbox.find_by(inbox: @inbox, contact: @contact)
Expand Down
2 changes: 2 additions & 0 deletions lib/digitaltolk/auto_assign_conversation_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def auto_assign!
unassigned_conversations.limit(20).each(&:auto_assign_to_latest_agent)

unassigned_csats.limit(20).each do |csat|
# rubocop:disable Rails/SkipsModelValidations
csat.update_column(:assigned_agent_id, csat.conversation.assignee_id)
# rubocop:enable Rails/SkipsModelValidations
end
end

Expand Down
Loading