Skip to content

Commit e614aeb

Browse files
committed
Add new page for adding and editing org domains
1 parent 298cdaf commit e614aeb

File tree

6 files changed

+176
-72
lines changed

6 files changed

+176
-72
lines changed
Lines changed: 111 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,126 @@
11
# frozen_string_literal: true
22

3-
# Controller for API routes that return orgs by domain.
4-
class OrgDomainController < ApplicationController
5-
6-
# PUTS /api/orgs-by-domain with parameter email.
7-
# TBD: Change these Rubocop Cops
8-
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
9-
def index
10-
email_param = search_params[:email]
11-
email_domain = email_param.split('@').last if email_param.present? && email_param.include?('@')
12-
render json: [], status: :ok if email_domain.blank?
13-
14-
# check if org exists already using domain provided
15-
org_results = OrgDomain.search_with_org_info(email_domain)
16-
result = org_results.map { |record|
17-
org_id_new_format = {id: record.id, name: record.org_name}.to_json
3+
# Controller for API routes that return orgs by domain.
4+
class OrgDomainController < ApplicationController
185

19-
{
20-
id: org_id_new_format,
21-
org_name: record.org_name,
22-
domain: record.domain,
23-
}
6+
# PUTS /orgs-by-domain with parameter email.
7+
# TBD: Change these Rubocop Cops
8+
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
9+
def index
10+
email_param = search_params[:email]
11+
email_domain = email_param.split('@').last if email_param.present? && email_param.include?('@')
12+
render json: [], status: :ok if email_domain.blank?
13+
14+
# check if org exists already using domain provided
15+
org_results = OrgDomain.search_with_org_info(email_domain)
16+
result = org_results.map { |record|
17+
org_id_new_format = {id: record.id, name: record.org_name}.to_json
18+
19+
{
20+
id: org_id_new_format,
21+
org_name: record.org_name,
22+
domain: record.domain,
2423
}
24+
}
25+
26+
unless result.empty?
27+
puts "result: #{result}"
28+
render json: result, status: :ok
29+
return
30+
end
31+
32+
# if org doesn't exist already call Orion API by passing domain
33+
begin
34+
full_org_json = ::ExternalApis::OrionService.search_by_domain(email_domain)
35+
puts "full_org_json: #{full_org_json}"
2536

26-
unless result.empty?
27-
puts "result: #{result}"
37+
unless full_org_json&.key?('orgs')
38+
puts 'Invalid response or no orgs key found'
39+
other_org = Org.find_other_org
40+
org_id_new_format = {id: other_org.id, name: other_org.name}.to_json
41+
result = [{
42+
id: org_id_new_format,
43+
org_name: other_org.name,
44+
domain: ''
45+
}]
2846
render json: result, status: :ok
2947
return
3048
end
31-
32-
# if org doesn't exist already call Orion API by passing domain
33-
begin
34-
full_org_json = ::ExternalApis::OrionService.search_by_domain(email_domain)
35-
puts "full_org_json: #{full_org_json}"
36-
37-
unless full_org_json&.key?('orgs')
38-
puts 'Invalid response or no orgs key found'
39-
other_org = Org.find_other_org
40-
org_id_new_format = {id: other_org.id, name: other_org.name}.to_json
41-
result = [{
42-
id: org_id_new_format,
43-
org_name: other_org.name,
44-
domain: ''
45-
}]
46-
render json: result, status: :ok
47-
return
48-
end
49-
50-
# Extract the values from API result
51-
result = full_org_json['orgs'].map do |org|
52-
title = org['names'].find { |n| n['types'].include?('ror_display') }
53-
# ror_id_formatted = org['id'].split('/').last
54-
org_id_new_format = {name: title['value']}.to_json
55-
{
56-
id: org_id_new_format,
57-
org_name: title ? title['value'] : 'Name not found',
58-
domain: '',
59-
}
60-
rescue => e
61-
puts "Failed request: #{e.message}"
62-
result = []
63-
end
64-
65-
# if no org exists - assign to org called 'Other'
66-
if result.blank?
67-
other_org = Org.find_other_org
68-
org_id_new_format = {id: other_org.id, name: other_org.org_name}.to_json
69-
result = [{
70-
id: org_id_new_format,
71-
org_name: other_org.name,
72-
domain: ''
73-
}]
74-
end
49+
50+
# Extract the values from API result
51+
result = full_org_json['orgs'].map do |org|
52+
title = org['names'].find { |n| n['types'].include?('ror_display') }
53+
# ror_id_formatted = org['id'].split('/').last
54+
org_id_new_format = {name: title['value']}.to_json
55+
{
56+
id: org_id_new_format,
57+
org_name: title ? title['value'] : 'Name not found',
58+
domain: '',
59+
}
60+
rescue => e
61+
puts "Failed request: #{e.message}"
62+
result = []
7563
end
76-
render json: result, status: :ok
64+
65+
# if no org exists - assign to org called 'Other'
66+
if result.blank?
67+
other_org = Org.find_other_org
68+
org_id_new_format = {id: other_org.id, name: other_org.org_name}.to_json
69+
result = [{
70+
id: org_id_new_format,
71+
org_name: other_org.name,
72+
domain: ''
73+
}]
74+
end
75+
end
76+
render json: result, status: :ok
77+
end
78+
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
79+
80+
def show
81+
@org_domains = OrgDomain.where(org_id: current_user.org_id).order(created_at: :desc)
82+
end
83+
84+
def new
85+
@org_domain = OrgDomain.new
86+
end
87+
88+
def create
89+
@org_domain = OrgDomain.new(org_domain_params)
90+
@org_domain.org_id = current_user.org_id
91+
92+
if @org_domain.save
93+
redirect_to org_domain_show_path, notice: "Domain created successfully."
94+
else
95+
render :new
7796
end
78-
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
97+
end
7998

80-
private
99+
def edit
100+
@org_domain = OrgDomain.find(params[:id])
101+
redirect_to org_domain_show_path, alert: "Unauthorized" unless @org_domain.org_id == current_user.org_id
102+
end
81103

82-
# Using Strong Parameters ensure only domain is permitted
83-
def search_params
84-
params.permit(:email, :format, :org_domain)
104+
def update
105+
@org_domain = OrgDomain.find(params[:id])
106+
if @org_domain.org_id != current_user.org_id
107+
redirect_to org_domain_show_path, alert: "Unauthorized"
108+
elsif @org_domain.update(org_domain_params)
109+
redirect_to org_domain_show_path, notice: "Domain updated successfully."
110+
else
111+
render :edit
85112
end
86113
end
87114

115+
private
116+
117+
# Using Strong Parameters ensure only domain is permitted
118+
def search_params
119+
params.permit(:email, :format, :org_domain)
120+
end
121+
122+
def org_domain_params
123+
params.require(:org_domain).permit(:domain)
124+
end
125+
end
126+

app/views/layouts/_branding.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
</li>
9999
<% end %>
100100
<% end %>
101+
<% if current_user.can_org_admin? %>
102+
<li class="nav-item <%= 'class=active' if active_page?(org_domain_show_path) %>">
103+
<%= link_to _('Organisation domains'), org_domain_show_path, class: 'nav-link dropdown-item px-3' %>
104+
</li>
105+
<% end %>
101106
<% if current_user.can_grant_permissions? %>
102107
<li class="nav-item <%= 'class=active' if active_page?(admin_index_users_path) %>">
103108
<%= link_to _('Users'), admin_index_users_path, class: 'main_nav_last_li nav-link dropdown-item px-3' %>

app/views/org_domain/edit.html.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h1>Edit Domain</h1>
2+
3+
<%= form_with model: @org_domain, url: org_domain_path(@org_domain), method: :patch, local: true do |form| %>
4+
<div>
5+
<%= form.label :domain %><br>
6+
<%= form.text_field :domain %>
7+
</div>
8+
9+
<br>
10+
<%= form.submit "Update Domain" %>
11+
<% end %>
12+
13+
<%= link_to "Back", org_domain_show_path %>

app/views/org_domain/new.html.erb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h1>Add New Domain</h1>
2+
3+
<%= form_with model: @org_domain, url: org_domain_index_path, method: :post, local: true do |form| %>
4+
<div>
5+
<%= form.label :domain %><br>
6+
<%= form.text_field :domain %>
7+
</div>
8+
9+
<br>
10+
<%= form.submit "Create Domain" %>
11+
<% end %>
12+
13+
<%= link_to "Back", org_domain_show_path %>

app/views/org_domain/show.html.erb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<h1>Org Domains</h1>
2+
<div class="table-responsive">
3+
<% if @org_domains.any? %>
4+
<table class="table table-hover">
5+
<thead>
6+
<tr>
7+
<th>Domain</th>
8+
<th>Created at</th>
9+
<th>Updated at</th>
10+
<th>Actions</th>
11+
</tr>
12+
</thead>
13+
<tbody>
14+
<% @org_domains.each do |domain| %>
15+
<tr>
16+
<td><%= domain.domain %></td>
17+
<td><%= domain.created_at.strftime("%Y-%m-%d %H:%M:%S") %></td>
18+
<td><%= domain.updated_at.strftime("%Y-%m-%d %H:%M:%S") %></td>
19+
<td>
20+
<%= link_to 'Edit', edit_org_domain_path(domain), class: "btn btn-primary" %>
21+
</td>
22+
</tr>
23+
<% end %>
24+
</tbody>
25+
</table>
26+
<% else %>
27+
<p>No domains found.</p>
28+
<% end %>
29+
30+
<br>
31+
<%= link_to 'Add New Domain', new_org_domain_path, class: "btn btn-success" %>
32+
</div>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@
207207
end
208208

209209
post 'orgs-by-domain', to: 'org_domain#index'
210+
get 'org_domain/show', to: 'org_domain#show', as: 'org_domain_show'
211+
resources :org_domain, only: [:edit, :update, :new, :create]
210212

211213

212214
namespace :paginable do

0 commit comments

Comments
 (0)