@@ -14,7 +14,7 @@ def index
1414 # check if org exists already using domain provided
1515 org_results = OrgDomain . search_with_org_info ( email_domain )
1616 result = org_results . map { |record |
17- org_id_new_format = { id : record . id , name : record . org_name } . to_json
17+ org_id_new_format = { id : record . id , name : record . org_name } . to_json
1818
1919 {
2020 id : org_id_new_format ,
@@ -36,6 +36,17 @@ def index
3636 full_org_json = ::ExternalApis ::OrionService . search_by_domain ( email_domain )
3737 puts "full_org_json: #{ full_org_json } "
3838
39+ # If no orgs found, retry with higher level domain by removing subdomains
40+ split_domain = email_domain . split ( '.' )
41+
42+ while !full_org_json &.key? ( 'orgs' ) && split_domain . length > 2
43+ split_domain . shift
44+ domain_to_search = split_domain . join ( '.' )
45+ puts "Retrying with #{ domain_to_search } "
46+ full_org_json = ::ExternalApis ::OrionService . search_by_domain ( domain_to_search )
47+ puts "Retry full_org_json with #{ domain_to_search } : #{ full_org_json } "
48+ end
49+
3950 unless full_org_json &.key? ( 'orgs' )
4051 puts 'Invalid response or no orgs key found'
4152 # Add Other org
@@ -45,10 +56,10 @@ def index
4556 end
4657
4758 # Extract the values from API result
48- result = full_org_json [ 'orgs' ] . map do |org |
59+ result = full_org_json [ 'orgs' ] . map do |org |
4960 # The ror_display value will be in the language of the country, and should always be present.
5061 ror_display_name_json = org [ 'names' ] . find { |n | n [ 'lang' ] && n [ 'types' ] &.include? ( 'ror_display' ) }
51- #puts "ror_display_name_json: #{ror_display_name_json}"
62+ # puts "ror_display_name_json: #{ror_display_name_json}"
5263 org_name = ror_display_name_json ? ror_display_name_json [ 'value' ] : nil
5364 puts "org_name: #{ org_name } "
5465
@@ -59,9 +70,10 @@ def index
5970 {
6071 id : org_id_new_format ,
6172 org_name : org_name ,
62- domain : '' ,
73+ domain : ''
6374 }
64- rescue => e
75+
76+ rescue StandardError => e
6577 puts "Failed request: #{ e . message } "
6678 end
6779
@@ -72,22 +84,23 @@ def index
7284 end
7385 render json : result , status : :ok
7486 end
87+
7588 # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
7689
7790 def show
7891 @org_domains = OrgDomain . where ( org_id : current_user . org_id ) . order ( :domain )
79-
8092 end
8193
8294 def new
8395 @org_domain = OrgDomain . new
8496 end
8597
98+ # rubocop:disable Metrics/AbcSize
8699 def create
87100 domain_input = params [ :org_domain ] [ :domain ] . to_s . downcase . gsub ( /\s +/ , '' )
88101
89102 if domain_input . blank?
90- flash . now [ :alert ] = " Domain can't be blank."
103+ flash . now [ :alert ] = ' Domain cannot be blank.'
91104 @org_domain = OrgDomain . new
92105 render :new and return
93106 end
@@ -96,53 +109,55 @@ def create
96109 @org_domain . org_id = current_user . org_id
97110
98111 if @org_domain . save
99- redirect_to org_domain_show_path , notice : " Domain created successfully."
112+ redirect_to org_domain_show_path , notice : ' Domain created successfully.'
100113 else
101114 render :new
102115 end
103116 end
117+ # rubocop:enable Metrics/AbcSize
104118
105119 def edit
106120 @org_domain = OrgDomain . find ( params [ :id ] )
107- redirect_to org_domain_show_path , alert : " Unauthorized" unless @org_domain . org_id == current_user . org_id
121+ redirect_to org_domain_show_path , alert : ' Unauthorized' unless @org_domain . org_id == current_user . org_id
108122 end
109123
124+ # rubocop:disable Metrics/AbcSize
110125 def update
111126 @org_domain = OrgDomain . find ( params [ :id ] )
112-
113- if @org_domain . org_id != current_user . org_id
114- redirect_to org_domain_show_path , alert : "Unauthorized"
115- else
127+
128+ if @org_domain . org_id == current_user . org_id
116129 domain_input = params [ :org_domain ] [ :domain ] . to_s . downcase . gsub ( /\s +/ , '' )
117-
130+
118131 if domain_input . blank?
119- flash . now [ :alert ] = " Domain can't be blank."
132+ flash . now [ :alert ] = ' Domain cannot be blank.'
120133 render :edit and return
121134 end
122-
135+
123136 if @org_domain . update ( domain : domain_input )
124- redirect_to org_domain_show_path , notice : " Domain updated successfully."
137+ redirect_to org_domain_show_path , notice : ' Domain updated successfully.'
125138 else
126139 render :edit
127140 end
141+ else
142+ redirect_to org_domain_show_path , alert : 'Unauthorized'
128143 end
129144 end
130-
145+ # rubocop:enable Metrics/AbcSize
146+
131147 def destroy
132148 @org_domain = OrgDomain . find ( params [ :id ] )
133-
149+
134150 if @org_domain . org_id != current_user . org_id
135- redirect_to org_domain_show_path , alert : " Unauthorized"
151+ redirect_to org_domain_show_path , alert : ' Unauthorized'
136152 return
137153 end
138-
154+
139155 if @org_domain . destroy
140- redirect_to org_domain_show_path , notice : " Domain deleted successfully."
156+ redirect_to org_domain_show_path , notice : ' Domain deleted successfully.'
141157 else
142- redirect_to org_domain_show_path , alert : " Failed to delete domain."
158+ redirect_to org_domain_show_path , alert : ' Failed to delete domain.'
143159 end
144160 end
145-
146161
147162 private
148163
@@ -157,17 +172,12 @@ def org_domain_params
157172
158173 def other_org_json
159174 other_org = Org . find_other_org
160- #add if condition here to check if other_org is nil or present
161- if other_org . present?
162- org_id_new_format = { id : other_org . id , name : other_org . name } . to_json
163- else
164- org_id_new_format = { name : "Other" } . to_json
165- end
166- {
167- id : org_id_new_format ,
168- org_name : other_org ? other_org . name : "Other" ,
169- domain : "" ,
170- }
175+ # add if condition here to check if other_org is nil or present
176+ org_id_new_format = other_org . present? ? { id : other_org . id , name : other_org . name } . to_json : { name : 'Other' } . to_json
177+ {
178+ id : org_id_new_format ,
179+ org_name : other_org ? other_org . name : 'Other' ,
180+ domain : ''
181+ }
171182 end
172183end
173-
0 commit comments