Ingen beskrivning
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

UserController.php 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. use App\User;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Http\Request;
  8. use Validator;
  9. use Hash;
  10. use Helper;
  11. use Illuminate\Support\Facades\Password;
  12. use App\Model\Profile;
  13. use App\Model\RefferalLog;
  14. use App\Model\PointLog;
  15. use App\Model\Points;
  16. class UserController extends Controller
  17. {
  18. public function __construct()
  19. {
  20. DB::enableQueryLog();
  21. }
  22. public function getAll()
  23. {
  24. $query = User::select('*', 'users.id as user_id', 'profiles.id as profile_id')->leftJoin('profiles', 'profiles.user_id', 'users.id')->get();
  25. if($query){
  26. $result = $query;
  27. $res_status = true;
  28. $msg = 'Mendapatkan Data';
  29. $status_msg = $msg;
  30. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  31. }else {
  32. $res_status = false;
  33. $msg = 'Data tidak ditemukan';
  34. $status_msg = $msg;
  35. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  36. }
  37. }
  38. public function getSingle($data_id)
  39. {
  40. $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();
  41. if($query){
  42. $result = $query;
  43. $res_status = true;
  44. $msg = 'Mendapatkan Data';
  45. $status_msg = $msg;
  46. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  47. }else {
  48. $res_status = false;
  49. $msg = 'Data tidak ditemukan';
  50. $status_msg = $msg;
  51. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  52. }
  53. }
  54. public function postLogin(Request $request)
  55. {
  56. $auth = Auth::attempt($request->only('email', 'password'));
  57. if($auth){
  58. $result['user'] = Auth::user();
  59. $res_status = true;
  60. $msg = 'Mendapatkan Data';
  61. $status_msg = $msg;
  62. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  63. }else {
  64. $res_status = false;
  65. $msg = 'Email atau Password yang anda masukkan salah';
  66. $status_msg = $msg;
  67. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  68. }
  69. }
  70. public function refferalCode($length) {
  71. $characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  72. $charactersLength = strlen($characters);
  73. $randomString = '';
  74. for ($i = 0; $i < $length; $i++) {
  75. $randomString .= $characters[rand(0, $charactersLength - 1)];
  76. }
  77. $check_reff = User::where('refferal_code', '=', $randomString)->first();
  78. if(!empty($check_reff)){
  79. $this->refferalCode(6);
  80. exit;
  81. }
  82. return $randomString;
  83. }
  84. public function postRegister(Request $request)
  85. {
  86. $validator = Validator::make($request->all(), [
  87. 'name' => ['required', 'string', 'max:255'],
  88. 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
  89. 'password' => ['required', 'string', 'min:8', 'confirmed'],
  90. ]);
  91. $check_refferal = User::where('refferal_code', '=', $request->refferal)->first();
  92. if(!$validator->fails()){
  93. $result = User::create([
  94. 'name' => $request->name,
  95. 'email' => $request->email,
  96. 'password' => Hash::make($request->password),
  97. 'refferal_code' => $this->refferalCode(6),
  98. 'gambar' => !empty($request->gambar)?Helper::base64_to_image($request->gambar, 'transportation'):'',
  99. ]);
  100. $refferal_log = null;
  101. if($check_refferal){
  102. $refferal_log = new RefferalLog();
  103. $refferal_log->user_id = $result->id;
  104. $refferal_log->user_refferal = $check_refferal->id;
  105. // $refferal_log->user_refferal = Helper::hash($check_refferal->id, 'decode');
  106. $refferal_log->type = "refferal";
  107. $refferal_log->save();
  108. $refferal_log->hash_id = Helper::hash($refferal_log->id, 'encode');
  109. $refferal_log->user_id = Helper::hash($refferal_log->user_id, 'encode');
  110. $refferal_log->user_refferal = Helper::hash($refferal_log->user_refferal, 'encode');
  111. $pointLog = new PointLog();
  112. $pointLog->user_id = $result->id;
  113. $pointLog->type_point = 'refferal';
  114. $pointLog->point = Points::where('type', 'refferal')->pluck('value')->first();
  115. $pointLog->save();
  116. $pointLog2 = new PointLog();
  117. $pointLog2->user_id = $check_refferal->id;
  118. $pointLog2->type_point = 'invitation';
  119. $pointLog2->point = Points::where('type', 'invitation')->pluck('value')->first();
  120. $pointLog2->save();
  121. }else{
  122. $res_status = false;
  123. $msg = 'Kode Refferal Tidak Valid';
  124. $status_msg = $validator->errors();
  125. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  126. }
  127. $data_role['role_id'] = 2; //driver role
  128. $result->assignRole($data_role);
  129. $result->refferal = $refferal_log;
  130. $res_status = true;
  131. $msg = 'Berhasil membuat user';
  132. $status_msg = $msg;
  133. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  134. }else {
  135. $res_status = false;
  136. $msg = 'Gagal Membuat user';
  137. $status_msg = $validator->errors();
  138. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  139. }
  140. }
  141. public function postCreateProfile(Request $request)
  142. {
  143. $validator = Validator::make($request->all(), [
  144. 'user_id' => ['required'],
  145. 'fullname' => ['required'],
  146. 'phone_number' => ['required'],
  147. 'gender' => ['required'],
  148. 'is_have_organization' => ['required'],
  149. 'organization_id' => [],
  150. ]);
  151. if(!$validator->fails()){
  152. $result = Profile::create([
  153. 'user_id' => $request->user_id,
  154. 'fullname' => $request->fullname,
  155. 'phone_number' => $request->phone_number,
  156. 'gender' => $request->gender,
  157. 'is_have_organization' => $request->is_have_organization,
  158. 'organization_id' => !empty($request->organization_id)?$request->organization_id:NULL,
  159. ]);
  160. $res_status = true;
  161. $msg = 'Berhasil membuat user';
  162. $status_msg = $msg;
  163. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  164. }else {
  165. $res_status = false;
  166. $msg = 'Gagal Membuat user';
  167. $status_msg = $validator->errors();
  168. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  169. }
  170. }
  171. public function postUpdateProfile(Request $request)
  172. {
  173. $validator = Validator::make($request->all(), [
  174. 'user_id' => ['required'],
  175. 'fullname' => ['required'],
  176. 'phone_number' => ['required'],
  177. 'gender' => ['required'],
  178. 'is_have_organization' => ['required'],
  179. 'organization_id' => [],
  180. ]);
  181. if(!$validator->fails()){
  182. $profile = Profile::where('user_id', $request->user_id)->firstOrFail();
  183. $profile->user_id = $request->user_id;
  184. $profile->fullname = $request->fullname;
  185. $profile->phone_number = $request->phone_number;
  186. $profile->gender = $request->gender;
  187. $profile->is_have_organization = $request->is_have_organization;
  188. $profile->organization_id = !empty($request->organization_id)?$request->organization_id:'';
  189. $res_status = true;
  190. $msg = 'Berhasil membuat user';
  191. $status_msg = $msg;
  192. return $this->resSuccess(null, $res_status, $msg, $status_msg, $result);
  193. }else {
  194. $res_status = false;
  195. $msg = 'Gagal Membuat user';
  196. $status_msg = $validator->errors();
  197. return $this->resSuccess(null, $res_status, $msg, $status_msg, null);
  198. }
  199. }
  200. public function postForgotPassword() {
  201. $credentials = request()->validate(['email' => 'required|email']);
  202. Password::sendResetLink($credentials);
  203. return response()->json(["msg" => 'Reset password link sent on your email id.']);
  204. }
  205. public function postResetPassword() {
  206. $credentials = request()->validate([
  207. 'email' => 'required|email',
  208. 'token' => 'required|string',
  209. 'password' => 'required|string|confirmed'
  210. ]);
  211. $reset_password_status = Password::reset($credentials, function ($user, $password) {
  212. $user->password = $password;
  213. $user->save();
  214. });
  215. if ($reset_password_status == Password::INVALID_TOKEN) {
  216. return response()->json(["msg" => "Invalid token provided"], 400);
  217. }
  218. return response()->json(["msg" => "Password has been successfully changed"]);
  219. }
  220. public function resSuccess($param = null, $status = null, $msg = null, $status_msg = null, $result = null)
  221. {
  222. $response['response'] = array(
  223. 'status' => $status,
  224. 'message' => $msg,
  225. 'status_msg' => $status_msg,
  226. );
  227. $response['param'] = !empty($param) ? $param : '';
  228. $response['results'] = $result;
  229. return response()->json($response, 200);
  230. }
  231. }