Skip to content

Commit

Permalink
Make create_person method directly create the correct person without …
Browse files Browse the repository at this point in the history
…creating a default person
  • Loading branch information
RandomTannenbaum committed Jul 29, 2024
1 parent dbc51e9 commit f8ada07
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
4 changes: 3 additions & 1 deletion app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ def show
def new
@person = Ptime::UpdatePersonData.new.create_person(params[:ptime_employee_id])
%w[DE EN FR].each do |language|
@person.language_skills.push(LanguageSkill.new({ language: language, level: 'A1' }))
unless @person.language_skills.pluck(:language).include?(language.to_s)
@person.language_skills.push(LanguageSkill.new({ language: language, level: 'A1' }))
end
end
redirect_to(@person)
#super
Expand Down
13 changes: 4 additions & 9 deletions app/domain/ptime/update_person_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ def create_person(ptime_employee_id)
person = Person.find_by(ptime_employee_id: ptime_employee_id)
return person unless person.nil?

Person.create!(name: 'Default name', company: Company.first, birthdate: '1.1.1970',
nationality: 'CH', location: 'Bern', title: 'Default title',
email: '[email protected]', ptime_employee_id: ptime_employee_id)
update_person_data(person)
end

# rubocop:disable Metrics
Expand All @@ -18,14 +16,11 @@ def update_person_data(person)
location: :location }.freeze

begin
return unless person.ptime_employee_id
raise 'Person has no ptime_employee_id' unless person.ptime_employee_id

ptime_employee = Ptime::Client.new.get("employees/#{person.ptime_employee_id}")['data']
rescue RestClient::NotFound
person.destroy! if person.created_at == person.updated_at
raise "Ptime_employee with id #{person.ptime_employee_id} not found"
rescue StandardError
nil
raise "Ptime_employee with ptime_employee_id #{person.ptime_employee_id} not found"
else
ptime_employee['attributes'].each do |key, value|
if key.to_sym.in?(attribute_mapping.keys)
Expand All @@ -37,8 +32,8 @@ def update_person_data(person)
ptime_employee_nationalities = ptime_employee['attributes']['nationalities']
person.nationality = ptime_employee_nationalities[0]
person.nationality2 = ptime_employee_nationalities[1]
person.destroy! if !person.valid? && person.created_at == person.updated_at
person.save!
person
end
end
# rubocop:enable Metrics
Expand Down

0 comments on commit f8ada07

Please sign in to comment.