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

Commit 185a82a

Browse files
committed
Replaced hard-coded values with live Charity Commission data
Addresses #2 An org's charity number is used to pull details from CharityBase (E&W only) and Find That Charity to populate the `organisation#show` view. Where we don't know an org's charity number, we look up its domain in the `charity_domain_lookups` table (imported from CSV)
1 parent e614c2e commit 185a82a

14 files changed

+156409
-24
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ gem 'airrecord', '~> 1.0.5'
6262
gem 'devise', '~> 4.7.1'
6363
gem 'google_drive', '~> 3.0.5'
6464
gem 'strong_password', '~> 0.0.8'
65+
gem 'faraday'

Gemfile.lock

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ GEM
101101
ffi (1.13.0)
102102
globalid (0.4.2)
103103
activesupport (>= 4.2.0)
104-
google-api-client (0.40.0)
104+
google-api-client (0.40.1)
105105
addressable (~> 2.5, >= 2.5.1)
106106
googleauth (~> 0.9)
107107
httpclient (>= 2.8.1, < 3.0)
@@ -201,7 +201,7 @@ GEM
201201
rb-fsevent (0.10.4)
202202
rb-inotify (0.10.1)
203203
ffi (~> 1.0)
204-
regexp_parser (1.7.0)
204+
regexp_parser (1.7.1)
205205
representable (3.0.4)
206206
declarative (< 0.1.0)
207207
declarative-option (< 0.2.0)
@@ -213,7 +213,7 @@ GEM
213213
reverse_markdown (2.0.0)
214214
nokogiri
215215
rexml (3.2.4)
216-
rubocop (0.85.0)
216+
rubocop (0.85.1)
217217
parallel (~> 1.10)
218218
parser (>= 2.7.0.1)
219219
rainbow (>= 2.2.2, < 4.0)
@@ -278,6 +278,8 @@ GEM
278278
turbolinks-source (5.2.0)
279279
tzinfo (1.2.7)
280280
thread_safe (~> 0.1)
281+
tzinfo-data (1.2020.1)
282+
tzinfo (>= 1.0.0)
281283
uber (0.1.0)
282284
unicode-display_width (1.7.0)
283285
warden (1.2.8)
@@ -313,6 +315,7 @@ DEPENDENCIES
313315
capybara (>= 2.15)
314316
devise (~> 4.7.1)
315317
dotenv-rails (~> 2.7.5)
318+
faraday
316319
google_drive (~> 3.0.5)
317320
jbuilder (~> 2.7)
318321
letter_opener (~> 1.7.0)

app/controllers/organisations_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ def index
99

1010
def show
1111
@organisation = Organisation.includes(:users).find_by(id: params[:id])
12+
@organisation.fetch_cc_data
1213
end
1314
end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class CharityDomainLookup < ApplicationRecord
2+
3+
def self.lookup_domain(domain)
4+
generic_domains = [
5+
'gmail.com',
6+
'hotmail.com',
7+
'btinternet.com',
8+
'hotmail.co.uk',
9+
'yahoo.co.uk',
10+
'outlook.com',
11+
'aol.com',
12+
'btconnect.com',
13+
'yahoo.com',
14+
'googlemail.com',
15+
'ntlworld.com',
16+
'talktalk.net',
17+
'sky.com',
18+
'live.co.uk',
19+
'ntlworld.com',
20+
'tiscali.co.uk',
21+
'icloud.com',
22+
'btopenworld.com',
23+
'blueyonder.co.uk',
24+
'virginmedia.com',
25+
'nhs.net',
26+
'me.com',
27+
'msn.com',
28+
'talk21.com',
29+
'aol.co.uk',
30+
'mail.com',
31+
'live.com',
32+
'virgin.net',
33+
'ymail.com',
34+
'mac.com',
35+
'waitrose.com',
36+
'gmail.co.uk'
37+
]
38+
39+
return nil if generic_domains.include? domain
40+
41+
@domains = self.where(email_domain: domain).or(self.where(web_domain: domain))
42+
43+
return nil if @domains.count == 0
44+
@domains.first.regno
45+
end
46+
end

app/models/organisation.rb

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,84 @@ class Organisation < ApplicationRecord
66
has_many :users, through: :affiliations, source: :individual, source_type: 'User', dependent: :destroy
77
has_many :tickets
88
has_many :actions
9+
10+
attr_accessor :employee_count, :volunteer_count, :income, :income_fy, :region, :beneficiary
11+
12+
def get_charity_number
13+
return self.charity_number if self.charity_number
14+
15+
if (charity_number = CharityDomainLookup.lookup_domain(self.domain))
16+
self.update!({ charity_number: charity_number })
17+
return charity_number
18+
end
19+
20+
nil
21+
end
22+
23+
def fetch_cc_data
24+
return unless get_charity_number
25+
26+
charity_no = get_charity_number.split('-').last.strip
27+
28+
ftc_resp = Faraday.get('https://findthatcharity.uk/charity/' + charity_no + '.json')
29+
ftc_data = JSON.parse(ftc_resp.body)
30+
self.company_number = !ftc_data['company_number'].empty? ? ftc_data['company_number'][0]['number'] : nil
31+
self.income = ftc_data['latest_income']
32+
33+
return unless england_and_wales?
34+
35+
cb_form_params = {
36+
query: 'query SearchCharities($num: [ID]) {
37+
CHC {
38+
getCharities(filters: {
39+
id: $num
40+
}) {
41+
list(limit: 30) {
42+
id
43+
areas{
44+
name
45+
}
46+
beneficiaries {
47+
name
48+
}
49+
geo {
50+
postcode
51+
european_electoral_region
52+
}
53+
finances {
54+
income
55+
financialYear {
56+
begin
57+
end
58+
}
59+
}
60+
numPeople {
61+
employees
62+
volunteers
63+
}
64+
}
65+
}
66+
}
67+
}',
68+
variables: {
69+
num: charity_no
70+
}
71+
}
72+
cb_resp = Faraday.post('https://charitybase.uk/api/graphql', cb_form_params.to_json, {
73+
'Content-Type' => 'application/json',
74+
'Authorization' => 'Apikey 41f6fde1-7ab2-47bd-a069-39112c1d4339'
75+
})
76+
cb_data = JSON.parse(cb_resp.body)['data']['CHC']['getCharities']['list'][0]
77+
78+
self.employee_count = cb_data['numPeople']['employees']
79+
self.volunteer_count = cb_data['numPeople']['volunteers']
80+
self.income = cb_data['finances'][0]['income']
81+
self.income_fy = Date.parse(cb_data['finances'][0]['financialYear']['end'])
82+
self.region = cb_data['geo']['european_electoral_region']
83+
self.beneficiary = cb_data['beneficiaries'][0]['name']
84+
end
85+
86+
def england_and_wales?
87+
get_charity_number.start_with?('GB-CHC')
88+
end
989
end

app/views/organisations/show.html.erb

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,64 +83,57 @@
8383
Charity number
8484
</dt>
8585
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
86-
123456
86+
<%= @organisation.charity_number || '<em class="text-gray-600">Not known</em>'.html_safe %>
87+
88+
<% if @organisation.charity_number %>
8789
<ul class="border border-gray-200 rounded-md mt-2">
8890
<li class="pl-3 pr-4 py-3 flex items-center justify-between text-sm leading-5 border-b">
8991
<em class="ml-2 flex-1 w-0 truncate text-gray-600">
90-
Data for financial year ending 31 March 2019
92+
Data for financial year ending <%= @organisation.income_fy&.strftime("%e %B %Y") %>
9193
</em>
9294
</li>
9395
<li class="pl-3 pr-4 py-3 flex items-center justify-between text-sm leading-5 border-b">
9496
<div class="w-0 flex-1 flex items-center ml-2">
9597
Employees
9698
</div>
9799
<div class="ml-4 flex-shrink-0">
98-
22
100+
<%= @organisation.employee_count || '<em class="text-gray-600">Not known</em>'.html_safe %>
99101
</div>
100102
</li>
101103
<li class="pl-3 pr-4 py-3 flex items-center justify-between text-sm leading-5">
102104
<div class="w-0 flex-1 flex items-center ml-2">
103105
Volunteers
104106
</div>
105107
<div class="ml-4 flex-shrink-0">
106-
8
108+
<%= @organisation.volunteer_count || '<em class="text-gray-600">Not known</em>'.html_safe %>
107109
</div>
108110
</li>
109111
<li class="pl-3 pr-4 py-3 flex items-center justify-between text-sm leading-5">
110112
<div class="w-0 flex-1 flex items-center ml-2">
111113
Income
112114
</div>
113115
<div class="ml-4 flex-shrink-0">
114-
£1,100,000
116+
<%= number_to_currency(@organisation.income, unit: '£', precision: 0) || '<em class="text-gray-600">Not known</em>'.html_safe %>
115117
</div>
116118
</li>
117119
</ul>
120+
<% end %>
118121
</dd>
119122
</div>
120123
<div class="bg-gray-50 px-4 py-4 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
121124
<dt class="text-sm leading-5 font-medium text-gray-600">
122125
Company number
123126
</dt>
124127
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
125-
123456
128+
<%= @organisation.company_number || '<em class="text-gray-600">Not known</em>'.html_safe %>
126129
</dd>
127130
</div>
128131
<div class="bg-gray-50 px-4 py-4 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
129132
<dt class="text-sm leading-5 font-medium text-gray-600">
130133
Location
131134
</dt>
132135
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
133-
<select>
134-
<option>East Midlands</option>
135-
<option>East of England</option>
136-
<option>London</option>
137-
<option>North East</option>
138-
<option>North West</option>
139-
<option>South East</option>
140-
<option>South West</option>
141-
<option>West Midlands</option>
142-
<option>Yorkshire and The Humber</option>
143-
</select>
136+
<%= @organisation.region %>
144137
<details class="text-xs text-gray-600">
145138
<summary class="link">Dev notes</summary>
146139
<p>
@@ -181,7 +174,7 @@
181174
</div>
182175
<div class="bg-white px-4 py-4 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-6">
183176
<dt class="text-sm leading-5 font-medium text-gray-600">
184-
COVID-19 Theme & Catagory
177+
COVID-19 Theme &amp; Category
185178
</dt>
186179
<dd class="mt-1 text-sm leading-5 text-gray-900 sm:mt-0 sm:col-span-2">
187180
<select class="w-full">
@@ -343,7 +336,7 @@
343336
</div>
344337
<%- @organisation.actions.each do |action| %>
345338
<div class="border-b border-gray-200 px-6 py-4 text-sm leading-5 font-medium text-gray-900">
346-
<b><%= action.start_time.strftime("%Y %b %d") %>:</b> <%= action.getString() %>
339+
<b><%= action.start_time.strftime("%Y %b %d") %>:</b> <%= action.get_string() %>
347340
</div>
348341
<% end %>
349342
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateCharityDomainLookups < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :charity_domain_lookups do |t|
4+
t.string :regno
5+
t.string :name
6+
t.string :domain
7+
8+
t.timestamps
9+
end
10+
end
11+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CreatePeople < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :people do |t|
4+
t.string :first_name
5+
t.string :last_name
6+
t.string :email
7+
t.references :organisation, null: false, foreign_key: true
8+
9+
t.timestamps
10+
end
11+
end
12+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddPersonToAction < ActiveRecord::Migration[6.0]
2+
def change
3+
add_reference :actions, :person, null: true, foreign_key: true
4+
end
5+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class AddColumnsToOrganisation < ActiveRecord::Migration[6.0]
2+
def change
3+
add_column :organisations, :charity_number, :string
4+
add_column :organisations, :company_number, :string
5+
add_column :organisations, :location, :jsonb
6+
add_column :organisations, :audience, :string
7+
add_column :organisations, :subsector, :jsonb
8+
add_column :organisations, :maturity, :int
9+
add_column :organisations, :anchor_org, :bool
10+
end
11+
end

0 commit comments

Comments
 (0)