diff --git a/.rubocop.yml b/.rubocop.yml index 02a694980..56460c4b8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,11 +3,11 @@ plugins: - rubocop-rails - rubocop-rspec - rubocop-factory_bot + - rubocop-rspec_rails require: - ./rubocop/use_from_email.rb - ./rubocop/custom_cop_location.rb - - rubocop-rspec_rails Layout/LineLength: Max: 150 @@ -161,6 +161,27 @@ Naming/MemoizedInstanceVariableName: Exclude: - 'app/models/message.rb' +Naming/PredicateMethod: + Exclude: + - 'app/builders/messages/instagram/base_message_builder.rb' + - 'app/controllers/public/api/v1/csat_survey_controller.rb' + - 'app/controllers/public/api/v1/inboxes/messages_controller.rb' + - 'app/jobs/webhooks/line_events_job.rb' + - 'app/services/account/sign_up_email_validation_service.rb' + - 'app/services/automation_rules/condition_validation_service.rb' + - 'app/services/line/incoming_message_service.rb' + - 'app/services/search_service.rb' + - 'app/services/telegram/send_attachments_service.rb' + - 'app/services/twitter/webhook_subscribe_service.rb' + - 'app/services/whatsapp/template_parameter_converter_service.rb' + - 'enterprise/app/jobs/captain/conversation/response_builder_job.rb' + - 'enterprise/app/services/captain/tools/base_service.rb' + - 'enterprise/lib/captain/tool.rb' + - 'lib/chatwoot_app.rb' + - 'lib/custom_markdown_renderer.rb' + - 'lib/linear.rb' + - 'lib/redis/lock_manager.rb' + Style/GuardClause: Exclude: - 'app/builders/account_builder.rb' diff --git a/Gemfile b/Gemfile index ee1943fcc..f37719a42 100644 --- a/Gemfile +++ b/Gemfile @@ -248,4 +248,4 @@ group :development, :test do gem 'simplecov', '0.17.1', require: false gem 'spring' gem 'spring-watcher-listen' -end \ No newline at end of file +end diff --git a/app/actions/contact_identify_action.rb b/app/actions/contact_identify_action.rb index 6db565959..a92d30376 100644 --- a/app/actions/contact_identify_action.rb +++ b/app/actions/contact_identify_action.rb @@ -55,7 +55,11 @@ class ContactIdentifyAction def existing_identified_contact return if params[:identifier].blank? - @existing_identified_contact ||= account.contacts.find_by(identifier: params[:identifier]) + if instance_variable_defined?(:@existing_identified_contact) + @existing_identified_contact + else + @existing_identified_contact = account.contacts.find_by(identifier: params[:identifier]) + end end def existing_email_contact @@ -67,7 +71,11 @@ class ContactIdentifyAction def existing_phone_number_contact return if params[:phone_number].blank? - @existing_phone_number_contact ||= account.contacts.find_by(phone_number: params[:phone_number]) + if instance_variable_defined?(:@existing_phone_number_contact) + @existing_phone_number_contact + else + @existing_phone_number_contact = account.contacts.find_by(phone_number: params[:phone_number]) + end end def merge_contacts?(existing_contact, key) diff --git a/app/actions/contact_merge_action.rb b/app/actions/contact_merge_action.rb index 2633c907d..d7a8599e2 100644 --- a/app/actions/contact_merge_action.rb +++ b/app/actions/contact_merge_action.rb @@ -1,5 +1,6 @@ class ContactMergeAction include Events::Types + pattr_initialize [:account!, :base_contact!, :mergee_contact!] def perform diff --git a/app/builders/account_builder.rb b/app/builders/account_builder.rb index 532487a1b..1a75ae6e3 100644 --- a/app/builders/account_builder.rb +++ b/app/builders/account_builder.rb @@ -2,6 +2,7 @@ class AccountBuilder include CustomExceptions::Account + pattr_initialize [:account_name, :email!, :confirmed, :user, :user_full_name, :user_password, :super_admin, :locale] def perform diff --git a/app/builders/messages/message_builder.rb b/app/builders/messages/message_builder.rb index e1087b19f..8bab28685 100644 --- a/app/builders/messages/message_builder.rb +++ b/app/builders/messages/message_builder.rb @@ -1,5 +1,6 @@ class Messages::MessageBuilder include ::FileTypeHelper + attr_reader :message def initialize(user, conversation, params) diff --git a/app/builders/notification_subscription_builder.rb b/app/builders/notification_subscription_builder.rb index 0dede970f..284bdbe32 100644 --- a/app/builders/notification_subscription_builder.rb +++ b/app/builders/notification_subscription_builder.rb @@ -17,7 +17,11 @@ class NotificationSubscriptionBuilder end def identifier_subscription - @identifier_subscription ||= NotificationSubscription.find_by(identifier: identifier) + if instance_variable_defined?(:@identifier_subscription) + @identifier_subscription + else + @identifier_subscription = NotificationSubscription.find_by(identifier: identifier) + end end def move_subscription_to_user diff --git a/app/builders/v2/report_builder.rb b/app/builders/v2/report_builder.rb index fb986d335..b935bd520 100644 --- a/app/builders/v2/report_builder.rb +++ b/app/builders/v2/report_builder.rb @@ -1,6 +1,7 @@ class V2::ReportBuilder include DateRangeHelper include ReportHelper + attr_reader :account, :params DEFAULT_GROUP_BY = 'day'.freeze diff --git a/app/builders/v2/reports/bot_metrics_builder.rb b/app/builders/v2/reports/bot_metrics_builder.rb index c46daf978..29406673a 100644 --- a/app/builders/v2/reports/bot_metrics_builder.rb +++ b/app/builders/v2/reports/bot_metrics_builder.rb @@ -1,5 +1,6 @@ class V2::Reports::BotMetricsBuilder include DateRangeHelper + attr_reader :account, :params def initialize(account, params) @@ -41,13 +42,13 @@ class V2::Reports::BotMetricsBuilder end def bot_resolution_rate - return 0 if bot_conversations.count.zero? + return 0 if bot_conversations.none? bot_resolutions_count.to_f / bot_conversations.count * 100 end def bot_handoff_rate - return 0 if bot_conversations.count.zero? + return 0 if bot_conversations.none? bot_handoffs_count.to_f / bot_conversations.count * 100 end diff --git a/app/builders/v2/reports/timeseries/base_timeseries_builder.rb b/app/builders/v2/reports/timeseries/base_timeseries_builder.rb index 50699417d..40305372b 100644 --- a/app/builders/v2/reports/timeseries/base_timeseries_builder.rb +++ b/app/builders/v2/reports/timeseries/base_timeseries_builder.rb @@ -1,6 +1,7 @@ class V2::Reports::Timeseries::BaseTimeseriesBuilder include TimezoneHelper include DateRangeHelper + DEFAULT_GROUP_BY = 'day'.freeze pattr_initialize :account, :params diff --git a/app/controllers/api/base_controller.rb b/app/controllers/api/base_controller.rb index 937cd5502..2708ecc8d 100644 --- a/app/controllers/api/base_controller.rb +++ b/app/controllers/api/base_controller.rb @@ -1,5 +1,6 @@ class Api::BaseController < ApplicationController include AccessTokenAuthHelper + respond_to :json before_action :authenticate_access_token!, if: :authenticate_by_access_token? before_action :validate_bot_access_token!, if: :authenticate_by_access_token? diff --git a/app/controllers/api/v1/accounts/base_controller.rb b/app/controllers/api/v1/accounts/base_controller.rb index e30effc59..54c58a701 100644 --- a/app/controllers/api/v1/accounts/base_controller.rb +++ b/app/controllers/api/v1/accounts/base_controller.rb @@ -1,6 +1,7 @@ class Api::V1::Accounts::BaseController < Api::BaseController include SwitchLocale include EnsureCurrentAccountHelper + before_action :current_account around_action :switch_locale_using_account_locale end diff --git a/app/controllers/api/v1/accounts/campaigns_controller.rb b/app/controllers/api/v1/accounts/campaigns_controller.rb index 2a1650e53..3bac067a0 100644 --- a/app/controllers/api/v1/accounts/campaigns_controller.rb +++ b/app/controllers/api/v1/accounts/campaigns_controller.rb @@ -24,7 +24,11 @@ class Api::V1::Accounts::CampaignsController < Api::V1::Accounts::BaseController private def campaign - @campaign ||= Current.account.campaigns.find_by(display_id: params[:id]) + if instance_variable_defined?(:@campaign) + @campaign + else + @campaign = Current.account.campaigns.find_by(display_id: params[:id]) + end end def campaign_params diff --git a/app/controllers/api/v1/accounts/categories_controller.rb b/app/controllers/api/v1/accounts/categories_controller.rb index 834b19ed9..66f08e561 100644 --- a/app/controllers/api/v1/accounts/categories_controller.rb +++ b/app/controllers/api/v1/accounts/categories_controller.rb @@ -39,7 +39,11 @@ class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseControlle end def portal - @portal ||= Current.account.portals.find_by(slug: params[:portal_id]) + if instance_variable_defined?(:@portal) + @portal + else + @portal = Current.account.portals.find_by(slug: params[:portal_id]) + end end def related_categories_records diff --git a/app/controllers/api/v1/accounts/contacts/contact_inboxes_controller.rb b/app/controllers/api/v1/accounts/contacts/contact_inboxes_controller.rb index d985c8a73..162d65f84 100644 --- a/app/controllers/api/v1/accounts/contacts/contact_inboxes_controller.rb +++ b/app/controllers/api/v1/accounts/contacts/contact_inboxes_controller.rb @@ -1,5 +1,6 @@ class Api::V1::Accounts::Contacts::ContactInboxesController < Api::V1::Accounts::Contacts::BaseController include HmacConcern + before_action :ensure_inbox, only: [:create] def create diff --git a/app/controllers/api/v1/accounts/conversations/direct_uploads_controller.rb b/app/controllers/api/v1/accounts/conversations/direct_uploads_controller.rb index f4ac05d6e..bcf4f0fb7 100644 --- a/app/controllers/api/v1/accounts/conversations/direct_uploads_controller.rb +++ b/app/controllers/api/v1/accounts/conversations/direct_uploads_controller.rb @@ -1,5 +1,6 @@ class Api::V1::Accounts::Conversations::DirectUploadsController < ActiveStorage::DirectUploadsController include EnsureCurrentAccountHelper + before_action :current_account before_action :conversation @@ -12,6 +13,10 @@ class Api::V1::Accounts::Conversations::DirectUploadsController < ActiveStorage: private def conversation - @conversation ||= Current.account.conversations.find_by(display_id: params[:conversation_id]) + if instance_variable_defined?(:@conversation) + @conversation + else + @conversation = Current.account.conversations.find_by(display_id: params[:conversation_id]) + end end end diff --git a/app/controllers/api/v1/accounts/integrations/shopify_controller.rb b/app/controllers/api/v1/accounts/integrations/shopify_controller.rb index 7fe31889b..e3b116e62 100644 --- a/app/controllers/api/v1/accounts/integrations/shopify_controller.rb +++ b/app/controllers/api/v1/accounts/integrations/shopify_controller.rb @@ -1,5 +1,6 @@ class Api::V1::Accounts::Integrations::ShopifyController < Api::V1::Accounts::BaseController include Shopify::IntegrationHelper + before_action :setup_shopify_context, only: [:orders] before_action :fetch_hook, except: [:auth] before_action :validate_contact, only: [:orders] @@ -45,7 +46,11 @@ class Api::V1::Accounts::Integrations::ShopifyController < Api::V1::Accounts::Ba end def contact - @contact ||= Current.account.contacts.find_by(id: params[:contact_id]) + if instance_variable_defined?(:@contact) + @contact + else + @contact = Current.account.contacts.find_by(id: params[:contact_id]) + end end def fetch_hook diff --git a/app/controllers/api/v1/widget/base_controller.rb b/app/controllers/api/v1/widget/base_controller.rb index 5b87e2d1a..d6f008a5b 100644 --- a/app/controllers/api/v1/widget/base_controller.rb +++ b/app/controllers/api/v1/widget/base_controller.rb @@ -25,7 +25,11 @@ class Api::V1::Widget::BaseController < ApplicationController end def inbox - @inbox ||= ::Inbox.find_by(id: auth_token_params[:inbox_id]) + if instance_variable_defined?(:@inbox) + @inbox + else + @inbox = ::Inbox.find_by(id: auth_token_params[:inbox_id]) + end end def conversation_params diff --git a/app/controllers/api/v1/widget/conversations_controller.rb b/app/controllers/api/v1/widget/conversations_controller.rb index fe5facc1a..4e696477c 100644 --- a/app/controllers/api/v1/widget/conversations_controller.rb +++ b/app/controllers/api/v1/widget/conversations_controller.rb @@ -1,5 +1,6 @@ class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController include Events::Types + before_action :render_not_found_if_empty, only: [:toggle_typing, :toggle_status, :set_custom_attributes, :destroy_custom_attributes] def index diff --git a/app/controllers/api/v1/widget/direct_uploads_controller.rb b/app/controllers/api/v1/widget/direct_uploads_controller.rb index a6abdb3e1..14e7f67f9 100644 --- a/app/controllers/api/v1/widget/direct_uploads_controller.rb +++ b/app/controllers/api/v1/widget/direct_uploads_controller.rb @@ -1,5 +1,6 @@ class Api::V1::Widget::DirectUploadsController < ActiveStorage::DirectUploadsController include WebsiteTokenHelper + before_action :set_web_widget before_action :set_contact diff --git a/app/controllers/devise_overrides/confirmations_controller.rb b/app/controllers/devise_overrides/confirmations_controller.rb index aaabbcf8c..c49af47b5 100644 --- a/app/controllers/devise_overrides/confirmations_controller.rb +++ b/app/controllers/devise_overrides/confirmations_controller.rb @@ -1,5 +1,6 @@ class DeviseOverrides::ConfirmationsController < Devise::ConfirmationsController include AuthHelper + skip_before_action :require_no_authentication, raise: false skip_before_action :authenticate_user!, raise: false diff --git a/app/controllers/public/api/v1/inboxes/conversations_controller.rb b/app/controllers/public/api/v1/inboxes/conversations_controller.rb index 4e3b5dca9..2d3c1dfdf 100644 --- a/app/controllers/public/api/v1/inboxes/conversations_controller.rb +++ b/app/controllers/public/api/v1/inboxes/conversations_controller.rb @@ -1,5 +1,6 @@ class Public::Api::V1::Inboxes::ConversationsController < Public::Api::V1::InboxesController include Events::Types + before_action :set_conversation, only: [:toggle_typing, :update_last_seen, :show, :toggle_status] def index diff --git a/app/controllers/public_controller.rb b/app/controllers/public_controller.rb index 3b83a2210..7366e576c 100644 --- a/app/controllers/public_controller.rb +++ b/app/controllers/public_controller.rb @@ -2,6 +2,7 @@ # One of the specs is failing when I tried doing that, lets revisit in future class PublicController < ActionController::Base include RequestExceptionHandler + skip_before_action :verify_authenticity_token private diff --git a/app/jobs/migration/conversations_first_reply_scheduler_job.rb b/app/jobs/migration/conversations_first_reply_scheduler_job.rb index 509357c1e..3b37add49 100644 --- a/app/jobs/migration/conversations_first_reply_scheduler_job.rb +++ b/app/jobs/migration/conversations_first_reply_scheduler_job.rb @@ -5,7 +5,7 @@ class Migration::ConversationsFirstReplySchedulerJob < ApplicationJob def perform(account) account.conversations.each do |conversation| # rubocop:disable Rails/SkipsModelValidations - if conversation.messages.outgoing.where("(additional_attributes->'campaign_id') is null").count.positive? + if conversation.messages.outgoing.where("(additional_attributes->'campaign_id') is null").any? conversation.update_columns(first_reply_created_at: conversation.messages.outgoing.where("(additional_attributes->'campaign_id') is null") .first.created_at) else diff --git a/app/mailers/conversation_reply_mailer.rb b/app/mailers/conversation_reply_mailer.rb index dba445170..e3071a2dd 100644 --- a/app/mailers/conversation_reply_mailer.rb +++ b/app/mailers/conversation_reply_mailer.rb @@ -30,7 +30,7 @@ class ConversationReplyMailer < ApplicationMailer @messages = @conversation.messages.chat.where(message_type: [:outgoing, :template]).where('id >= ?', last_queued_id) @messages = @messages.reject { |m| m.template? && !m.input_csat? } - return false if @messages.count.zero? + return false if @messages.none? prepare_mail(false) end diff --git a/app/models/application_record.rb b/app/models/application_record.rb index c6877c883..cdb7d9a5f 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,5 +1,6 @@ class ApplicationRecord < ActiveRecord::Base include Events::Types + self.abstract_class = true before_validation :validates_column_content_length diff --git a/app/models/campaign.rb b/app/models/campaign.rb index 2927181c6..5600dae67 100644 --- a/app/models/campaign.rb +++ b/app/models/campaign.rb @@ -31,6 +31,7 @@ # class Campaign < ApplicationRecord include UrlHelper + validates :account_id, presence: true validates :inbox_id, presence: true validates :title, presence: true diff --git a/app/models/channel/instagram.rb b/app/models/channel/instagram.rb index 964a4c1a2..520197297 100644 --- a/app/models/channel/instagram.rb +++ b/app/models/channel/instagram.rb @@ -17,6 +17,7 @@ class Channel::Instagram < ApplicationRecord include Channelable include Reauthorizable + self.table_name = 'channel_instagram' AUTHORIZATION_ERROR_THRESHOLD = 1 diff --git a/app/models/concerns/access_tokenable.rb b/app/models/concerns/access_tokenable.rb index 0bb8daa4f..76a0811bf 100644 --- a/app/models/concerns/access_tokenable.rb +++ b/app/models/concerns/access_tokenable.rb @@ -1,5 +1,6 @@ module AccessTokenable extend ActiveSupport::Concern + included do has_one :access_token, as: :owner, dependent: :destroy_async after_create :create_access_token diff --git a/app/models/concerns/channelable.rb b/app/models/concerns/channelable.rb index e3e9eba94..afdd218cd 100644 --- a/app/models/concerns/channelable.rb +++ b/app/models/concerns/channelable.rb @@ -1,5 +1,6 @@ module Channelable extend ActiveSupport::Concern + included do validates :account_id, presence: true belongs_to :account diff --git a/app/models/concerns/featurable.rb b/app/models/concerns/featurable.rb index daa0b4bf6..23b83006b 100644 --- a/app/models/concerns/featurable.rb +++ b/app/models/concerns/featurable.rb @@ -14,6 +14,7 @@ module Featurable included do include FlagShihTzu + has_flags FEATURES.merge(column: 'feature_flags').merge(QUERY_MODE) before_create :enable_default_features diff --git a/app/models/concerns/sort_handler.rb b/app/models/concerns/sort_handler.rb index 00eb73717..29724f441 100644 --- a/app/models/concerns/sort_handler.rb +++ b/app/models/concerns/sort_handler.rb @@ -21,7 +21,7 @@ module SortHandler def last_messaged_conversations Message.except(:order).select( 'DISTINCT ON (conversation_id) conversation_id, id, created_at, message_type' - ).order('conversation_id, created_at DESC') + ).order(:conversation_id, created_at: :desc) end def sort_on_last_user_message_at diff --git a/app/models/contact_inbox.rb b/app/models/contact_inbox.rb index 6d034880e..1eb6f8793 100644 --- a/app/models/contact_inbox.rb +++ b/app/models/contact_inbox.rb @@ -23,6 +23,7 @@ class ContactInbox < ApplicationRecord include Pubsubable include RegexHelper + validates :inbox_id, presence: true validates :contact_id, presence: true validates :source_id, presence: true diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 1c898ba7f..947b0682f 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -153,7 +153,7 @@ class Inbox < ApplicationRecord def active_bot? agent_bot_inbox&.active? || hooks.where(app_id: %w[dialogflow], - status: 'enabled').count.positive? + status: 'enabled').any? end def inbox_type diff --git a/app/models/integrations/app.rb b/app/models/integrations/app.rb index 6a1378f1e..1ede8637d 100644 --- a/app/models/integrations/app.rb +++ b/app/models/integrations/app.rb @@ -1,5 +1,6 @@ class Integrations::App include Linear::IntegrationHelper + attr_accessor :params def initialize(params) diff --git a/app/models/message.rb b/app/models/message.rb index 878d4b91d..96e4d8279 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -208,7 +208,7 @@ class Message < ApplicationRecord return false if conversation.messages.outgoing .where.not(sender_type: ['AgentBot', 'Captain::Assistant']) .where.not(private: true) - .where("(additional_attributes->'campaign_id') is null").count > 1 + .where("(additional_attributes->'campaign_id') is null").many? true end diff --git a/app/models/notification.rb b/app/models/notification.rb index 8c4162702..5b7697334 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -28,6 +28,7 @@ class Notification < ApplicationRecord include MessageFormatHelper + belongs_to :account belongs_to :user diff --git a/app/services/account/sign_up_email_validation_service.rb b/app/services/account/sign_up_email_validation_service.rb index 8889145b2..4c16585c7 100644 --- a/app/services/account/sign_up_email_validation_service.rb +++ b/app/services/account/sign_up_email_validation_service.rb @@ -2,6 +2,7 @@ class Account::SignUpEmailValidationService include CustomExceptions::Account + attr_reader :email def initialize(email) diff --git a/app/services/crm/leadsquared/setup_service.rb b/app/services/crm/leadsquared/setup_service.rb index 0433f68fd..849c9dfe8 100644 --- a/app/services/crm/leadsquared/setup_service.rb +++ b/app/services/crm/leadsquared/setup_service.rb @@ -90,13 +90,13 @@ class Crm::Leadsquared::SetupService [ { name: "#{brand_name} Conversation Started", - score: @hook.settings['conversation_activity_score'].to_i || 0, + score: @hook.settings['conversation_activity_score'].to_i, direction: 0, setting_key: 'conversation_activity_code' }, { name: "#{brand_name} Conversation Transcript", - score: @hook.settings['transcript_activity_score'].to_i || 0, + score: @hook.settings['transcript_activity_score'].to_i, direction: 0, setting_key: 'transcript_activity_code' } diff --git a/app/services/facebook/send_on_facebook_service.rb b/app/services/facebook/send_on_facebook_service.rb index ed3b7e4ab..373865851 100644 --- a/app/services/facebook/send_on_facebook_service.rb +++ b/app/services/facebook/send_on_facebook_service.rb @@ -102,7 +102,7 @@ class Facebook::SendOnFacebookService < Base::SendOnChannelService def sent_first_outgoing_message_after_24_hours? # we can send max 1 message after 24 hour window - conversation.messages.outgoing.where('id > ?', conversation.last_incoming_message.id).count == 1 + conversation.messages.outgoing.where('id > ?', conversation.last_incoming_message.id).one? end def handle_facebook_error(exception) diff --git a/app/services/instagram/read_status_service.rb b/app/services/instagram/read_status_service.rb index 42022c01b..a27891d3f 100644 --- a/app/services/instagram/read_status_service.rb +++ b/app/services/instagram/read_status_service.rb @@ -14,6 +14,10 @@ class Instagram::ReadStatusService def message return unless params[:read][:mid] - @message ||= @channel.inbox.messages.find_by(source_id: params[:read][:mid]) + if instance_variable_defined?(:@message) + @message + else + @message = @channel.inbox.messages.find_by(source_id: params[:read][:mid]) + end end end diff --git a/app/services/line/incoming_message_service.rb b/app/services/line/incoming_message_service.rb index 4761292d0..2cf08f7e2 100644 --- a/app/services/line/incoming_message_service.rb +++ b/app/services/line/incoming_message_service.rb @@ -3,6 +3,7 @@ class Line::IncomingMessageService include ::FileTypeHelper + pattr_initialize [:inbox!, :params!] LINE_STICKER_IMAGE_URL = 'https://stickershop.line-scdn.net/stickershop/v1/sticker/%s/android/sticker.png'.freeze diff --git a/app/services/message_templates/hook_execution_service.rb b/app/services/message_templates/hook_execution_service.rb index 6205c8f3c..19f31b621 100644 --- a/app/services/message_templates/hook_execution_service.rb +++ b/app/services/message_templates/hook_execution_service.rb @@ -33,7 +33,7 @@ class MessageTemplates::HookExecutionService end def first_message_from_contact? - conversation.messages.outgoing.count.zero? && conversation.messages.template.count.zero? + conversation.messages.outgoing.none? && conversation.messages.template.none? end def should_send_greeting? diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 56c15b2f3..b099278fa 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -2,7 +2,11 @@ class SearchService pattr_initialize [:current_user!, :current_account!, :params!, :search_type!] def account_user - @account_user ||= current_account.account_users.find_by(user: current_user) + if instance_variable_defined?(:@account_user) + @account_user + else + @account_user = current_account.account_users.find_by(user: current_user) + end end def perform diff --git a/app/services/sms/delivery_status_service.rb b/app/services/sms/delivery_status_service.rb index dd071e242..7bd10e193 100644 --- a/app/services/sms/delivery_status_service.rb +++ b/app/services/sms/delivery_status_service.rb @@ -47,6 +47,10 @@ class Sms::DeliveryStatusService def message return unless params[:message][:id] - @message ||= inbox.messages.find_by(source_id: params[:message][:id]) + if instance_variable_defined?(:@message) + @message + else + @message = inbox.messages.find_by(source_id: params[:message][:id]) + end end end diff --git a/app/services/telegram/incoming_message_service.rb b/app/services/telegram/incoming_message_service.rb index 6eeb192f2..5f5db5fd5 100644 --- a/app/services/telegram/incoming_message_service.rb +++ b/app/services/telegram/incoming_message_service.rb @@ -4,6 +4,7 @@ class Telegram::IncomingMessageService include ::FileTypeHelper include ::Telegram::ParamHelpers + pattr_initialize [:inbox!, :params!] def perform diff --git a/app/services/twilio/delivery_status_service.rb b/app/services/twilio/delivery_status_service.rb index bf8422fcd..5d9254e68 100644 --- a/app/services/twilio/delivery_status_service.rb +++ b/app/services/twilio/delivery_status_service.rb @@ -54,6 +54,10 @@ class Twilio::DeliveryStatusService def message return unless params[:MessageSid] - @message ||= twilio_channel.inbox.messages.find_by(source_id: params[:MessageSid]) + if instance_variable_defined?(:@message) + @message + else + @message = twilio_channel.inbox.messages.find_by(source_id: params[:MessageSid]) + end end end diff --git a/app/workers/conversation_reply_email_worker.rb b/app/workers/conversation_reply_email_worker.rb index 0eddecaf7..991e366bb 100644 --- a/app/workers/conversation_reply_email_worker.rb +++ b/app/workers/conversation_reply_email_worker.rb @@ -1,6 +1,7 @@ # TODO: lets move this to active job, since thats what we use over all class ConversationReplyEmailWorker include Sidekiq::Worker + sidekiq_options queue: :mailers def perform(conversation_id, last_queued_id) diff --git a/app/workers/email_reply_worker.rb b/app/workers/email_reply_worker.rb index 14b668637..0a43cc9e2 100644 --- a/app/workers/email_reply_worker.rb +++ b/app/workers/email_reply_worker.rb @@ -1,5 +1,6 @@ class EmailReplyWorker include Sidekiq::Worker + sidekiq_options queue: :mailers, retry: 3 def perform(message_id) diff --git a/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb b/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb index 339fbdf3c..77791cf4d 100644 --- a/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb +++ b/enterprise/app/controllers/enterprise/api/v1/accounts_controller.rb @@ -1,5 +1,6 @@ class Enterprise::Api::V1::AccountsController < Api::BaseController include BillingHelper + before_action :fetch_account before_action :check_authorization before_action :check_cloud_env, only: [:limits, :toggle_deletion] diff --git a/enterprise/app/helpers/captain/chat_helper.rb b/enterprise/app/helpers/captain/chat_helper.rb index f90b8d07e..9f0d380d5 100644 --- a/enterprise/app/helpers/captain/chat_helper.rb +++ b/enterprise/app/helpers/captain/chat_helper.rb @@ -8,7 +8,7 @@ module Captain::ChatHelper messages: @messages, tools: @tool_registry&.registered_tools || [], response_format: { type: 'json_object' }, - temperature: @assistant&.config&.[]('temperature').to_f || 1 + temperature: @assistant&.config&.[]('temperature').to_f } ) diff --git a/enterprise/app/listeners/enterprise/action_cable_listener.rb b/enterprise/app/listeners/enterprise/action_cable_listener.rb index fbeee170a..a18c8fe58 100644 --- a/enterprise/app/listeners/enterprise/action_cable_listener.rb +++ b/enterprise/app/listeners/enterprise/action_cable_listener.rb @@ -1,5 +1,6 @@ module Enterprise::ActionCableListener include Events::Types + def copilot_message_created(event) copilot_message = event.data[:copilot_message] copilot_thread = copilot_message.copilot_thread diff --git a/enterprise/app/models/concerns/captain_tools_helpers.rb b/enterprise/app/models/concerns/captain_tools_helpers.rb index 5a660310c..413dd54db 100644 --- a/enterprise/app/models/concerns/captain_tools_helpers.rb +++ b/enterprise/app/models/concerns/captain_tools_helpers.rb @@ -31,7 +31,7 @@ module Concerns::CaptainToolsHelpers # # @return [Array] Array of available tool IDs def available_tool_ids - @available_tool_ids ||= available_agent_tools.map { |tool| tool[:id] } + @available_tool_ids ||= available_agent_tools.pluck(:id) end private diff --git a/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb b/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb index ce03efa41..f5789ca5e 100644 --- a/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb +++ b/enterprise/app/models/enterprise/account/plan_usage_and_limits.rb @@ -16,7 +16,7 @@ module Enterprise::Account::PlanUsageAndLimits end def increment_response_usage - current_usage = custom_attributes[CAPTAIN_RESPONSES_USAGE].to_i || 0 + current_usage = custom_attributes[CAPTAIN_RESPONSES_USAGE].to_i custom_attributes[CAPTAIN_RESPONSES_USAGE] = current_usage + 1 save end @@ -54,9 +54,9 @@ module Enterprise::Account::PlanUsageAndLimits total_count = captain_monthly_limit[type.to_s].to_i consumed = if type == :documents - custom_attributes[CAPTAIN_DOCUMENTS_USAGE].to_i || 0 + custom_attributes[CAPTAIN_DOCUMENTS_USAGE].to_i else - custom_attributes[CAPTAIN_RESPONSES_USAGE].to_i || 0 + custom_attributes[CAPTAIN_RESPONSES_USAGE].to_i end consumed = 0 if consumed.negative? diff --git a/enterprise/app/services/captain/tools/copilot/search_linear_issues_service.rb b/enterprise/app/services/captain/tools/copilot/search_linear_issues_service.rb index 0d59e194d..d091521c6 100644 --- a/enterprise/app/services/captain/tools/copilot/search_linear_issues_service.rb +++ b/enterprise/app/services/captain/tools/copilot/search_linear_issues_service.rb @@ -58,7 +58,7 @@ class Captain::Tools::Copilot::SearchLinearIssuesService < Captain::Tools::BaseS State: #{issue['state']['name']} Priority: #{format_priority(issue['priority'])} #{issue['assignee'] ? "Assignee: #{issue['assignee']['name']}" : 'Assignee: Unassigned'} - #{issue['description'].present? ? "\nDescription: #{issue['description']}" : ''} + #{"\nDescription: #{issue['description']}" if issue['description'].present?} ISSUE end diff --git a/lib/integrations/facebook/delivery_status.rb b/lib/integrations/facebook/delivery_status.rb index 1d6257bba..512701565 100644 --- a/lib/integrations/facebook/delivery_status.rb +++ b/lib/integrations/facebook/delivery_status.rb @@ -32,6 +32,10 @@ class Integrations::Facebook::DeliveryStatus end def facebook_channel - @facebook_channel ||= Channel::FacebookPage.find_by(page_id: params.recipient_id) + if instance_variable_defined?(:@facebook_channel) + @facebook_channel + else + @facebook_channel = Channel::FacebookPage.find_by(page_id: params.recipient_id) + end end end diff --git a/lib/integrations/google_translate/processor_service.rb b/lib/integrations/google_translate/processor_service.rb index 33fe56770..7497167e8 100644 --- a/lib/integrations/google_translate/processor_service.rb +++ b/lib/integrations/google_translate/processor_service.rb @@ -61,7 +61,11 @@ class Integrations::GoogleTranslate::ProcessorService end def hook - @hook ||= message.account.hooks.find_by(app_id: 'google_translate') + if instance_variable_defined?(:@hook) + @hook + else + @hook = message.account.hooks.find_by(app_id: 'google_translate') + end end def client diff --git a/lib/integrations/openai_base_service.rb b/lib/integrations/openai_base_service.rb index f06baf5b5..5458c484d 100644 --- a/lib/integrations/openai_base_service.rb +++ b/lib/integrations/openai_base_service.rb @@ -65,7 +65,11 @@ class Integrations::OpenaiBaseService end def conversation - @conversation ||= hook.account.conversations.find_by(display_id: event['data']['conversation_display_id']) + if instance_variable_defined?(:@conversation) + @conversation + else + @conversation = hook.account.conversations.find_by(display_id: event['data']['conversation_display_id']) + end end def valid_event_name? diff --git a/lib/integrations/slack/incoming_message_builder.rb b/lib/integrations/slack/incoming_message_builder.rb index 7eab71eae..51d281285 100644 --- a/lib/integrations/slack/incoming_message_builder.rb +++ b/lib/integrations/slack/incoming_message_builder.rb @@ -86,7 +86,11 @@ class Integrations::Slack::IncomingMessageBuilder end def integration_hook - @integration_hook ||= Integrations::Hook.find_by(reference_id: params[:event][:channel]) + if instance_variable_defined?(:@integration_hook) + @integration_hook + else + @integration_hook = Integrations::Hook.find_by(reference_id: params[:event][:channel]) + end end def slack_client diff --git a/lib/integrations/slack/send_on_slack_service.rb b/lib/integrations/slack/send_on_slack_service.rb index 0563ffd73..91ba751c9 100644 --- a/lib/integrations/slack/send_on_slack_service.rb +++ b/lib/integrations/slack/send_on_slack_service.rb @@ -1,5 +1,6 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService include RegexHelper + pattr_initialize [:message!, :hook!] def perform diff --git a/lib/seeders/reports/message_creator.rb b/lib/seeders/reports/message_creator.rb index fc10716d9..cf3e826ab 100644 --- a/lib/seeders/reports/message_creator.rb +++ b/lib/seeders/reports/message_creator.rb @@ -15,7 +15,7 @@ class Seeders::Reports::MessageCreator end def create_messages - message_count = rand(MESSAGES_PER_CONVERSATION..MESSAGES_PER_CONVERSATION + 5) + message_count = rand(MESSAGES_PER_CONVERSATION..(MESSAGES_PER_CONVERSATION + 5)) first_agent_reply = true message_count.times do |i| diff --git a/lib/webhooks/trigger.rb b/lib/webhooks/trigger.rb index 41b3a415d..24fab2ffd 100644 --- a/lib/webhooks/trigger.rb +++ b/lib/webhooks/trigger.rb @@ -48,7 +48,11 @@ class Webhooks::Trigger def message return if message_id.blank? - @message ||= Message.find_by(id: message_id) + if instance_variable_defined?(:@message) + @message + else + @message = Message.find_by(id: message_id) + end end def message_id diff --git a/spec/builders/v2/report_builder_spec.rb b/spec/builders/v2/report_builder_spec.rb index 2ad62e2d6..2c9604284 100644 --- a/spec/builders/v2/report_builder_spec.rb +++ b/spec/builders/v2/report_builder_spec.rb @@ -2,6 +2,7 @@ require 'rails_helper' describe V2::ReportBuilder do include ActiveJob::TestHelper + let_it_be(:account) { create(:account) } let_it_be(:label_1) { create(:label, title: 'Label_1', account: account) } let_it_be(:label_2) { create(:label, title: 'Label_2', account: account) } diff --git a/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb b/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb index ec6af61be..ccc655a15 100644 --- a/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/bulk_actions_controller_spec.rb @@ -2,6 +2,7 @@ require 'rails_helper' RSpec.describe 'Api::V1::Accounts::BulkActionsController', type: :request do include ActiveJob::TestHelper + let(:account) { create(:account) } let(:agent_1) { create(:user, account: account, role: :agent) } let(:agent_2) { create(:user, account: account, role: :agent) } diff --git a/spec/controllers/platform/api/v1/accounts_controller_spec.rb b/spec/controllers/platform/api/v1/accounts_controller_spec.rb index 63f53d0d6..e8ed5fe54 100644 --- a/spec/controllers/platform/api/v1/accounts_controller_spec.rb +++ b/spec/controllers/platform/api/v1/accounts_controller_spec.rb @@ -109,7 +109,7 @@ RSpec.describe 'Platform Accounts API', type: :request do expect(response).to have_http_status(:success) json_response = response.parsed_body expect(json_response.size).to eq(2) - expect(json_response.map { |acc| acc['name'] }).to include('Account A', 'Account B') + expect(json_response.pluck('name')).to include('Account A', 'Account B') end end end diff --git a/spec/enterprise/jobs/enterprise/create_stripe_customer_job_spec.rb b/spec/enterprise/jobs/enterprise/create_stripe_customer_job_spec.rb index b2c945800..89d3d5e4a 100644 --- a/spec/enterprise/jobs/enterprise/create_stripe_customer_job_spec.rb +++ b/spec/enterprise/jobs/enterprise/create_stripe_customer_job_spec.rb @@ -2,6 +2,7 @@ require 'rails_helper' RSpec.describe Enterprise::CreateStripeCustomerJob, type: :job do include ActiveJob::TestHelper + subject(:job) { described_class.perform_later(account) } let(:account) { create(:account) } diff --git a/spec/enterprise/jobs/enterprise/delete_object_job_spec.rb b/spec/enterprise/jobs/enterprise/delete_object_job_spec.rb index 53c1af7a8..00296bec0 100644 --- a/spec/enterprise/jobs/enterprise/delete_object_job_spec.rb +++ b/spec/enterprise/jobs/enterprise/delete_object_job_spec.rb @@ -2,6 +2,7 @@ require 'rails_helper' RSpec.describe DeleteObjectJob, type: :job do include ActiveJob::TestHelper + subject(:job) { described_class.perform_later(account) } let(:account) { create(:account) } diff --git a/spec/enterprise/models/sla_policy_spec.rb b/spec/enterprise/models/sla_policy_spec.rb index dc3c5cb87..48fa18fe1 100644 --- a/spec/enterprise/models/sla_policy_spec.rb +++ b/spec/enterprise/models/sla_policy_spec.rb @@ -2,6 +2,7 @@ require 'rails_helper' RSpec.describe SlaPolicy, type: :model do include ActiveJob::TestHelper + let(:account) { create(:account) } let(:admin) { create(:user, account: account, role: :administrator) } diff --git a/spec/finders/email_channel_finder_spec.rb b/spec/finders/email_channel_finder_spec.rb index fe57dec0b..29f85479d 100644 --- a/spec/finders/email_channel_finder_spec.rb +++ b/spec/finders/email_channel_finder_spec.rb @@ -2,6 +2,7 @@ require 'rails_helper' describe EmailChannelFinder do include ActionMailbox::TestHelper + let!(:channel_email) { create(:channel_email) } describe '#perform' do diff --git a/spec/mailboxes/mailbox_helper_spec.rb b/spec/mailboxes/mailbox_helper_spec.rb index 8352a46a1..8f2191c84 100644 --- a/spec/mailboxes/mailbox_helper_spec.rb +++ b/spec/mailboxes/mailbox_helper_spec.rb @@ -7,6 +7,7 @@ RSpec.describe MailboxHelper do let(:mailbox_helper_obj) do Class.new do include MailboxHelper + attr_accessor :conversation, :processed_mail def initialize(conversation, processed_mail) diff --git a/spec/models/enterprise/audit/conversation_spec.rb b/spec/models/enterprise/audit/conversation_spec.rb index 56ea2910d..eeaf87958 100644 --- a/spec/models/enterprise/audit/conversation_spec.rb +++ b/spec/models/enterprise/audit/conversation_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe 'Conversation Audit', type: :model do +RSpec.describe Conversation do let(:account) { create(:account) } let(:conversation) { create(:conversation, account: account) } diff --git a/spec/services/imap/fetch_email_service_spec.rb b/spec/services/imap/fetch_email_service_spec.rb index 46336bf0f..5ddd1adf1 100644 --- a/spec/services/imap/fetch_email_service_spec.rb +++ b/spec/services/imap/fetch_email_service_spec.rb @@ -2,6 +2,7 @@ require 'rails_helper' RSpec.describe Imap::FetchEmailService do include ActionMailbox::TestHelper + let(:logger) { instance_double(ActiveSupport::Logger, info: true, error: true) } let(:account) { create(:account) } let(:imap_email_channel) { create(:channel_email, :imap_email, account: account) } diff --git a/spec/services/imap/microsoft_fetch_email_service_spec.rb b/spec/services/imap/microsoft_fetch_email_service_spec.rb index a4a0a62d1..2d81fe77e 100644 --- a/spec/services/imap/microsoft_fetch_email_service_spec.rb +++ b/spec/services/imap/microsoft_fetch_email_service_spec.rb @@ -2,6 +2,7 @@ require 'rails_helper' RSpec.describe Imap::MicrosoftFetchEmailService do include ActionMailbox::TestHelper + let(:logger) { instance_double(ActiveSupport::Logger, info: true, error: true) } let(:account) { create(:account) } let(:microsoft_channel) { create(:channel_email, :microsoft_email, account: account) }