Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit 32a8662

Browse files
committed
Improved data import from other services
Addresses #3 Added people associated with orgs. Import jobs now create new orgs and people as needed.
1 parent 185a82a commit 32a8662

File tree

10 files changed

+133
-73
lines changed

10 files changed

+133
-73
lines changed

app/jobs/design_hops_job.rb

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,48 @@
1-
class DesignHopsJob < ActiveJob::Base
1+
# frozen_string_literal: true
2+
3+
class DesignHopsJob < ApplicationJob
24
queue_as :default
35

4-
def perform()
5-
@hopAttendees = Airrecord.table(ENV['AIRTABLE_API_KEY'], "appchfb5dUtfrlVSZ", "Attendee sign-up data")
6+
def perform
7+
@hop_attendees = Airrecord.table(ENV['AIRTABLE_API_KEY'], 'appchfb5dUtfrlVSZ', 'Attendee sign-up data')
68

7-
@hopAttendees.all.each do |record|
8-
domain = record['Email'].split("@").last.strip
9+
@hop_attendees.all.each do |record|
10+
domain = record['Email'].split('@').last.strip
911
org_name = record['Organisation name'].strip
1012

11-
@org = Organisation.find_by({name: org_name}) || Organisation.find_by({domain: domain]})
12-
if @org
13-
Action.create(
14-
potential_action: PotentialAction.find(1),
15-
organisation: @org,
16-
details: {
17-
person_name: record['First name'] + " " + record['Last name'],
18-
hop: record['Which design hop are you applying to join?'].first
19-
},
20-
start_time: record.created_at,
21-
end_time: record.created_at
13+
# Create new org
14+
@org = Organisation.find_by({ name: org_name }) || Organisation.find_by({ domain: domain })
15+
if @org.nil?
16+
@org = Organisation.create!(
17+
name: org_name,
18+
domain: domain
2219
)
20+
@org.get_charity_number
2321
end
22+
23+
# Create new org
24+
@person = Person.find_by({ email: record['Email'] })
25+
if @person.nil?
26+
@person = Person.create!(
27+
first_name: record['First name'],
28+
last_name: record['Last name'],
29+
email: record['Email'],
30+
organisation: @org
31+
)
32+
end
33+
34+
# Record action
35+
Action.create!(
36+
potential_action: PotentialAction.find(1),
37+
organisation_id: @org.id,
38+
person_id: @person.id,
39+
details: {
40+
person_name: record['First name'] + ' ' + record['Last name'],
41+
hop: record['Which design hop are you applying to join?'].first
42+
},
43+
start_time: record.created_at,
44+
end_time: record.created_at
45+
)
2446
end
2547
end
26-
end
48+
end

app/jobs/digital_candle_job.rb

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,56 @@
1-
class DigitalCandleJob < ActiveJob::Base
1+
# frozen_string_literal: true
2+
3+
class DigitalCandleJob < ApplicationJob
24
queue_as :default
35

4-
def perform()
5-
6+
def perform
67
@rows = GoogleSheetsImport.new.extract(ENV['DIGITAL_CANDLE_CONFIG'])
7-
8-
@hopAttendees = Airrecord.table(ENV['AIRTABLE_API_KEY'], "appchfb5dUtfrlVSZ", "Attendee sign-up data")
98

109
@rows.each do |row|
11-
domain = row[:email].split("@").last.strip
10+
domain = row[:email].split('@').last.strip
11+
first_name = row[:name].match('^(\w+) ')[1].strip
12+
last_name = row[:name].match(' (.*)$')[1].strip
13+
14+
# Find org
15+
@org = Organisation.find_by({ domain: domain })
1216

13-
@org = Organisation.find_by({domain: domain})
14-
if @org
15-
Action.create(
16-
potential_action: PotentialAction.find(3),
17-
organisation: @org,
17+
unless @org.nil?
18+
# Create new person
19+
@person = Person.find_by({ email: row[:email] })
20+
if @person.nil?
21+
@person = Person.create!(
22+
first_name: first_name,
23+
last_name: last_name,
24+
email: row[:email],
25+
organisation: @org
26+
)
27+
end
28+
29+
# Record actions
30+
Action.create!(
31+
potential_action: PotentialAction.find(3),
32+
organisation_id: @org.id,
33+
person_id: @person.id,
1834
details: {
1935
person_name: row[:name]
20-
},
21-
start_time: row[:timestamp],
36+
},
37+
start_time: row[:timestamp],
2238
end_time: row[:timestamp]
2339
)
24-
25-
Action.create(
26-
potential_action: PotentialAction.find(4),
27-
organisation: @org,
40+
41+
Action.create!(
42+
potential_action: PotentialAction.find(4),
43+
organisation_id: @org.id,
44+
person_id: @person.id,
2845
details: {
2946
person_name: row[:name],
3047
intro: row[:intro],
3148
intro_date: row[:intro_date]
32-
},
33-
start_time: row[:intro_date],
49+
},
50+
start_time: row[:intro_date],
3451
end_time: row[:intro_date]
3552
)
3653
end
3754
end
3855
end
39-
end
56+
end

app/jobs/service_recipes_job.rb

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1-
class ServiceRecipesJob < ActiveJob::Base
1+
# frozen_string_literal: true
2+
3+
class ServiceRecipesJob < ApplicationJob
24
queue_as :default
3-
4-
def perform()
5-
@hopAttendees = Airrecord.table(ENV['AIRTABLE_API_KEY'], "appL7gqKhmfsrP0Ex", "Recipes to go online")
65

7-
@hopAttendees.all(filter: "{Recipe progress} = 'Step 10: Published'").each do |record|
6+
def perform
7+
@service_recipes = Airrecord.table(ENV['AIRTABLE_API_KEY'], 'appL7gqKhmfsrP0Ex', 'Recipes to go online')
8+
9+
@service_recipes.all(filter: "{Recipe progress} = 'Step 10: Published'").each do |record|
810
org_name = record['Charity / org'].strip
9-
10-
@org = Organisation.find_by({name: org_name})
11-
if @org
12-
Action.create(
13-
potential_action: PotentialAction.find(2),
14-
organisation: @org,
15-
start_time: record.created_at,
16-
end_time: record.created_at,
17-
details: {
18-
recipe_name: record['Name of the thing']
19-
}
11+
12+
# Create new org
13+
@org = Organisation.find_by({ name: org_name })
14+
if @org.nil?
15+
@org = Organisation.create(
16+
name: org_name
2017
)
18+
@org.get_charity_number
2119
end
20+
21+
# Record action
22+
Action.create(
23+
potential_action: PotentialAction.find(2),
24+
organisation_id: @org.id,
25+
person_id: nil,
26+
start_time: record.created_at,
27+
end_time: record.created_at,
28+
details: {
29+
recipe_name: record['Name of the thing']
30+
}
31+
)
2232
end
2333
end
24-
end
34+
end

app/models/action.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1+
# frozen_string_literal: true
2+
13
class Action < ApplicationRecord
24
belongs_to :potential_action
35
belongs_to :organisation
4-
5-
def getString()
6-
7-
baseString = self.potential_action.format
8-
9-
unless self.details.nil?
10-
self.details.each do |key, val|
11-
baseString = baseString.sub('{{'+key+'}}', val)
12-
end
6+
7+
def get_string
8+
base_string = potential_action.format
9+
10+
details&.each do |key, val|
11+
base_string = base_string.sub('{{' + key + '}}', val)
1312
end
14-
15-
return baseString
13+
14+
base_string
1615
end
17-
1816
end

app/models/person.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Person < ApplicationRecord
2+
belongs_to :organisation
3+
end

app/models/platform.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
# frozen_string_literal: true
2+
13
class Platform < ApplicationRecord
24
end

app/models/potential_action.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
# frozen_string_literal: true
2+
13
class PotentialAction < ApplicationRecord
24
end

app/services/google_sheets_import.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ def extract(config, row_start: 2)
4747

4848
worksheet = @session.spreadsheet_by_key(config.file_id)
4949
.worksheet_by_sheet_id(config.sheet_id)
50-
50+
5151
ret = []
52-
52+
5353
(row_start..worksheet.num_rows).each do |row|
5454
unless worksheet[row, 1].empty?
5555
ret_row = {}
5656
config.fields.to_h.each { |k, v| ret_row[k] = worksheet[row, v] }
5757
ret << ret_row
5858
end
5959
end
60-
61-
return ret
60+
61+
ret
6262
end
6363
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class MakeOrganisationOptional < ActiveRecord::Migration[6.0]
2+
def change
3+
change_column :actions, :person_id, :bigint, null: true
4+
change_column :actions, :organisation_id, :bigint, null: true
5+
end
6+
end

db/schema.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_06_10_140241) do
13+
ActiveRecord::Schema.define(version: 2020_06_10_154932) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
1717

1818
create_table "actions", force: :cascade do |t|
1919
t.bigint "potential_action_id", null: false
20-
t.bigint "organisation_id", null: false
20+
t.bigint "organisation_id"
2121
t.jsonb "details"
2222
t.datetime "start_time"
2323
t.datetime "end_time"
@@ -83,7 +83,7 @@
8383
t.string "first_name"
8484
t.string "last_name"
8585
t.string "email"
86-
t.bigint "organisation_id", null: false
86+
t.bigint "organisation_id"
8787
t.datetime "created_at", precision: 6, null: false
8888
t.datetime "updated_at", precision: 6, null: false
8989
t.index ["organisation_id"], name: "index_people_on_organisation_id"

0 commit comments

Comments
 (0)