2021-11-25 14:07:38 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-25 01:34:14 +02:00
|
|
|
class Trends::PreviewCardBatch
|
2021-11-25 14:07:38 +02:00
|
|
|
include ActiveModel::Model
|
|
|
|
include Authorization
|
|
|
|
|
|
|
|
attr_accessor :preview_card_ids, :action, :current_account, :precision
|
|
|
|
|
|
|
|
def save
|
|
|
|
case action
|
|
|
|
when 'approve'
|
|
|
|
approve!
|
2022-02-25 01:34:14 +02:00
|
|
|
when 'approve_providers'
|
|
|
|
approve_providers!
|
2021-11-25 14:07:38 +02:00
|
|
|
when 'reject'
|
|
|
|
reject!
|
2022-02-25 01:34:14 +02:00
|
|
|
when 'reject_providers'
|
|
|
|
reject_providers!
|
2021-11-25 14:07:38 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def preview_cards
|
|
|
|
@preview_cards ||= PreviewCard.where(id: preview_card_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def preview_card_providers
|
|
|
|
@preview_card_providers ||= preview_cards.map(&:domain).uniq.map { |domain| PreviewCardProvider.matching_domain(domain) || PreviewCardProvider.new(domain: domain) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def approve!
|
2022-02-25 01:34:14 +02:00
|
|
|
preview_cards.each { |preview_card| authorize(preview_card, :review?) }
|
2021-11-25 14:07:38 +02:00
|
|
|
preview_cards.update_all(trendable: true)
|
|
|
|
end
|
|
|
|
|
2022-02-25 01:34:14 +02:00
|
|
|
def approve_providers!
|
2021-11-25 14:07:38 +02:00
|
|
|
preview_card_providers.each do |provider|
|
2022-02-25 01:34:14 +02:00
|
|
|
authorize(provider, :review?)
|
2021-11-25 14:07:38 +02:00
|
|
|
provider.update(trendable: true, reviewed_at: action_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reset any individual overrides
|
|
|
|
preview_cards.update_all(trendable: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reject!
|
2022-02-25 01:34:14 +02:00
|
|
|
preview_cards.each { |preview_card| authorize(preview_card, :review?) }
|
2021-11-25 14:07:38 +02:00
|
|
|
preview_cards.update_all(trendable: false)
|
|
|
|
end
|
|
|
|
|
2022-02-25 01:34:14 +02:00
|
|
|
def reject_providers!
|
2021-11-25 14:07:38 +02:00
|
|
|
preview_card_providers.each do |provider|
|
2022-02-25 01:34:14 +02:00
|
|
|
authorize(provider, :review?)
|
2021-11-25 14:07:38 +02:00
|
|
|
provider.update(trendable: false, reviewed_at: action_time)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reset any individual overrides
|
|
|
|
preview_cards.update_all(trendable: nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def action_time
|
|
|
|
@action_time ||= Time.now.utc
|
|
|
|
end
|
|
|
|
end
|