Merge branch 'ur-SCI-2961-SN-emails' into features/system-notifications

This commit is contained in:
Urban Rotnik 2019-02-19 12:52:00 +01:00
commit a31181e3d3
12 changed files with 94 additions and 4 deletions

View file

@ -59,7 +59,7 @@ gem 'delayed_paperclip',
git: 'https://github.com/jrgifford/delayed_paperclip.git',
ref: 'fcf574c'
gem 'faker' # Generate fake data
gem 'httparty'
gem 'httparty', '~> 0.13.1'
gem 'i18n-js', '~> 3.0' # Localization in javascript files
gem 'jbuilder' # JSON structures via a Builder-style DSL
gem 'logging', '~> 2.0.0'

View file

@ -247,7 +247,8 @@ GEM
hammerjs-rails (2.0.8)
hashdiff (0.3.8)
hashie (3.5.7)
httparty (0.16.2)
httparty (0.13.7)
json (~> 1.8)
multi_xml (>= 0.5.2)
i18n (0.9.5)
concurrent-ruby (~> 1.0)
@ -600,7 +601,7 @@ DEPENDENCIES
faker
figaro
hammerjs-rails
httparty
httparty (~> 0.13.1)
i18n-js (~> 3.0)
jbuilder
jquery-rails

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class AppMailer < Devise::Mailer
helper :application, :mailer, :input_sanitize
include Devise::Controllers::UrlHelpers
@ -20,4 +22,16 @@ class AppMailer < Devise::Mailer
}.merge(opts)
mail(headers)
end
def system_notification(user, system_notification, opts = {})
@user = user
@system_notification = system_notification
headers = {
to: @user.email,
subject: t('system_notifications.emails.subject')
}.merge(opts)
mail(headers)
end
end

View file

@ -4,6 +4,9 @@ class UserSystemNotification < ApplicationRecord
belongs_to :user
belongs_to :system_notification
after_create :send_email,
if: proc { |sn| sn.user.system_message_email_notification }
scope :unseen, -> { where(seen_at: nil) }
def self.mark_as_seen(notifications_id)
@ -42,4 +45,10 @@ class UserSystemNotification < ApplicationRecord
notification
end
end
private
def send_email
AppMailer.delay.system_notification(user, system_notification)
end
end

View file

@ -81,6 +81,7 @@ module Notifications
.where(source_id: attrs[:source_id]).first_or_initialize(attrs)
if n.new_record?
n.users = User.all
n.save!
elsif n.last_time_changed_at < attrs[:last_time_changed_at]
n.update_attributes!(attrs)

View file

@ -0,0 +1,10 @@
<p>
<%= I18n.t("system_notifications.emails.intro_paragraph", user_name: @user.name) %>
</p>
<p>
<%= link_to @system_notification.title.html_safe, system_notifications_url %>
</p>
<p>
<%= @system_notification.description.html_safe %>
</p>

View file

@ -57,6 +57,8 @@ Rails.application.configure do
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
config.action_mailer.preview_path = "#{Rails.root}/test/mailers/previews"
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load

View file

@ -56,7 +56,7 @@ Rails.application.configure do
config.action_mailer.default_url_options = { host: Rails.application.secrets.mail_server_url }
config.action_mailer.default_options = { from: Rails.application.secrets.mail_from }
config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
# config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: Rails.application.secrets.mailer_address,

View file

@ -1292,6 +1292,9 @@ en:
create:
success_flash: "Successfully added sample group <strong>%{sample_group}</strong> to team <strong>%{team}</strong>."
system_notifications:
emails:
subject: "You've received a What's new notification"
intro_paragraph: "Hello %{user_name}, you received new What's new notification!"
index:
whats_new: "What's New"
more_notifications: "More system notifications"

View file

@ -15,6 +15,36 @@ describe UserSystemNotification do
it { is_expected.to belong_to(:system_notification) }
end
describe '.create' do
before do
Delayed::Worker.delay_jobs = false
end
after do
Delayed::Worker.delay_jobs = true
end
context 'when user has enabled notifications' do
it 'calls send an email on creation' do
allow(user_system_notification.user)
.to receive(:system_message_email_notification).and_return(true)
expect(user_system_notification).to receive(:send_email)
user_system_notification.save
end
end
context 'when user has disabled notifications' do
it 'doesn\'t call send an email on createion' do
allow(user_system_notification.user)
.to receive(:system_message_email_notification).and_return(false)
expect(user_system_notification).not_to receive(:send_email)
user_system_notification.save
end
end
end
describe 'Methods' do
let(:notifcation_one) { create :system_notification }
let(:notifcation_two) { create :system_notification }

View file

@ -17,6 +17,14 @@ describe Notifications::SyncSystemNotificationsService do
{ notifications: notifications }
end
before(:all) do
Timecop.freeze
end
after(:all) do
Timecop.return
end
context 'when request is successful' do
before do |test|
if test.metadata[:add_notifications_before]
@ -66,6 +74,12 @@ describe Notifications::SyncSystemNotificationsService do
expect(service_call.errors).to have_key(:last_sync_timestamp)
end
it 'adds 20 user_system_notifications records' do
create :user # add another user, so have 2 users in DB
expect { service_call }.to change { UserSystemNotification.count }.by(20)
end
end
context 'when request is unsuccessful' do

View file

@ -82,6 +82,12 @@ class AppMailerPreview < ActionMailer::Preview
)
end
def system_notification
sn = FactoryBot.build(:system_notification)
user = FactoryBot.build(:user)
AppMailer.system_notification(user, sn)
end
private
def fake_user