leftJoin('profiles', 'profiles.user_id', 'users.id')->get(); if($query){ $result = $query; $res_status = true; $msg = 'Mendapatkan Data'; $status_msg = $msg; return $this->resSuccess(null, $res_status, $msg, $status_msg, $result); }else { $res_status = false; $msg = 'Data tidak ditemukan'; $status_msg = $msg; return $this->resSuccess(null, $res_status, $msg, $status_msg, null); } } public function getSingle($data_id) { $query = User::select('users.*', 'profiles.*', 'users.id as user_id', 'profiles.id as profile_id', DB::raw('SUM(point_logs.point) as total_point'))->leftJoin('profiles', 'profiles.user_id', 'users.id')->leftJoin('point_logs', 'point_logs.user_id', 'users.id')->where('users.id', Helper::hash($data_id, 'decode'))->get(); if($query){ $result = $query; $res_status = true; $msg = 'Mendapatkan Data'; $status_msg = $msg; return $this->resSuccess(null, $res_status, $msg, $status_msg, $result); }else { $res_status = false; $msg = 'Data tidak ditemukan'; $status_msg = $msg; return $this->resSuccess(null, $res_status, $msg, $status_msg, null); } } public function postLogin(Request $request) { $auth = Auth::attempt($request->only('email', 'password')); if($auth){ $result['user'] = Auth::user(); $res_status = true; $msg = 'Mendapatkan Data'; $status_msg = $msg; return $this->resSuccess(null, $res_status, $msg, $status_msg, $result); }else { $res_status = false; $msg = 'Email atau Password yang anda masukkan salah'; $status_msg = $msg; return $this->resSuccess(null, $res_status, $msg, $status_msg, null); } } public function refferalCode($length) { $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } $check_reff = User::where('refferal_code', '=', $randomString)->first(); if(!empty($check_reff)){ $this->refferalCode(6); exit; } return $randomString; } public function postRegister(Request $request) { $validator = Validator::make($request->all(), [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], 'password' => ['required', 'string', 'min:8', 'confirmed'], ]); $check_refferal = User::where('refferal_code', '=', $request->refferal)->first(); if(!$validator->fails()){ $result = User::create([ 'name' => $request->name, 'email' => $request->email, 'password' => Hash::make($request->password), 'refferal_code' => $this->refferalCode(6), 'gambar' => !empty($request->gambar)?Helper::base64_to_image($request->gambar, 'transportation'):'', ]); $refferal_log = null; if($check_refferal){ $refferal_log = new RefferalLog(); $refferal_log->user_id = $result->id; $refferal_log->user_refferal = $check_refferal->id; // $refferal_log->user_refferal = Helper::hash($check_refferal->id, 'decode'); $refferal_log->type = "refferal"; $refferal_log->save(); $refferal_log->hash_id = Helper::hash($refferal_log->id, 'encode'); $refferal_log->user_id = Helper::hash($refferal_log->user_id, 'encode'); $refferal_log->user_refferal = Helper::hash($refferal_log->user_refferal, 'encode'); $pointLog = new PointLog(); $pointLog->user_id = $result->id; $pointLog->type_point = 'refferal'; $pointLog->point = Points::where('type', 'refferal')->pluck('value')->first(); $pointLog->save(); $pointLog2 = new PointLog(); $pointLog2->user_id = $check_refferal->id; $pointLog2->type_point = 'invitation'; $pointLog2->point = Points::where('type', 'invitation')->pluck('value')->first(); $pointLog2->save(); }else{ $res_status = false; $msg = 'Kode Refferal Tidak Valid'; $status_msg = $validator->errors(); return $this->resSuccess(null, $res_status, $msg, $status_msg, null); } $data_role['role_id'] = 2; //driver role $result->assignRole($data_role); $result->refferal = $refferal_log; $res_status = true; $msg = 'Berhasil membuat user'; $status_msg = $msg; return $this->resSuccess(null, $res_status, $msg, $status_msg, $result); }else { $res_status = false; $msg = 'Gagal Membuat user'; $status_msg = $validator->errors(); return $this->resSuccess(null, $res_status, $msg, $status_msg, null); } } public function postCreateProfile(Request $request) { $validator = Validator::make($request->all(), [ 'user_id' => ['required'], 'fullname' => ['required'], 'phone_number' => ['required'], 'gender' => ['required'], 'is_have_organization' => ['required'], 'organization_id' => [], ]); if(!$validator->fails()){ $result = Profile::create([ 'user_id' => $request->user_id, 'fullname' => $request->fullname, 'phone_number' => $request->phone_number, 'gender' => $request->gender, 'is_have_organization' => $request->is_have_organization, 'organization_id' => !empty($request->organization_id)?$request->organization_id:NULL, ]); $res_status = true; $msg = 'Berhasil membuat user'; $status_msg = $msg; return $this->resSuccess(null, $res_status, $msg, $status_msg, $result); }else { $res_status = false; $msg = 'Gagal Membuat user'; $status_msg = $validator->errors(); return $this->resSuccess(null, $res_status, $msg, $status_msg, null); } } public function postUpdateProfile(Request $request) { $validator = Validator::make($request->all(), [ 'user_id' => ['required'], 'fullname' => ['required'], 'phone_number' => ['required'], 'gender' => ['required'], 'is_have_organization' => ['required'], 'organization_id' => [], ]); if(!$validator->fails()){ $profile = Profile::where('user_id', $request->user_id)->firstOrFail(); $profile->user_id = $request->user_id; $profile->fullname = $request->fullname; $profile->phone_number = $request->phone_number; $profile->gender = $request->gender; $profile->is_have_organization = $request->is_have_organization; $profile->organization_id = !empty($request->organization_id)?$request->organization_id:''; $res_status = true; $msg = 'Berhasil membuat user'; $status_msg = $msg; return $this->resSuccess(null, $res_status, $msg, $status_msg, $result); }else { $res_status = false; $msg = 'Gagal Membuat user'; $status_msg = $validator->errors(); return $this->resSuccess(null, $res_status, $msg, $status_msg, null); } } public function postForgotPassword() { $credentials = request()->validate(['email' => 'required|email']); Password::sendResetLink($credentials); return response()->json(["msg" => 'Reset password link sent on your email id.']); } public function postResetPassword() { $credentials = request()->validate([ 'email' => 'required|email', 'token' => 'required|string', 'password' => 'required|string|confirmed' ]); $reset_password_status = Password::reset($credentials, function ($user, $password) { $user->password = $password; $user->save(); }); if ($reset_password_status == Password::INVALID_TOKEN) { return response()->json(["msg" => "Invalid token provided"], 400); } return response()->json(["msg" => "Password has been successfully changed"]); } public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null) { $response['response'] = array( 'status' => $status, 'message' => $msg, 'status_msg' => $status_msg, ); $response['param'] = !empty($param) ? $param : ''; $response['results'] = $result; return response()->json($response, 200); } }