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

Commit 2ef400c

Browse files
committed
Resolve #8 Replace Ticket with Action
1 parent 32a8662 commit 2ef400c

35 files changed

+794
-475
lines changed

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
6060

6161
gem 'airrecord', '~> 1.0.5'
6262
gem 'devise', '~> 4.7.1'
63+
gem 'faraday', '~> 1.0.1'
6364
gem 'google_drive', '~> 3.0.5'
65+
gem 'ransack', '~> 2.3.2'
6466
gem 'strong_password', '~> 0.0.8'
65-
gem 'faraday'

Gemfile.lock

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ GEM
162162
parser (2.7.1.3)
163163
ast (~> 2.4.0)
164164
pg (1.2.3)
165+
polyamorous (2.3.2)
166+
activerecord (>= 5.2.1)
165167
public_suffix (4.0.5)
166168
puma (4.3.5)
167169
nio4r (~> 2.0)
@@ -198,6 +200,11 @@ GEM
198200
thor (>= 0.20.3, < 2.0)
199201
rainbow (3.0.0)
200202
rake (13.0.1)
203+
ransack (2.3.2)
204+
activerecord (>= 5.2.1)
205+
activesupport (>= 5.2.1)
206+
i18n
207+
polyamorous (= 2.3.2)
201208
rb-fsevent (0.10.4)
202209
rb-inotify (0.10.1)
203210
ffi (~> 1.0)
@@ -278,8 +285,6 @@ GEM
278285
turbolinks-source (5.2.0)
279286
tzinfo (1.2.7)
280287
thread_safe (~> 0.1)
281-
tzinfo-data (1.2020.1)
282-
tzinfo (>= 1.0.0)
283288
uber (0.1.0)
284289
unicode-display_width (1.7.0)
285290
warden (1.2.8)
@@ -315,14 +320,15 @@ DEPENDENCIES
315320
capybara (>= 2.15)
316321
devise (~> 4.7.1)
317322
dotenv-rails (~> 2.7.5)
318-
faraday
323+
faraday (~> 1.0.1)
319324
google_drive (~> 3.0.5)
320325
jbuilder (~> 2.7)
321326
letter_opener (~> 1.7.0)
322327
listen (~> 3.2)
323328
pg (>= 0.18, < 2.0)
324329
puma (~> 4.1)
325330
rails (~> 6.0.3, >= 6.0.3.1)
331+
ransack (~> 2.3.2)
326332
sass-rails (>= 6)
327333
selenium-webdriver
328334
solargraph (~> 0.39.8)

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,20 @@ To ensure consistency in code style configure your editor to work with [Solargra
2222
### Importing data
2323

2424
```ruby
25-
GoogleSheetsImport.new.import!(ENV['MY_CONFIG_JSON_STRING'])
25+
GoogleSheetsImport.new.extract(ENV['MY_CONFIG_JSON_STRING'])
2626
```
2727

28-
Below is the structure of the config JSON the `#import!` method expects - remember to convert the JSON to a string. The integers - i.e. `"user_email": 2` - indicate the column number from the sheet to import. In the example below column 2 contains the email addresses of users you'd like to import.
28+
Below is the structure of the config JSON the `#extract` method expects - remember to convert the JSON to a string. The integers - i.e. `"user_email": 2` - indicate the column number from the sheet to import. In the example below column 2 contains the email addresses of users you'd like to import.
2929

3030
```json
3131
{
3232
"file_id": "11NZ5o5TqSvvLtM...",
3333
"sheet_id": "13477...",
34-
"timestamp": 1,
35-
"user_email": 2,
36-
"organisation_name": 3,
37-
"custom": {
38-
"custom_column_1": 4,
39-
"custom_column_2": 5
34+
"fields": {
35+
"timestamp": 1,
36+
"user_email": 2,
37+
"organisation_name": 3,
38+
"etc": 4
4039
}
4140
}
4241
```

app/controllers/organisations_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ class OrganisationsController < ApplicationController
44
before_action :authenticate_admin!
55

66
def index
7-
@organisations = Organisation.order(:name)
7+
@q = Organisation.order(:name).ransack(params[:q])
8+
@organisations = @q.result(distinct: true)
89
end
910

1011
def show
11-
@organisation = Organisation.includes(:users).find_by(id: params[:id])
12+
@organisation = Organisation.includes(:users).find_by(slug: params[:id])
1213
@organisation.fetch_cc_data
1314
end
1415
end

app/controllers/tickets_controller.rb

Lines changed: 0 additions & 9 deletions
This file was deleted.

app/jobs/anchor_org_form_job.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
class AnchorOrgFormJob < ApplicationJob
4+
queue_as :default
5+
6+
def perform(row_start: 2)
7+
@rows = GoogleSheetsImport.new.extract(ENV['ANCHOR_CONFIG'], row_start: row_start)
8+
9+
@rows.each do |row|
10+
org = Organisation.new_or_reconcile(domain_or_email: row[:user_email], name: row[:organisation_name])
11+
12+
if org.new_record? && row[:organisation_name].present?
13+
org.name = row[:organisation_name]
14+
org.save!
15+
end
16+
17+
next if org.new_record?
18+
19+
create_affiliation(org, row[:user_email])
20+
21+
action = build_action('Submitted Anchor Org Form', org, row[:timestamp])
22+
action.details = { name: row[:user_name], email: row[:user_email] }
23+
action.save!
24+
end
25+
end
26+
end

app/jobs/application_job.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
# frozen_string_literal: true
2+
13
class ApplicationJob < ActiveJob::Base
24
# Automatically retry jobs that encountered a deadlock
35
# retry_on ActiveRecord::Deadlocked
46

57
# Most jobs are safe to ignore if the underlying records are no longer available
68
# discard_on ActiveJob::DeserializationError
9+
10+
def build_action(action_name, org, timestamp)
11+
Action.find_or_initialize_by(
12+
potential_action: PotentialAction.find_by(name: action_name),
13+
organisation: org,
14+
start_time: timestamp,
15+
end_time: timestamp
16+
)
17+
end
18+
19+
def create_affiliation(org, email)
20+
return if org.new_record? || email.blank?
21+
22+
user = User.find_or_create_by!(email: email.strip.downcase)
23+
Affiliation.find_or_create_by!(organisation: org, individual: user)
24+
end
725
end

app/jobs/design_hops_job.rb

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,25 @@ def perform
88

99
@hop_attendees.all.each do |record|
1010
domain = record['Email'].split('@').last.strip
11-
org_name = record['Organisation name'].strip
12-
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
19-
)
20-
@org.get_charity_number
21-
end
11+
org_name = record['Organisation name']&.strip
12+
13+
org = Organisation.new_or_reconcile(domain_or_email: domain, name: org_name)
2214

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-
)
15+
if org.new_record? && org_name.present?
16+
org.name = org_name
17+
org.save!
3218
end
3319

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-
)
20+
next if org.new_record?
21+
22+
create_affiliation(org, record['Email'])
23+
24+
action = build_action('Signed Up For Design Hop', org, record.created_at)
25+
action.details = {
26+
name: "#{record['First name']} #{record['Last name']}",
27+
email: record['Email']
28+
}
29+
action.save!
4630
end
4731
end
4832
end

app/jobs/digital_candle_job.rb

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,15 @@ def perform
77
@rows = GoogleSheetsImport.new.extract(ENV['DIGITAL_CANDLE_CONFIG'])
88

99
@rows.each do |row|
10-
domain = row[:email].split('@').last.strip
11-
first_name = row[:name].match('^(\w+) ')[1].strip
12-
last_name = row[:name].match(' (.*)$')[1].strip
10+
org = Organisation.new_or_reconcile(domain_or_email: row[:email])
1311

14-
# Find org
15-
@org = Organisation.find_by({ domain: domain })
12+
next if org.new_record?
1613

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
14+
create_affiliation(org, row[:email])
2815

29-
# Record actions
30-
Action.create!(
31-
potential_action: PotentialAction.find(3),
32-
organisation_id: @org.id,
33-
person_id: @person.id,
34-
details: {
35-
person_name: row[:name]
36-
},
37-
start_time: row[:timestamp],
38-
end_time: row[:timestamp]
39-
)
40-
41-
Action.create!(
42-
potential_action: PotentialAction.find(4),
43-
organisation_id: @org.id,
44-
person_id: @person.id,
45-
details: {
46-
person_name: row[:name],
47-
intro: row[:intro],
48-
intro_date: row[:intro_date]
49-
},
50-
start_time: row[:intro_date],
51-
end_time: row[:intro_date]
52-
)
53-
end
16+
action = build_action('Submitted Digital Candle Form', org, row[:timestamp])
17+
action.details = { name: row[:name], email: row[:email] }
18+
action.save!
5419
end
5520
end
5621
end

app/jobs/nesta_form_job.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
class NestaFormJob < ApplicationJob
4+
queue_as :default
5+
6+
def perform(row_start: 2)
7+
@rows = GoogleSheetsImport.new.extract(ENV['NESTA_CONFIG'], row_start: row_start)
8+
9+
@rows.each do |row|
10+
org = Organisation.new_or_reconcile(domain_or_email: row[:user_email], name: row[:organisation_name])
11+
12+
if org.new_record? && row[:organisation_name].present?
13+
org.name = row[:organisation_name]
14+
org.save!
15+
end
16+
17+
next if org.new_record?
18+
19+
create_affiliation(org, row[:user_email])
20+
21+
action = build_action('Submitted Nesta Needs Assessment Form', org, row[:timestamp])
22+
action.details = { name: row[:user_name], email: row[:user_email] }
23+
action.save!
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)