Add user_system_notification records when syncing notifications, update email sending

This commit is contained in:
Urban Rotnik 2019-02-14 14:16:49 +01:00
parent 29d77ca81d
commit f63eaaefe7
4 changed files with 41 additions and 13 deletions

View file

@ -4,17 +4,13 @@ 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 send_email
if user.system_message_email_notification
AppMailer
.system_notification(user, system_notification)
end
end
def self.mark_as_seen(notifications)
where(system_notification_id: notifications)
def self.mark_as_seen(notifications_id)
where(system_notification_id: notifications_id)
.update_all(seen_at: Time.now)
end
@ -49,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

@ -16,19 +16,30 @@ describe UserSystemNotification do
end
describe '.send_email' 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 'delivers new email' do
it 'delivers an email on creating new user_system_notification' do
allow(user_system_notification.user)
.to receive(:system_message_email_notification).and_return(true)
expect { user_system_notification.send_email&.deliver_now }
expect { user_system_notification.save }
.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
context 'when user has disabled notifications' do
it 'doesn\'t deliver new email' do
expect { user_system_notification.send_email&.deliver_now }
it 'doesn\'t deliver email on creating new user_system_notification' do
allow(user_system_notification.user)
.to receive(:system_message_email_notification).and_return(false)
expect { user_system_notification.save }
.not_to(change { ActionMailer::Base.deliveries.count })
end
end

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