Skip to content

Commit

Permalink
Merge pull request #2116 from brave-intl/staging
Browse files Browse the repository at this point in the history
Release 2019-08-07
  • Loading branch information
yachtcaptain23 authored Aug 7, 2019
2 parents 57294fc + 15c0e54 commit 235f4c6
Show file tree
Hide file tree
Showing 44 changed files with 571 additions and 188 deletions.
11 changes: 11 additions & 0 deletions app/assets/stylesheets/admin/pages/publisher.scss
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,14 @@
}
}
}

.not-verified {
color: $braveGray-6;

img {
filter: grayscale(100%);
}
a {
color: $braveGray-6;
}
}
20 changes: 12 additions & 8 deletions app/assets/stylesheets/pages/home.scss
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,6 @@
}

.channel--promo-info-container {
position: absolute;
right: 24px;

@media only screen and (max-width: 520px) {
margin: 8px 0;
display: flex;
Expand Down Expand Up @@ -700,6 +697,11 @@
.channel-summary {
width: 100%;

// Apply a max width when the screen gets smaller to trim long channel names
@media only screen and (max-width: 768px) {
max-width: 10rem;
}

h3 {
font-size: 20px;
}
Expand All @@ -718,6 +720,13 @@
}
}

.channel-secondary-information {
@media only screen and (max-width: 560px) {
width: 100%;
margin: 0.5em 0;
}
}

&.channel-incomplete,
&.channel-started,
&.channel-failed {
Expand Down Expand Up @@ -870,7 +879,6 @@
}

&--intro {
margin-bottom: 8px;
display: flex;
align-items: center;
}
Expand Down Expand Up @@ -901,7 +909,6 @@
.bat-channel {
&--amount {
font-size: 28px;
font-weight: bolder;
display: inline;
line-height: 1;
}
Expand Down Expand Up @@ -1058,9 +1065,6 @@
overflow-y: auto;
max-height: calc(100vh - 150px);
}
&--icon {
margin: 0;
}
}

.new-tag {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/publishers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def refresh_uphold
connection = UpholdConnection.find_by(publisher: params[:publisher_id])
if connection.present?
connection.sync_from_uphold!
CreateUpholdCardsJob.perform_now(uphold_connection_id: connection.id)
connection.create_uphold_cards
end
redirect_to admin_publisher_path(@publisher.id)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/publishers/uphold_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def confirm_default_currency
uphold_connection.update(confirm_default_currency_params.merge(default_currency_confirmed_at: Time.now))

if uphold_connection.can_create_uphold_cards?
uphold_connection.create_uphold_card_for_default_currency
uphold_connection.create_uphold_cards

# TODO do we need this refresh?
render(json: {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/publishers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def home
uphold_connection.sync_from_uphold!

# Handles legacy case where user is missing an Uphold card
uphold_connection.create_uphold_card_for_default_currency if uphold_connection.missing_card?
uphold_connection.create_uphold_cards if uphold_connection.missing_card?
end
end

Expand Down
7 changes: 4 additions & 3 deletions app/helpers/admin_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,13 @@ def publisher_link(publisher)
end

def payout_report_status_header(account_type)
report_date = PayoutReport.most_recent_final_report.created_at.strftime("%b %d")
report_date = PayoutReport.most_recent_final_report.created_at
report_date = report_date.strftime("%B #{report_date.day.ordinalize}")

if account_type == 'owner'
"#{report_date}'s Payout Report Status (Referrals)"
"#{report_date} Payout (Referrals)"
else account_type == 'channel'
"#{report_date}'s Payout Report Status (Contributions)"
"#{report_date} Payout Contributions"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const Container = styled.div`
display: none;
}
background-color: white;
box-shadow: rgba(99, 105, 110, 0.18) 0px 1px 12px 0px;
padding: 28px;
margin-top: -53px;
`;
Expand Down
11 changes: 10 additions & 1 deletion app/jobs/create_uphold_cards_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ def perform(uphold_connection_id:)
return
end

card = nil

# Search for an existing card
card = uphold_connection.uphold_client.card.where(uphold_connection: uphold_connection)&.first
cards = uphold_connection.uphold_client.card.where(uphold_connection: uphold_connection)
existing_private_cards = UpholdConnectionForChannel.select(:card_id).where(uphold_connection: uphold_connection).to_a

cards.each do |c|
# We don't want to accidentally set the Publisher's card used for auto contribute and referral deposits into a channel card
# So we should check the address for the card and make sure that we haven't already used it
card = c and break if existing_private_cards.exclude?(c.id)
end

# If the card doesn't exist so we should create it
if card.blank?
Expand Down
70 changes: 70 additions & 0 deletions app/jobs/create_uphold_channel_card_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class CreateUpholdChannelCardJob < ApplicationJob
queue_as :default

def perform(uphold_connection_id:, channel_id:)
uphold_connection = UpholdConnection.find(uphold_connection_id)
channel = Channel.find(channel_id)
return unless uphold_connection&.is_member? && channel&.verified?

unless uphold_connection.can_create_uphold_cards?
Rails.logger.info("Could not create uphold card for channel #{uphold_connection.publisher_id}. Uphold Verified: #{uphold_connection.uphold_verified}")
return
end

upfc = UpholdConnectionForChannel.find_by(
uphold_connection: uphold_connection,
currency: uphold_connection.default_currency,
channel_identifier: channel.details.channel_identifier
)

if upfc.present?
card_id = upfc.card_id
else
(card_id, upfc) = create_card(uphold_connection, channel)
end

# If the channel was deleted and then recreated we should update this to be the new channel id
upfc.update(
address: get_address(uphold_connection, card_id),
channel_id: channel.id
)
end

def create_card(uphold_connection, channel)
card_label = "#{channel.type_display} - #{channel.details.publication_title} - Brave Rewards"

# If the card doesn't exist so we should create it
card_id = uphold_connection.uphold_client.card.create(
uphold_connection: uphold_connection,
currency: uphold_connection.default_currency,
label: card_label
).id

upfc = UpholdConnectionForChannel.create(
uphold_connection: uphold_connection,
channel: channel,
card_id: card_id,
currency: uphold_connection.default_currency,
channel_identifier: channel.details.channel_identifier
)

[card_id, upfc]
end

def get_address(uphold_connection, card_id)
addresses = uphold_connection.uphold_client.address.all(
uphold_connection: uphold_connection,
id: card_id
)
address = addresses.detect { |a| a.type == UpholdConnectionForChannel::NETWORK }
address = address.formats.first.dig('value') if address.present?

return address if address.present?

uphold_connection.uphold_client.address.create(
uphold_connection: uphold_connection,
id: card_id,
network: UpholdConnectionForChannel::NETWORK
)
end
end
2 changes: 1 addition & 1 deletion app/jobs/migrate_uphold_access_parameters_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def perform(publisher_id:, parameters:, default_currency:)
connection.reload

# Sync the uphold card or create it if the card is missing
CreateUpholdCardsJob.perform_later(uphold_connection_id: connection.id)
connection.create_uphold_cards
else
Rails.logger.info("Couldn't find publisher #{publisher_id} in creator's database but exists on mongo owner's database (Probably not a big deal)")
end
Expand Down
11 changes: 10 additions & 1 deletion app/models/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Channel < ApplicationRecord
}, foreign_key: 'details_id'

has_one :promo_registration, dependent: :destroy
has_many :uphold_connection_for_channel

has_one :contesting_channel, class_name: "Channel", foreign_key: 'contested_by_channel_id'

Expand Down Expand Up @@ -76,7 +77,7 @@ class Channel < ApplicationRecord
validate :verified_duplicate_channels_must_be_contested, if: -> { verified? }

after_save :register_channel_for_promo, if: :should_register_channel_for_promo
after_save :notify_slack, if: :saved_change_to_verified?
after_save :create_channel_card, :notify_slack, if: -> { :saved_change_to_verified? && verified? }

before_save :clear_verified_at_if_necessary

Expand Down Expand Up @@ -360,6 +361,10 @@ def most_recent_potential_payment
PayoutReport.most_recent_final_report&.potential_payments&.where(channel_id: id)&.first
end

def uphold_connection
@uphold_connection ||= UpholdConnectionForChannel.joins(:uphold_connection).where(channel_id: id).where("uphold_connections.default_currency = uphold_connection_for_channels.currency").first
end

private

def should_register_channel_for_promo
Expand All @@ -376,6 +381,10 @@ def register_channel_for_promo
Promo::RegisterChannelForPromoJob.new.perform(channel: self)
end

def create_channel_card
CreateUpholdChannelCardJob.perform_later(uphold_connection_id: publisher.uphold_connection&.id, channel_id: id)
end

def notify_slack
return unless verified?
emoji =
Expand Down
4 changes: 2 additions & 2 deletions app/models/json_builders/channels_json_builder_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def include_verified_channel(verified_channel)
# Skip if channel publisher has not yet KYC'd
return unless verified_channel.publisher&.uphold_connection&.is_member

wallet_address_id = verified_channel.publisher&.uphold_connection&.address
wallet_address_id = verified_channel.uphold_connection&.address

return if wallet_address_id.nil?
return if existing_wallet_address?(wallet_address_id)

@channels.push([
verified_channel.details.channel_identifier,
Expand Down
11 changes: 9 additions & 2 deletions app/models/uphold_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class UpholdAccountState

belongs_to :publisher

has_many :uphold_connection_for_channels

# uphold_code is an intermediate step to acquiring uphold_access_parameters
# and should be cleared once it has been used to get uphold_access_parameters
validates :uphold_code, absence: true, if: -> { uphold_access_parameters.present? || uphold_verified? }
Expand All @@ -38,7 +40,7 @@ class UpholdAccountState
}

# If the user became KYC'd let's create the uphold card for them
after_save :create_uphold_card_for_default_currency, if: -> { saved_change_to_is_member? && is_member? }
after_save :create_uphold_cards, if: -> { saved_change_to_is_member? && uphold_verified? }

# publishers that have access params that havent accepted by eyeshade
# can be cleared after 2 hours
Expand Down Expand Up @@ -141,9 +143,14 @@ def wallet
@wallet ||= publisher&.wallet
end

def create_uphold_card_for_default_currency
def create_uphold_cards
return unless can_create_uphold_cards?

CreateUpholdCardsJob.perform_now(uphold_connection_id: id)

publisher.channels.each do |channel|
CreateUpholdChannelCardJob.perform_now(uphold_connection_id: id, channel_id: channel.id)
end
end

def missing_card?
Expand Down
14 changes: 14 additions & 0 deletions app/models/uphold_connection_for_channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

# Creates a connection between uphold and a channel
#
# We use this to look up existing cards. If a user deletes a channel then we should not destroy this.
# Really the only time we should delete is if the uphold connection gets deleted, which should only happen if the publisher
class UpholdConnectionForChannel < ApplicationRecord
belongs_to :uphold_connection
belongs_to :channel

NETWORK = 'anonymous'

validates :channel_identifier, uniqueness: { scope: [:uphold_connection_id, :channel_identifier, :currency] }
end
2 changes: 1 addition & 1 deletion app/services/payout_report_publisher_includer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def perform

uphold_connection.sync_from_uphold!
if uphold_connection.missing_card?
uphold_connection.create_uphold_card_for_default_currency
uphold_connection.create_uphold_cards
end

probi = wallet.referral_balance.amount_probi # probi = balance
Expand Down
4 changes: 4 additions & 0 deletions app/services/uphold/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ def initialize(params = {})
@connection = connection
end

def address
@address ||= Uphold::Models::Address.new
end

def card
@card ||= Uphold::Models::Card.new
end
Expand Down
Loading

0 comments on commit 235f4c6

Please sign in to comment.