Browse Source

Merge pull request 'company management' (#7) from dev-wahyun into staging

Reviewed-on: ibnu/generic-ospro-backend#7
pull/1/head
farhantock 12 months ago
parent
commit
a07b243100
  1. 29
      app/Http/Controllers/AuthController.php
  2. 18
      app/Http/Controllers/CompanyController.php
  3. 45
      app/Http/Controllers/MenuCompanyController.php
  4. 3
      app/Models/Company.php

29
app/Http/Controllers/AuthController.php

@ -11,6 +11,7 @@ use App\Models\Role;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Password; use Illuminate\Support\Facades\Password;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
class AuthController extends Controller class AuthController extends Controller
{ {
public function __construct() public function __construct()
@ -25,8 +26,8 @@ class AuthController extends Controller
$remember = $request->remember; $remember = $request->remember;
$is_mobile = $request->is_mobile; $is_mobile = $request->is_mobile;
if(empty($username) || empty($password)) if (empty($username) || empty($password))
return response()->json(['status'=>'error','message'=>'You must fill all the fields'], 400); return response()->json(['status' => 'error', 'message' => 'You must fill all the fields'], 400);
$usernameCheck = false; $usernameCheck = false;
$passwordCheck = false; $passwordCheck = false;
@ -37,34 +38,38 @@ class AuthController extends Controller
if (User::where('password', md5($password))->exists()) if (User::where('password', md5($password))->exists())
$passwordCheck = true; $passwordCheck = true;
if ($usernameCheck & $passwordCheck){ if ($usernameCheck & $passwordCheck) {
$user = User::where('username', $username)->where('password', md5($password))->first(); $user = User::where('username', $username)->where('password', md5($password))->first();
if($is_mobile){ if ($is_mobile) {
$fcm_token = $request->fcm_token; $fcm_token = $request->fcm_token;
if(!$fcm_token || $fcm_token=="") if (!$fcm_token || $fcm_token == "")
return response()->json(['status'=>'error','message'=>'FCM Token is required'], 400); return response()->json(['status' => 'error', 'message' => 'FCM Token is required'], 400);
$dataUpdateFcm = array( $dataUpdateFcm = array(
"fcm_token"=>$fcm_token "fcm_token" => $fcm_token
); );
$hr = User::find($user->id); $hr = User::find($user->id);
if($hr) if ($hr)
$hr->update($dataUpdateFcm); $hr->update($dataUpdateFcm);
} }
$dataRole = Role::find($user->role_id); $dataRole = Role::find($user->role_id);
$dataHierarchy = $this->getDataHierarchy($user->divisi_id, $user->id);
if($dataRole) if ($dataRole)
$user->role = $dataRole; $user->role = $dataRole;
if (! $token =Auth::login($user)) if ($dataHierarchy)
$user->hierarchy = $dataHierarchy;
if (!$token = Auth::login($user))
return response()->json(['error' => 'Unauthorized'], 401); return response()->json(['error' => 'Unauthorized'], 401);
$ttl = 60; $ttl = 60;
if($remember) if ($remember)
$ttl = 10080; $ttl = 10080;
// todo : change existing md5 hashed function to laravel's originally bcrypt // todo : change existing md5 hashed function to laravel's originally bcrypt
@ -80,7 +85,7 @@ class AuthController extends Controller
'expires_in' => auth()->factory()->getTTL() * $ttl, 'expires_in' => auth()->factory()->getTTL() * $ttl,
), ),
]); ]);
}else { } else {
if (!$usernameCheck && !$passwordCheck) if (!$usernameCheck && !$passwordCheck)
return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201); return response()->json(['code' => 201, 'message' => "username and password doesn't match"], 201);
if (!$passwordCheck) if (!$passwordCheck)

18
app/Http/Controllers/CompanyController.php

@ -3,6 +3,7 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Models\Company; use App\Models\Company;
use App\Models\MenuCompany;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class CompanyController extends Controller class CompanyController extends Controller
@ -10,18 +11,22 @@ class CompanyController extends Controller
public function add(Request $request) public function add(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'name' => 'required', 'company_name' => 'required',
'description' => 'required' 'registration_no' => 'required',
'template_id' => 'required',
'is_active' => 'required'
]); ]);
$data = $request->all(); $data = $request->all();
$data['created_by'] = $this->currentName; $data['created_by'] = $this->currentName;
if(isset($data['base_url']) && $data['base_url'] != NULL) {
$data['base_url'] = json_encode($data['base_url'], true);
}
$result = Company::create($data); $result = Company::create($data);
if ($result) { if ($result) {
return response()->json(['status' => 'success', 'message' => 'add Company successfully!', 'code' => 200], 200); return response()->json(['status' => 'success','data' => $result, 'message' => 'add Company successfully!', 'code' => 200], 200);
} else { } else {
return response()->json(['status' => 'failed', 'message' => 'add data Company failed!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'add data Company failed!', 'code' => 400], 400);
} }
@ -51,6 +56,10 @@ class CompanyController extends Controller
$data = Company::find($id); $data = Company::find($id);
if(isset($data['base_url']) && $data['base_url'] != NULL) {
$data->update(['base_url'=>null]);
$data['base_url'] = json_encode($data['base_url'], true);
}
if ($data) { if ($data) {
$result = $data->update($request->all()); $result = $data->update($request->all());
} else { } else {
@ -71,6 +80,7 @@ class CompanyController extends Controller
$data = Company::find($id); $data = Company::find($id);
if ($data) { if ($data) {
MenuCompany::where('company_id',$id)->delete();
$delete = $data->delete(); $delete = $data->delete();
} else { } else {
return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'data Company not found!', 'code' => 400], 400);

45
app/Http/Controllers/MenuCompanyController.php

@ -9,33 +9,15 @@ use Illuminate\Support\Facades\Log;
class MenuCompanyController extends Controller class MenuCompanyController extends Controller
{ {
// public function add(Request $request)
// {
// $this->validate($request, [
// 'icon' => 'required',
// 'name' => 'string|required',
// 'url' => 'required|unique:m_company_menu,url',
// 'sequence' => 'required|integer',
// ]);
// $data = $request->all();
// $data['created_by'] = $this->currentName;
// $result = MenuCompany::create($data);
// if ($result) {
// return response()->json(['status' => 'success', 'message' => 'add data menu successfully!', 'code' => 200], 200);
// } else {
// return response()->json(['status' => 'failed', 'message' => 'add data menu failed!', 'code' => 400], 400);
// }
// }
public function addMultiple(Request $request) public function addMultiple(Request $request)
{ {
$menus = $request->all(); $menus = $request->all();
if (is_array($menus) && count($menus) > 0) { $data = MenuCompany::where('company_id',$menus[0]['company_id']);
if($data){
$data->delete();
}
if (is_array($menus) && count($menus) > 0 && isset($menus[0]['menu_id'])) {
$countRes = 0; $countRes = 0;
foreach ($menus as $menu) { foreach ($menus as $menu) {
@ -60,12 +42,17 @@ class MenuCompanyController extends Controller
} }
if ($countRes > 0) { if ($countRes > 0) {
return response()->json(['status' => 'success', 'message' => 'add data menu successfully!', 'code' => 200]); return response()->json(['status' => 'success', 'message' => 'Add data menu successfully!', 'code' => 200]);
} else { } else {
return response()->json(['status' => 'failed', 'message' => 'add data menu failed created!', 'code' => 400]); return response()->json(['status' => 'failed', 'message' => 'Add data menu failed created!', 'code' => 500]);
} }
} else { } else {
return response()->json(['status' => 'failed', 'message' => 'add data menu failed created!', 'code' => 400]); if(isset($data))
{
return response()->json(['status' => 'success', 'message' => 'Add data menu successfully!', 'code' => 200]);
}else{
return response()->json(['status' => 'failed', 'message' => 'Add data menu failed created!', 'code' => 500]);
}
} }
} }
@ -120,15 +107,15 @@ class MenuCompanyController extends Controller
if ($data) { if ($data) {
$delete = $data->delete(); $delete = $data->delete();
} else { } else {
return response()->json(['status' => 'failed', 'message' => 'data menu not found!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'Data menu not found!', 'code' => 400], 400);
die(); die();
} }
if ($delete) { if ($delete) {
return response()->json(['status' => 'success', 'message' => 'data menu successfully deleted!', 'code' => 200], 200); return response()->json(['status' => 'success', 'message' => 'Data menu successfully deleted!', 'code' => 200], 200);
} else { } else {
return response()->json(['status' => 'failed', 'message' => 'data menu failed deleted!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'Data menu failed deleted!', 'code' => 400], 400);
} }
} }

3
app/Models/Company.php

@ -31,7 +31,6 @@ class Company extends Model
'created_at', 'created_at',
'created_by', 'created_by',
'updated_at', 'updated_at',
'updated_by', 'updated_by'
'company_id'
]; ];
} }

Loading…
Cancel
Save