Skip to content

Commit

Permalink
feat: Adding default mailer endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
martintomas committed Oct 23, 2024
1 parent 877968f commit 6da7a8d
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
GCS_CREDENTIALS_FILE=
GCS_BUCKET=
MQ_BETA_ENABLED=true

# SMTP API
SMTP_API_KEY=
SMTP_API_SECRET=
SMTP_DEFAULT_FROM=
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ gem 'react-rails'
gem 'appsignal'
gem 'roo'
gem 'caxlsx'
gem 'mailjet'

group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ GEM
net-imap
net-pop
net-smtp
mailjet (1.8.0)
activesupport (>= 5.0.0)
faraday (~> 2.1)
rack (>= 1.4.0)
yajl-ruby
marcel (1.0.4)
matrix (0.4.2)
memoist (0.16.2)
Expand Down Expand Up @@ -547,6 +552,7 @@ GEM
rails (>= 3.2.16)
xpath (3.2.0)
nokogiri (~> 1.8)
yajl-ruby (1.4.3)
zeitwerk (2.6.18)

PLATFORMS
Expand Down Expand Up @@ -589,6 +595,7 @@ DEPENDENCIES
image_processing
language_list
listen (>= 3.0.5, < 3.2)
mailjet
pg (>= 0.18, < 2.0)
pg_search (= 2.3.2)
pry
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/tpi/sectors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def cp_performance_chart_data
render json: data.chart_json
end

def send_download_file_info_email
DataDownloadMailer.send_download_file_info_email(permitted_email_params).deliver_now
head :ok
end

def user_download_all
send_user_download_file(
Company.published.select(:id).where(sector_id: @sectors.pluck(:id)),
Expand Down Expand Up @@ -163,5 +168,9 @@ def companies_scope(params)
Company.published.active
end
end

def permitted_email_params
params.permit(:email, :job_title, :forename, :surname, :location, :organisation, purposes: [])
end
end
end
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: '[email protected]'
default from: ENV['SMTP_DEFAULT_FROM']
layout 'mailer'
end
6 changes: 6 additions & 0 deletions app/mailers/data_download_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class DataDownloadMailer < ApplicationMailer
def send_download_file_info_email(data)
@data = data
mail(to: '[email protected]', subject: 'TPI data has been downloaded')
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<h1>Following user downloaded data from TPI website</h1>

<p><strong>Email:</strong> <%= @data[:email] %></p>
<p><strong>Job Title:</strong> <%= @data[:job_title] %></p>
<p><strong>Forename:</strong> <%= @data[:forename] %></p>
<p><strong>Surname:</strong> <%= @data[:surname] %></p>
<p><strong>Location:</strong> <%= @data[:location] %></p>
<p><strong>Organisation:</strong> <%= @data[:organisation] %></p>

<h2>Purposes</h2>
<ul>
<% Array.wrap(@data[:purposes]).each do |purpose| %>
<li><%= purpose %></li>
<% end %>
</ul>
4 changes: 1 addition & 3 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,8 @@
# Store uploaded files on the local file system (see config/storage.yml for options)
config.active_storage.service = :local

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

config.action_mailer.perform_caching = false
config.action_mailer.perform_deliveries = false

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
Expand Down
8 changes: 3 additions & 5 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@
# config.active_job.queue_adapter = :resque
# config.active_job.queue_name_prefix = "laws_and_pathways_production"

config.action_mailer.perform_caching = false

# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :mailjet
config.action_mailer.perform_deliveries = false

# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
Expand Down
8 changes: 3 additions & 5 deletions config/environments/staging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,9 @@
# config.active_job.queue_adapter = :resque
# config.active_job.queue_name_prefix = "laws_and_pathways_#{Rails.env}"

config.action_mailer.perform_caching = false

# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.
# config.action_mailer.raise_delivery_errors = false
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :mailjet
config.action_mailer.perform_deliveries = false

# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation cannot be found).
Expand Down
6 changes: 6 additions & 0 deletions config/initializers/mailjet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# kindly generated by appropriated Rails generator
Mailjet.configure do |config|
config.api_key = ENV['SMTP_API_KEY']
config.secret_key = ENV['SMTP_API_SECRET']
config.default_from = ENV['SMTP_DEFAULT_FROM']
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
get :cp_performance_chart_data
get :user_download_all
get :user_download_methodology
post :send_download_file_info_email
end
member do
get :levels_chart_data
Expand Down
24 changes: 24 additions & 0 deletions spec/controllers/tpi/sectors_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,28 @@
end
end
end

describe 'POST send_download_file_info_email' do
subject { post :send_download_file_info_email, params: data }
let(:data) do
{
email: '[email protected]',
job_title: 'job_title',
forename: 'forename',
surname: 'surname',
location: 'location',
organisation: 'organisation',
purposes: %w[purposes1 purposes2]
}
end

it 'returns ok status' do
subject
expect(response).to have_http_status(:ok)
end

it 'sends email' do
expect { subject }.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
end
33 changes: 33 additions & 0 deletions spec/mailers/data_download_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'rails_helper'

RSpec.describe DataDownloadMailer, type: :mailer do
describe 'send_download_file_info_email' do
let(:data) {
{
email: '[email protected]',
job_title: 'job_title',
forename: 'forename',
surname: 'surname',
location: 'location',
organisation: 'organisation',
purposes: ['purpose1', 'purpose2']
}
}
let(:mail) { DataDownloadMailer.send_download_file_info_email data }

it 'renders the headers' do
expect(mail.subject).to eq('TPI data has been downloaded')
expect(mail.to).to eq(['[email protected]'])
expect(mail.from).to eq([ENV['SMTP_DEFAULT_FROM']].reject(&:blank?))

expect(mail.body.encoded).to include('[email protected]')
expect(mail.body.encoded).to include('job_title')
expect(mail.body.encoded).to include('forename')
expect(mail.body.encoded).to include('surname')
expect(mail.body.encoded).to include('location')
expect(mail.body.encoded).to include('organisation')
expect(mail.body.encoded).to include('purpose1')
expect(mail.body.encoded).to include('purpose2')
end
end
end

0 comments on commit 6da7a8d

Please sign in to comment.