|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
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 |
18 | 5 |
|
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, |
24 | 23 | } |
| 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}" |
25 | 36 |
|
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 | + }] |
28 | 46 | render json: result, status: :ok |
29 | 47 | return |
30 | 48 | 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 = [] |
75 | 63 | 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 |
77 | 96 | end |
78 | | - # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity |
| 97 | + end |
79 | 98 |
|
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 |
81 | 103 |
|
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 |
85 | 112 | end |
86 | 113 | end |
87 | 114 |
|
| 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 | + |
0 commit comments