Created
June 6, 2018 18:54
-
-
Save fmundaca/e44f20f68a298f9643890aa3bfe25f11 to your computer and use it in GitHub Desktop.
This file contains 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
attr_accessor :is_manager_evaluator, :user_area, :is_manager_evaluator, | |
:is_performance, :user_area_permissions, :is_admin | |
def initialize(user_id, process_id, survey_id = nil) | |
@user_id = user_id | |
@process_id = process_id | |
@survey_id = survey_id | |
end | |
def perform | |
return process if process.respond_to?(:process_not_found) | |
return user if user.respond_to?(:user_not_found) | |
self.is_admin = false | |
if process.rankmi_module.module_code.eql? 'rkm_03' | |
role_at_process_service = get_user_role.role_at_survey(@survey_id) | |
# si es un proceso de clima, el admin se verifica para la encuesta | |
self.is_admin = !role_at_process_service.role.eql?('manager') | |
if self.is_admin | |
self.user_area_permissions = survey.enterprise_areas | |
else | |
self.user_area_permissions = user.user_area_permissions.where(survey_id: survey.id).all | |
end | |
else | |
role_at_process_service = get_user_role.role_at_process(@process_id) | |
# si es un proceso de clima, el admin se verifica para la encuesta | |
self.is_admin = !role_at_process_service.role.eql?('manager') | |
if self.is_admin | |
# self.is_admin = true | |
self.user_area_permissions = process.enterprise_areas | |
else | |
self.user_area_permissions = user.user_area_permissions.joins(:enterprise_area) | |
.where('enterprise_process_id = ?', process.id) | |
end | |
self.is_performance = process.rankmi_module.module_code.eql? 'rkm_01' | |
end | |
return ImmutableStruct.new(:enterprise_areas).new(enterprise_areas: []) if self.user_area_permissions.empty? | |
ImmutableStruct.new(:enterprise_areas).new(enterprise_areas: self.to_json_areas) | |
rescue => e | |
process_exception(e) | |
end | |
def to_json_areas | |
# RKM-3257 Jonathan Rojas 23-05-18 carga el area de su role evaluador y le da permisos | |
self.user_area = user.enterprise_areas.find_by(enterprise_process_id: process.id) | |
self.user_area_permissions = add_evaluator_area_to_manager | |
enterprise_areas = self.user_area_permissions.map do |permission| | |
area = self.is_admin ? permission : permission.enterprise_area | |
area_hash = { | |
area: area.name, | |
area_id: area.id, | |
segment_type: area.segment_base, | |
type: 'segment', | |
can_create_action_plan: (self.is_admin ? true : permission.can_create_action_plan), | |
can_view_actionplans: (self.is_admin ? true : permission.can_view_actionplans), | |
include_children: (self.is_admin || permission.include_children), # since 13/9/2017 this always should be false #permission.include_children), | |
allow_monitoring: (self.is_admin ? true : permission.allow_monitoring), | |
allow_results: (self.is_admin ? true : permission.allow_results), | |
added_by_evaluator: (self.is_admin ? false : permission.added_by_evaluator ||= false), | |
is_manager_evaluator: self.is_manager_evaluator | |
} | |
area_hash[:flat_children] = area.all_children.map {|a| {area_name: a.name, | |
area_id: a.id, | |
segment_type: a.segment_base}} if self.is_admin || self.is_performance || (!self.is_admin && permission.include_children) | |
area_hash[:children] = area.children_areas.map {|a| {segment_type: a.segment_base, | |
area: a.name, | |
area_id: a.id, | |
childrens_size: a.children_areas.size, | |
childrens_count: a.children_areas.size}} if (self.is_admin || self.is_performance || (!self.is_admin && permission.include_children)) | |
area_hash[:childrens_count] = (!area_hash[:children].nil?) ? area.children_areas.count : 0 | |
area_hash | |
end | |
enterprise_areas | |
end | |
private | |
# RKM-3257 Jonathan Rojas 23-05-18 carga el area de su role evaluador | |
# y le asigna permisos a su role manager solo es posible | |
# si es desempeño, si es manager_evaluador | |
# y si el area de su role evaluador no esta en los permisos del role manager | |
def add_evaluator_area_to_manager | |
return self.user_area_permissions unless self.is_performance | |
manager = check_manager | |
evaluator = manage_evaluators | |
self.is_manager_evaluator = manager.is_manager? && evaluator.is_evaluator | |
return self.user_area_permissions unless self.is_manager_evaluator | |
return self.user_area_permissions unless self.user_area | |
return self.user_area_permissions if self.user_area_permissions.any? {|area| area.enterprise_area_id == self.user_area.id} | |
permission_area = UserAreaPermission.new({ | |
user_id: user.id, | |
enterprise_area_id: self.user_area.id, | |
can_create_action_plan: true, | |
can_view_actionplans: true, | |
added_by_evaluator: true | |
}) | |
self.user_area_permissions << permission_area | |
end | |
def check_manager | |
@check_user_role = ManageUserAccess::CheckUserRole.new(user.id, process.id) | |
@check_user_role.check_is_manager_at_process(process.id) | |
end | |
def manage_evaluators | |
@manage_evaluators ||= CollaboratorsPerformance::ManageEvaluators.new(user.id, process.id) | |
@manage_evaluators.is_evaluator_at_process | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment