From 39acf6e4d0c722f4db0c69b53865c0d99535e53a Mon Sep 17 00:00:00 2001 From: Manuel Date: Mon, 15 Jul 2024 09:51:35 +0200 Subject: [PATCH] Fix logic of rendering people and cleanup code --- app/domain/ptime/client.rb | 3 ++- app/helpers/person_helper.rb | 21 +++++++-------------- app/views/people/_search.html.haml | 9 +++++---- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/app/domain/ptime/client.rb b/app/domain/ptime/client.rb index bed6204a4..2dd72f743 100644 --- a/app/domain/ptime/client.rb +++ b/app/domain/ptime/client.rb @@ -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 @@ -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) diff --git a/app/helpers/person_helper.rb b/app/helpers/person_helper.rb index 8c563123b..61bf95bbb 100644 --- a/app/helpers/person_helper.rb +++ b/app/helpers/person_helper.rb @@ -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) @@ -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 diff --git a/app/views/people/_search.html.haml b/app/views/people/_search.html.haml index a3cfcb51f..23ea31441 100644 --- a/app/views/people/_search.html.haml +++ b/app/views/people/_search.html.haml @@ -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