From 59b31615ed9991354b6075f2e2623df2b13038d5 Mon Sep 17 00:00:00 2001 From: Jordan Brough Date: Tue, 13 Sep 2022 08:10:06 -0400 Subject: [PATCH] chore: Use "create!" and "save!" bang methods when not checking the result (#5358) * Use "create!" when not checking for errors on the result * Use "save!" when not checking the result --- app/builders/notification_subscription_builder.rb | 2 +- .../api/v1/accounts/automation_rules_controller.rb | 2 +- .../accounts/channels/twilio_channels_controller.rb | 2 +- .../api/v1/accounts/contacts_controller.rb | 2 +- .../api/v1/widget/conversations_controller.rb | 4 ++-- .../devise_overrides/sessions_controller.rb | 2 +- app/controllers/twitter/callbacks_controller.rb | 4 ++-- app/jobs/conversations/activity_message_job.rb | 2 +- app/jobs/conversations/user_mention_job.rb | 2 +- app/listeners/reporting_event_listener.rb | 4 ++-- app/mailboxes/mailbox_helper.rb | 2 +- app/models/channel/facebook_page.rb | 2 +- app/services/line/incoming_message_service.rb | 2 +- app/services/sms/incoming_message_service.rb | 2 +- app/services/telegram/incoming_message_service.rb | 2 +- app/services/twilio/incoming_message_service.rb | 2 +- .../twitter/direct_message_parser_service.rb | 6 +++--- app/services/twitter/tweet_parser_service.rb | 2 +- lib/chatwoot_hub.rb | 2 +- lib/integrations/csml/processor_service.rb | 4 ++-- lib/integrations/dialogflow/processor_service.rb | 10 +++++----- lib/integrations/slack/incoming_message_builder.rb | 2 +- .../api/v1/widget/labels_controller_spec.rb | 2 +- spec/listeners/automation_rule_listener_spec.rb | 2 +- spec/mailboxes/reply_mailbox_spec.rb | 4 ++-- spec/mailboxes/support_mailbox_spec.rb | 4 ++-- spec/mailers/conversation_reply_mailer_spec.rb | 12 ++++++------ spec/models/campaign_spec.rb | 2 +- spec/models/conversation_spec.rb | 2 +- spec/workers/conversation_reply_email_worker_spec.rb | 2 +- 30 files changed, 47 insertions(+), 47 deletions(-) diff --git a/app/builders/notification_subscription_builder.rb b/app/builders/notification_subscription_builder.rb index af75ef2fd..0dede970f 100644 --- a/app/builders/notification_subscription_builder.rb +++ b/app/builders/notification_subscription_builder.rb @@ -25,7 +25,7 @@ class NotificationSubscriptionBuilder end def build_identifier_subscription - @identifier_subscription = user.notification_subscriptions.create(params.merge(identifier: identifier)) + @identifier_subscription = user.notification_subscriptions.create!(params.merge(identifier: identifier)) end def update_identifier_subscription diff --git a/app/controllers/api/v1/accounts/automation_rules_controller.rb b/app/controllers/api/v1/accounts/automation_rules_controller.rb index 859c8a4bf..0bb78d3ea 100644 --- a/app/controllers/api/v1/accounts/automation_rules_controller.rb +++ b/app/controllers/api/v1/accounts/automation_rules_controller.rb @@ -49,7 +49,7 @@ class Api::V1::Accounts::AutomationRulesController < Api::V1::Accounts::BaseCont def clone automation_rule = Current.account.automation_rules.find_by(id: params[:automation_rule_id]) new_rule = automation_rule.dup - new_rule.save + new_rule.save! @automation_rule = new_rule end diff --git a/app/controllers/api/v1/accounts/channels/twilio_channels_controller.rb b/app/controllers/api/v1/accounts/channels/twilio_channels_controller.rb index 55a9456cf..e6e5b63c3 100644 --- a/app/controllers/api/v1/accounts/channels/twilio_channels_controller.rb +++ b/app/controllers/api/v1/accounts/channels/twilio_channels_controller.rb @@ -44,7 +44,7 @@ class Api::V1::Accounts::Channels::TwilioChannelsController < Api::V1::Accounts: phone_number: phone_number, medium: medium ) - @inbox = Current.account.inboxes.create( + @inbox = Current.account.inboxes.create!( name: permitted_params[:name], channel: @twilio_channel ) diff --git a/app/controllers/api/v1/accounts/contacts_controller.rb b/app/controllers/api/v1/accounts/contacts_controller.rb index 7ef3c0d87..1c56e9c04 100644 --- a/app/controllers/api/v1/accounts/contacts_controller.rb +++ b/app/controllers/api/v1/accounts/contacts_controller.rb @@ -135,7 +135,7 @@ class Api::V1::Accounts::ContactsController < Api::V1::Accounts::BaseController inbox = Current.account.inboxes.find(params[:inbox_id]) source_id = params[:source_id] || SecureRandom.uuid - ContactInbox.create(contact: @contact, inbox: inbox, source_id: source_id) + ContactInbox.create!(contact: @contact, inbox: inbox, source_id: source_id) end def permitted_params diff --git a/app/controllers/api/v1/widget/conversations_controller.rb b/app/controllers/api/v1/widget/conversations_controller.rb index a01d33cdf..83206de90 100644 --- a/app/controllers/api/v1/widget/conversations_controller.rb +++ b/app/controllers/api/v1/widget/conversations_controller.rb @@ -9,7 +9,7 @@ class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController ActiveRecord::Base.transaction do process_update_contact @conversation = create_conversation - conversation.messages.create(message_params) + conversation.messages.create!(message_params) end end @@ -59,7 +59,7 @@ class Api::V1::Widget::ConversationsController < Api::V1::Widget::BaseController unless conversation.resolved? conversation.status = :resolved - conversation.save + conversation.save! end head :ok end diff --git a/app/controllers/devise_overrides/sessions_controller.rb b/app/controllers/devise_overrides/sessions_controller.rb index 289684f17..3bbc6ee3b 100644 --- a/app/controllers/devise_overrides/sessions_controller.rb +++ b/app/controllers/devise_overrides/sessions_controller.rb @@ -23,7 +23,7 @@ class DeviseOverrides::SessionsController < ::DeviseTokenAuth::SessionsControlle def authenticate_resource_with_sso_token @token = @resource.create_token - @resource.save + @resource.save! sign_in(:user, @resource, store: false, bypass: false) # invalidate the token after the user is signed in diff --git a/app/controllers/twitter/callbacks_controller.rb b/app/controllers/twitter/callbacks_controller.rb index 32fffbceb..0c2dc8263 100644 --- a/app/controllers/twitter/callbacks_controller.rb +++ b/app/controllers/twitter/callbacks_controller.rb @@ -44,12 +44,12 @@ class Twitter::CallbacksController < Twitter::BaseController end def create_inbox - twitter_profile = account.twitter_profiles.create( + twitter_profile = account.twitter_profiles.create!( twitter_access_token: parsed_body['oauth_token'], twitter_access_token_secret: parsed_body['oauth_token_secret'], profile_id: parsed_body['user_id'] ) - account.inboxes.create( + account.inboxes.create!( name: parsed_body['screen_name'], channel: twitter_profile ) diff --git a/app/jobs/conversations/activity_message_job.rb b/app/jobs/conversations/activity_message_job.rb index a0982f472..54f631ee6 100644 --- a/app/jobs/conversations/activity_message_job.rb +++ b/app/jobs/conversations/activity_message_job.rb @@ -2,6 +2,6 @@ class Conversations::ActivityMessageJob < ApplicationJob queue_as :default def perform(conversation, message_params) - conversation.messages.create(message_params) + conversation.messages.create!(message_params) end end diff --git a/app/jobs/conversations/user_mention_job.rb b/app/jobs/conversations/user_mention_job.rb index 1eed044a5..fe162792e 100644 --- a/app/jobs/conversations/user_mention_job.rb +++ b/app/jobs/conversations/user_mention_job.rb @@ -10,7 +10,7 @@ class Conversations::UserMentionJob < ApplicationJob ) if mention.nil? - Mention.create( + Mention.create!( user_id: mentioned_user_id, conversation_id: conversation_id, mentioned_at: Time.zone.now, diff --git a/app/listeners/reporting_event_listener.rb b/app/listeners/reporting_event_listener.rb index abd28ad32..3a787e417 100644 --- a/app/listeners/reporting_event_listener.rb +++ b/app/listeners/reporting_event_listener.rb @@ -16,7 +16,7 @@ class ReportingEventListener < BaseListener event_start_time: conversation.created_at, event_end_time: conversation.updated_at ) - reporting_event.save + reporting_event.save! end def first_reply_created(event) @@ -39,6 +39,6 @@ class ReportingEventListener < BaseListener # rubocop:disable Rails/SkipsModelValidations conversation.update_columns(first_reply_created_at: message.created_at) # rubocop:enable Rails/SkipsModelValidations - reporting_event.save + reporting_event.save! end end diff --git a/app/mailboxes/mailbox_helper.rb b/app/mailboxes/mailbox_helper.rb index f0abc0eba..1519343ca 100644 --- a/app/mailboxes/mailbox_helper.rb +++ b/app/mailboxes/mailbox_helper.rb @@ -4,7 +4,7 @@ module MailboxHelper def create_message return if @conversation.messages.find_by(source_id: processed_mail.message_id).present? - @message = @conversation.messages.create( + @message = @conversation.messages.create!( account_id: @conversation.account_id, sender: @conversation.contact, content: mail_content&.truncate(150_000), diff --git a/app/models/channel/facebook_page.rb b/app/models/channel/facebook_page.rb index 3737279f8..2153708e2 100644 --- a/app/models/channel/facebook_page.rb +++ b/app/models/channel/facebook_page.rb @@ -39,7 +39,7 @@ class Channel::FacebookPage < ApplicationRecord def create_contact_inbox(instagram_id, name) ActiveRecord::Base.transaction do contact = inbox.account.contacts.create!(name: name) - ::ContactInbox.create( + ::ContactInbox.create!( contact_id: contact.id, inbox_id: inbox.id, source_id: instagram_id diff --git a/app/services/line/incoming_message_service.rb b/app/services/line/incoming_message_service.rb index e3c85ef99..535c03dc7 100644 --- a/app/services/line/incoming_message_service.rb +++ b/app/services/line/incoming_message_service.rb @@ -30,7 +30,7 @@ class Line::IncomingMessageService def message_created?(event) return unless event_type_message?(event) - @message = @conversation.messages.create( + @message = @conversation.messages.create!( content: event['message']['text'], account_id: @inbox.account_id, inbox_id: @inbox.id, diff --git a/app/services/sms/incoming_message_service.rb b/app/services/sms/incoming_message_service.rb index ec9cf0a21..7ee6e3e63 100644 --- a/app/services/sms/incoming_message_service.rb +++ b/app/services/sms/incoming_message_service.rb @@ -6,7 +6,7 @@ class Sms::IncomingMessageService def perform set_contact set_conversation - @message = @conversation.messages.create( + @message = @conversation.messages.create!( content: params[:text], account_id: @inbox.account_id, inbox_id: @inbox.id, diff --git a/app/services/telegram/incoming_message_service.rb b/app/services/telegram/incoming_message_service.rb index ae26db899..d56e20e8d 100644 --- a/app/services/telegram/incoming_message_service.rb +++ b/app/services/telegram/incoming_message_service.rb @@ -12,7 +12,7 @@ class Telegram::IncomingMessageService set_contact update_contact_avatar set_conversation - @message = @conversation.messages.create( + @message = @conversation.messages.create!( content: params[:message][:text].presence || params[:message][:caption], account_id: @inbox.account_id, inbox_id: @inbox.id, diff --git a/app/services/twilio/incoming_message_service.rb b/app/services/twilio/incoming_message_service.rb index 39d125eee..50c77111c 100644 --- a/app/services/twilio/incoming_message_service.rb +++ b/app/services/twilio/incoming_message_service.rb @@ -8,7 +8,7 @@ class Twilio::IncomingMessageService set_contact set_conversation - @message = @conversation.messages.create( + @message = @conversation.messages.create!( content: params[:Body], account_id: @inbox.account_id, inbox_id: @inbox.id, diff --git a/app/services/twitter/direct_message_parser_service.rb b/app/services/twitter/direct_message_parser_service.rb index e3679f278..5383e2e89 100644 --- a/app/services/twitter/direct_message_parser_service.rb +++ b/app/services/twitter/direct_message_parser_service.rb @@ -7,7 +7,7 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService set_inbox ensure_contacts set_conversation - @message = @conversation.messages.create( + @message = @conversation.messages.create!( content: message_create_data['message_data']['text'], account_id: @inbox.account_id, inbox_id: @inbox.id, @@ -30,7 +30,7 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService def save_media_urls(file) @message.content_attributes[:media_url] = file['media_url'] @message.content_attributes[:display_url] = file['display_url'] - @message.save + @message.save! end def direct_message_events_params @@ -121,6 +121,6 @@ class Twitter::DirectMessageParserService < Twitter::WebhooksBaseService content_type: media['type'] } ) - @message.save + @message.save! end end diff --git a/app/services/twitter/tweet_parser_service.rb b/app/services/twitter/tweet_parser_service.rb index 82664ea5f..207f14abe 100644 --- a/app/services/twitter/tweet_parser_service.rb +++ b/app/services/twitter/tweet_parser_service.rb @@ -80,7 +80,7 @@ class Twitter::TweetParserService < Twitter::WebhooksBaseService def create_message find_or_create_contact(user) set_conversation - @conversation.messages.create( + @conversation.messages.create!( account_id: @inbox.account_id, sender: @contact, content: tweet_text, diff --git a/lib/chatwoot_hub.rb b/lib/chatwoot_hub.rb index f909527fc..68bae8267 100644 --- a/lib/chatwoot_hub.rb +++ b/lib/chatwoot_hub.rb @@ -7,7 +7,7 @@ class ChatwootHub def self.installation_identifier identifier = InstallationConfig.find_by(name: 'INSTALLATION_IDENTIFIER')&.value - identifier ||= InstallationConfig.create(name: 'INSTALLATION_IDENTIFIER', value: SecureRandom.uuid).value + identifier ||= InstallationConfig.create!(name: 'INSTALLATION_IDENTIFIER', value: SecureRandom.uuid).value identifier end diff --git a/lib/integrations/csml/processor_service.rb b/lib/integrations/csml/processor_service.rb index e9f7e85ef..3a8f21077 100644 --- a/lib/integrations/csml/processor_service.rb +++ b/lib/integrations/csml/processor_service.rb @@ -84,7 +84,7 @@ class Integrations::Csml::ProcessorService < Integrations::BotProcessorService end def process_text_messages(message_payload, conversation) - conversation.messages.create( + conversation.messages.create!( { message_type: :outgoing, account_id: conversation.account_id, @@ -99,7 +99,7 @@ class Integrations::Csml::ProcessorService < Integrations::BotProcessorService buttons = message_payload['content']['buttons'].map do |button| { title: button['content']['title'], value: button['content']['payload'] } end - conversation.messages.create( + conversation.messages.create!( { message_type: :outgoing, account_id: conversation.account_id, diff --git a/lib/integrations/dialogflow/processor_service.rb b/lib/integrations/dialogflow/processor_service.rb index b23b8e082..384c41c11 100644 --- a/lib/integrations/dialogflow/processor_service.rb +++ b/lib/integrations/dialogflow/processor_service.rb @@ -43,10 +43,10 @@ class Integrations::Dialogflow::ProcessorService < Integrations::BotProcessorSer return if content_params.blank? conversation = message.conversation - conversation.messages.create(content_params.merge({ - message_type: :outgoing, - account_id: conversation.account_id, - inbox_id: conversation.inbox_id - })) + conversation.messages.create!(content_params.merge({ + message_type: :outgoing, + account_id: conversation.account_id, + inbox_id: conversation.inbox_id + })) end end diff --git a/lib/integrations/slack/incoming_message_builder.rb b/lib/integrations/slack/incoming_message_builder.rb index 966d897f9..a2f75ce3a 100644 --- a/lib/integrations/slack/incoming_message_builder.rb +++ b/lib/integrations/slack/incoming_message_builder.rb @@ -83,7 +83,7 @@ class Integrations::Slack::IncomingMessageBuilder def create_message return unless conversation - @message = conversation.messages.create( + @message = conversation.messages.create!( message_type: :outgoing, account_id: conversation.account_id, inbox_id: conversation.inbox_id, diff --git a/spec/controllers/api/v1/widget/labels_controller_spec.rb b/spec/controllers/api/v1/widget/labels_controller_spec.rb index bdd47f9c9..fe242c29b 100644 --- a/spec/controllers/api/v1/widget/labels_controller_spec.rb +++ b/spec/controllers/api/v1/widget/labels_controller_spec.rb @@ -26,7 +26,7 @@ RSpec.describe '/api/v1/widget/labels', type: :request do context 'with correct website token and a defined label' do before do - account.labels.create(title: 'customer-support') + account.labels.create!(title: 'customer-support') end it 'add the label to the conversation' do diff --git a/spec/listeners/automation_rule_listener_spec.rb b/spec/listeners/automation_rule_listener_spec.rb index 5ca377df8..2b31fa8ae 100644 --- a/spec/listeners/automation_rule_listener_spec.rb +++ b/spec/listeners/automation_rule_listener_spec.rb @@ -43,7 +43,7 @@ describe AutomationRuleListener do ]) file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') automation_rule.files.attach(file) - automation_rule.save + automation_rule.save! end describe '#conversation_updated with contacts attributes' do diff --git a/spec/mailboxes/reply_mailbox_spec.rb b/spec/mailboxes/reply_mailbox_spec.rb index c73de877c..a383e3e5c 100644 --- a/spec/mailboxes/reply_mailbox_spec.rb +++ b/spec/mailboxes/reply_mailbox_spec.rb @@ -19,7 +19,7 @@ RSpec.describe ReplyMailbox, type: :mailbox do before do # this UUID is hardcoded in the reply.eml, that's why we are updating this conversation.uuid = '6bdc3f4d-0bec-4515-a284-5d916fdde489' - conversation.save + conversation.save! described_subject end @@ -129,7 +129,7 @@ RSpec.describe ReplyMailbox, type: :mailbox do before do # this UUID is hardcoded in the reply.eml, that's why we are updating this conversation.uuid = '6bdc3f4d-0bec-4515-a284-5d916fdde489' - conversation.save + conversation.save! end it 'add the mail content as new message on the conversation' do diff --git a/spec/mailboxes/support_mailbox_spec.rb b/spec/mailboxes/support_mailbox_spec.rb index 88ded0831..59217b212 100644 --- a/spec/mailboxes/support_mailbox_spec.rb +++ b/spec/mailboxes/support_mailbox_spec.rb @@ -32,7 +32,7 @@ RSpec.describe SupportMailbox, type: :mailbox do before do # this email is hardcoded in the support.eml, that's why we are updating this channel_email.email = 'care@example.com' - channel_email.save + channel_email.save! end describe 'covers email address format' do @@ -121,7 +121,7 @@ RSpec.describe SupportMailbox, type: :mailbox do before do # this email is hardcoded eml fixture file that's why we are updating this channel_email.email = 'support@chatwoot.com' - channel_email.save + channel_email.save! end it 'create new contact with original sender' do diff --git a/spec/mailers/conversation_reply_mailer_spec.rb b/spec/mailers/conversation_reply_mailer_spec.rb index cb3c4a9f9..67645e101 100644 --- a/spec/mailers/conversation_reply_mailer_spec.rb +++ b/spec/mailers/conversation_reply_mailer_spec.rb @@ -54,15 +54,15 @@ RSpec.describe ConversationReplyMailer, type: :mailer do it 'renders the subject in conversation as reply' do conversation.additional_attributes = { 'mail_subject': 'Mail Subject' } - conversation.save - new_message.save + conversation.save! + new_message.save! expect(mail.subject).to eq('Re: Mail Subject') end it 'not have private notes' do # make the message private private_message.private = true - private_message.save + private_message.save! expect(mail.body.decoded).not_to include(private_message.content) expect(mail.body.decoded).to include(message.content) @@ -104,7 +104,7 @@ RSpec.describe ConversationReplyMailer, type: :mailer do let(:mail) { described_class.reply_without_summary(message_2.conversation, message_2.id).deliver_now } before do - message_2.save + message_2.save! end it 'renders the default subject' do @@ -113,14 +113,14 @@ RSpec.describe ConversationReplyMailer, type: :mailer do it 'renders the subject in conversation' do conversation.additional_attributes = { 'mail_subject': 'Mail Subject' } - conversation.save + conversation.save! expect(mail.subject).to eq('Mail Subject') end it 'not have private notes' do # make the message private private_message.private = true - private_message.save + private_message.save! expect(mail.body.decoded).not_to include(private_message.content) end diff --git a/spec/models/campaign_spec.rb b/spec/models/campaign_spec.rb index 53890458e..f99f18390 100644 --- a/spec/models/campaign_spec.rb +++ b/spec/models/campaign_spec.rb @@ -15,7 +15,7 @@ RSpec.describe Campaign, type: :model do let(:campaign) { build(:campaign, inbox: website_inbox, display_id: nil, trigger_rules: { url: 'https://test.com' }) } before do - campaign.save + campaign.save! campaign.reload end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 22979b7c6..3271bdf19 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -19,7 +19,7 @@ RSpec.describe Conversation, type: :model do let(:conversation) { build(:conversation, display_id: nil) } before do - conversation.save + conversation.save! conversation.reload end diff --git a/spec/workers/conversation_reply_email_worker_spec.rb b/spec/workers/conversation_reply_email_worker_spec.rb index 936242a06..3483c04a5 100644 --- a/spec/workers/conversation_reply_email_worker_spec.rb +++ b/spec/workers/conversation_reply_email_worker_spec.rb @@ -37,7 +37,7 @@ RSpec.describe ConversationReplyEmailWorker, type: :worker do end it 'calls ConversationSummaryMailer#reply_without_summary when last incoming message was from email' do - message.save + message.save! described_class.new.perform(1, message.id) expect(mailer).to have_received(:reply_without_summary) end