diff --git a/app/Helper/Frontend/ApiUtilities.php b/app/Helper/Frontend/ApiUtilities.php index cc7101c..9055dc3 100644 --- a/app/Helper/Frontend/ApiUtilities.php +++ b/app/Helper/Frontend/ApiUtilities.php @@ -9,6 +9,9 @@ use Illuminate\Http\Request; * Note: * if any of frontend component need specific api query / data specific. * Please add function to get data here. + * + * But, if the parent component still needs to pass the url to the + * child component, the url api can be placed in the app/Http/Controllers/api/superadmin/GeneralController */ class ApiUtilities { diff --git a/app/Http/Controllers/api/superadmin/GeneralController.php b/app/Http/Controllers/api/superadmin/GeneralController.php new file mode 100644 index 0000000..24bfd28 --- /dev/null +++ b/app/Http/Controllers/api/superadmin/GeneralController.php @@ -0,0 +1,16 @@ +validate(['search' => 'nullable|string']); + $tvs = Tv::multiSearch($request->search, ['code'])->orderBy('code', 'asc')->limit(10)->get(); + return JSONResponse::Success(['tvs' => $tvs ]); + } +} diff --git a/app/Models/NewTvRequest.php b/app/Models/NewTvRequest.php index cfce1be..775338a 100644 --- a/app/Models/NewTvRequest.php +++ b/app/Models/NewTvRequest.php @@ -126,68 +126,83 @@ class NewTvRequest extends Model { public static function approveFromRequest(Request $request) { $request->validate([ 'id' => 'required|integer|exists:App\Models\NewTvRequest', - 'tv' => 'required|array', - ]); - - $tvRequest = new Request($request->tv); - if($tvRequest->code) $tvRequest->merge(['code' => strtoupper($tvRequest->code)]); - - $tvRequest->validate([ - 'company_name' => 'nullable|string', - 'address' => 'nullable|string', - 'street_address' => 'nullable|string', - 'col1' => 'nullable|string', - 'col2' => 'nullable|string', - 'col3' => 'nullable|string', - 'col4' => 'nullable|string', - 'col5' => 'nullable|string', - 'col6' => 'nullable|string', - 'col7' => 'nullable|string', - 'col8' => 'nullable|string', - 'col9' => 'nullable|string', - 'col10' => 'nullable|string', - 'notes' => 'nullable|string', + 'tv' => 'nullable|array', + 'existingTv' => 'nullable|array', + 'action' => 'required|string', ]); $newTvReq = NewTvRequest::addColumnCanApprove() - ->findOrFail($request->id); + ->findOrFail($request->id); if(!$newTvReq->can_approve) throw new \Exception('Cannot approve current request'); try { - DB::beginTransaction(); - $tv = new Tv(); - $tv->code = TV::generateUniqueCode(); - $tv->company_name = $tvRequest->company_name; - $tv->address = $tvRequest->address; - $tv->street_address = $tvRequest->street_address; - $tv->col1 = $tvRequest->col1; - $tv->col2 = $tvRequest->col2; - $tv->col3 = $tvRequest->col3; - $tv->col4 = $tvRequest->col4; - $tv->col5 = $tvRequest->col5; - $tv->col6 = $tvRequest->col6; - $tv->col7 = $tvRequest->col7; - $tv->col8 = $tvRequest->col8; - $tv->col9 = $tvRequest->col9; - $tv->col10 = $tvRequest->col10; - $tv->notes = $tvRequest->notes; - $tv->device_info = $newTvReq->device_info; - $tv->installed_at = now(); - $tv->save(); - TVLog::historyCreate($request->user(), $tv->id, $tv); + $tv = null; + if($request->action == 'existing') { + $existingTvRequest = new Request($request->existingTv); + $existingTvRequest->validate([ + 'id' => 'required|integer|exists:App\Models\Tv', + ]); + + $tv = TV::findOrFail($existingTvRequest->id); + } else if ($request->action == 'new') { + $tvRequest = new Request($request->tv); + if($tvRequest->code) $tvRequest->merge(['code' => strtoupper($tvRequest->code)]); + + $tvRequest->validate([ + 'company_name' => 'nullable|string', + 'address' => 'nullable|string', + 'street_address' => 'nullable|string', + 'col1' => 'nullable|string', + 'col2' => 'nullable|string', + 'col3' => 'nullable|string', + 'col4' => 'nullable|string', + 'col5' => 'nullable|string', + 'col6' => 'nullable|string', + 'col7' => 'nullable|string', + 'col8' => 'nullable|string', + 'col9' => 'nullable|string', + 'col10' => 'nullable|string', + 'notes' => 'nullable|string', + ]); + + DB::beginTransaction(); + $tv = new Tv(); + $tv->code = TV::generateUniqueCode(); + $tv->company_name = $tvRequest->company_name; + $tv->address = $tvRequest->address; + $tv->street_address = $tvRequest->street_address; + $tv->col1 = $tvRequest->col1; + $tv->col2 = $tvRequest->col2; + $tv->col3 = $tvRequest->col3; + $tv->col4 = $tvRequest->col4; + $tv->col5 = $tvRequest->col5; + $tv->col6 = $tvRequest->col6; + $tv->col7 = $tvRequest->col7; + $tv->col8 = $tvRequest->col8; + $tv->col9 = $tvRequest->col9; + $tv->col10 = $tvRequest->col10; + $tv->notes = $tvRequest->notes; + $tv->device_info = $newTvReq->device_info; + $tv->installed_at = now(); + $tv->save(); + TVLog::historyCreate($request->user(), $tv->id, $tv); + + // TODO: waiting execution until update from ops + // NEED TO REFACTOR (cause by add existing tv code) + // // try to sys_to_sys with indokargo + // DB::commit(); + // $jsonResponse = Indokargo::createTVAddress($tvRequest, $tv->id); + // return $jsonResponse; + } else { + throw new \Exception('INVALID FORM ACTION'); + } $newTvReq->approved_at = now(); $newTvReq->tv_fk = $tv->id; $newTvReq->save(); DB::commit(); - return JSONResponse::Success(['message'=>'Success to update tv data']); - - // TODO: waiting execution until update from ops - // // try to sys_to_sys with indokargo - // DB::commit(); - // $jsonResponse = Indokargo::createTVAddress($tvRequest, $tv->id); - // return $jsonResponse; + return JSONResponse::Success(['message'=>'Success to approve new tv request']); } catch(\Throwable $th) { DB::rollback(); throw $th; diff --git a/routes/api/superadmin.php b/routes/api/superadmin.php index 4421764..0dd9d48 100644 --- a/routes/api/superadmin.php +++ b/routes/api/superadmin.php @@ -1,12 +1,17 @@ group(function() { + Route::post('/general/tv/search', 'tvSearch'); +}); + Route::controller(VideoUploadController::class)->group(function() { Route::post('/video-upload', 'init'); Route::post('/video-upload/save', 'save'); @@ -42,5 +47,6 @@ Route::controller(NewTvRequestController::class)->group(function() { Route::post('/tv/new-tv-request', 'init'); Route::post('/tv/new-tv-request/approve', 'approve'); Route::post('/tv/new-tv-request/reject', 'reject'); + Route::post('/tv/new-tv-request/tv/search', 'tvSearch'); }); ?> \ No newline at end of file