Skip to content

Commit

Permalink
Data to csv (#1900)
Browse files Browse the repository at this point in the history
Co-authored-by: Aashish Passrija <[email protected]>
Co-authored-by: Aashish Passrija <[email protected]>
Co-authored-by: Julia Nguyen <[email protected]>
  • Loading branch information
4 people authored Oct 23, 2020
1 parent 2acc7a3 commit 1eb6d8b
Show file tree
Hide file tree
Showing 33 changed files with 710 additions and 5 deletions.
7 changes: 6 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ defaults: &defaults
POSTGRES_USER: circleci
POSTGRES_DB: ifme_test
POSTGRES_PASSWORD: ""
- image: redis
environment:
REDIS_HOST: redis
# The default Redis port
REDIS_PORT: 6379
steps:
- checkout
jobs:
Expand Down Expand Up @@ -202,4 +207,4 @@ workflows:
branches:
ignore: /.*/
tags:
only: /^v[0-9]+\.[0-9]+\.[0-9]+/
only: /^v[0-9]+\.[0-9]+\.[0-9]+/
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ gem 'selenium-webdriver', '~> 3.142.3'

gem 'rubyzip', '~> 1.3.0'

gem 'sidekiq', '5.0.5'
gem 'sidekiq-middleware'
gem 'sidekiq-failures'
gem "sidekiq-cron", "~> 1.1"

group :development, :test do
gem 'bundler-audit'
gem 'dotenv-rails', '~> 2.7.2'
Expand Down
16 changes: 16 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,18 @@ GEM
faraday (>= 0.7.6, < 1.0)
shoulda-matchers (4.4.1)
activesupport (>= 4.2.0)
sidekiq (5.0.5)
concurrent-ruby (~> 1.0)
connection_pool (~> 2.2, >= 2.2.0)
rack-protection (>= 1.5.0)
redis (>= 3.3.4, < 5)
sidekiq-cron (1.2.0)
fugit (~> 1.1)
sidekiq (>= 4.2.1)
sidekiq-failures (1.0.0)
sidekiq (>= 4.0.0)
sidekiq-middleware (0.3.0)
sidekiq (>= 2.12.4)
signet (0.14.0)
addressable (~> 2.3)
faraday (>= 0.17.3, < 2.0)
Expand Down Expand Up @@ -583,6 +595,10 @@ DEPENDENCIES
selenium-webdriver (~> 3.142.3)
sentry-raven
shoulda-matchers
sidekiq (= 5.0.5)
sidekiq-cron (~> 1.1)
sidekiq-failures
sidekiq-middleware
simplecov (= 0.16.1)
spring
turbolinks (~> 5.2.0)
Expand Down
28 changes: 28 additions & 0 deletions app/controllers/users/reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true
module Users
class ReportsController < ApplicationController
before_action :authenticate_user!
include Users::ReportsHelper

def submit_request
status, response = submit_request_helper(current_user)
render json: response, status: status
end

def fetch_request_status
status, response = fetch_request_status_helper(current_user,
params[:request_id])
render json: response, status: status
end

def download_data
status, response = download_data_helper(current_user,
params[:request_id])
if status != 200
render json: response, status: status
else
send_file(response, status: 200)
end
end
end
end
35 changes: 35 additions & 0 deletions app/helpers/users/reports_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true
module Users
module ReportsHelper
def submit_request_helper(user)
[200, { request_id: user.generate_data_request }]
rescue StandardError => e
[422, { error: e.message }]
end

def fetch_request_status_helper(user, request_id)
return 400, { error: "Request id can't be blank." } if request_id.blank?

data_request = user.data_requests.find_by(request_id: request_id)
if data_request.blank?
return 404, { error: 'No such request exists for current user.' }
end

[200, { current_status: data_request.status_id }]
end

def download_data_helper(user, request_id)
return 400, { error: "Request id can't be blank." } if request_id.blank?

data_request = user.data_requests.find_by(
request_id: request_id,
status_id: Users::DataRequest::STATUS[:success]
)
if data_request.blank? || !File.exist?(data_request.file_path.to_s)
return 404, { error: 'Requested csv not found.' }
end

[200, data_request.file_path]
end
end
end
8 changes: 8 additions & 0 deletions app/models/allyship.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#

class Allyship < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
created_at
updated_at
ally_id
status
].map!(&:freeze).freeze

enum status: { accepted: 0, pending_from_user: 1, pending_from_ally: 2 }

validate :different_users
Expand Down
17 changes: 17 additions & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true

def self.build_csv_rows(objects)
return [] if objects.blank?

klass = objects.klass
data = [["#{klass.name.underscore}_info"]]
attributes = if klass.const_defined?('USER_DATA_ATTRIBUTES')
klass.const_get('USER_DATA_ATTRIBUTES')
else
klass.column_names
end
data << attributes
objects.each do |object|
data << attributes.map { |attribute| object.send(attribute.to_sym) }
end
data
end
end
8 changes: 8 additions & 0 deletions app/models/care_plan_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#

class CarePlanContact < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
name
phone
created_at
updated_at
].map!(&:freeze).freeze

validates :user_id, :name, presence: true
belongs_to :user
end
10 changes: 10 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,14 @@ class Category < ApplicationRecord
has_many :moments_categories, dependent: :destroy
has_many :strategies_categories, dependent: :destroy
validates :visible, inclusion: [true, false]

USER_DATA_ATTRIBUTES = %w[
id
name
description
created_at
updated_at
slug
visible
].map!(&:freeze).freeze
end
9 changes: 9 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class Group < ApplicationRecord
through: :group_members, source: :user
after_destroy :destroy_notifications

USER_DATA_ATTRIBUTES = %w[
id
name
created_at
updated_at
description
slug
].map!(&:freeze).freeze

def led_by?(user)
leaders.include? user
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/group_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#

class GroupMember < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
group_id
leader
created_at
updated_at
].map!(&:freeze).freeze

after_destroy :destroy_meeting_memberships

validates :group_id, :user_id, presence: true
Expand Down
25 changes: 23 additions & 2 deletions app/models/medication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,35 @@
# comments :text
# slug :string
# add_to_google_cal :boolean default(FALSE)
# weekly_dosage :integer
# default(["0", "1", "2", "3", "4", "5", "6"]), is an Array
# rubocop:disable Layout/LineLength
# weekly_dosage :integer default(["0", "1", "2", "3", "4", "5", "6"]), is an Array
# rubocop:enable Layout/LineLength
#

class Medication < ApplicationRecord
# dosage: amount of medication taken at one time
# total: total quantity of medication
# strength: strength of medication

USER_DATA_ATTRIBUTES = %w[
id
name
dosage
refill
created_at
updated_at
user_id
total
strength
strength_unit
dosage_unit
total_unit
comments
slug
add_to_google_cal
weekly_dosage
].map!(&:freeze).freeze

extend FriendlyId
friendly_id :name
belongs_to :user, foreign_key: :user_id
Expand Down
9 changes: 9 additions & 0 deletions app/models/meeting_member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
#

class MeetingMember < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
meeting_id
leader
created_at
updated_at
google_cal_event_id
].map!(&:freeze).freeze

validates :meeting_id, :user_id, presence: true
validates :leader, inclusion: [true, false]

Expand Down
17 changes: 17 additions & 0 deletions app/models/moment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ class Moment < ApplicationRecord
include CommonMethods
extend FriendlyId

USER_DATA_ATTRIBUTES = %w[
id
name
why
fix
created_at
updated_at
viewers
comment
slug
secret_share_identifier
secret_share_expires_at
published_at
bookmarked
resource_recommendations
].map!(&:freeze).freeze

friendly_id :name
serialize :viewers, Array

Expand Down
10 changes: 10 additions & 0 deletions app/models/mood.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
#

class Mood < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
name
description
created_at
updated_at
slug
visible
].map!(&:freeze).freeze

extend FriendlyId
friendly_id :name
validates :user_id, :name, presence: true
Expand Down
8 changes: 8 additions & 0 deletions app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
#

class Notification < ApplicationRecord
USER_DATA_ATTRIBUTES = %w[
id
uniqueid
data
created_at
updated_at
].map!(&:freeze).freeze

validates :user_id, :uniqueid, :data, presence: true
belongs_to :user, foreign_key: :user_id

Expand Down
14 changes: 14 additions & 0 deletions app/models/strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ class Strategy < ApplicationRecord
include CommonMethods
extend FriendlyId

USER_DATA_ATTRIBUTES = %w[
id
description
viewers
comment
created_at
updated_at
name
slug
published_at
visible
bookmarked
].map!(&:freeze).freeze

friendly_id :name
serialize :viewers, Array

Expand Down
Loading

0 comments on commit 1eb6d8b

Please sign in to comment.