feat: api for new tv request > approve existing tv

v2.0.0
ricky rx 1 year ago
parent 2ed57016bc
commit e2ab453d66

@ -9,6 +9,9 @@ use Illuminate\Http\Request;
* Note: * Note:
* if any of frontend component need specific api query / data specific. * if any of frontend component need specific api query / data specific.
* Please add function to get data here. * 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 { class ApiUtilities {

@ -0,0 +1,16 @@
<?php
namespace App\Http\Controllers\api\superadmin;
use App\Helper\JSONResponse;
use App\Http\Controllers\Controller;
use App\Models\Tv;
use Illuminate\Http\Request;
class GeneralController extends Controller {
public function tvSearch(Request $request) {
$request->validate(['search' => 'nullable|string']);
$tvs = Tv::multiSearch($request->search, ['code'])->orderBy('code', 'asc')->limit(10)->get();
return JSONResponse::Success(['tvs' => $tvs ]);
}
}

@ -126,68 +126,83 @@ class NewTvRequest extends Model {
public static function approveFromRequest(Request $request) { public static function approveFromRequest(Request $request) {
$request->validate([ $request->validate([
'id' => 'required|integer|exists:App\Models\NewTvRequest', 'id' => 'required|integer|exists:App\Models\NewTvRequest',
'tv' => 'required|array', 'tv' => 'nullable|array',
]); 'existingTv' => 'nullable|array',
'action' => 'required|string',
$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',
]); ]);
$newTvReq = NewTvRequest::addColumnCanApprove() $newTvReq = NewTvRequest::addColumnCanApprove()
->findOrFail($request->id); ->findOrFail($request->id);
if(!$newTvReq->can_approve) throw new \Exception('Cannot approve current request'); if(!$newTvReq->can_approve) throw new \Exception('Cannot approve current request');
try { try {
DB::beginTransaction(); $tv = null;
$tv = new Tv(); if($request->action == 'existing') {
$tv->code = TV::generateUniqueCode(); $existingTvRequest = new Request($request->existingTv);
$tv->company_name = $tvRequest->company_name; $existingTvRequest->validate([
$tv->address = $tvRequest->address; 'id' => 'required|integer|exists:App\Models\Tv',
$tv->street_address = $tvRequest->street_address; ]);
$tv->col1 = $tvRequest->col1;
$tv->col2 = $tvRequest->col2; $tv = TV::findOrFail($existingTvRequest->id);
$tv->col3 = $tvRequest->col3; } else if ($request->action == 'new') {
$tv->col4 = $tvRequest->col4; $tvRequest = new Request($request->tv);
$tv->col5 = $tvRequest->col5; if($tvRequest->code) $tvRequest->merge(['code' => strtoupper($tvRequest->code)]);
$tv->col6 = $tvRequest->col6;
$tv->col7 = $tvRequest->col7; $tvRequest->validate([
$tv->col8 = $tvRequest->col8; 'company_name' => 'nullable|string',
$tv->col9 = $tvRequest->col9; 'address' => 'nullable|string',
$tv->col10 = $tvRequest->col10; 'street_address' => 'nullable|string',
$tv->notes = $tvRequest->notes; 'col1' => 'nullable|string',
$tv->device_info = $newTvReq->device_info; 'col2' => 'nullable|string',
$tv->installed_at = now(); 'col3' => 'nullable|string',
$tv->save(); 'col4' => 'nullable|string',
TVLog::historyCreate($request->user(), $tv->id, $tv); '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->approved_at = now();
$newTvReq->tv_fk = $tv->id; $newTvReq->tv_fk = $tv->id;
$newTvReq->save(); $newTvReq->save();
DB::commit(); DB::commit();
return JSONResponse::Success(['message'=>'Success to update tv data']); return JSONResponse::Success(['message'=>'Success to approve new tv request']);
// TODO: waiting execution until update from ops
// // try to sys_to_sys with indokargo
// DB::commit();
// $jsonResponse = Indokargo::createTVAddress($tvRequest, $tv->id);
// return $jsonResponse;
} catch(\Throwable $th) { } catch(\Throwable $th) {
DB::rollback(); DB::rollback();
throw $th; throw $th;

@ -1,12 +1,17 @@
<?php <?php
use App\Http\Controllers\api\superadmin\ApkUploadController; use App\Http\Controllers\api\superadmin\ApkUploadController;
use App\Http\Controllers\api\superadmin\GeneralController;
use App\Http\Controllers\api\superadmin\tv\NewTvRequestController; use App\Http\Controllers\api\superadmin\tv\NewTvRequestController;
use App\Http\Controllers\api\superadmin\tv\TvController; use App\Http\Controllers\api\superadmin\tv\TvController;
use App\Http\Controllers\api\superadmin\UserManagementController; use App\Http\Controllers\api\superadmin\UserManagementController;
use App\Http\Controllers\api\superadmin\VideoUploadController; use App\Http\Controllers\api\superadmin\VideoUploadController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
Route::controller(GeneralController::class)->group(function() {
Route::post('/general/tv/search', 'tvSearch');
});
Route::controller(VideoUploadController::class)->group(function() { Route::controller(VideoUploadController::class)->group(function() {
Route::post('/video-upload', 'init'); Route::post('/video-upload', 'init');
Route::post('/video-upload/save', 'save'); 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', 'init');
Route::post('/tv/new-tv-request/approve', 'approve'); Route::post('/tv/new-tv-request/approve', 'approve');
Route::post('/tv/new-tv-request/reject', 'reject'); Route::post('/tv/new-tv-request/reject', 'reject');
Route::post('/tv/new-tv-request/tv/search', 'tvSearch');
}); });
?> ?>
Loading…
Cancel
Save