public function __invoke(array $input)
 {
     $userId = $input['userId'];
     $startTime = null;
     $endTime = null;
     $withContactDetails = false;
     // make sure that employees are not accessing information for another employee
     if (Auth::isEmployee() && Auth::getId() != $userId) {
         return InvalidPayload::create('You can only access your own shifts');
     }
     if (!empty($input['startTime'])) {
         $startTime = urldecode($input['startTime']);
     }
     if (!empty($input['endTime'])) {
         $endTime = urldecode($input['endTime']);
     }
     $Employee = new Employee();
     $Manager = new Manager();
     // Attempt to load employee
     if ($Employee->load($userId)) {
         $User = $Employee;
         $withContactDetails = isset($_GET['manager']) ? true : false;
     } elseif ($Manager->load($userId)) {
         $User = $Manager;
         $withContactDetails = isset($_GET['employee']) ? true : false;
     } else {
         return ErrorPayload::create('User not found');
     }
     $shifts = $User->shifts($startTime, $endTime, $withContactDetails);
     if (count($shifts)) {
         return SuccessPayload::create($shifts);
     } else {
         return ErrorPayload::create('No shifts found');
     }
 }
 public function __invoke(array $input)
 {
     $employeeId = $input['employeeId'];
     $shiftId = $input['shiftId'];
     // Make sure this employee isn't looking at another employee's information
     if (Auth::isEmployee() && Auth::getId() != $employeeId) {
         return InvalidPayload::create('You can not list coworkers for another employee');
     }
     // Make sure this is a valid employee
     $Employee = new Employee();
     if (!$Employee->load($employeeId)) {
         return ErrorPayload::create('Employee not found');
     }
     // Make sure this is a valid shift
     $Shift = new Shift();
     if (!$Shift->load($shiftId)) {
         return ErrorPayload::create('Shift not found');
     }
     // Make sure that this employee is assigned to this shift
     if ($Shift->employee_id != $employeeId) {
         return ErrorPayload::create('You do not work this shift');
     }
     $coworkers = $Shift->listCoworkers();
     if (count($coworkers)) {
         return SuccessPayload::create($coworkers);
     } else {
         return ErrorPayload::create('No coworkers found for this shift');
     }
 }
 public function __invoke(array $input)
 {
     $role = $input['role'];
     $Auth = new Auth();
     switch ($role) {
         case 'employee':
             $Auth->flagAsEmployee();
             return SuccessPayload::create(['message' => 'Logged in as employee']);
             break;
         case 'manager':
             $Auth->flagAsManager();
             return SuccessPayload::create(['message' => 'Logged in as manager']);
             break;
         default:
             return ErrorPayload::create('Invalid role: must be employee or manager');
             break;
     }
 }
 public function __invoke(array $input)
 {
     $employeeId = $input['employeeId'];
     $startTime = Carbon::now()->startOfYear();
     $endTime = Carbon::now()->endOfYear();
     // Make sure this employee isn't looking at another employee's information
     if (Auth::isEmployee() && Auth::getId() != $employeeId) {
         return InvalidPayload::create('You can not list the summary for another employee');
     }
     if (!empty($input['startTime'])) {
         $startTime = $input['startTime'];
     }
     if (!empty($input['endTime'])) {
         $endTime = $input['endTime'];
     }
     $Employee = new Employee();
     if (!$Employee->load($employeeId)) {
         return ErrorPayload::create('Employee not found');
     }
     // limit filtered results to this employee and within the start/end times
     $filters = [];
     $filters[] = ['key' => 'employee_id', 'value' => $employeeId];
     $filters[] = ['key' => 'start_time', 'operator' => '>=', 'value' => $startTime];
     $filters[] = ['key' => 'end_time', 'operator' => '<=', 'value' => $endTime];
     $summary = [];
     $Shift = new Shift();
     if ($Shifts = $Shift->filter($startTime, $endTime, $filters)) {
         foreach ($Shifts as $Shift) {
             $startTime = Carbon::createFromFormat('Y-m-d H:i:s', $Shift['startTime']);
             $endTime = Carbon::createFromFormat('Y-m-d H:i:s', $Shift['endTime']);
             $startOfWeek = max($startTime->copy()->startOfWeek(), $startTime->copy()->startOfYear());
             $endOfWeek = min($endTime->copy()->endOfWeek(), $endTime->copy()->endOfYear());
             $weekDates = $startOfWeek->format('Y-m-d') . ' to ' . $endOfWeek->format('Y-m-d');
             $diff = $endTime->diff($startTime);
             $timeWorked = $diff->d * 24 + $diff->h + $diff->i / 60;
             $timeWorked = $timeWorked - $Shift['break'];
             $summary[$weekDates] = @$summary[$weekDates] + $timeWorked;
         }
         foreach ($summary as $week => $hours) {
             $summary[$week] = "{$hours} hour" . ($hours == 1 ? '' : 's');
         }
     }
     if (count($summary)) {
         return SuccessPayload::create($summary);
     } else {
         return ErrorPayload::create('No shifts found');
     }
 }
$app = Spark\Application::boot();
$app->setMiddleware(['Relay\\Middleware\\ResponseSender', 'Spark\\Handler\\ExceptionHandler', 'Spark\\Handler\\RouteHandler', 'Spark\\Handler\\ActionHandler']);
$app->addRoutes(function (Spark\Router $r) {
    // helper function just to initalize database
    $r->get('/api/initialize', 'SampleProject\\Domain\\InitializeDatabase');
    // helper function to assign active role (employee or manager)
    $r->get('/api/login/{role}', 'SampleProject\\Domain\\AssignRole');
    // list shifts (handles employee list, manager list, and employee list with managers)
    // to display an employee list with managers, simply append ?manager
    $r->get('/api/shifts/{userId}[/{startTime}[/{endTime}]]', 'SampleProject\\Domain\\ListShifts');
    // list other employees with any overlapping shifts
    $r->get('/api/coworkers/{employeeId}/{shiftId}', 'SampleProject\\Domain\\ListCoworkers');
    // generate a weekly summary of employee's time worked
    $r->get('/api/summary/{employeeId}', 'SampleProject\\Domain\\EmployeeSummary');
    // These routes are only available to managers
    if (Auth::isManager()) {
        // list all shifts by date
        $r->get('/api/shiftsByDate/{startTime}/{endTime}', 'SampleProject\\Domain\\ListShiftsByDate');
        // create new shift (post defined for ease of testing/demonstration)
        $r->put('/api/shift/create/{employeeId}', 'SampleProject\\Domain\\CreateShift');
        $r->post('/api/shift/create/{employeeId}', 'SampleProject\\Domain\\CreateShift');
        // make update times for a shift (post defined for ease of testing/demonstration)
        $r->put('/api/shift/{shiftId}/update', 'SampleProject\\Domain\\UpdateShift');
        $r->post('/api/shift/{shiftId}/update', 'SampleProject\\Domain\\UpdateShift');
        // update employee working a shift (post defined for ease of testing/demonstration)
        $r->put('/api/shift/{shiftId}/assign/{employeeId}', 'SampleProject\\Domain\\AssignShift');
        $r->post('/api/shift/{shiftId}/assign/{employeeId}', 'SampleProject\\Domain\\AssignShift');
        // see employee details
        $r->get('/api/employee/{employeeId}', 'SampleProject\\Domain\\EmployeeDetails');
    }
});
 public function __invoke(array $input)
 {
     $employeeId = $input['employeeId'];
     $managerId = null;
     $break = empty($input['break'] ? 0 : $input['break']);
     $startTime = null;
     $endTime = null;
     // Validate the startTime and endTime variables
     if (empty($input['startTime']) || empty($input['endTime'])) {
         return InvalidPayload::create('Missing required inputs (startTime and endTime are required');
     } else {
         $startTime = Carbon::createFromFormat('Y-m-d H:i:s', urldecode($input['startTime']));
         $endTime = Carbon::createFromFormat('Y-m-d H:i:s', urldecode($input['endTime']));
     }
     // validation for start and end times
     $error_message = null;
     if ($startTime > $endTime) {
         $error_message = "startTime must be before endTime";
     } elseif ($startTime->diffInHours($endTime) > 24) {
         $error_message = "startTime and endTime can not differ by more than 24 hours";
     }
     if ($error_message) {
         return ErrorPayload::create($error_message);
     }
     // validation for employeeId
     $employeeId = null;
     if (!empty($input['employeeId'])) {
         $employeeId = $input['employeeId'];
         // Make sure this is a valid employee
         $Employee = new Employee();
         if (!$Employee->load($employeeId)) {
             return ErrorPayload::create('Employee not found');
         } else {
             // make sure this employee doesn't have any overlapping shifts that would conflict...
             $filters = [];
             $filters[] = ['key' => 'employee_id', 'value' => $employeeId];
             $Shift = new Shift();
             if ($Shifts = $Shift->filter($startTime, $endTime, $filters)) {
                 foreach ($Shifts as $checkShift) {
                     if (count($Shifts)) {
                         return ErrorPayload::create('Creating this shift for this employee would result in overlapping shifts for this employee');
                     }
                 }
             }
         }
     }
     $managerId = null;
     if (!empty($input['managerId'])) {
         $managerId = $input['managerId'];
     } else {
         // NOTE: This would pull the actual managerId in the live application (set self to default if no managerId provided)
         // here we are simply going to assume that it is manager 4 since there isn't a login in this sample project
         $managerId = Auth::getId();
     }
     // Make sure this is a valid employee
     $Manager = new Manager();
     if (!$Manager->load($managerId)) {
         return ErrorPayload::create('Manager not found');
     }
     try {
         Shift::create(['manager_id' => $managerId, 'employee_id' => $employeeId, 'break' => $break, 'start_time' => $startTime, 'end_time' => $endTime]);
         return SuccessPayload::create(['message' => 'Shift created successfully']);
     } catch (\Exception $e) {
         return ErrorPayload::create('There was an error creating this shift');
     }
 }