public function __invoke(array $input)
 {
     $employeeId = $input['employeeId'];
     $shiftId = $input['shiftId'];
     // 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');
     }
     if ($Shift->employee_id == $employeeId) {
         return SuccessPayload::create(['message' => 'Employee already assigned to this shift']);
     } else {
         // make sure this employee doesn't have any overlapping shifts that would conflict...
         $filters = [];
         $filters[] = ['key' => 'employee_id', 'value' => $employeeId];
         if ($Shifts = $Shift->filter($Shift->start_time, $Shift->end_time, $filters)) {
             if (count($Shifts)) {
                 return ErrorPayload::create('Assiging the employee to this shift would result in overlapping shifts for this employee');
             }
         }
         try {
             $Shift->update(['employee_id' => $employeeId]);
             return SuccessPayload::create(['message' => 'Shift updated successfully']);
         } catch (\Exception $e) {
             return ErrorPayload::create('There was an error updating this shift');
         }
     }
 }
 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)
 {
     $startTime = urldecode($input['startTime']);
     $endTime = urldecode($input['endTime']);
     $Shift = new Shift();
     $shifts = $Shift->filter($startTime, $endTime);
     if (count($shifts)) {
         return SuccessPayload::create($shifts);
     } else {
         return ErrorPayload::create('No shifts found');
     }
 }
 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');
     }
 }
 public function __invoke(array $input)
 {
     // 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 {
         try {
             $startTime = Carbon::createFromFormat('Y-m-d H:i:s', urldecode($input['startTime']));
             $endTime = Carbon::createFromFormat('Y-m-d H:i:s', urldecode($input['endTime']));
         } catch (\Exception $e) {
             return InvalidPayload::create('Invalid date format: startTime and endTime must be in "Y-m-d H:i:s" format');
         }
     }
     $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);
     }
     // Load shift
     $shiftId = $input['shiftId'];
     $Shift = new Shift();
     if (!$Shift->load($shiftId)) {
         return ErrorPayload::create('Shift not found');
     }
     $update_array = ['start_time' => $startTime->format("Y-m-d H:i:s"), 'end_time' => $endTime->format("Y-m-d H:i:s")];
     try {
         $Shift->update($update_array);
         return SuccessPayload::create(['message' => 'Shift updated successfully']);
     } catch (\Exception $e) {
         return ErrorPayload::create('There was an error updating this shift');
     }
 }
 public function __invoke(array $input)
 {
     // down
     DatabaseManager::schema()->dropIfExists('users');
     DatabaseManager::schema()->dropIfExists('shifts');
     // up
     DatabaseManager::schema()->create('users', function ($table) {
         $table->increments('id');
         $table->string('name');
         $table->enum('role', array('employee', 'manager'));
         $table->string('email');
         $table->string('phone');
         $table->timestamps();
     });
     // up
     DatabaseManager::schema()->create('shifts', function ($table) {
         $table->increments('id');
         $table->unsignedInteger('manager_id')->nullable();
         $table->foreign('manager_id')->references('id')->on('users')->onDelete('set null');
         $table->unsignedInteger('employee_id')->nullable();
         $table->foreign('employee_id')->references('id')->on('users')->onDelete('set null');
         $table->decimal('break', 5, 2);
         $table->dateTime('start_time');
         $table->dateTime('end_time');
         $table->timestamps();
     });
     // seed database
     // create employees
     $Employees = [];
     $Employees[] = Employee::create(['name' => 'John Doe', 'email' => '*****@*****.**', 'phone' => '123-123-1234']);
     $Employees[] = Employee::create(['name' => 'Bob Jones', 'email' => '*****@*****.**', 'phone' => '234-234-1234']);
     $Employees[] = Employee::create(['name' => 'Jill Thompson', 'email' => '*****@*****.**', 'phone' => '345-345-1234']);
     // create managers
     $Managers = [];
     $Managers[] = Manager::create(['name' => 'Nathan Mickler', 'email' => '*****@*****.**', 'phone' => '456-456-4567']);
     $Managers[] = Manager::create(['name' => 'Elizabeth Jackson', 'email' => '*****@*****.**', 'phone' => '567-567-4567']);
     // create four overlapping shifts
     $Date = Carbon::createFromFormat('Y-m-d H:i:s', '2015-09-05 00:00:00');
     $Shift = Shift::create(['break' => 1, 'start_time' => $Date->copy()->addHours(8)->format('Y-m-d H:i:s'), 'end_time' => $Date->copy()->addHours(17)->format('Y-m-d H:i:s')]);
     // assign manager
     $Shift->assign($Managers[0]);
     $Shift->assign($Employees[0]);
     // shift #2
     $Date = Carbon::createFromFormat('Y-m-d H:i:s', '2015-09-05 00:00:00');
     $Shift = Shift::create(['break' => 1, 'start_time' => $Date->copy()->addHours(5)->format('Y-m-d H:i:s'), 'end_time' => $Date->copy()->addHours(9)->format('Y-m-d H:i:s')]);
     // assign manager
     $Shift->assign($Managers[0]);
     $Shift->assign($Employees[1]);
     // shift #3
     $Date = Carbon::createFromFormat('Y-m-d H:i:s', '2015-09-05 00:00:00');
     $Shift = Shift::create(['break' => 1, 'start_time' => $Date->copy()->addHours(12)->format('Y-m-d H:i:s'), 'end_time' => $Date->copy()->addHours(19)->format('Y-m-d H:i:s')]);
     // assign manager
     $Shift->assign($Managers[0]);
     $Shift->assign($Employees[1]);
     // shift #4
     $Date = Carbon::createFromFormat('Y-m-d H:i:s', '2015-09-05 00:00:00');
     $Shift = Shift::create(['break' => 1, 'start_time' => $Date->copy()->addHours(10)->format('Y-m-d H:i:s'), 'end_time' => $Date->copy()->addHours(15)->format('Y-m-d H:i:s')]);
     // assign manager
     $Shift->assign($Managers[0]);
     $Shift->assign($Employees[2]);
     // create random shifts (with random employees and managers)
     $Date = Carbon::createFromFormat('Y-m-d H:i:s', '2015-09-07 00:00:00');
     for ($days = 0; $days < 28; $days++) {
         $Shift = Shift::create(['break' => 1, 'start_time' => $Date->copy()->addDays($days)->addHours(8)->format('Y-m-d H:i:s'), 'end_time' => $Date->copy()->addDays($days)->addHours(17)->format('Y-m-d H:i:s')]);
         // assign manager
         $Shift->assign($Managers[rand(0, 1)]);
         $Shift->assign($Employees[rand(0, 2)]);
     }
     return SuccessPayload::create(['message' => 'Database Initialized']);
 }
 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');
     }
 }