From 989518fa68924a9b8ef1745d7b24f5b463d6412d Mon Sep 17 00:00:00 2001 From: Seth Call Date: Thu, 23 Apr 2015 10:34:17 -0500 Subject: [PATCH] * VRFS-3082 - email alert if jamtrack fails --- admin/config/environments/development.rb | 1 + ruby/lib/jam_ruby/app/mailers/admin_mailer.rb | 8 ++++ .../models/recurly_transaction_web_hook.rb | 8 ++-- ruby/lib/jam_ruby/models/user.rb | 5 +++ .../jam_ruby/resque/google_analytics_event.rb | 2 + ruby/spec/support/utilities.rb | 4 ++ .../javascripts/download_jamtrack.js.coffee | 1 + web/app/assets/javascripts/jam_rest.js | 14 +++++++ web/app/controllers/api_alerts_controller.rb | 39 +++++++++++++++++++ web/config/application.rb | 4 +- web/config/environments/development.rb | 4 +- web/config/routes.rb | 2 + 12 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 web/app/controllers/api_alerts_controller.rb diff --git a/admin/config/environments/development.rb b/admin/config/environments/development.rb index b5242d965..b449898e8 100644 --- a/admin/config/environments/development.rb +++ b/admin/config/environments/development.rb @@ -45,4 +45,5 @@ JamAdmin::Application.configure do config.show_log_configuration = true config.email_generic_from = 'nobody-dev@jamkazam.com' + config.email_alerts_alias = 'alerts-dev@jamkazam.com' end diff --git a/ruby/lib/jam_ruby/app/mailers/admin_mailer.rb b/ruby/lib/jam_ruby/app/mailers/admin_mailer.rb index 2e9570523..1a7f35e37 100644 --- a/ruby/lib/jam_ruby/app/mailers/admin_mailer.rb +++ b/ruby/lib/jam_ruby/app/mailers/admin_mailer.rb @@ -13,6 +13,14 @@ module JamRuby sendgrid_unique_args :env => Environment.mode def alerts(options) + mail(to: APP_CONFIG.email_alerts_alias, + from: APP_CONFIG.email_generic_from, + body: options[:body], + content_type: "text/plain", + subject: options[:subject]) + end + + def recurly_alerts(options) mail(to: APP_CONFIG.email_recurly_notice, from: APP_CONFIG.email_generic_from, body: options[:body], diff --git a/ruby/lib/jam_ruby/models/recurly_transaction_web_hook.rb b/ruby/lib/jam_ruby/models/recurly_transaction_web_hook.rb index d94dca2e0..1766a913e 100644 --- a/ruby/lib/jam_ruby/models/recurly_transaction_web_hook.rb +++ b/ruby/lib/jam_ruby/models/recurly_transaction_web_hook.rb @@ -91,19 +91,19 @@ module JamRuby jam_track_right = jam_track.right_for_user(transaction.user) if jam_track if jam_track_right jam_track_right.destroy - AdminMailer.alerts({ + AdminMailer.recurly_alerts({ subject:"NOTICE: #{transaction.user.email} has had JamTrack: #{jam_track.name} revoked", body: "A void event came from Recurly for sale with Recurly invoice ID #{sale.recurly_invoice_id}. We deleted their right to the track in our own database as a result." }).deliver else - AdminMailer.alerts({ + AdminMailer.recurly_alerts({ subject:"NOTICE: #{transaction.user.email} got a refund, but unable to find JamTrackRight to delete", body: "This should just mean the user already has no rights to the JamTrackRight when the refund came in. Not a big deal, but sort of weird..." }).deliver end else - AdminMailer.alerts({ + AdminMailer.recurly_alerts({ subject:"ACTION REQUIRED: #{transaction.user.email} got a refund it was not for total value of a JamTrack sale", body: "We received a refund notice for an amount that was not the same as the original sale. So, no action was taken in the database. sale total: #{sale.recurly_total_in_cents}, refund amount: #{transaction.amount_in_cents}" }).deliver @@ -111,7 +111,7 @@ module JamRuby else - AdminMailer.alerts({ + AdminMailer.recurly_alerts({ subject: "ACTION REQUIRED: #{transaction.user.email} has refund on invoice with multiple JamTracks", body: "You will have to manually revoke any JamTrackRights in our database for the appropriate JamTracks" }).deliver diff --git a/ruby/lib/jam_ruby/models/user.rb b/ruby/lib/jam_ruby/models/user.rb index 7ca9e8bec..d05f325de 100644 --- a/ruby/lib/jam_ruby/models/user.rb +++ b/ruby/lib/jam_ruby/models/user.rb @@ -1568,6 +1568,11 @@ module JamRuby verifier.generate(user.id) end + # URL to jam-admin + def admin_url + APP_CONFIG.admin_root_url + "/admin/users/" + id + end + private def create_remember_token self.remember_token = SecureRandom.urlsafe_base64 diff --git a/ruby/lib/jam_ruby/resque/google_analytics_event.rb b/ruby/lib/jam_ruby/resque/google_analytics_event.rb index 0c1cbbf06..5394744f2 100644 --- a/ruby/lib/jam_ruby/resque/google_analytics_event.rb +++ b/ruby/lib/jam_ruby/resque/google_analytics_event.rb @@ -101,7 +101,9 @@ module JamRuby el: 'data', ev: data.to_s } + RestClient.post(APP_CONFIG.ga_endpoint, params: params, timeout: 8, open_timeout: 8) + @@log.info("done (#{category}, #{action})") end diff --git a/ruby/spec/support/utilities.rb b/ruby/spec/support/utilities.rb index f0da62594..c033de858 100644 --- a/ruby/spec/support/utilities.rb +++ b/ruby/spec/support/utilities.rb @@ -3,6 +3,10 @@ JAMKAZAM_TESTING_BUCKET = 'jamkazam-testing' # cuz i'm not comfortable using aws def app_config klass = Class.new do + def admin_root_url + 'http://localhost:3333' + end + def email_alerts_alias 'alerts@jamkazam.com' end diff --git a/web/app/assets/javascripts/download_jamtrack.js.coffee b/web/app/assets/javascripts/download_jamtrack.js.coffee index 3d9c1ac38..75967ced5 100644 --- a/web/app/assets/javascripts/download_jamtrack.js.coffee +++ b/web/app/assets/javascripts/download_jamtrack.js.coffee @@ -176,6 +176,7 @@ context.JK.DownloadJamTrack = class DownloadJamTrack if @state == @states.errored data.result = 'error' data.detail = @errorReason + @rest.createAlert("JamTrack Sync failed for #{context.JK.currentUserName}", data) else data.result = 'success' diff --git a/web/app/assets/javascripts/jam_rest.js b/web/app/assets/javascripts/jam_rest.js index 8e8226643..0640f83b8 100644 --- a/web/app/assets/javascripts/jam_rest.js +++ b/web/app/assets/javascripts/jam_rest.js @@ -1662,6 +1662,19 @@ }); } + function createAlert(subject, data) { + var message = {subject:subject}; + $.extend(message, data); + console.log("message", message) + return $.ajax({ + type: "POST", + url: '/api/alerts', + dataType: "json", + contentType: 'application/json', + data: JSON.stringify(message), + }); + } + function initialize() { return self; } @@ -1811,6 +1824,7 @@ this.addRecordingTimeline = addRecordingTimeline; this.playJamTrack = playJamTrack; this.createSignupHint = createSignupHint; + this.createAlert = createAlert; return this; }; diff --git a/web/app/controllers/api_alerts_controller.rb b/web/app/controllers/api_alerts_controller.rb new file mode 100644 index 000000000..0dad78499 --- /dev/null +++ b/web/app/controllers/api_alerts_controller.rb @@ -0,0 +1,39 @@ + +class ApiAlertsController < ApiController + + before_filter :api_signed_in_user + + + def log + @log || Logging.logger[ApiAlertsController] + end + + + def create + + if Rails.application.config.alerts_api_enabled + + result = params[:result] + detail = params[:detail] + + body = "" + body << "User: " + current_user.admin_url + "\n" + body << "Reason: #{result}\n" if result + body << "Detail: #{detail}\n" if detail + body << "\n\n\ndata:\n-----\n" + body << JSON.pretty_generate(params) + + AdminMailer.alerts({ + subject:params[:subject] || 'Alerts API (no subject)', + body:body + }).deliver + + + end + + render json: {}, :status => :ok + + + end + +end diff --git a/web/config/application.rb b/web/config/application.rb index b57464159..ac6553438 100644 --- a/web/config/application.rb +++ b/web/config/application.rb @@ -126,7 +126,8 @@ if defined?(Bundler) config.external_port = ENV['EXTERNAL_PORT'] || 3000 config.external_protocol = ENV['EXTERNAL_PROTOCOL'] || 'http://' config.external_root_url = "#{config.external_protocol}#{config.external_hostname}#{(config.external_port == 80 || config.external_port == 443) ? '' : ':' + config.external_port.to_s}" - + config.admin_port = ENV['ADMIN_PORT'] || 3333 + config.admin_root_url = "#{config.external_protocol}#{config.external_hostname}#{(config.admin_port == 80 || config.admin_port == 443) ? '' : ':' + config.admin_port.to_s}" # set this to false if you want to disable signups (lock down public user creation) config.signup_enabled = true @@ -328,5 +329,6 @@ if defined?(Bundler) config.minimal_curtain = false config.video_available = "none" + config.alerts_api_enabled = true end end diff --git a/web/config/environments/development.rb b/web/config/environments/development.rb index 2225000ef..ceb4e50e8 100644 --- a/web/config/environments/development.rb +++ b/web/config/environments/development.rb @@ -91,5 +91,7 @@ SampleApp::Application.configure do config.youtube_app_name = "JamKazamDev" config.jam_tracks_available=true config.minimal_curtain = true - config.video_available="full" + config.video_available= ENV['VIDEO_AVAILABILITY'] || "none" + config.email_generic_from = 'nobody-dev@jamkazam.com' + config.email_alerts_alias = 'alerts-dev@jamkazam.com' end diff --git a/web/config/routes.rb b/web/config/routes.rb index 70d71e5da..fc8ada1a0 100644 --- a/web/config/routes.rb +++ b/web/config/routes.rb @@ -563,5 +563,7 @@ SampleApp::Application.routes.draw do match '/signup_hints' => 'api_signup_hints#create', :via => :post match '/signup_hints/:id' => 'api_signup_hints#show', :via => :get, :as => :api_signup_hint_detail + + match '/alerts' => 'api_alerts#create', :via => :post end end