From 522fd9ba940998d1e94797cbc0997375c8b225eb Mon Sep 17 00:00:00 2001 From: wahyun Date: Wed, 31 Jan 2024 10:39:26 +0700 Subject: [PATCH 1/3] update compro endpoint --- app/Http/Controllers/AuthController.php | 207 +++++++++--------- .../Controllers/HumanResourceController.php | 94 +++++++- .../Controllers/ScheduleDemoController.php | 32 +++ app/Models/Company.php | 1 + app/Models/Demo.php | 26 +++ app/Models/HumanResource.php | 1 + routes/web.php | 6 + 7 files changed, 263 insertions(+), 104 deletions(-) create mode 100644 app/Http/Controllers/ScheduleDemoController.php create mode 100644 app/Models/Demo.php diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 78fda02..f650e8d 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -1,103 +1,104 @@ -middleware('auth:api', ['except' => ['login']]); - } - - public function login(Request $request) - { - $username = $request->username; - $password = $request->password; - $remember = $request->remember; - $is_mobile = $request->is_mobile; - - if (empty($username) || empty($password)) - return response()->json(['status' => 'error', 'message' => 'You must fill all the fields'], 400); - - $usernameCheck = false; - $passwordCheck = false; - - if (User::where('username', $username)->exists()) - $usernameCheck = true; - - if (User::where('password', md5($password))->exists()) - $passwordCheck = true; - - if ($usernameCheck & $passwordCheck) { - $user = User::where('username', $username)->where('password', md5($password))->first(); - if ($is_mobile) { - $fcm_token = $request->fcm_token; - - if (!$fcm_token || $fcm_token == "") - return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400); - - $dataUpdateFcm = array( - "fcm_token" => $fcm_token - ); - - $hr = User::find($user->id); - - if ($hr) - $hr->update($dataUpdateFcm); - } - - $dataRole = Role::find($user->role_id); - $dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id); - $configApp = Company::where('id', $user->company_id)->first(); - - if ($configApp) - $user->configApp = $configApp; - - if ($dataRole) - $user->role = $dataRole; - - if ($dataHierarchy) - $user->hierarchy = $dataHierarchy; - - if (!$token = Auth::login($user)) - return response()->json(['error' => 'Unauthorized'], 401); - - $ttl = 60; - if ($remember) - $ttl = 10080; - - // todo : change existing md5 hashed function to laravel's originally bcrypt - /* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */ - /* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */ - - return response()->json([ - 'code' => 200, - 'data' => array( - 'data_user' => $user, - 'access_token' => $token, - 'token_type' => 'bearer', - 'expires_in' => auth()->factory()->getTTL() * $ttl, - ), - ]); - } else { - if (!$usernameCheck && !$passwordCheck) - return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201); - if (!$passwordCheck) - return response()->json(['code' => 201, 'message' => "password doesn't match"], 201); - if (!$usernameCheck) - return response()->json(['code' => 201, 'message' => "username doesn't match"], 201); - } - } -} +middleware('auth:api', ['except' => ['login']]); + } + + public function login(Request $request) + { + $username = $request->username; + $email = $request->email; + $password = $request->password; + $remember = $request->remember; + $is_mobile = $request->is_mobile; + + if (empty($email) && empty($username) || empty($password)) + return response()->json(['status' => 'error', 'message' => 'You must fill all the fields'], 400); + + $usernameCheck = false; + $passwordCheck = false; + + if (User::where('username', $username)->orWhere('email', $email)->exists()) + $usernameCheck = true; + + if (User::where('password', md5($password))->exists()) + $passwordCheck = true; + + if ($usernameCheck & $passwordCheck) { + $user = User::where('username', $username)->where('password', md5($password))->orWhere('email', $email)->first(); + if ($is_mobile) { + $fcm_token = $request->fcm_token; + + if (!$fcm_token || $fcm_token == "") + return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400); + + $dataUpdateFcm = array( + "fcm_token" => $fcm_token + ); + + $hr = User::find($user->id); + + if ($hr) + $hr->update($dataUpdateFcm); + } + + $dataRole = Role::find($user->role_id); + $dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id); + $configApp = Company::where('id', $user->company_id)->first(); + + if ($configApp) + $user->configApp = $configApp; + + if ($dataRole) + $user->role = $dataRole; + + if ($dataHierarchy) + $user->hierarchy = $dataHierarchy; + + if (!$token = Auth::login($user)) + return response()->json(['error' => 'Unauthorized'], 401); + + $ttl = 60; + if ($remember) + $ttl = 10080; + + // todo : change existing md5 hashed function to laravel's originally bcrypt + /* $token = auth()->setTTL($ttl)->attempt(['username' => $username, 'password' => Hash::make($password)]); */ + /* dd(response()->json(['code'=>'200', 'token' => $token, 'ttl' => $ttl])); */ + + return response()->json([ + 'code' => 200, + 'data' => array( + 'data_user' => $user, + 'access_token' => $token, + 'token_type' => 'bearer', + 'expires_in' => auth()->factory()->getTTL() * $ttl, + ), + ]); + } else { + if (!$usernameCheck && !$passwordCheck) + return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201); + if (!$passwordCheck) + return response()->json(['code' => 201, 'message' => "password doesn't match"], 201); + if (!$usernameCheck) + return response()->json(['code' => 201, 'message' => "username doesn't match"], 201); + } + } +} diff --git a/app/Http/Controllers/HumanResourceController.php b/app/Http/Controllers/HumanResourceController.php index 6c78f6c..a10877f 100644 --- a/app/Http/Controllers/HumanResourceController.php +++ b/app/Http/Controllers/HumanResourceController.php @@ -2,12 +2,16 @@ namespace App\Http\Controllers; +use App\Models\User; +use Illuminate\Support\Str; +use App\Models\UserToProyek; use Illuminate\Http\Request; use App\Models\HumanResource; use App\Models\UserToActivity; -use App\Models\UserToProyek; use Illuminate\Support\Facades\Artisan; +const URL_EMAIL = 'https://notifapp.odm-iu.com/service-mail/notif_mail.php'; + class HumanResourceController extends Controller { public function add(Request $request) @@ -35,6 +39,34 @@ class HumanResourceController extends Controller } } + public function add_user(Request $request) + { + $this->validate($request, [ + 'role_id' => 'required', + 'name' => 'required', + 'ktp_number' => 'required|numeric|unique:m_users,ktp_number', + 'employee_type' => 'required', + 'company_id' => 'required', + 'username' => 'required', + 'email' => 'required' + ]); + + $data = $request->all(); + + if (isset($request->password) && $request->password != "") { + $data['password'] = md5($request->password); + } + + $result = HumanResource::create($data); + + if ($result) { + $this->sendEmail($request->email, $data['password']); + return response()->json(['status' => 'success', 'message' => 'Human Resource Pool successfull created', 'code' => 200]); + } else { + return response()->json(['status' => 'failed', 'message' => 'Human Resource Pool failed created', 'code' => 400]); + } + } + public function edit($id) { if (!$id || (int) $id < 0 || $id == "") { @@ -197,4 +229,64 @@ class HumanResourceController extends Controller { Artisan::call('sync:integration-human-resources'); } + + public function sendEmail($email, $hashPassword) + { + $hashed = $hashPassword; + $user = User::select('email', 'name')->where('email', $email)->first(); + + if (!$user) { + return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400); + } else { + $this->reqHttpCurl($email, $hashed, $user->name); + // if (User::where('email', $email)->update(['password'=> md5($hashed)])) + // return response()->json(['status' => 'success', 'code'=>200, 'message' => 'Password already sent to mail'], 200); + + return response()->json(['status' => 'error', 'code'=>400, 'message' => 'e-mail not found '], 400); + } + } + + private function reqHttpCurl($email, $password, $username) + { + $postData = [ + "to" => $email, + "password"=> $password, + "name" => $username, + "from" => "app.integrasia@integrasiautama.com", + "alias_from" => "ADW", + "subject" => "Informasi Akun", + "body" => "informasi-akun-adw" + ]; + + $curl = curl_init(); + + curl_setopt_array($curl, array( + CURLOPT_URL => URL_EMAIL, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => "", + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 30000, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => "POST", + CURLOPT_POSTFIELDS => json_encode($postData), + CURLOPT_HTTPHEADER => array( + // Set here requred headers + "accept: */*", + "accept-language: en-US,en;q=0.8", + "content-type: application/json", + ), + )); + + $response = curl_exec($curl); + $err = curl_error($curl); + + curl_close($curl); + + if ($err) { + echo "cURL Error #:" . $err; + } else { + print_r(json_decode($response)); + } + + } } diff --git a/app/Http/Controllers/ScheduleDemoController.php b/app/Http/Controllers/ScheduleDemoController.php new file mode 100644 index 0000000..f3a5130 --- /dev/null +++ b/app/Http/Controllers/ScheduleDemoController.php @@ -0,0 +1,32 @@ +validate($request, [ + 'name' => 'required', + 'role' => 'required', + 'email' => 'required', + 'number_phone' => 'required', + 'status' => 'required', + 'message' => 'required' + ]); + + $data = $request->all(); + + $data['created_by'] = $this->currentName; + $result = Demo::create($data); + + if ($result) { + return response()->json(['status' => 'success','data' => $result, 'message' => 'add schedule successfully!', 'code' => 200], 200); + } else { + return response()->json(['status' => 'failed', 'message' => 'add data schedule failed!', 'code' => 400], 400); + } + } +} diff --git a/app/Models/Company.php b/app/Models/Company.php index 7593aed..403c27d 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -32,6 +32,7 @@ class Company extends Model 'created_by', 'updated_at', 'updated_by', + 'cluster', 'date_register' ]; } diff --git a/app/Models/Demo.php b/app/Models/Demo.php new file mode 100644 index 0000000..7b1f0a1 --- /dev/null +++ b/app/Models/Demo.php @@ -0,0 +1,26 @@ +get('/', function () use ($router) { $router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($router) { $router->post('/login', 'AuthController@login'); + $router->post('/company-management-user/add', 'CompanyController@add'); + $router->post('/company-management-user/search', 'CompanyController@search'); + $router->post('/human-resource-user/add', 'HumanResourceController@add_user'); + $router->get('/company-management-user/edit/{id}', 'CompanyController@edit'); + $router->post('/schedule-demo/add','ScheduleDemoController@add'); + $router->post('/menu/add', 'MenuController@add'); $router->get('/menu/edit/{id}', 'MenuController@edit'); $router->put('/menu/update/{id}', 'MenuController@update'); From 5e0e4ad0f6c59b679207b72e621450de5b9b891d Mon Sep 17 00:00:00 2001 From: wahyun Date: Wed, 31 Jan 2024 11:25:42 +0700 Subject: [PATCH 2/3] update conflict --- app/Http/Controllers/DemoController.php | 4 ++- .../Controllers/ScheduleDemoController.php | 32 ------------------- routes/web.php | 2 +- 3 files changed, 4 insertions(+), 34 deletions(-) delete mode 100644 app/Http/Controllers/ScheduleDemoController.php diff --git a/app/Http/Controllers/DemoController.php b/app/Http/Controllers/DemoController.php index a0bda60..3e44c29 100644 --- a/app/Http/Controllers/DemoController.php +++ b/app/Http/Controllers/DemoController.php @@ -12,8 +12,10 @@ class DemoController extends Controller $this->validate($request, [ 'name' => 'required', 'role' => 'required', + 'email' => 'required', 'number_phone' => 'required', - 'message' => 'required', + 'status' => 'required', + 'message' => 'required' ]); $data = $request->all(); diff --git a/app/Http/Controllers/ScheduleDemoController.php b/app/Http/Controllers/ScheduleDemoController.php deleted file mode 100644 index f3a5130..0000000 --- a/app/Http/Controllers/ScheduleDemoController.php +++ /dev/null @@ -1,32 +0,0 @@ -validate($request, [ - 'name' => 'required', - 'role' => 'required', - 'email' => 'required', - 'number_phone' => 'required', - 'status' => 'required', - 'message' => 'required' - ]); - - $data = $request->all(); - - $data['created_by'] = $this->currentName; - $result = Demo::create($data); - - if ($result) { - return response()->json(['status' => 'success','data' => $result, 'message' => 'add schedule successfully!', 'code' => 200], 200); - } else { - return response()->json(['status' => 'failed', 'message' => 'add data schedule failed!', 'code' => 400], 400); - } - } -} diff --git a/routes/web.php b/routes/web.php index 31882d2..c40d8e9 100644 --- a/routes/web.php +++ b/routes/web.php @@ -12,7 +12,7 @@ $router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($ro $router->post('/company-management-user/search', 'CompanyController@search'); $router->post('/human-resource-user/add', 'HumanResourceController@add_user'); $router->get('/company-management-user/edit/{id}', 'CompanyController@edit'); - $router->post('/schedule-demo/add','ScheduleDemoController@add'); + $router->post('/demo-management-user/add', 'DemoController@add'); $router->post('/menu/add', 'MenuController@add'); $router->get('/menu/edit/{id}', 'MenuController@edit'); From b5b94e58ba38e3b7714db83adf0264e44751bcba Mon Sep 17 00:00:00 2001 From: wahyun Date: Thu, 1 Feb 2024 11:45:26 +0700 Subject: [PATCH 3/3] finish register & email notif --- .../Controllers/HumanResourceController.php | 32 ++++++++----------- app/Http/Controllers/RoleController.php | 2 +- routes/web.php | 4 ++- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/HumanResourceController.php b/app/Http/Controllers/HumanResourceController.php index a10877f..2ea52d7 100644 --- a/app/Http/Controllers/HumanResourceController.php +++ b/app/Http/Controllers/HumanResourceController.php @@ -19,7 +19,7 @@ class HumanResourceController extends Controller $this->validate($request, [ 'role_id' => 'required', 'name' => 'required', - 'ktp_number' => 'required|numeric|unique:m_users,ktp_number', + 'ktp_number' => 'required|string|unique:m_users,ktp_number', 'employee_type' => 'required', 'company_id' => 'required' ]); @@ -44,7 +44,7 @@ class HumanResourceController extends Controller $this->validate($request, [ 'role_id' => 'required', 'name' => 'required', - 'ktp_number' => 'required|numeric|unique:m_users,ktp_number', + 'ktp_number' => 'required|string|unique:m_users,ktp_number', 'employee_type' => 'required', 'company_id' => 'required', 'username' => 'required', @@ -60,7 +60,7 @@ class HumanResourceController extends Controller $result = HumanResource::create($data); if ($result) { - $this->sendEmail($request->email, $data['password']); + $this->sendEmail($request->email, $request->password); return response()->json(['status' => 'success', 'message' => 'Human Resource Pool successfull created', 'code' => 200]); } else { return response()->json(['status' => 'failed', 'message' => 'Human Resource Pool failed created', 'code' => 400]); @@ -91,7 +91,7 @@ class HumanResourceController extends Controller } $humanresource = HumanResource::find($id); - $request->ktp_number !== $humanresource['ktp_number'] ? $this->validate($request,['ktp_number'=>'unique:m_users,ktp_number|numeric']) : ''; + $request->ktp_number !== $humanresource['ktp_number'] ? $this->validate($request,['ktp_number'=>'unique:m_users,ktp_number|string']) : ''; $request->email !== $humanresource['email'] ? $this->validate($request,['email'=>'unique:m_users,email|email']) : ''; $request->password ? $this->validate($request,['password'=>'required|min:8']) : ''; @@ -230,32 +230,28 @@ class HumanResourceController extends Controller Artisan::call('sync:integration-human-resources'); } - public function sendEmail($email, $hashPassword) + public function sendEmail($email, $password) { - $hashed = $hashPassword; - $user = User::select('email', 'name')->where('email', $email)->first(); - + $user = User::select('email', 'name', 'username')->where('email', $email)->first(); if (!$user) { return response()->json(['status' => 'error', 'message' => 'e-mail not found '], 400); } else { - $this->reqHttpCurl($email, $hashed, $user->name); - // if (User::where('email', $email)->update(['password'=> md5($hashed)])) - // return response()->json(['status' => 'success', 'code'=>200, 'message' => 'Password already sent to mail'], 200); - + $this->reqHttpCurl($email, $password, $user->username, $user->name); return response()->json(['status' => 'error', 'code'=>400, 'message' => 'e-mail not found '], 400); } } - private function reqHttpCurl($email, $password, $username) + private function reqHttpCurl($email, $password, $username, $name) { $postData = [ "to" => $email, - "password"=> $password, - "name" => $username, + "name" => $name, + "username" => $username, + "password" => $password, "from" => "app.integrasia@integrasiautama.com", - "alias_from" => "ADW", - "subject" => "Informasi Akun", - "body" => "informasi-akun-adw" + "alias_from" => "OSPRO", + "subject" => "Registration OSPRO", + "body" => "registration-ospro" ]; $curl = curl_init(); diff --git a/app/Http/Controllers/RoleController.php b/app/Http/Controllers/RoleController.php index 9b1c8b5..371ba5c 100644 --- a/app/Http/Controllers/RoleController.php +++ b/app/Http/Controllers/RoleController.php @@ -28,7 +28,7 @@ class RoleController extends Controller if (!$result) return response()->json(['status' => 'failed', 'message' => 'Failed to add data!', 'code' => 500], 500); - return response()->json(['status' => 'success', 'message' => 'Data added!', 'code' => 200], 200); + return response()->json(['status' => 'success', 'data' => $result, 'message' => 'Data added!', 'code' => 200], 200); } public function edit($id) diff --git a/routes/web.php b/routes/web.php index c40d8e9..69147fb 100644 --- a/routes/web.php +++ b/routes/web.php @@ -6,13 +6,15 @@ $router->get('/', function () use ($router) { $router->group(['prefix' => 'api', 'middleware' => 'cors'], function () use ($router) { $router->post('/login', 'AuthController@login'); + // Landing Route $router->post('/send-email', 'AuthController@sendEmail'); - $router->post('/company-management-user/add', 'CompanyController@add'); $router->post('/company-management-user/search', 'CompanyController@search'); $router->post('/human-resource-user/add', 'HumanResourceController@add_user'); $router->get('/company-management-user/edit/{id}', 'CompanyController@edit'); $router->post('/demo-management-user/add', 'DemoController@add'); + $router->post('/role-user/add', 'RoleController@add'); + $router->post('/menu-company-user/add-multiple', 'MenuCompanyController@addMultiple'); $router->post('/menu/add', 'MenuController@add'); $router->get('/menu/edit/{id}', 'MenuController@edit');