App-Dochazka-CLI
view release on metacpan or search on metacpan
lib/App/Dochazka/CLI/Commands/Employee.pm view on Meta::CPAN
Set supervisor of an arbitrary employee
EMPLOYEE_SPEC SUPERVISOR _TERM
EMPLOYEE_SPEC SET SUPERVISOR _TERM
=cut
sub set_employee_supervisor {
print "Entering " . __PACKAGE__ . "::set_employee_supervisor\n" if $debug_mode;
my ( $ts, $th ) = @_;
# parse test
return parse_test( $ts, $th ) if $ts eq 'PARSE_TEST';
# get employee object
my $status = determine_employee( $th->{EMPLOYEE_SPEC} );
return $status unless $status->ok;
my $emp = $status->payload;
my $emp_eid = $emp->eid;
# get supervisor employee object
$status = determine_employee( 'EMPL=' . $th->{_TERM} );
return $status unless $status->ok;
my $supervisor = $status->payload;
my $supervisor_eid = $supervisor->eid;
# send the HTTP request
$status = send_req( 'POST', "employee/eid", <<"EOS" );
{ "eid" : $emp_eid, "supervisor" : $supervisor_eid }
EOS
return $status unless $status->ok;
# display the employee profile -> it will include the new supervisor
$emp->reset( $status->payload );
return _display_employee_ok( $emp );
}
=head2 Helper functions
Functions used by multiple handlers
=head3 determine_priv
Given an employee object, return the current priv level of that employee.
If the employee doesn't exist, the return value will be undef.
=cut
sub determine_priv {
my ( $emp ) = @_;
return undef unless ref( $emp ) eq 'App::Dochazka::REST::Model::Employee';
return undef unless $emp->eid and $emp->nick;
# GET priv/eid/:eid
my $status = send_req( 'GET', 'priv/eid/' . $emp->eid );
if ( $status->not_ok ) {
$log->error( "Could not determine priv level of employee -> " . $emp->nick .
"<- because: " . $status->text );
return undef;
}
return $status->payload->{'priv'};
}
=head3 determine_supervisor
Given an employee object, return supervisor employee object.
If no supervisor can be determined, the 'eid' and 'nick' attributes of the
resulting supervisor object will be undefined.
=cut
sub determine_supervisor {
my ( $emp ) = @_;
my $supervisor = App::Dochazka::Common::Model::Employee->spawn();
if ( my $supervisor_eid = $emp->supervisor ) {
my $status = determine_employee( "EMPL=$supervisor_eid" );
if ( $status->ok ) {
$supervisor = $status->payload;
} else {
$log->warn( "Failed to look up supervisor by EID $supervisor_eid; error was " . $status->text );
}
}
return $supervisor;
}
=head3 _set_employee
Function that the handlers are wrappers of
=cut
sub _set_employee {
my %PROPLIST = @_;
my $status;
my $emp_obj;
if ( my $e_spec = $PROPLIST{'emp_spec'} ) {
$status = determine_employee( $e_spec );
return $status unless $status->ok;
$emp_obj = $status->payload;
} elsif ( $emp_obj = $PROPLIST{'emp_obj'} ) {
} else {
die "AAAAAAAAAAAAAHHHHH!";
}
my $eid = $emp_obj->eid;
my $prop = $PROPLIST{'prop'};
my $val = $PROPLIST{'val'};
$val =~ s/['"]//g;
$status = send_req( 'POST', "employee/eid", <<"EOS" );
{ "eid" : $eid, "$prop" : "$val" }
EOS
return rest_error( $status, "Modify employee profile" ) unless $status->ok;
my $message = "Profile of employee " . $emp_obj->nick .
" has been modified ($prop -> $val)\n";
( run in 0.671 second using v1.01-cache-2.11-cpan-5a3173703d6 )