-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f5b801
commit 0e4d4f4
Showing
6 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module PushNotifications | ||
class Creator | ||
# Given an Event it will create as many PushNotification resources | ||
# necessary as the resource associated to the Event will require. | ||
# | ||
# @param [Hash] event: <Event> | ||
def initialize(event:) | ||
@event = event | ||
end | ||
|
||
def create! | ||
EventNotifier.new(event: event).users.each do | ||
PushNotification.create!(event: event) | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_accessor :event | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module PushNotifications | ||
class EventNotifier | ||
def initialize(event:) | ||
@event = event | ||
end | ||
|
||
# Who to notify | ||
# | ||
# @return [<User>] | ||
def users | ||
resource_logic.users | ||
end | ||
|
||
private | ||
|
||
attr_accessor :event | ||
|
||
def resource_logic | ||
return PostLogic.new(event: event) if event.post_id | ||
|
||
raise 'The resource associated to the Event is not supported' | ||
end | ||
end | ||
end |
26 changes: 26 additions & 0 deletions
26
app/services/push_notifications/event_notifier/post_logic.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module PushNotifications | ||
class EventNotifier | ||
class PostLogic | ||
def initialize(event:) | ||
@event = event | ||
@post = event.post | ||
end | ||
|
||
# Conditions for Post: | ||
# | ||
# We need to notify all the users that: | ||
# - are members of the Post's organization | ||
# - have a DeviceToken associated | ||
# | ||
# @return [<User>] | ||
def users | ||
organization = post.organization | ||
User.joins(:device_tokens).where(id: organization.user_ids) | ||
end | ||
|
||
private | ||
|
||
attr_accessor :event, :post | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe PushNotifications::Creator do | ||
let(:user) { Fabricate.build(:user) } | ||
let(:organization) { Fabricate(:organization) } | ||
let(:post) { Fabricate(:post, organization: organization, user: user) } | ||
let(:event) { Fabricate.build(:event, post: post, action: :created) } | ||
let(:creator) { described_class.new(event: event) } | ||
|
||
before do | ||
organization.members.create(user: user) | ||
Fabricate(:device_token, user: user, token: 'aloha') | ||
end | ||
|
||
describe '#create!' do | ||
it 'creates as many PushNotification resources as needed' do | ||
expect(PushNotification).to receive(:create!).once | ||
|
||
creator.create! | ||
end | ||
end | ||
end |
36 changes: 36 additions & 0 deletions
36
spec/services/push_notifications/event_notifier/post_logic_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe ::PushNotifications::EventNotifier::PostLogic do | ||
let(:user) { Fabricate.build(:user) } | ||
let(:organization) { Fabricate(:organization) } | ||
let(:post) { Fabricate(:post, organization: organization, user: user) } | ||
let(:event) { Fabricate.build(:event, post: post, action: :created) } | ||
|
||
describe '#users' do | ||
context 'when a user pertains to the Post\'s organization' do | ||
before { organization.members.create(user: user) } | ||
|
||
context 'and the user has a DeviceToken associated' do | ||
before { Fabricate(:device_token, user: user, token: 'aloha') } | ||
|
||
it 'returns the user' do | ||
expect(described_class.new(event: event).users).to include(user) | ||
end | ||
end | ||
|
||
context 'but the user has no DeviceToken associated' do | ||
it 'doesn\'t return the user' do | ||
expect(described_class.new(event: event).users).to_not include(user) | ||
end | ||
end | ||
end | ||
|
||
context 'when a user doesn\'t pertain to the Post\'s organization' do | ||
let(:other_user) { Fabricate.build(:user) } | ||
|
||
it 'doesn\'t return the user' do | ||
expect(described_class.new(event: event).users).to_not include(other_user) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe PushNotifications::EventNotifier do | ||
describe '#users' do | ||
let(:user) { Fabricate.build(:user) } | ||
let(:organization) { Fabricate(:organization) } | ||
|
||
context 'when the given Event is associated to a Post' do | ||
let(:post) { Fabricate(:post, organization: organization, user: user) } | ||
let(:event) { Fabricate.build(:event, post: post, action: :created) } | ||
|
||
it 'delegates #users to PostLogic' do | ||
logic = instance_double(::PushNotifications::EventNotifier::PostLogic) | ||
expect(::PushNotifications::EventNotifier::PostLogic).to receive(:new).and_return(logic) | ||
expect(logic).to receive(:users) | ||
|
||
described_class.new(event: event).users | ||
end | ||
end | ||
|
||
context 'when the given Event is associated to a resource not supported' do | ||
let(:event) { Fabricate.build(:event, action: :created) } | ||
|
||
it 'raises an error' do | ||
expect { | ||
described_class.new(event: event).users | ||
}.to raise_error('The resource associated to the Event is not supported') | ||
end | ||
end | ||
end | ||
end |