Skip to content

Commit

Permalink
Add PushNotification creation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
enricostano committed May 28, 2018
1 parent 2f5b801 commit 0e4d4f4
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/services/push_notifications/creator.rb
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
24 changes: 24 additions & 0 deletions app/services/push_notifications/event_notifier.rb
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 app/services/push_notifications/event_notifier/post_logic.rb
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
22 changes: 22 additions & 0 deletions spec/services/push_notifications/creator_spec.rb
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 spec/services/push_notifications/event_notifier/post_logic_spec.rb
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
31 changes: 31 additions & 0 deletions spec/services/push_notifications/event_notifier_spec.rb
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

0 comments on commit 0e4d4f4

Please # to comment.