diff --git a/.env.example b/.env.example index c4febee6c..8dc799c50 100644 --- a/.env.example +++ b/.env.example @@ -28,3 +28,7 @@ FULL_URI=http://localhost:3000 # Use anything you want for DURABLE_JWT_CODE (but it needs to be not nil) DURABLE_JWT_CODE=durable-jwt + +# Insert your own email for ADMIN_EMAIL + +ADMIN_EMAIL=your.email@example.com diff --git a/app/controllers/api/v1/staging_controller.rb b/app/controllers/api/v1/staging_controller.rb index 393a7f856..c12e352c4 100644 --- a/app/controllers/api/v1/staging_controller.rb +++ b/app/controllers/api/v1/staging_controller.rb @@ -44,6 +44,7 @@ def post_event_course_org setter = EventCourseOrgSetter.new(event: event, event_group: event_group, course: course, organization: organization, params: params) setter.set_resources if setter.status == :ok + AdminMailer.new_event(event, current_user).deliver_later if params[:id] == 'new' render json: setter.resources.map { |resource| [resource.class.to_s.underscore, resource] }.to_h, status: setter.status else render json: {errors: setter.resources.map { |resource| jsonapi_error_object(resource) }}, status: setter.status diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 2ff8112af..1cac4067d 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -60,7 +60,7 @@ def destroy authorize @event @event.destroy - redirect_to events_path + redirect_to event_groups_path end # Special views with results diff --git a/app/mailers/admin_mailer.rb b/app/mailers/admin_mailer.rb new file mode 100644 index 000000000..2e581d59a --- /dev/null +++ b/app/mailers/admin_mailer.rb @@ -0,0 +1,7 @@ +class AdminMailer < ApplicationMailer + def new_event(event, user) + @event = event + @user = user + mail(to: ENV['ADMIN_EMAIL'], subject: "New event: #{@event.name}") + end +end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index a11d923d3..2461c4a6f 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,4 +1,4 @@ class ApplicationMailer < ActionMailer::Base default from: "no-reply@#{ENV['BASE_URI']}" - # layout 'mailer' + layout 'mailer' end diff --git a/app/views/admin_mailer/new_event.html.erb b/app/views/admin_mailer/new_event.html.erb new file mode 100644 index 000000000..e5b50db5e --- /dev/null +++ b/app/views/admin_mailer/new_event.html.erb @@ -0,0 +1,4 @@ +

+ <%= "#{@user.full_name} has created a new event called" %> + <%= link_to @event.name, event_url(@event) %> +

diff --git a/app/views/admin_mailer/new_event.text.erb b/app/views/admin_mailer/new_event.text.erb new file mode 100644 index 000000000..251d1feb8 --- /dev/null +++ b/app/views/admin_mailer/new_event.text.erb @@ -0,0 +1 @@ +<%= "#{@user.full_name} has created a new event called #{@event.name}." %> diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb new file mode 100644 index 000000000..cbd34d2e9 --- /dev/null +++ b/app/views/layouts/mailer.html.erb @@ -0,0 +1,13 @@ + + + + + + + + + <%= yield %> + + diff --git a/app/views/layouts/mailer.text.erb b/app/views/layouts/mailer.text.erb new file mode 100644 index 000000000..37f0bddbd --- /dev/null +++ b/app/views/layouts/mailer.text.erb @@ -0,0 +1 @@ +<%= yield %> diff --git a/config/environment.rb b/config/environment.rb index 4c464ae10..ee8d90dc6 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -3,18 +3,3 @@ # Initialize the Rails application. Rails.application.initialize! - -Time::DATE_FORMATS[:submitted] = "%b %d %Y" - -Time::DATE_FORMATS[:time_short] = "" -Time::DATE_FORMATS[:time_long] = "" - -ActionMailer::Base.smtp_settings = { - :address => 'smtp.sendgrid.net', - :port => '587', - :authentication => :plain, - :user_name => ENV['SENDGRID_USERNAME'], - :password => ENV['SENDGRID_PASSWORD'], - :domain => 'heroku.com', - :enable_starttls_auto => true -} \ No newline at end of file diff --git a/config/environments/production.rb b/config/environments/production.rb index add6dd159..771190d39 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -13,6 +13,16 @@ config.action_mailer.delivery_method = :smtp config.action_mailer.default_url_options = {host: ENV['BASE_URI'], protocol: 'http'} + config.action_mailer.smtp_settings = { + address: 'smtp.sendgrid.net', + port: '587', + authentication: :plain, + user_name: ENV['SENDGRID_USERNAME'], + password: ENV['SENDGRID_PASSWORD'], + domain: 'heroku.com', + enable_starttls_auto: true + } + # Full error reports are disabled and caching is turned on. config.consider_all_requests_local = false config.action_controller.perform_caching = true diff --git a/spec/controllers/api/v1/events_controller_spec.rb b/spec/controllers/api/v1/events_controller_spec.rb index 0e0977f8f..140d27474 100644 --- a/spec/controllers/api/v1/events_controller_spec.rb +++ b/spec/controllers/api/v1/events_controller_spec.rb @@ -199,7 +199,7 @@ context 'when provided with a file' do let(:request_params) { {id: event.id, data_format: 'csv_efforts', file: file} } - let(:file) { file_fixture('test_efforts.csv') } # Should work in Rails 5 + let(:file) { file_fixture('test_efforts.csv') } it 'returns a successful json response' do skip 'Until Rails 5 upgrade' @@ -344,6 +344,42 @@ end end + context 'when provided with multiple live_times relating the same bib number in a multi-lap event' do + let(:data) { + [{'type' => 'live_time', + 'attributes' => + {'source' => source, + 'sub_split_kind' => 'in', + 'with_pacer' => 'false', + 'bib_number' => efforts.first.bib_number, + 'split_id' => splits.first.id, + 'stopped_here' => 'false', + 'absolute_time' => '2018-02-10 11:13:17-7:00'}}, + {'type' => 'live_time', + 'attributes' => + {'source' => source, + 'sub_split_kind' => 'in', + 'with_pacer' => 'false', + 'bib_number' => efforts.second.bib_number, + 'split_id' => splits.first.id, + 'stopped_here' => 'false', + 'absolute_time' => '2018-02-10 09:16:07-7:00'}}, + {'type' => 'live_time', + 'attributes' => + {'source' => source, + 'sub_split_kind' => 'in', + 'with_pacer' => 'false', + 'bib_number' => efforts.first.bib_number, + 'split_id' => splits.first.id, + 'stopped_here' => 'false', + 'absolute_time' => '2018-02-10 09:21:54-7:00'}}] + } + + it 'imports data in the expected order' do + skip + end + end + context 'when provided with an adilas url and data_format adilas_bear_times' do let(:request_params) { {id: event.id, data_format: 'adilas_bear_times', data: source_data} } let(:source_data) { Net::HTTP.get(URI(url)) } diff --git a/spec/mailers/admin_mailer_spec.rb b/spec/mailers/admin_mailer_spec.rb new file mode 100644 index 000000000..bbce4a6bf --- /dev/null +++ b/spec/mailers/admin_mailer_spec.rb @@ -0,0 +1,32 @@ +require "rails_helper" + +RSpec.describe AdminMailer, type: :mailer do + include ActiveJob::TestHelper + let(:user){ create(:user) } + let(:event) { create(:event) } + + it 'creates a job' do + ActiveJob::Base.queue_adapter = :test + expect { AdminMailer.new_event(event, user).deliver_later }.to have_enqueued_job.on_queue('mailers') + end + + # Because we have config.action_mailer.delivery_method set to :test in our :test.rb, + # all 'sent' emails are gathered into the ActionMailer::Base.deliveries array. + + it 'sends an email' do + expect { + perform_enqueued_jobs do + AdminMailer.new_event(event, user).deliver_later + end + }.to change { ActionMailer::Base.deliveries.size }.by(1) + end + + it 'sends email to the correct admin' do + perform_enqueued_jobs do + AdminMailer.new_event(event, user).deliver_later + end + + mail = ActionMailer::Base.deliveries.last + expect(mail.to[0]).to eq ENV['ADMIN_EMAIL'] + end +end diff --git a/spec/mailers/previews/admin_mailer_preview.rb b/spec/mailers/previews/admin_mailer_preview.rb new file mode 100644 index 000000000..51e9acbb7 --- /dev/null +++ b/spec/mailers/previews/admin_mailer_preview.rb @@ -0,0 +1,8 @@ +# Preview all emails at http://localhost:3000/rails/mailers/admin_mailer +class AdminMailerPreview < ActionMailer::Preview + def new_event + event = Event.first + user = User.first + AdminMailer.new_event(event, user) + end +end