public function deleteGoogleProfile($id)
 {
     $token = GoogleUser::getTokenById($id);
     GoogleUser::destroy($id);
     Tokens::revokeToken($token);
     return response()->json(['msg' => 'User disconnected'], 200);
 }
 public static function freeBusy($user_id, $start_date, $end_date)
 {
     $tokens = GoogleUser::select('id', 'googleAccessToken')->with(['calendars' => function ($q) {
         $q->select('user_id', 'calendar_id');
     }])->where('user_id', '=', $user_id)->get();
     $free_busy_requests = [];
     foreach ($tokens as $token) {
         $calendars = $token->calendars->pluck('calendar_id');
         $free_busy_requests = array_merge($free_busy_requests, self::freeBusyRequest($token->id, $token->googleAccessToken, $calendars, $start_date, $end_date));
     }
     return $free_busy_requests;
 }
 /**
  * Obtain the user information from Google.
  *
  * @param Request $request
  * @return Socialite ->user()
  */
 public function handleGoogleProviderCallback(Request $request)
 {
     if ($request->get('error') == "access_denied") {
         return redirect('/');
     }
     $authorizationCode = $request->get('code');
     $googleTokens = Tokens::getGoogleTokens($authorizationCode);
     //return var_dump($googleTokens);
     $user = Socialite::driver('google')->getUserByToken($googleTokens->access_token);
     $email = $user['emails'][0]['value'];
     $name = $user['name']['givenName'] . " " . $user['name']['familyName'];
     $avatar = $user['image']['url'];
     if (!GoogleUser::existsByEmailAndId($email, Auth::user()->id) && isset($googleTokens->refresh_token)) {
         GoogleUser::create(['user_id' => Auth::user()->id, 'email' => $email, 'names' => $name, 'avatar' => $avatar, 'googleAccessToken' => $googleTokens->access_token, 'googleRefreshToken' => $googleTokens->refresh_token, 'uriCode' => $authorizationCode, 'expireValue' => $googleTokens->expires_in]);
         return redirect('/availability/google')->with(['message' => 'You have linked this profile (' . $email . ')']);
     } elseif (GoogleUser::existsByEmailAndId($email, Auth::user()->id) && isset($googleTokens->refresh_token)) {
         $id = GoogleUser::where('email', '=', $email)->where('user_id', '=', Auth::user()->id)->select('id')->first()->pluck('id')['id'];
         GoogleUser::updateTokens($id, ['googleAccessToken' => $googleTokens->access_token, 'googleRefreshToken' => $googleTokens->refresh_token, 'uriCode' => $authorizationCode, 'expireValue' => $googleTokens->expires_in]);
         return redirect('/availability/google')->with(['message' => 'You already have this profile (' . $email . '). Data is updated.']);
     } elseif (GoogleUser::existsByEmailAndId($email, Auth::user()->id) && !isset($googleTokens->refresh_token)) {
         return redirect('/availability/google')->with(['message' => 'You already have this profile (' . $email . ')']);
     } elseif (!GoogleUser::existsByEmailAndId($email, Auth::user()->id) && !isset($googleTokens->refresh_token)) {
         return redirect('/availability/google')->withErrors(['Missing refresh token. Contact the administrator.']);
     } else {
         return var_dump($authorizationCode, $googleTokens, $user);
         abort(500, "Auth error. Contact the admin");
     }
     return redirect('/availability/google');
 }