Skip to content

Commit

Permalink
Create Uphold Report (#2219)
Browse files Browse the repository at this point in the history
* Create Uphold Report

* UpholdReport -> UpholdStatusReport

* Tests pass

* Apply suggestions from code review

Co-Authored-By: Albert Wang <amwang217@gmail.com>

* Apply suggestions from code review

Co-Authored-By: Albert Wang <amwang217@gmail.com>

* Apply suggestions from code review

Co-Authored-By: Albert Wang <amwang217@gmail.com>

* Rename relevant files from uphold_reports to uphold_status_reports (#2240)

* KYC report route fix (#2241)

* Fix route to use uphold_status_report
  • Loading branch information
Cory McDonald authored Sep 19, 2019
1 parent a95106b commit f3627df
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 5 deletions.
3 changes: 1 addition & 2 deletions app/assets/stylesheets/admin/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
grid-gap: 32px;
align-items: center;
}

// Bootstrap overrides. Could be refined
.btn-default {
color: $braveGray-2 !important;
Expand Down Expand Up @@ -58,7 +58,6 @@
bottom: 0;
background: $braveGray-2;
margin: 60px 0 0 0;
padding: 24px 0;
overflow: auto;

.sidebar-menu {
Expand Down
30 changes: 30 additions & 0 deletions app/controllers/admin/uphold_status_reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'csv'

module Admin
class UpholdStatusReportsController < AdminController
def index
@uphold_status_reports = UpholdStatusReport.
group('(EXTRACT(YEAR FROM created_at))::integer').
group('(EXTRACT(MONTH FROM created_at))::integer').
order('2 DESC, 3 DESC').count
end

def show
date = DateTime.strptime(params[:id], "%Y-%m")
start_date = date.at_beginning_of_month
end_date = date.at_end_of_month

uphold_status_reports = UpholdStatusReport.where("created_at >= :start AND created_at <= :finish", start: start_date, finish: end_date)

generated = []
generated << ["publisher id", "publisher created at", "uphold id", "uphold connected time"].to_csv

uphold_status_reports.each do |report|
generated << [report.publisher_id, report.publisher.created_at, report.uphold_id, report.created_at].to_csv
end

send_data generated.join(''), filename: "uphold-#{params[:id]}.csv"
end
end
end

15 changes: 15 additions & 0 deletions app/controllers/publishers/uphold_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def create

ExchangeUpholdCodeForAccessTokenJob.perform_now(publisher_id: current_publisher.id)

connection.reload
create_uphold_report!(connection)

redirect_to(home_publishers_path)
rescue UpholdError, Faraday::Error => e
Rails.logger.info("Uphold Error: #{e.message}")
Expand All @@ -95,6 +98,18 @@ def destroy

class UpholdError < StandardError; end

def create_uphold_report!(connection)
uphold_id = connection.uphold_details&.id
return if uphold_id.blank?
# Return if we've already created a report for this id
return if UpholdStatusReport.find_by(uphold_id: uphold_id).present?

UpholdStatusReport.create(
publisher: current_publisher,
uphold_id: uphold_id
)
end

def validate_uphold!(connection)
# Ensure the uphold_state_token has been set. If not send back to try again
raise UpholdError.new, t('.missing_state') if connection&.uphold_state_token.blank? && !connection.uphold_verified?
Expand Down
5 changes: 3 additions & 2 deletions app/models/uphold_connection.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class UpholdConnection < ActiveRecord::Base
has_paper_trail only: [:is_member, :uphold_id, :address, :status, :default_currency]
has_paper_trail only: [:is_member, :member_at, :uphold_id, :address, :status, :default_currency]

UPHOLD_CODE_TIMEOUT = 5.minutes
UPHOLD_ACCESS_PARAMS_TIMEOUT = 2.hours
Expand Down Expand Up @@ -100,7 +100,7 @@ def uphold_client
def uphold_details
@user ||= uphold_client.user.find(self)
rescue Faraday::ClientError => e
if e.response[:status] == 401
if e.response&.dig(:status) == 401
Rails.logger.info("#{e.response[:body]} for uphold connection #{id}")
update(uphold_access_parameters: nil)
nil
Expand Down Expand Up @@ -169,6 +169,7 @@ def sync_from_uphold!

update(
is_member: uphold_information.memberAt.present?,
member_at: uphold_information.memberAt,
status: uphold_information.status,
uphold_id: uphold_information.id,
country: uphold_information.country
Expand Down
3 changes: 3 additions & 0 deletions app/models/uphold_status_report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class UpholdStatusReport < ApplicationRecord
belongs_to :publisher
end
8 changes: 7 additions & 1 deletion app/views/admin/publishers/_uphold.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ h3.text-dark.d-flex.align-items-center
td User has completed KYC
td
span class=(@publisher.uphold_connection.is_member? ? 'text-success' : 'text-danger')
= @publisher.uphold_connection.is_member? ? fa_icon("check", text: "Yes") : fa_icon("times", text: "No")
- if @publisher.uphold_connection.is_member?
= fa_icon("check", text: "Yes")
span.text-muted
span.mx-2= " – "
= @publisher.uphold_connection.member_at.strftime("%B %d, %Y %k:%M %Z")
- else
= fa_icon("times", text: "No")
tr
td Uphold ID
td= link_to_if @publisher.uphold_connection.uphold_id, @publisher.uphold_connection.uphold_id, admin_publishers_path(q: @publisher.uphold_connection.uphold_id)
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/shared/_sidebar.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ aside
= nav_link "FAQs", admin_faq_categories_path
= nav_link "Payout Reports", admin_payout_reports_path
= nav_link "Referral Promo", admin_unattached_promo_registrations_path
= nav_link "Uphold Reports", admin_uphold_status_reports_path
22 changes: 22 additions & 0 deletions app/views/admin/uphold_status_reports/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.panel-heading
h4 Uphold Reports

div
.font-weight-bold How this works:
p Anytime a publisher connects their uphold account we create an entry in our database that logs this. If an uphold id has already been reported we do not create an entry. This data is then aggregated and grouped by month and displayed here.

table.table
thead
tr
td Period
td Number of users
td
tbody
- @uphold_status_reports.each do |report|
tr
td
- date = report.as_json.first
= Date::MONTHNAMES[date.second]
= " #{date.first}"
td= report.as_json.second
td= link_to("Download", admin_uphold_status_report_path(date.join('-')), class:'btn btn-default')
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@
end
resources :promo_campaigns, only: %i(create)
root to: "dashboard#index" # <--- Root route

resources :uphold_status_reports, only: [:index, :show]
end

resources :errors, only: [], path: "/" do
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20190910163930_create_uphold_status_report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateUpholdStatusReport < ActiveRecord::Migration[5.2]
def change
add_column :uphold_connections, :member_at, :datetime

create_table :uphold_status_reports, id: :uuid, default: -> { "uuid_generate_v4()"}, force: :cascade do |t|
t.belongs_to :publisher, index: true, type: :uuid
t.uuid :uphold_id, index: true
t.timestamps
end

add_index :uphold_status_reports, :created_at
end
end
11 changes: 11 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -529,9 +529,20 @@
t.string "country"
t.string "default_currency"
t.datetime "default_currency_confirmed_at"
t.datetime "member_at"
t.index ["publisher_id"], name: "index_uphold_connections_on_publisher_id", unique: true
end

create_table "uphold_status_reports", id: :uuid, default: -> { "uuid_generate_v4()" }, force: :cascade do |t|
t.uuid "publisher_id"
t.uuid "uphold_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["created_at"], name: "index_uphold_status_reports_on_created_at"
t.index ["publisher_id"], name: "index_uphold_status_reports_on_publisher_id"
t.index ["uphold_id"], name: "index_uphold_status_reports_on_uphold_id"
end

create_table "user_authentication_tokens", id: :uuid, default: -> { "uuid_generate_v4()" }, force: :cascade do |t|
t.string "encrypted_authentication_token"
t.string "encrypted_authentication_token_iv"
Expand Down

0 comments on commit f3627df

Please sign in to comment.