Skip to content

Process invalid push notifications #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/models/push_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ class PushNotification < ActiveRecord::Base
belongs_to :event, foreign_key: 'event_id'
belongs_to :device_token, foreign_key: 'device_token_id'

validates :event, :device_token, :title, presence: true
validates :event, :device_token, presence: true
validates :title, :body, presence: true, allow_blank: false

delegate :token, to: :device_token
end
17 changes: 17 additions & 0 deletions db/migrate/20180924164456_process_invalid_push_notifications.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ProcessInvalidPushNotifications < ActiveRecord::Migration
def up
PushNotification.where(processed_at: nil).find_in_batches do |batch|
batch.each do |push_notification|
unless push_notification.valid?
now = Time.now.utc
push_notification.update_columns(processed_at: now, updated_at: now)
puts "Updating invalid PushNotification ##{push_notification.id}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Rails logger?

end
end
end
end

def down
puts 'no.'
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180831161349) do
ActiveRecord::Schema.define(version: 20180924164456) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down
4 changes: 3 additions & 1 deletion spec/jobs/send_push_notifications_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
:push_notification,
event: event_created,
device_token: device_token,
title: 'A new Post hase been created.'
title: 'A new Post hase been created.',
body: 'A push notification body.'
)
end
let(:processed_push_notification) do
Expand All @@ -21,6 +22,7 @@
event: event_updated,
device_token: device_token,
title: 'A new Post hase been created.',
body: 'A push notification body.',
processed_at: Time.zone.now
)
end
Expand Down
33 changes: 30 additions & 3 deletions spec/models/push_notification_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
require 'spec_helper'

RSpec.describe PushNotification do
let!(:event) { Fabricate.build(:event) }
let!(:device_token) { Fabricate.build(:device_token, token: 'token') }
let!(:push_notification) { described_class.new(device_token: device_token) }

describe 'Validations' do
it { is_expected.to validate_presence_of(:event) }
it { is_expected.to validate_presence_of(:device_token) }
it { is_expected.to validate_presence_of(:title) }
it { is_expected.to validate_presence_of(:body) }
end

describe 'Not blank validations' do
let(:valid_push_notification) {
PushNotification.new(
event: event,
device_token: device_token,
title: "A title",
body: "A body"
)
}

it 'validates non blank fields' do
expect(valid_push_notification).to be_valid

valid_push_notification.tap do |with_blank_title|
with_blank_title.title = ''
expect(with_blank_title).to_not be_valid
end

valid_push_notification.tap do |with_blank_body|
with_blank_body.body = ''
expect(with_blank_body).to_not be_valid
end
end
end

describe 'Associations' do
Expand All @@ -16,9 +46,6 @@
end

describe "#token" do
let(:device_token) { Fabricate.build(:device_token, token: 'token') }
let(:push_notification) { described_class.new(device_token: device_token) }

it 'returns the associated DeviceToken\'s token' do
expect(push_notification.token).to eq('token')
end
Expand Down