Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/746 update or create with ptime employee data #749

Draft
wants to merge 32 commits into
base: ptime-mapper-script
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
c969ed0
Remove unwanted things from delayed job and start writing updating an…
RandomTannenbaum Jul 3, 2024
622913d
Make show method use new record if necessary
RandomTannenbaum Jul 4, 2024
80d6b89
Move new create and update logic to new class and make create redirec…
RandomTannenbaum Jul 4, 2024
0d08852
Make newly created people have default languages, refactor code
RandomTannenbaum Jul 4, 2024
c071a85
Start writing tests for updating person data on visit and remove spec…
RandomTannenbaum Jul 4, 2024
b4d0f78
Fix assigning and updating of nationalities and write test for creati…
RandomTannenbaum Jul 5, 2024
12ab636
Write a spec that tests if create_person can return an already existi…
RandomTannenbaum Jul 5, 2024
ec2bb14
Write spec that tests ensures an error is raised when the person is n…
RandomTannenbaum Jul 5, 2024
193f80a
Make create_person method directly create the correct person without …
RandomTannenbaum Jul 29, 2024
61541a7
Clean up and fix specs, fix create_person method in update_person_dat…
RandomTannenbaum Jul 29, 2024
c06233d
Start updating default language logic
RandomTannenbaum Jul 29, 2024
58a9824
Rename update_person_data from ptime namespace to people_employees
RandomTannenbaum Jul 29, 2024
a724c45
Rename create_person method to create_or_find
RandomTannenbaum Jul 29, 2024
7037d7f
Change way in which default languages are set
RandomTannenbaum Jul 30, 2024
9f018bc
Update tests with new helpers
RandomTannenbaum Jul 30, 2024
5a81213
Make person controller work without time again, start writing initial…
RandomTannenbaum Jul 30, 2024
b9c7959
Move setting of default languages to after_initialize and cleanup code
RandomTannenbaum Jul 31, 2024
3874b7c
Move ptime connection to concerns and rename it to people_controller
RandomTannenbaum Jul 31, 2024
f5c5af3
Rename create_or_find to find_or_create
RandomTannenbaum Jul 31, 2024
684816e
Fix a bug where default languages where added every time person was v…
RandomTannenbaum Jul 31, 2024
54925b5
Move some code from the update_person_data method to another method f…
RandomTannenbaum Jul 31, 2024
b5f1053
Fix typo in people_employees
RandomTannenbaum Jul 31, 2024
027e2f9
Write spec to test if default languages are created correctly
RandomTannenbaum Jul 31, 2024
2bcde37
Change insert to prepend in ptime initializer
RandomTannenbaum Jul 31, 2024
8cd1708
Simplify people_controller in ptime namespace
RandomTannenbaum Aug 2, 2024
0277ae5
Rewrite people_employees feature spec as request spec
RandomTannenbaum Aug 2, 2024
4946cc9
Fix ptime specs
RandomTannenbaum Aug 2, 2024
b968351
Make rubocop happy
RandomTannenbaum Aug 2, 2024
6eb889e
Make controller read person id from params and remove unwanted spec
RandomTannenbaum Aug 2, 2024
2001e79
Make ptime client correctly throw PTimeTemporarilyUnavailableError an…
RandomTannenbaum Aug 2, 2024
f621ac0
Check if correct template is rendered in ptime temporarely unavailabl…
RandomTannenbaum Aug 2, 2024
6d5eb50
Replace config.to_prepare with config.after_initialize in ptime initi…
RandomTannenbaum Aug 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/controllers/concerns/ptime/people_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Ptime
module PeopleController
def show
super
Ptime::PeopleEmployees.new.update_person_data(@person)
end

def new
@person = Ptime::PeopleEmployees.new.find_or_create(params[:ptime_employee_id])
redirect_to(@person)
end
end
end
8 changes: 0 additions & 8 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class PeopleController < CrudController
self.permitted_attrs = [:birthdate, :location, :marital_status, :updated_by, :name, :nationality,
:nationality2, :title, :competence_notes, :company_id, :email,
:department_id, :shortname, :picture, :picture_cache,
{ person_roles_attributes: [:role_id, :person_role_level_id,
:percent, :id, :_destroy] },
{ person_roles_attributes:
[:role_id, :person_role_level_id, :percent, :id, :_destroy] },
{ language_skills_attributes:
Expand All @@ -27,14 +25,8 @@ def show

@person = Person.includes(projects: :project_technologies,
person_roles: [:role, :person_role_level]).find(params.fetch(:id))
super
end

def new
super
%w[DE EN FR].each do |language|
@person.language_skills.push(LanguageSkill.new({ language: language }))
end
end

def create
Expand Down
2 changes: 1 addition & 1 deletion app/domain/ptime/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def request(method, endpoint, params = {})
if last_ptime_error_more_than_5_minutes_ago?
send_request_and_parse_response(method, url, params)
else
raise CustomExceptions::PTimeClientError, 'Error'
raise CustomExceptions::PTimeTemporarilyUnavailableError, 'PTime is temporarily unavailable'
end
end

Expand Down
44 changes: 44 additions & 0 deletions app/domain/ptime/people_employees.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Ptime
class PeopleEmployees
ATTRIBUTES_MAPPING = { full_name: :name, shortname: :shortname, email: :email, marital_status:
:marital_status, graduation: :title, birthdate: :birthdate,
location: :location }.freeze
def find_or_create(ptime_employee_id)
raise 'No ptime_employee_id provided' unless ptime_employee_id

person = Person.find_by(ptime_employee_id: ptime_employee_id)
return person unless person.nil?

new_person = Person.new(ptime_employee_id: ptime_employee_id)

update_person_data(new_person)
end

# rubocop:disable Metrics
def update_person_data(person)
raise 'Person has no ptime_employee_id' unless person.ptime_employee_id

ptime_employee = Ptime::Client.new.request(:get, "employees/#{person.ptime_employee_id}")
rescue CustomExceptions::PTimeTemporarilyUnavailableError
# Ignored
else
ptime_employee[:attributes].each do |key, value|
if key.to_sym.in?(ATTRIBUTES_MAPPING.keys)
person[ATTRIBUTES_MAPPING[key.to_sym]] = (value.presence || '-')
end
end
set_additional_attributes(person, ptime_employee)
person.save!
person
end
# rubocop:enable Metrics

def set_additional_attributes(person, ptime_employee)
ptime_employee_employed = ptime_employee[:attributes][:is_employed]
person.company = Company.find_by(name: ptime_employee_employed ? 'Firma' : 'Ex-Mitarbeiter')
ptime_employee_nationalities = ptime_employee[:attributes][:nationalities]
person.nationality = ptime_employee_nationalities[0]
person.nationality2 = ptime_employee_nationalities[1]
end
end
end
1 change: 1 addition & 0 deletions app/exceptions/custom_exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module CustomExceptions

class PTimeClientError < StandardError; end
class PTimeTemporarilyUnavailableError < StandardError; end

end
10 changes: 10 additions & 0 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
class Person < ApplicationRecord
include PgSearch::Model

after_initialize :set_default_languages

belongs_to :company
belongs_to :department, optional: true

Expand Down Expand Up @@ -103,4 +105,12 @@ def picture_size

errors.add(:picture, 'grösse kann maximal 10MB sein')
end

def set_default_languages
if new_record?
%w[DE EN FR].each do |language|
language_skills.push(LanguageSkill.new({ language: language, level: 'Keine' }))
end
end
end
end
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

module Skills
def self.ptime_available?
ActiveModel::Type::Boolean.new.cast(ENV.fetch('PTIME_API_ACCESSIBLE', true))
%w[true True 1].include?(ENV.fetch('PTIME_API_ACCESSIBLE', false))
end

class Application < Rails::Application
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/ptime.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Rails.application.config.after_initialize do
PeopleController.prepend Ptime::PeopleController if Skills.ptime_available?
end
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ services:
/bin/bash -c "
curl -fsSL https://deb.nodesource.com/setup_18.x | bash - &&
apt-get install -y nodejs &&
npm install -g yarn && yarn add nodemon esbuild && bin/assets &&
npm install -g yarn && bin/assets &&
sleep infinity"
volumes:
- ./:/myapp
Expand Down
16 changes: 16 additions & 0 deletions spec/domain/ptime/people_employees_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rails_helper'

describe Ptime::PeopleEmployees do
it 'should raise error when no ptime_employee_id is passed to new action' do
expect{ Ptime::PeopleEmployees.new.find_or_create(nil) }.to raise_error(RuntimeError, 'No ptime_employee_id provided')
end

it 'should return person if it has the given ptime_employee_id' do
person_wally = people(:wally)
person_wally.ptime_employee_id = 123
person_wally.save!

new_person = Ptime::PeopleEmployees.new.find_or_create(person_wally.ptime_employee_id)
expect(person_wally.attributes.except(*%w[created_at updated_at])).to eql(new_person.attributes.except(*%w[created_at updated_at]))
end
end
23 changes: 23 additions & 0 deletions spec/fixtures/files/json/wally.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"data": {
"id": 50,
"type": "employee",
"attributes": {
"shortname": "CAL",
"firstname": "Changed Wally",
"lastname": "Allround",
"full_name": "Changed Wally Allround",
"email": "[email protected]",
"marital_status": "single",
"nationalities": [
"DE", "DK"
],
"graduation": "Quarter-Stack Developer",
"department_shortname": "SYS",
"employment_roles2": [],
"is_employed": false,
"birthdate": "1.1.2000",
"location": "Basel"
}
}
}
8 changes: 8 additions & 0 deletions spec/models/person_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,12 @@
expect(person.errors[:shortname].first).to eq('ist zu lang (mehr als 100 Zeichen)')
end
end

context 'language_skills' do
it 'should automatically add default language on person creation' do
new_person = Person.new
expect(new_person.language_skills.length).to eq(3)
expect(new_person.language_skills.map(&:language)).to eql(%w[DE EN FR])
end
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

# Helpers from gems
config.include(Devise::Test::IntegrationHelpers, type: :feature)
config.include(Devise::Test::IntegrationHelpers, type: :request)
config.include(Devise::Test::ControllerHelpers, type: :controller)
config.include(ActionView::RecordIdentifier, type: :feature)

Expand Down
79 changes: 79 additions & 0 deletions spec/requests/people_employees_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require "rails_helper"

describe 'Update or create person' do
before(:each) do
sign_in auth_users(:user), scope: :auth_user
end

before(:all) do
PeopleController.prepend(Ptime::PeopleController)
end

after(:all) do
Ptime::PeopleController.remove_method(:new)
Ptime::PeopleController.remove_method(:show)
end

it 'should update person when visited' do
stub_ptime_request(fixture_data("wally").to_json, "employees/50", 200)

Company.create!(name: "Ex-Mitarbeiter")
person_wally = people(:wally)
person_wally.ptime_employee_id = 50
person_wally.save!
expect(person_wally.name).to eq('Wally Allround')
expect(person_wally.email).to eq('[email protected]')

get "/people/#{person_wally.id}"
expect(response).to render_template(:show)

person_wally.reload
expect(person_wally.name).to eq('Changed Wally Allround')
expect(person_wally.shortname).to eq('CAL')
expect(person_wally.email).to eq('[email protected]')
expect(person_wally.marital_status).to eq('single')
expect(person_wally.title).to eq('Quarter-Stack Developer')
expect(person_wally.birthdate).to eq('1.1.2000')
expect(person_wally.location).to eq('Basel')
expect(person_wally.nationality).to eq('DE')
expect(person_wally.nationality2).to eq('DK')
end

it 'should create person when visited' do
stub_ptime_request(fixture_data("wally").to_json, "employees/50", 200)

Company.create!(name: "Ex-Mitarbeiter")
person_wally = people(:wally)
person_wally.destroy!
expect(Person.find_by(name: "Wally Allround")).to be_nil

get "/people/new?ptime_employee_id=50"

new_wally = Person.find_by(name: "Changed Wally Allround")
expect(new_wally.name).to eq('Changed Wally Allround')
expect(new_wally.shortname).to eq('CAL')
expect(new_wally.email).to eq('[email protected]')
expect(new_wally.marital_status).to eq('single')
expect(new_wally.title).to eq('Quarter-Stack Developer')
expect(new_wally.birthdate).to eq('1.1.2000')
expect(new_wally.location).to eq('Basel')
expect(new_wally.nationality).to eq('DE')
expect(new_wally.nationality2).to eq('DK')
end

it 'should not update but still show person if ptime temporarily unavailable' do
stub_env_var('LAST_PTIME_ERROR', 1.minute.ago)
stub_ptime_request(fixture_data("wally").to_json, "employees/50", 500)

Company.create!(name: "Ex-Mitarbeiter")
person_wally = people(:wally)
person_wally.ptime_employee_id = 50
person_wally.save!

get "/people/#{person_wally.id}"

expect(response).to render_template(:show)

expect(person_wally.attributes.except(*%w[created_at updated_at])).to eq(Person.find(person_wally.id).attributes.except(*%w[created_at updated_at]))
end
end
Loading