Skip to content

Commit

Permalink
include responsible account is slack messages. datacite/datacite#918
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Apr 25, 2020
1 parent a68fcb3 commit 90bfbff
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 43 deletions.
3 changes: 2 additions & 1 deletion app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def create
authorize! :create, @client

if @client.save
@client.send_welcome_email(responsible_id: current_user.uid) unless Rails.env.test?
options = {}
options[:is_collection] = false
options[:params] = { current_ability: current_ability }
Expand Down Expand Up @@ -143,7 +144,7 @@ def destroy
Rails.logger.warn message
render json: { errors: [{ status: status.to_s, title: message }] }.to_json, status: status
elsif @client.update(is_active: nil, deleted_at: Time.zone.now)
@client.send_delete_email unless Rails.env.test?
@client.send_delete_email(responsible_id: current_user.uid) unless Rails.env.test?
head :no_content
else
Rails.logger.error @client.errors.inspect
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/providers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def create
authorize! :create, @provider

if @provider.save
@provider.send_welcome_email(responsible_id: current_user.uid) unless Rails.env.test?
options = {}
options[:include] = @include
options[:is_collection] = false
Expand Down Expand Up @@ -206,7 +207,7 @@ def destroy
Rails.logger.warn message
render json: { errors: [{ status: status.to_s, title: message }] }.to_json, status: status
elsif @provider.update_attributes(is_active: nil, deleted_at: Time.zone.now)
@provider.send_delete_email unless Rails.env.test?
@provider.send_delete_email(responsible_id: current_user.uid) unless Rails.env.test?
head :no_content
else
Rails.logger.error @provider.errors.inspect
Expand Down
2 changes: 0 additions & 2 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ class Client < ActiveRecord::Base
before_create { self.created = Time.zone.now.utc.iso8601 }
before_save { self.updated = Time.zone.now.utc.iso8601 }

after_create :send_welcome_email, unless: Proc.new { Rails.env.test? }

attr_accessor :target_id

# use different index for testing
Expand Down
12 changes: 8 additions & 4 deletions app/models/concerns/mailable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Mailable
require 'slack-notifier'

included do
def send_welcome_email
def send_welcome_email(responsible_id: nil)
if self.class.name == "Provider"
client_id = nil
provider_id = symbol.downcase
Expand All @@ -29,6 +29,7 @@ def send_welcome_email
title = Rails.env.stage? ? "DataCite Fabrica Test" : "DataCite Fabrica"
subject = "#{title}: New Account"
account_type = self.class.name == "Provider" ? member_type.humanize : client_type.humanize
responsible_id ||= "ADMIN"
text = User.format_message_text(template: "users/welcome.text.erb", title: title, contact_name: name, name: symbol, url: url, reset_url: reset_url)
html = User.format_message_html(template: "users/welcome.html.erb", title: title, contact_name: name, name: symbol, url: url, reset_url: reset_url)

Expand All @@ -38,17 +39,19 @@ def send_welcome_email
{ title: "Account ID", value: symbol, short: true },
{ title: "Account type", value: account_type, short: true },
{ title: "Account name", value: name, short: true },
{ title: "System email", value: system_email, short: true }
{ title: "System email", value: system_email, short: true },
{ title: "Responsible Account ID", value: responsible_id }
]
User.send_notification_to_slack(nil, title: subject, level: "good", fields: fields)

response
end

def send_delete_email
def send_delete_email(responsible_id: nil)
title = Rails.env.stage? ? "DataCite Fabrica Test" : "DataCite Fabrica"
subject = "#{title}: Account Deleted"
account_type = self.class.name == "Provider" ? member_type.humanize : client_type.humanize
responsible_id ||= "ADMIN"
text = User.format_message_text(template: "users/delete.text.erb", title: title, contact_name: name, name: symbol)
html = User.format_message_html(template: "users/delete.html.erb", title: title, contact_name: name, name: symbol)

Expand All @@ -58,7 +61,8 @@ def send_delete_email
{ title: "Account ID", value: symbol, short: true },
{ title: "Account type", value: account_type, short: true },
{ title: "Account name", value: name, short: true },
{ title: "System email", value: system_email, short: true }
{ title: "System email", value: system_email, short: true },
{ title: "Responsible Account ID", value: responsible_id }
]
User.send_notification_to_slack(nil, title: subject, level: "warning", fields: fields)

Expand Down
2 changes: 0 additions & 2 deletions app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ class Provider < ActiveRecord::Base
before_create { self.created = Time.zone.now.utc.iso8601 }
before_save { self.updated = Time.zone.now.utc.iso8601 }

after_create :send_welcome_email, unless: Proc.new { Rails.env.test? }

accepts_nested_attributes_for :prefixes

# use different index for testing
Expand Down
6 changes: 4 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@ def self.reset(username)
reset_url = ENV['BRACCO_URL'] + "/reset"
title = Rails.env.stage? ? "DataCite Fabrica Test" : "DataCite Fabrica"
subject = "#{title}: Password Reset Request"
account_type = user.class.name == "Provider" ? user.member_type.humanize : user.client_type.humanize
text = User.format_message_text(template: "users/reset.text.erb", title: title, contact_name: user.name, name: user.symbol, url: url, reset_url: reset_url)
html = User.format_message_html(template: "users/reset.html.erb", title: title, contact_name: user.name, name: user.symbol, url: url, reset_url: reset_url)
response = self.send_message(name: user.name, email: user.system_email, subject: subject, text: text, html: html)

fields = [
{ title: "Account ID", value: uid.upcase},
{ title: "Name", value: user.name, short: true },
{ title: "Account ID", value: uid.upcase, short: true },
{ title: "Account type", value: account_type, short: true },
{ title: "Account name", value: user.name, short: true },
{ title: "System email", value: user.system_email, short: true }
]
slack_title = subject + (response[:status] == 200 ? " Sent" : " Failed")
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/client.rake
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace :client do
end

if client.update_attributes(is_active: nil, deleted_at: Time.zone.now)
client.send_delete_email unless Rails.env.test?
client.send_delete_email(responsible_id: "admin") unless Rails.env.test?
puts "Client with client ID #{ENV['CLIENT_ID']} deleted."
else
puts client.errors.inspect
Expand Down
8 changes: 7 additions & 1 deletion spec/concerns/mailable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
let(:title) { "DataCite Fabrica" }

it "send_welcome_email" do
response = client.send_welcome_email
response = client.send_welcome_email(responsible_id: provider.symbol)
expect(response[:status]).to eq(200)
expect(response[:message]).to eq("Queued. Thank you.")
end

it "send_delete_email" do
response = client.send_delete_email(responsible_id: provider.symbol)
expect(response[:status]).to eq(200)
expect(response[:message]).to eq("Queued. Thank you.")
end
Expand Down
50 changes: 50 additions & 0 deletions spec/fixtures/vcr_cassettes/Mailable/send_delete_email.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions spec/fixtures/vcr_cassettes/Mailable/send_welcome_email.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 90bfbff

Please sign in to comment.