docbrown/app/queries/consumer_apps/rpush_app_query.rb
Fernando Valverde e45b6c5ab2
Adds validations to avoid creating unnecessary PNs (#14621)
* Adds validations to avoid creating unnecessary PNs

* Fix conditional + add tests

* Trigger CI

* Trigger CI

* Add missing rpush mock in spec

* Mock before testing d'oh

* Extend ability to mock rpush in helpers

* Remove unnecessary user3 from spec
2021-08-31 15:00:30 -06:00

58 lines
1.5 KiB
Ruby

module ConsumerApps
class RpushAppQuery
def self.call(app_bundle:, platform:)
new(app_bundle: app_bundle, platform: platform).call
end
def initialize(app_bundle:, platform:)
@app_bundle = app_bundle
@platform = platform
@consumer_app = ConsumerApps::FindOrCreateByQuery.call(
app_bundle: @app_bundle,
platform: @platform,
)
end
def call
if consumer_app.android?
android_app = Rpush::Gcm::App.where(name: app_bundle).first
android_app || recreate_android_app!
elsif consumer_app.ios?
ios_app = Rpush::Apns2::App.where(name: app_bundle).first
ios_app || recreate_ios_app!
end
end
private
attr_reader :app_bundle, :consumer_app
def recreate_ios_app!
# If the ConsumerApp doesn't have credentials there's no need to create it
return if consumer_app.auth_credentials.blank?
app = Rpush::Apns2::App.new
app.name = app_bundle
app.certificate = consumer_app.auth_credentials.to_s.gsub("\\n", "\n")
app.environment = Rails.env
app.password = ""
app.bundle_id = app_bundle
app.connections = 1
app.save!
app
end
def recreate_android_app!
# If the ConsumerApp doesn't have credentials there's no need to create it
return if consumer_app.auth_credentials.blank?
app = Rpush::Gcm::App.new
app.name = app_bundle
app.auth_key = consumer_app.auth_credentials.to_s
app.connections = 1
app.save!
app
end
end
end