diff --git a/app/controllers/admin/publishers_controller.rb b/app/controllers/admin/publishers_controller.rb index d6f2489cf0..ea09837421 100644 --- a/app/controllers/admin/publishers_controller.rb +++ b/app/controllers/admin/publishers_controller.rb @@ -62,7 +62,6 @@ def show def wallet_info @publisher = Publisher.find(params[:publisher_id]) - @referral_owner_status = PromoClient.owner_state.find(id: @publisher.id) render partial: "wallet_info" end diff --git a/app/controllers/publishers/promo_registrations_controller.rb b/app/controllers/publishers/promo_registrations_controller.rb index 2c1edfab47..888b6ba0c5 100644 --- a/app/controllers/publishers/promo_registrations_controller.rb +++ b/app/controllers/publishers/promo_registrations_controller.rb @@ -30,16 +30,6 @@ def index end end - def create - return unless Rails.env.development? || Rails.env.test? - @publisher = current_publisher - current_publisher.channels.find_each do |channel| - channel.register_channel_for_promo # Callee does a check - end - @promo_enabled_channels = @publisher.channels.joins(:promo_registration) - @publisher_has_verified_channel = @publisher.has_verified_channel? - end - def for_referral_code promo_registration = user.promo_registrations.find_by(referral_code: params[:referral_code]) diff --git a/app/jobs/promo/register_channel_for_promo_job.rb b/app/jobs/promo/register_channel_for_promo_job.rb deleted file mode 100644 index bd8335f05b..0000000000 --- a/app/jobs/promo/register_channel_for_promo_job.rb +++ /dev/null @@ -1,13 +0,0 @@ -# typed: ignore - -# Registers a single channel for a promo immediately after verification -class Promo::RegisterChannelForPromoJob < ApplicationJob - include PromosHelper - queue_as :default - - def perform(channel_id, attempt_count = 0) - channel = Channel.find(channel_id) - Promo::AssignPromoToChannelService.new(channel: channel, attempt_count: attempt_count).perform - Rails.logger.warn("Failed to register newly verified channel #{channel} with promo server") - end -end diff --git a/app/jobs/promo/update_status.rb b/app/jobs/promo/update_status.rb deleted file mode 100644 index 703de65c26..0000000000 --- a/app/jobs/promo/update_status.rb +++ /dev/null @@ -1,22 +0,0 @@ -# typed: true - -# Registers a single channel for a promo immediately after verification -module Promo - class UpdateStatus < ApplicationJob - queue_as :default - - def perform(id, status) - # Remove previous states, this prevents from any unique index constraints from being violated - states = PromoClient.owner_state.find(id: id) - states.each do |state| - PromoClient.owner_state.destroy(id: id, state: state) - end - - if status == PublisherStatusUpdate::SUSPENDED - PromoClient.owner_state.create(id: id, state: Promo::Models::OwnerState::State::SUSPEND) - elsif status == PublisherStatusUpdate::ONLY_USER_FUNDS - PromoClient.owner_state.create(id: id, state: Promo::Models::OwnerState::State::NO_UGP) - end - end - end -end diff --git a/app/models/channel.rb b/app/models/channel.rb index 7e145710dd..820496de2b 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -75,7 +75,6 @@ class Channel < ApplicationRecord # *ChannelDetails get autosaved from above. after_save :update_site_banner_lookup!, if: -> { saved_change_to_verified && verified? } - after_commit :register_channel_for_promo, if: :should_register_channel_for_promo? after_commit :create_channel_card, if: -> { saved_change_to_verified? && verified? } before_save :clear_verified_at_if_necessary @@ -334,10 +333,6 @@ def gemini_connection @gemini_connection ||= gemini_connection_for_channel.first end - def register_channel_for_promo - Promo::RegisterChannelForPromoJob.perform_now(id, 0) - end - def update_site_banner_lookup!(skip_site_banner_info_lookup: false) return unless verified? site_banner_lookup = SiteBannerLookup.find_or_initialize_by( @@ -379,14 +374,6 @@ def set_public_identifier! private - def should_register_channel_for_promo? - publisher.may_create_referrals? && - publisher.may_register_promo? && - saved_change_to_verified? && - verified && - !publisher.only_user_funds? - end - def clear_verified_at_if_necessary self.verified_at = nil if verified == false && verified_at.present? end diff --git a/app/models/concerns/referral_promo.rb b/app/models/concerns/referral_promo.rb index 22601b4630..b563fb074e 100644 --- a/app/models/concerns/referral_promo.rb +++ b/app/models/concerns/referral_promo.rb @@ -28,28 +28,4 @@ def promo_lockout_time_passed? return promo_lockout_time < DateTime.now if promo_lockout_time.present? false end - - # Public: Validates if the user's country is on the excluded list. - # - # Returns true or false depending on if the country is included. - def valid_promo_country? - PromoRegistration::RESTRICTED_COUNTRIES.exclude?(country) - end - - def may_register_promo? - # If the user doesn't have the referral_kyc_flag on then we can register them still. - return true unless referral_kyc_required? - - # Otherwise they must be brave payable and from a valid country - brave_payable? && valid_promo_country? - end - - # Public: Enqueues a job which allows publishers referrals to work if they are payable and in a valid promo_country - # - # Returns nil - def update_promo_status! - return unless may_register_promo? - - Promo::UpdateStatus.perform_later(id, PublisherStatusUpdate::ACTIVE) - end end diff --git a/app/models/publisher_status_update.rb b/app/models/publisher_status_update.rb index 49f2c131b8..3bd6dc7228 100644 --- a/app/models/publisher_status_update.rb +++ b/app/models/publisher_status_update.rb @@ -34,18 +34,8 @@ class PublisherStatusUpdate < ApplicationRecord validates :publisher_id, presence: true - # After a user creates a new status then we should check to see the previous staus and call backing server - after_create :update_services, if: :should_update? - after_create :make_previously_suspended_channels - # Queues a job to call the promo server to update the owner state for the publisher based on the status - # - # @return [nil] - def update_services - Promo::UpdateStatus.perform_later(publisher_id, status) - end - def should_update? valid_transitions = [ACTIVE, SUSPENDED, ONLY_USER_FUNDS] previous_status = publisher.status_updates.second&.status diff --git a/app/models/uphold_connection.rb b/app/models/uphold_connection.rb index 1468220879..af8e559902 100644 --- a/app/models/uphold_connection.rb +++ b/app/models/uphold_connection.rb @@ -73,7 +73,6 @@ class UpholdAccountState ################ after_save :update_site_banner_lookup!, if: -> { saved_change_to_attribute(:is_member) } - after_save :update_promo_status, if: -> { saved_change_to_attribute(:is_member) } after_commit :create_uphold_cards, on: :create ################# @@ -290,13 +289,6 @@ def update_site_banner_lookup! publisher.update_site_banner_lookup! end - # Internal: If the publisher previously had referral codes and then we will re-activate their referral codes. - # - # Returns nil - def update_promo_status - publisher.update_promo_status! - end - def japanese_account? country == JAPAN end diff --git a/app/services/promo/assign_promo_to_channel_service.rb b/app/services/promo/assign_promo_to_channel_service.rb deleted file mode 100644 index 712160a7d7..0000000000 --- a/app/services/promo/assign_promo_to_channel_service.rb +++ /dev/null @@ -1,140 +0,0 @@ -# typed: true - -# Registers a promo registration for each verified channel for a publisher -class Promo::AssignPromoToChannelService < BaseApiClient - include PromosHelper - - attr_reader :channel - - MAX_ATTEMPTS = 3 - - def initialize(channel:, promo_id: active_promo_id, attempt_count: 0) - @channel = channel - @promo_id = promo_id - @attempt_count = attempt_count - end - - def perform - return if !channel.verified? || channel.promo_registration.present? - result = register_channel(channel) - if result.nil? && @attempt_count < MAX_ATTEMPTS - Promo::RegisterChannelForPromoJob.set(wait: 30.minutes).perform_later(@channel.id, @attempt_count + 1) and return - elsif result.nil? && @attempt_count >= MAX_ATTEMPTS - return - end - - referral_code = result[:referral_code] - should_update_promo_server = result[:should_update_promo_server] - begin - if referral_code.present? - promo_registration = PromoRegistration.new(channel_id: channel.id, - promo_id: @promo_id, - kind: PromoRegistration::CHANNEL, - publisher_id: channel.publisher_id, - referral_code: referral_code) - - success = promo_registration.save! - if success - PromoMailer.new_channel_registered_2018q1(channel.publisher, channel).deliver_later - Promo::ChannelOwnerUpdater.new(publisher_id: channel.publisher_id, referral_code: referral_code).perform if should_update_promo_server - end - end - rescue ActiveRecord::RecordInvalid => e - Rails.logger.error("#{self.class.name} perform: #{referral_code} channel_id: #{channel.id} exception: #{e}") - LogException.perform("Promo::PublisherChannelsRegistrar #perform referral_code: #{referral_code}, error: #{e}") - end - rescue Faraday::ClientError - # When the owner is "no-ugp" the promo server will return 409. - nil - end - - def register_channel(channel) - return register_channel_offline if perform_promo_offline? - response = connection.put do |request| - request.headers["Authorization"] = api_authorization_header - request.headers["Content-Type"] = "application/json" - request.body = request_body(channel) - request.url("/api/1/promo/publishers") - end - - { - referral_code: JSON.parse(response.body)["referral_code"], - should_update_promo_server: false - } - rescue Faraday::ClientError => e - if e.response[:status] == 409 - change_ownership(channel) - end - rescue Faraday::Error => e - Rails.logger.error("Promo::PublisherChannelsRegistrar #register_channel error: #{e}") - LogException.perform("Promo::PublisherChannelsRegistrar #register_channel error: #{e}") - nil - end - - def change_ownership(channel) - Rails.logger.warn("Promo::PublisherChannelsRegistrar #register_channel returned 409, channel already registered. Using Promo::RegistrationGetter to get the referral_code.") - - # Get the referral code - registration = Promo::RegistrationGetter.new(publisher: channel.publisher, channel: channel).perform - - { - referral_code: registration["referral_code"], - should_update_promo_server: registration["owner_id"] != channel.publisher_id - } - rescue Faraday::ClientError - nil - end - - def register_channel_offline - Rails.logger.info("Promo::PublisherChannelsRegistrar #register_channel offline.") - { - referral_code: offline_referral_code, - should_update_promo_server: false - } - end - - private - - def api_base_uri - Rails.configuration.pub_secrets[:api_promo_base_uri] - end - - def api_authorization_header - "Bearer #{Rails.configuration.pub_secrets[:api_promo_key]}" - end - - def proxy_url - nil - end - - def request_body(channel) - case channel.details_type - when "SiteChannelDetails" - site_request_body(channel) - else - channel_request_body(channel) - end - end - - def channel_request_body(channel) - { - owner_id: channel.publisher_id, - promo: @promo_id, - channel: channel.channel_id, - title: channel.publication_title, - channel_type: channel.type_display.downcase, - thumbnail_url: channel.details.thumbnail_url, - description: nil - }.to_json - end - - def site_request_body(channel) - { - owner_id: channel.publisher_id, - promo: @promo_id, - channel: channel.channel_id, - title: channel.publication_title, - channel_type: "website" - }.to_json - end -end diff --git a/app/services/promo/client.rb b/app/services/promo/client.rb index a2fb03da87..835ec38751 100644 --- a/app/services/promo/client.rb +++ b/app/services/promo/client.rb @@ -6,10 +6,6 @@ def initialize(connection = nil, options = {}) @connection = connection end - def owner_state - @owner_state ||= Promo::Models::OwnerState.new(connection) - end - def reporting @reporting ||= Promo::Models::Reporting.new(connection) end diff --git a/app/views/admin/publishers/_wallet_info.html.slim b/app/views/admin/publishers/_wallet_info.html.slim index e8c50755af..1e93be4cd4 100644 --- a/app/views/admin/publishers/_wallet_info.html.slim +++ b/app/views/admin/publishers/_wallet_info.html.slim @@ -43,14 +43,6 @@ .db-info-row .db-field = "Referral confirmations:" .db-value = publisher_referral_totals(@publisher)[PromoRegistration::FINALIZED] - .db-info-row - .db-field - = "Referral owner status" - span data-tooltip="If the user is 'no-ugp' this means their promos will 404" - = fa_icon 'question-circle', class: 'mx-1' - = ": " - - .db-value= @referral_owner_status #statement-section.split-row .payout-report-status-section diff --git a/test/controllers/admin/publishers/publisher_status_updates_controller_test.rb b/test/controllers/admin/publishers/publisher_status_updates_controller_test.rb index 9020fe0cf2..abad78997d 100644 --- a/test/controllers/admin/publishers/publisher_status_updates_controller_test.rb +++ b/test/controllers/admin/publishers/publisher_status_updates_controller_test.rb @@ -52,7 +52,7 @@ class Admin::Publishers::PublisherStatusUpdatesControllerTest < ActionDispatch:: sign_in admin publisher = publishers(:uphold_connected) - assert_enqueued_jobs(2) do + assert_enqueued_jobs(1) do post( admin_publisher_publisher_status_updates_path( publisher_id: publisher.id, @@ -69,7 +69,7 @@ class Admin::Publishers::PublisherStatusUpdatesControllerTest < ActionDispatch:: admin = publishers(:admin) sign_in admin publisher = publishers(:uphold_connected) - assert_enqueued_jobs(1) do + assert_enqueued_jobs(0) do post( admin_publisher_publisher_status_updates_path( publisher_id: publisher.id, diff --git a/test/controllers/channels_controller_test.rb b/test/controllers/channels_controller_test.rb index cdf5260e8b..71b0af8861 100644 --- a/test/controllers/channels_controller_test.rb +++ b/test/controllers/channels_controller_test.rb @@ -46,24 +46,6 @@ class ChannelsControllerTest < ActionDispatch::IntegrationTest end end - test "delete removes a channel even if promo is enabled" do - publisher = publishers(:small_media_group) - channel = channels(:global_verified) - sign_in publisher - - post promo_registrations_path - - Promo::RegisterChannelForPromoJob.perform_now(publisher.channels.first.id) - assert_not_nil publisher.channels.first.promo_registration.referral_code - - assert_difference("publisher.channels.count", 0) do - assert_difference("SiteChannelDetails.count", 0) do - delete channel_path(channel), headers: {"HTTP_ACCEPT" => "application/json"} - assert_response 404 - end - end - end - test "cancel_add removes an unverified channel and redirects to the dashboard" do publisher = publishers(:default) channel = channels(:new_site) diff --git a/test/jobs/delete_publisher_channel_job_test.rb b/test/jobs/delete_publisher_channel_job_test.rb index ee5486da39..8f96f0eba8 100644 --- a/test/jobs/delete_publisher_channel_job_test.rb +++ b/test/jobs/delete_publisher_channel_job_test.rb @@ -26,19 +26,6 @@ class DeletePublisherChannelJobTest < ActionDispatch::IntegrationTest end end - test "does not throw error if channel has a promo_regsitration" do - publisher = publishers(:completed) - sign_in publisher - - # enable the promo - post promo_registrations_path - Promo::RegisterChannelForPromoJob.perform_now(publisher.channels.first.id) - assert_not_nil publisher.channels.first.promo_registration.referral_code - assert_nothing_raised do - DeletePublisherChannelJob.perform_now(publisher.channels.first.id) - end - end - test "deletes unverifed channel" do channel = channels(:default) DeletePublisherChannelJob.perform_now(channel.id) diff --git a/test/models/channel_test.rb b/test/models/channel_test.rb index 11a910aec4..36a2d1d1ef 100644 --- a/test/models/channel_test.rb +++ b/test/models/channel_test.rb @@ -162,62 +162,6 @@ class ChannelTest < ActionDispatch::IntegrationTest assert Channel.search("global%").map(&:id).include? channel.id end - test "Test should_register_channel_for_promo" do - channel = channels(:promo_enabled_site_not_verified) - assert_equal false, channel.send(:should_register_channel_for_promo?) - publisher = channel.publisher - publisher.feature_flags[UserFeatureFlags::REFERRAL_ENABLED_OVERRIDE] = true - publisher.save - assert_equal false, channel.send(:should_register_channel_for_promo?) - channel.verified = true - channel.expects(:register_channel_for_promo).once - channel.save - end - - # Maybe put this in a RegisterChannelForPromoJobTest? - test "verifying a channel calls register_channel_for_promo (site)" do - channel = channels(:default) - publisher = channel.publisher - publisher.save! - - # verify RegisterChannelForPromoJob is called - channel.verified = true - channel.save! - Promo::RegisterChannelForPromoJob.perform_now(channel.id) - - # verify it worked and the channel has a referral code - channel.reload - assert channel.promo_registration.referral_code - - # verify nothing happens if verified_changed? to false, or to true but not saved - assert_enqueued_jobs(0) do - channel.verified = false - channel.save! - channel.verified = true - end - end - - test "verifying a channel calls register_channel_for_promo (youtube)" do - # To 'verify' a new youtube channel, we delete a previously verified channel then instantiate it - channel_original = channels(:google_verified) - publisher = channel_original.publisher - - # grab the details first, then destroy the original - channel_details_copy = channel_original.details.dup - channel_original.destroy! - - publisher.save! - - # check that RegisterChannelForPromoJob is called when it is verified - # channel_copy.verified = true - channel_copy = Channel.new(details: channel_details_copy, verified: true, publisher: publisher) - channel_copy.save! - Promo::RegisterChannelForPromoJob.perform_now(channel_copy.id) - - channel_copy.reload - assert channel_copy.promo_registration.referral_code - end - test "verification_failed! updates verification status" do channel = channels(:default) diff --git a/test/services/promo/publisher_channels_registrar_test.rb b/test/services/promo/publisher_channels_registrar_test.rb deleted file mode 100644 index cbac0bc2bb..0000000000 --- a/test/services/promo/publisher_channels_registrar_test.rb +++ /dev/null @@ -1,101 +0,0 @@ -# typed: false - -require "test_helper" -require "webmock/minitest" - -class Promo::PublisherChannelsRegistrarTest < ActiveJob::TestCase - include PromosHelper - - before(:example) do - @prev_offline = Rails.configuration.pub_secrets[:api_promo_base_uri] - end - - after(:example) do - Rails.configuration.pub_secrets[:api_promo_base_uri] = @prev_offline - end - - test "registrar registers a verified channel" do - publisher = publishers(:completed) # has one verified channel - - assert_difference "PromoRegistration.count", 1 do - publisher.channels.find_each do |channel| - Promo::AssignPromoToChannelService.new(channel: channel).perform - end - end - end - - test "registrar does not register unverified channel" do - publisher = publishers(:fake1) # has one unverified channel - - assert_no_difference "PromoRegistration.count" do - publisher.channels.find_each do |channel| - Promo::AssignPromoToChannelService.new(channel: channel).perform - end - end - end - - test "registrar does not attempt make api call if channel registered" do - publisher = publishers(:completed) - channel = publisher.channels.first - - assigning_service = Promo::AssignPromoToChannelService.new(channel: channel) - - assigning_service.perform - channel.reload - - # verify no registrations created for good measure - assert_no_difference "PromoRegistration.count" do - assigning_service.perform - end - end - - test "registrar requests promo code for channel if encounters duplicate error" do - Rails.configuration.pub_secrets[:api_promo_base_uri] = "http://127.0.0.1:8194" - publisher = publishers(:completed) - - # Stub Promo response saying the ref code has been taken - stub_request(:put, "#{Rails.configuration.pub_secrets[:api_promo_base_uri]}/api/1/promo/publishers") - .to_return(status: 409) - - # Stub Promo response with the referral code and current code owner - stub_request(:get, "#{Rails.configuration.pub_secrets[:api_promo_base_uri]}/api/2/promo/referral_code/channel/completed.org") - .to_return(status: 200, body: {referral_code: "COM001", owner_id: "invalid"}.to_json) - - # Stub Promo response when we update the owner - stub_request(:put, "#{Rails.configuration.pub_secrets[:api_promo_base_uri]}/api/1/promo/publishers/COM001") - .to_return(status: 200, body: [].to_json) - - assert_difference "PromoRegistration.count", 1 do - publisher.channels.find_each do |channel| - Promo::AssignPromoToChannelService.new(channel: channel).perform - end - end - - assert_equal PromoRegistration.order("created_at").last.referral_code, "COM001" - end - - test "reenqueue getting a new referral code if promo server doesn't give back a referral code" do - Rails.configuration.pub_secrets[:api_promo_base_uri] = "http://127.0.0.1:8194" - publisher = publishers(:completed) - - # Stub Promo response with the referral code and current code owner - stub_request(:put, "#{Rails.configuration.pub_secrets[:api_promo_base_uri]}/api/1/promo/publishers") - .to_return(status: 500) - - assert_performed_jobs 0 - - assert_difference "PromoRegistration.count", 0 do - publisher.channels.find_each do |channel| - Promo::AssignPromoToChannelService.new(channel: channel).perform - assert_enqueued_with(job: Promo::RegisterChannelForPromoJob, args: [channel.id, 1]) - end - end - - perform_enqueued_jobs(only: Promo::RegisterChannelForPromoJob) do - publisher.channels.find_each do |channel| - Promo::AssignPromoToChannelService.new(channel: channel).perform - end - end - assert_performed_jobs Promo::AssignPromoToChannelService::MAX_ATTEMPTS - end -end