Commit cf6d4dc10c8d6488153c73a3586d2d3477d735fc
1 parent
448152ab
Exists in
master
and in
4 other branches
NotificationService for resolving email notification logic
Showing
2 changed files
with
98 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,51 @@ | @@ -0,0 +1,51 @@ | ||
1 | +# NotificationService class | ||
2 | +# | ||
3 | +# Used for notifing users with emails about different events | ||
4 | +# | ||
5 | +# Ex. | ||
6 | +# NotificationService.new.new_issue(issue, current_user) | ||
7 | +# | ||
8 | +class NotificationService | ||
9 | + # Always notify user about ssh key added | ||
10 | + # only if ssh key is not deploy key | ||
11 | + def new_key(key) | ||
12 | + if key.user | ||
13 | + Notify.delay.new_ssh_key_email(key.id) | ||
14 | + end | ||
15 | + end | ||
16 | + | ||
17 | + # TODO: When we close an issue we should send next emails: | ||
18 | + # | ||
19 | + # * issue author if his notification level is not Disabled | ||
20 | + # * issue assignee if his notification level is not Disabled | ||
21 | + # * project team members with notification level higher then Participating | ||
22 | + # | ||
23 | + def close_issue(issue, current_user) | ||
24 | + [issue.author, issue.assignee].compact.uniq.each do |recipient| | ||
25 | + Notify.delay.issue_status_changed_email(recipient.id, issue.id, issue.state, current_user.id) | ||
26 | + end | ||
27 | + end | ||
28 | + | ||
29 | + # When we reassign an issue we should send next emails: | ||
30 | + # | ||
31 | + # * issue author if his notification level is not Disabled | ||
32 | + # * issue assignee if his notification level is not Disabled | ||
33 | + # | ||
34 | + def reassigned_issue(issue, current_user) | ||
35 | + recipient_ids = [issue.assignee_id, issue.assignee_id_was].keep_if {|id| id && id != current_user.id } | ||
36 | + | ||
37 | + recipient_ids.each do |recipient_id| | ||
38 | + Notify.delay.reassigned_issue_email(recipient_id, issue.id, issue.assignee_id_was) | ||
39 | + end | ||
40 | + end | ||
41 | + | ||
42 | + # When we reassign an issue we should send next emails: | ||
43 | + # | ||
44 | + # * issue assignee if his notification level is not Disabled | ||
45 | + # | ||
46 | + def new_issue(issue, current_user) | ||
47 | + if issue.assignee && issue.assignee != current_user | ||
48 | + Notify.delay.new_issue_email(issue.id) | ||
49 | + end | ||
50 | + end | ||
51 | +end |
@@ -0,0 +1,47 @@ | @@ -0,0 +1,47 @@ | ||
1 | +require 'spec_helper' | ||
2 | + | ||
3 | +describe NotificationService do | ||
4 | + # Disable observers to prevent factory trigger notification service | ||
5 | + before { ActiveRecord::Base.observers.disable :all } | ||
6 | + | ||
7 | + let(:notification) { NotificationService.new } | ||
8 | + | ||
9 | + describe 'Keys' do | ||
10 | + describe :new_key do | ||
11 | + let(:key) { create(:personal_key) } | ||
12 | + | ||
13 | + it { notification.new_key(key).should be_true } | ||
14 | + | ||
15 | + it 'should sent email to key owner' do | ||
16 | + Notify.should_receive(:new_ssh_key_email).with(key.id) | ||
17 | + notification.new_key(key) | ||
18 | + end | ||
19 | + end | ||
20 | + end | ||
21 | + | ||
22 | + describe 'Issues' do | ||
23 | + let(:issue) { create :issue, assignee: create(:user) } | ||
24 | + | ||
25 | + describe :new_issue do | ||
26 | + it 'should sent email to issue assignee' do | ||
27 | + Notify.should_receive(:new_issue_email).with(issue.id) | ||
28 | + notification.new_issue(issue, nil) | ||
29 | + end | ||
30 | + end | ||
31 | + | ||
32 | + describe :reassigned_issue do | ||
33 | + it 'should sent email to issue old assignee and new issue assignee' do | ||
34 | + Notify.should_receive(:reassigned_issue_email).twice | ||
35 | + notification.reassigned_issue(issue, issue.author) | ||
36 | + end | ||
37 | + end | ||
38 | + | ||
39 | + describe :close_issue do | ||
40 | + it 'should sent email to issue assignee and issue author' do | ||
41 | + Notify.should_receive(:issue_status_changed_email).twice | ||
42 | + notification.close_issue(issue, issue.author) | ||
43 | + end | ||
44 | + end | ||
45 | + end | ||
46 | +end | ||
47 | + |