From 04f6460afb2cdaf880eda7d07d8c3cb9f1eae055 Mon Sep 17 00:00:00 2001 From: Sojan Jose Date: Tue, 16 Jun 2020 19:39:57 +0530 Subject: [PATCH] Chore: Fix failing sidekiq events for contact create (#966) --- .rubocop.yml | 2 +- app/models/contact.rb | 4 ++-- .../notification/push_notification_service.rb | 7 ++++++- config/initializers/00_init.rb | 2 +- config/initializers/custom_error_codes.rb | 6 ++---- config/routes.rb | 11 +++++------ .../notification/push_notification_service_spec.rb | 4 ++++ 7 files changed, 21 insertions(+), 15 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index af3e3f831..d1a1cb5f5 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -102,8 +102,8 @@ AllCops: Exclude: - 'bin/**/*' - 'db/schema.rb' - - 'config/**/*' - 'public/**/*' + - 'config/initializers/bot.rb' - 'vendor/**/*' - 'node_modules/**/*' - 'lib/tasks/auto_annotate_models.rake' diff --git a/app/models/contact.rb b/app/models/contact.rb index 00783f5bc..9ea82ba4c 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -38,8 +38,8 @@ class Contact < ApplicationRecord has_many :messages, dependent: :destroy before_validation :downcase_email - after_create :dispatch_create_event - after_update :dispatch_update_event + after_create_commit :dispatch_create_event + after_update_commit :dispatch_update_event def get_source_id(inbox_id) contact_inboxes.find_by!(inbox_id: inbox_id).source_id diff --git a/app/services/notification/push_notification_service.rb b/app/services/notification/push_notification_service.rb index 275e10c2b..76aa44478 100644 --- a/app/services/notification/push_notification_service.rb +++ b/app/services/notification/push_notification_service.rb @@ -41,8 +41,12 @@ class Notification::PushNotificationService app_account_conversation_url(account_id: conversation.account_id, id: conversation.display_id) end + def send_browser_push?(subscription) + ENV['VAPID_PUBLIC_KEY'] && subscription.browser_push? + end + def send_browser_push(subscription) - return unless subscription.browser_push? + return unless send_browser_push?(subscription) Webpush.payload_send( message: JSON.generate(push_message), @@ -63,6 +67,7 @@ class Notification::PushNotificationService end def send_fcm_push(subscription) + return unless ENV['FCM_SERVER_KEY'] return unless subscription.fcm? fcm = FCM.new(ENV['FCM_SERVER_KEY']) diff --git a/config/initializers/00_init.rb b/config/initializers/00_init.rb index b72fc4be1..8a12cc226 100644 --- a/config/initializers/00_init.rb +++ b/config/initializers/00_init.rb @@ -1 +1 @@ -APPS_CONFIG = YAML.load_file(File.join(Rails.root, 'config/integration', 'apps.yml')) \ No newline at end of file +APPS_CONFIG = YAML.load_file(Rails.root.join('config/integration/apps.yml')) diff --git a/config/initializers/custom_error_codes.rb b/config/initializers/custom_error_codes.rb index d5efcb034..e2800b2a3 100644 --- a/config/initializers/custom_error_codes.rb +++ b/config/initializers/custom_error_codes.rb @@ -1,4 +1,2 @@ -Rack::Utils::HTTP_STATUS_CODES.merge!( - 901 => 'Trial Expired', - 902 => 'Account Suspended' -) +Rack::Utils::HTTP_STATUS_CODES[901] = 'Trial Expired' +Rack::Utils::HTTP_STATUS_CODES[902] = 'Account Suspended' diff --git a/config/routes.rb b/config/routes.rb index 9602020e3..c4b6aeb62 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -87,11 +87,11 @@ Rails.application.routes.draw do end resource :notification_settings, only: [:show, :update] - resources :webhooks, except: [:show] - namespace :integrations do - resources :apps, only: [:index, :show] - resources :slack, only: [:create, :update, :destroy] - end + resources :webhooks, except: [:show] + namespace :integrations do + resources :apps, only: [:index, :show] + resources :slack, only: [:create, :update, :destroy] + end end end # end of account scoped api routes @@ -106,7 +106,6 @@ Rails.application.routes.draw do resources :agent_bots, only: [:index] - namespace :widget do resources :events, only: [:create] resources :messages, only: [:index, :create, :update] diff --git a/spec/services/notification/push_notification_service_spec.rb b/spec/services/notification/push_notification_service_spec.rb index 67fc9f999..44a5dbaf5 100644 --- a/spec/services/notification/push_notification_service_spec.rb +++ b/spec/services/notification/push_notification_service_spec.rb @@ -14,19 +14,23 @@ describe Notification::PushNotificationService do describe '#perform' do it 'sends webpush notifications for webpush subscription' do + ENV['VAPID_PUBLIC_KEY'] = 'test' create(:notification_subscription, user: notification.user) described_class.new(notification: notification).perform expect(Webpush).to have_received(:payload_send) expect(FCM).not_to have_received(:new) + ENV['ENABLE_ACCOUNT_SIGNUP'] = nil end it 'sends a fcm notification for firebase subscription' do + ENV['FCM_SERVER_KEY'] = 'test' create(:notification_subscription, user: notification.user, subscription_type: 'fcm') described_class.new(notification: notification).perform expect(FCM).to have_received(:new) expect(Webpush).not_to have_received(:payload_send) + ENV['ENABLE_ACCOUNT_SIGNUP'] = nil end end end