samedi 10 novembre 2018

Add a new attendance record to database and json return user_id null using JWT AUthentication

I have created the attendance record but when i test on POSTMAN, the user_id is null and the scan_in_status is null also.

{
    "success": true,
    "user_id": null,
    "action": "SCAN_IN",
    "scan_in_location": null,
    "date": "2018/11/11",
    "time": "05:51:06",
    "scan_in_status": null
}

i am using the JWT Authentication and when i add, it should be added the user_id because i am using the login account with user_id existed in but it is null. This is my code below

public function store(Request $request){
        //declare variable for taking current date - time
        $input = $request->all();
        $user_id = request('user_id');
        $scan_out_location = request('scan_out_location');
        $work_hour = request('work_hour');
        $work_hour_total = request('work_hour_total');
        $scan_in_location = request('scan_in_location');
        $scan_in_status = request('scan_in_status');
        //check attendance using user_id
        $res = Attendance::select('id')
        ->where('user_id' , '=' , $user_id)
        ->where('attendance_date', '=' , DB::RAW('DATE(NOW())'))
        ->where('user_id' , '!=' , null)
        ->where('scan_in_time', '!=' , null)
        ->where('scan_out_time', '=', null)->get()->count();

        //set status 0 - Absent, 1 - On time, 2 - Late, 3 - very late, 4 - On leave
      $attendance_status = Attendance::where('user_id', '=' , $user_id)
      ->select(DB::RAW("CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00' 
      THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' END"))
      ->value('scan_in_status');

      if($res > 0){
        $res = DB::statement("UPDATE attendances SET scan_out_time = NOW(),
        scan_out_location = ? ,work_hour = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time,lunch_break), 
        work_hour_total = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time) 
        WHERE user_id =  ? AND NOT scan_in_time IS NULL AND 
        scan_out_time IS NULL", [$user_id, $user_id, $scan_out_location, $work_hour, $work_hour_total]);

        return response()->json(
            array(
               'success' => true,
               'user_id' => $user_id,
               'action' => 'SCAN_OUT',
               'scan_out_location' => $scan_out_location,
               'date' => date('Y/m/d'),
               'time' => date('H:i:s')
           )
        );
     }else{
         $res = DB::statement("UPDATE attendances 
         SET scan_in_time = NOW(), scan_in_location = ?, scan_in_status = ?
             WHERE attendance_date = DATE(NOW()) 
             AND user_id = ?", [$scan_in_location, $scan_in_status, $user_id]);

         return response()->json(
           array(
              'success' => true,
              'user_id' => $user_id,
              'action' => 'SCAN_IN',
              'scan_in_location' => $scan_in_location,
              'date' => date('Y/m/d'),
              'time' => date('H:i:s'),
              'scan_in_status' => $scan_in_status
          )
       );
     }

    //  $attendance = new Attendance();
    $input['user_id'] = Auth::user()->user_id;
    $input['attendance_date'] = Carbon::now()->format('Y/m/d');

     if ($this->user->attendance()->save($input))
        return response()->json([
            'success' => true,
            'attendance' => $attendance
        ]);
    else
        return response()->json([
            'success' => false,
            'message' => 'Sorry, check in attendance could not be added'
        ], 500);

 }

Should i change the input to New Attendance or anything else that when i use token of my account to check in, it is recognized with the following user_id and added to database. Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire