-
Notifications
You must be signed in to change notification settings - Fork 20
Ptime mapper script #737
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
Draft
RandomTannenbaum
wants to merge
29
commits into
master
Choose a base branch
from
ptime-mapper-script
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Ptime mapper script #737
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
31cc487
Squash branch
ManuelMoeri 923b345
Fix tests
ManuelMoeri 2420260
Remove duplicate in gemfile.lock and fix test
ManuelMoeri ff10d9a
Remove gem that was present twice in the gemfile
ManuelMoeri a697b3a
Fix preloading and showing the correct person in the dropdown
ManuelMoeri 4fc97e7
Try to fix tests by removing local from routes
ManuelMoeri c1462e7
Fix tests for now, but make rubocop mad
ManuelMoeri 13bf453
Fix the broken dropdown again
ManuelMoeri 445609b
Fix currently selected person for ptime data
ManuelMoeri 7b8d485
Make fetch method less redundant and make rubocop happy
ManuelMoeri a57e024
Rename env variable to more fitting of its purpose
ManuelMoeri 47f4d6e
Fix helper tests to check for the correct path
ManuelMoeri 726dcf2
Comment in method
ManuelMoeri 637de97
Fix fetching of some people data and only show active employees in dr…
ManuelMoeri 6802f9f
Add .envrc to gitignore and change logic to correctly handle roles
ManuelMoeri ae606bc
Fix logic of roles and prepare for the newly added attribues
ManuelMoeri c88db4e
Comment out method that is not used right now and fix tests
ManuelMoeri 54ad28b
Fix naming of tests, make method in helper shorter and replace method…
ManuelMoeri ff60cd5
Change fixtures to better match something and add extended tests for …
ManuelMoeri 05dd95b
Fix and enhance tests aswell as json fixtures
ManuelMoeri 1c8f129
Make constant non-private to make rubocop happy once more
ManuelMoeri 98e8c0e
Fix failing tests by including the newly added person in the fixtures
ManuelMoeri 5bf42e2
Delete unnecessary file and the corresponding spec and rake task
ManuelMoeri 8f517e5
Extend mapping tests to include roles
ManuelMoeri 4b0c714
Fix people_spec by setting the default nationality correct
ManuelMoeri d77e006
Change logic to only send requests to ptime when it has not been fetc…
ManuelMoeri bc08eb2
Feature/733 make attr readonly for ptime attrs (#846)
Miguel7373 6d0dfeb
Update nodemon and esbuild versions, remove unnecessary changes from …
RandomTannenbaum 7f3cba4
Readd schema.rb
ManuelMoeri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,3 +97,6 @@ config/docker/development/home/rails/.bash_history | |
|
||
/app/assets/builds/* | ||
!/app/assets/builds/.keep | ||
|
||
.sec | ||
.envrc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# This script will assign each person the corresponding employee ID from PuzzleTime | ||
module Ptime | ||
class AssignEmployeeIds | ||
MAX_NUMBER_OF_FETCHED_EMPLOYEES = 1000 | ||
|
||
# rubocop:disable Rails/Output | ||
def run(should_map: false) | ||
puts 'Notice this is a dry run and mapping will not happen!' unless should_map | ||
fetch_data | ||
|
||
puts "Currently there are: | ||
- #{@ptime_employees.length} employees in PuzzleTime | ||
- #{@skills_people.count} people in PuzzleSkills | ||
This is a difference of #{(@skills_people.count - @ptime_employees.length).abs} entries" | ||
map_employees(should_map) | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable Metrics | ||
def map_employees(should_map) | ||
puts 'Assigning employee IDs now...' if should_map | ||
|
||
unmatched_entries = [] | ||
mapped_people_count = 0 | ||
|
||
@ptime_employees.each do |ptime_employee| | ||
ptime_employee_name = ptime_employee[:attributes][:full_name] | ||
ptime_employee_email = ptime_employee[:attributes][:email] | ||
matched_person = Person.where(ptime_employee_id: nil).find_by(email: ptime_employee_email) | ||
|
||
if matched_person.nil? | ||
unmatched_entries << { name: ptime_employee_name, id: ptime_employee[:id] } | ||
else | ||
if should_map | ||
matched_person.ptime_employee_id = ptime_employee[:id] | ||
matched_person.save! | ||
end | ||
mapped_people_count += 1 | ||
end | ||
end | ||
|
||
puts '--------------------------' | ||
puts "#{mapped_people_count} people were matched successfully" | ||
puts '--------------------------' | ||
puts "#{unmatched_entries.size} people didn't match" | ||
unmatched_entries.each { |entry| puts "- #{entry[:name]} with id #{entry[:id]}" } | ||
end | ||
# rubocop:enable Metrics | ||
|
||
def fetch_data | ||
puts 'Fetching required data...' | ||
@ptime_employees = Ptime::Client.new.request(:get, 'employees', | ||
{ per_page: MAX_NUMBER_OF_FETCHED_EMPLOYEES }) | ||
@skills_people = Person.all | ||
puts 'Successfully fetched data' | ||
end | ||
# rubocop:enable Rails/Output | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rest_client' | ||
require 'base64' | ||
|
||
module Ptime | ||
class Client | ||
def initialize | ||
@base_url = "#{ENV.fetch('PTIME_BASE_URL')}/api/v1/" | ||
end | ||
|
||
def request(method, endpoint, params = {}) | ||
url = @base_url + endpoint | ||
|
||
if last_error_stale? | ||
execute_request(method, url, params) | ||
else | ||
raise CustomExceptions::PTimeTemporarilyUnavailableError, 'PTime is temporarily unavailable' | ||
end | ||
end | ||
|
||
private | ||
|
||
def last_error_stale? | ||
last_request_time = ENV.fetch('LAST_PTIME_ERROR', nil) | ||
return true if last_request_time.nil? | ||
|
||
last_request_time.to_datetime <= 5.minutes.ago | ||
end | ||
|
||
def build_request(method, url) | ||
RestClient::Request.new( | ||
:method => method, | ||
:url => url, | ||
:user => ENV.fetch('PTIME_API_USERNAME'), | ||
:password => ENV.fetch('PTIME_API_PASSWORD'), | ||
:headers => { :accept => :json, :content_type => :json } | ||
) | ||
end | ||
|
||
def execute_request(method, url, params) | ||
url += "?#{params.to_query}" if method == :get && params.present? | ||
response = build_request(method, url).execute | ||
JSON.parse(response.body, symbolize_names: true)[:data] | ||
rescue RestClient::ExceptionWithResponse | ||
raise CustomExceptions::PTimeClientError, 'Error' | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
module Ptime | ||
class PeopleEmployees | ||
ATTRIBUTES_MAPPING = { | ||
firstname: :name, | ||
shortname: :shortname, | ||
email: :email, | ||
marital_status: :marital_status, | ||
graduation: :title, | ||
city: :location, | ||
birthday: :birthdate | ||
}.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 if person | ||
|
||
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 | ||
|
||
begin | ||
ptime_employee = Ptime::Client.new.request(:get, "employees/#{person.ptime_employee_id}") | ||
rescue CustomExceptions::PTimeTemporarilyUnavailableError | ||
return | ||
end | ||
|
||
ptime_employee[:attributes].each do |key, value| | ||
if ATTRIBUTES_MAPPING.key?(key.to_sym) | ||
person[ATTRIBUTES_MAPPING[key.to_sym]] = | ||
value.presence || '-' | ||
end | ||
end | ||
|
||
set_additional_attributes(person, ptime_employee) | ||
|
||
person.save! | ||
set_person_roles(person, ptime_employee) | ||
person | ||
end | ||
# rubocop:enable Metrics | ||
|
||
private | ||
|
||
def set_additional_attributes(person, ptime_employee) | ||
is_employed = ptime_employee[:attributes][:is_employed] | ||
person.company = Company.find_by(name: is_employed ? 'Firma' : 'Ex-Mitarbeiter') | ||
|
||
nationalities = ptime_employee[:attributes][:nationalities] || [] | ||
person.nationality = nationalities[0] | ||
person.nationality2 = nationalities[1] | ||
person.name = append_ptime_employee_name(ptime_employee) | ||
end | ||
|
||
def append_ptime_employee_name(ptime_employee) | ||
"#{ptime_employee[:attributes][:firstname]} #{ptime_employee[:attributes][:lastname]}" | ||
end | ||
|
||
def set_person_roles(person, ptime_employee) | ||
PersonRole.where(person_id: person.id).destroy_all | ||
|
||
ptime_employee[:attributes][:employment_roles].each do |role| | ||
role_id = Role.find_or_create_by(name: sanitized_role_name(role[:name])).id | ||
# role_level_id = map_role_level(role) | ||
PersonRole.create!(person_id: person.id, | ||
role_id: role_id, | ||
percent: role[:percent], | ||
person_role_level_id: PersonRoleLevel.first.id) | ||
end | ||
end | ||
|
||
# Remove prefix of role such as 'T1' or 'M2' which is not needed | ||
def sanitized_role_name(role_name) | ||
role_name.gsub(/\A[A-Z]\d+\s/, '') | ||
end | ||
|
||
# def map_role_level(role) | ||
# PersonRoleLevel.find_by(level: role[role_level]).id | ||
# end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module CustomExceptions | ||
|
||
class PTimeClientError < StandardError; end | ||
class PTimeTemporarilyUnavailableError < StandardError; end | ||
|
||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.