Skip to content

Commit

Permalink
Fix logic of rendering people and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelMoeri committed Jul 15, 2024
1 parent c9e9825 commit 39acf6e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
3 changes: 2 additions & 1 deletion app/domain/ptime/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize

def request(method, endpoint, params = {})
path = @base_url + endpoint
if ENV['PTIME_API_ACCESSIBLE'].nil? && last_ptime_api_request_more_than_5_minutes
if last_ptime_api_request_more_than_5_minutes
send_request_and_parse_response(method, path, params)
else
skills_database_request
Expand Down Expand Up @@ -48,6 +48,7 @@ def last_ptime_api_request_more_than_5_minutes
end

def send_request_and_parse_response(method, path, params)
ENV['PTIME_API_ACCESSIBLE'] = 'true'
path = "#{path}?#{URI.encode_www_form(params)}" if method == :get
response = RestClient.send(method, path, headers)
JSON.parse(response.body)
Expand Down
21 changes: 7 additions & 14 deletions app/helpers/person_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,13 @@ def not_rated_default_skills(person)
end
end

def fetch_ptime_employees
ptime_employees = fetch_all_ptime_employees
ptime_employee_ids = fetch_all_ptime_employee_ids
build_dropdown_data(ptime_employees, ptime_employee_ids)
end
def fetch_ptime_or_skills_data
ptime_employees = Ptime::Client.new.request(:get, 'employees', { per_page: 1000 })['data']
all_skills_people = Person.all
return all_skills_people if ENV['PTIME_API_ACCESSIBLE'] == 'false'

def fetch_all_ptime_employees
Ptime::Client.new.request(:get, 'employees', { per_page: 1000 })['data']
end

def fetch_all_ptime_employee_ids
Person.all.pluck(:ptime_employee_id)
ptime_employee_ids = all_skills_people.pluck(:ptime_employee_id)
build_dropdown_data(ptime_employees, ptime_employee_ids)
end

def build_dropdown_data(ptime_employees, ptime_employee_ids)
Expand All @@ -113,8 +108,6 @@ def map_ptime_employee_id(ptime_employee)
end

def append_ptime_employee_name(ptime_employee)
firstname = ptime_employee['attributes']['firstname']
lastname = ptime_employee['attributes']['lastname']
"#{firstname} #{lastname}"
"#{ptime_employee['attributes']['firstname']} #{ptime_employee['attributes']['lastname']}"
end
end
9 changes: 5 additions & 4 deletions app/views/people/_search.html.haml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
%div.d-flex.align-items-center.justify-content-between
%div.d-flex.col-9.gap-3
%span.col-6{"data-controller": "dropdown"}
- if ptime_broken?
= collection_select :person_id, :person, Person.all.sort_by {|p| p.name.downcase }, :id, :name, select_when_availabale(person), {data:{"dropdown-target": "dropdown" , action: "change->dropdown#handleChange", value: "/people/"}}
- else
- sorted_employees = fetch_ptime_employees.sort_by { |p| p[:name] }
- people_data = fetch_ptime_or_skills_data
- if ptime_broken? # people_data == skills
= collection_select :person_id, :person, people_data.sort_by {|p| p.name.downcase }, :id, :name, select_when_availabale(person), {data:{"dropdown-target": "dropdown" , action: "change->dropdown#handleChange", value: "/people/"}}
- elsif ENV['PTIME_API_ACCESSIBLE'] = 'true' # people_data == time
- sorted_employees = people_data.sort_by { |p| p[:name] }
= select :person_id, :person, options_for_select(sorted_employees.map { |p| [ p[:name], p[:already_exists] ? p[:id] : "new?ptime_employee_id=#{p[:ptime_employee_id]}" ] }), {}, {data: { "dropdown-target" => "dropdown", action: "change->dropdown#handleChange", value: "/people/" }}
%div.d-flex.align-items-center.text-gray
= "#{t 'profile.updated_at'}: #{@person&.last_updated_at.strftime("%d.%m.%Y")}" if @person&.last_updated_at
Expand Down

0 comments on commit 39acf6e

Please sign in to comment.