Added Post API for storing LeakTestStatus
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
Some checks failed
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Has been cancelled
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Item;
|
||||
use App\Models\Line;
|
||||
use App\Models\LeakTestReading;
|
||||
use App\Models\Machine;
|
||||
use App\Models\MotorTestingMaster;
|
||||
use App\Models\Plant;
|
||||
@@ -23,9 +23,9 @@ class TestingPanelController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
* Store a newly created readings in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
public function storeReadings(Request $request)
|
||||
{
|
||||
$expectedUser = env('API_AUTH_USER');
|
||||
$expectedPw = env('API_AUTH_PW');
|
||||
@@ -338,6 +338,299 @@ class TestingPanelController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created leak test status in storage.
|
||||
*/
|
||||
public function storeLeakTestStatus(Request $request)
|
||||
{
|
||||
$expectedUser = env('API_AUTH_USER');
|
||||
$expectedPw = env('API_AUTH_PW');
|
||||
|
||||
$header_auth = $request->header('Authorization');
|
||||
$expectedToken = $expectedUser.':'.$expectedPw;
|
||||
|
||||
if ('Bearer '.$expectedToken != $header_auth) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Invalid authorization token!',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$data = $request->all();
|
||||
|
||||
$plantCode = $data['plant_code'] ?? '';
|
||||
$itemCode = $data['item_code'] ?? '';
|
||||
$serialNumber = $data['serial_number'] ?? '';
|
||||
$testStatus = $data['test_status'] ?? 'F';
|
||||
|
||||
if ($plantCode == null || $plantCode == '' || ! $plantCode) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant code can't be empty!",
|
||||
], 400);
|
||||
} elseif (! is_numeric($plantCode)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant code '{$plantCode}' should contain only numeric values!",
|
||||
], 400);
|
||||
} elseif (Str::length($plantCode) < 4 || Str::length($plantCode) > 7) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant code '{$plantCode}' must be between 4 and 7 digits only!",
|
||||
], 400);
|
||||
} elseif (! preg_match('/^[1-9]\d{3,6}$/', $plantCode)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
||||
], 400);
|
||||
}
|
||||
|
||||
if ($itemCode == null || $itemCode == '' || ! $itemCode) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Item code can't be empty!",
|
||||
], 404);
|
||||
} elseif (Str::length($itemCode) < 6) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Item code '{$itemCode}' should contain minimum 6 digits!",
|
||||
], 404);
|
||||
} elseif (! ctype_alnum($itemCode)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Item code '{$itemCode}' should contain only alpha-numeric values!",
|
||||
], 404);
|
||||
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $itemCode)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Item code '{$itemCode}' should not begin with '0'!",
|
||||
], 404);
|
||||
}
|
||||
|
||||
if ($serialNumber == null || $serialNumber == '' || ! $serialNumber) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Serial number can't be empty!",
|
||||
], 404);
|
||||
} elseif (Str::length($serialNumber) < 9) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Serial number '{$serialNumber}' should contain minimum 9 digits!",
|
||||
], 404);
|
||||
} elseif (! ctype_alnum($serialNumber)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Serial number '{$serialNumber}' should contain only alpha-numeric values!",
|
||||
], 404);
|
||||
} elseif (! preg_match('/^[1-9][a-zA-Z0-9]{8,}$/', $serialNumber)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Serial number '{$serialNumber}' should not begin with '0' or letter!",
|
||||
], 404);
|
||||
}
|
||||
|
||||
if ($testStatus == null || $testStatus == '') {
|
||||
$testStatus = 'F';
|
||||
|
||||
} elseif (Str::contains($testStatus, 'P', ignoreCase: true)) {
|
||||
$testStatus = 'P';
|
||||
} elseif (Str::contains($testStatus, 'F', ignoreCase: true)) {
|
||||
$testStatus = 'F';
|
||||
} else {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Leak test status must be 'P' or 'F'!",
|
||||
], 404);
|
||||
}
|
||||
|
||||
$plant = Plant::where('code', $plantCode)->first();
|
||||
if (! $plant) {
|
||||
// return response("Plant not found.", 400)->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant code '{$plantCode}' not found!",
|
||||
], 400);
|
||||
}
|
||||
|
||||
$plantId = $plant->id;
|
||||
|
||||
try {
|
||||
LeakTestReading::create([
|
||||
'plant_id' => $plantId,
|
||||
'item_code' => $itemCode,
|
||||
'serial_number' => $serialNumber,
|
||||
'test_status' => $testStatus,
|
||||
]);
|
||||
|
||||
$existing = LeakTestReading::where('serial_number', $serialNumber)->where('item_code', $itemCode)->where('test_status', $testStatus)->where('plant_id', $plantId)->latest()->first();
|
||||
|
||||
if ($existing) {
|
||||
return response()->json([
|
||||
'status_code' => 'SUCCESS',
|
||||
'status_description' => 'Successfully updated leak test status.',
|
||||
], 200);
|
||||
} else {
|
||||
return response()->json([
|
||||
'status_code' => 'SUCCESS',
|
||||
'status_description' => 'Failed to update leak test status!',
|
||||
], 200);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// return response($e->getMessage(), 500)->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Store leak test status internal server error : '.$e?->getCode(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get leak test status.
|
||||
*/
|
||||
public function getLeakTestStatus(Request $request)
|
||||
{
|
||||
$expectedUser = env('API_AUTH_USER');
|
||||
$expectedPw = env('API_AUTH_PW');
|
||||
$header_auth = $request->header('Authorization');
|
||||
$expectedToken = $expectedUser.':'.$expectedPw;
|
||||
|
||||
// $data = $request->all();
|
||||
if ('Bearer '.$expectedToken != $header_auth) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Invalid authorization token!',
|
||||
], 403);
|
||||
}
|
||||
|
||||
// $plantCode = $request->header('plant-code');
|
||||
// $itemCode = $request->header('item-code');
|
||||
// // $description = $item ? $item->description : '';
|
||||
|
||||
// if ($plantCode == null || $plantCode == '') {
|
||||
// // return response("ERROR: Plant Name can't be empty", 400)
|
||||
// // ->header('Content-Type', 'text/plain');
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => "Plant code can't be empty!",
|
||||
// ], 400);
|
||||
// } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) {
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => 'Invalid plant code found!',
|
||||
// ], 400);
|
||||
// } elseif ($itemCode == null || $itemCode == '' || ! $itemCode) {
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => "Item code can't be empty!",
|
||||
// ], 404);
|
||||
// } elseif (Str::length($itemCode) < 6) {
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => "Item code '{$itemCode}' should contain minimum 6 digits!",
|
||||
// ], 404);
|
||||
// } elseif (! ctype_alnum($itemCode)) {
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => "Item code '{$itemCode}' should contain only alpha-numeric values!",
|
||||
// ], 404);
|
||||
// } elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{5,}$/', $itemCode)) {
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => "Item code '{$itemCode}' should not begin with '0'!",
|
||||
// ], 404);
|
||||
// }
|
||||
|
||||
// $plant = Plant::where('code', $plantCode)->first();
|
||||
|
||||
// if (! $plant) {
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => 'Plant not found!',
|
||||
// ], 400);
|
||||
// }
|
||||
|
||||
// $plantId = $plant->id;
|
||||
|
||||
// $item = Item::where('code', $itemCode)->first();
|
||||
|
||||
// if (! $item) {
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => 'Item code not found in item table!',
|
||||
// ], 404);
|
||||
// }
|
||||
|
||||
// $item = Item::where('plant_id', $plantId)->where('code', $itemCode)->first();
|
||||
|
||||
// if (! $item) {
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => "Item code not found in item table for the plant : '$plant->name'!",
|
||||
// ], 404);
|
||||
// }
|
||||
|
||||
// $itemId = $item->id;
|
||||
|
||||
// $description = $item ? $item->description : '';
|
||||
|
||||
// $category = $item ? $item->category : '';
|
||||
|
||||
// $motorTestingMaster = MotorTestingMaster::where('item_id', $itemId)->first();
|
||||
|
||||
// if (! $motorTestingMaster) {
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => 'Item code not found in motor testing master table!',
|
||||
// ], 404);
|
||||
// }
|
||||
|
||||
// $motorTestingMaster = MotorTestingMaster::where('plant_id', $plantId)->where('item_id', $itemId)->first();
|
||||
|
||||
// if (! $motorTestingMaster) {
|
||||
// return response()->json([
|
||||
// 'status_code' => 'ERROR',
|
||||
// 'status_description' => "Item code not found in motor testing master table for the plant : '$plant->name'!",
|
||||
// ], 404);
|
||||
// }
|
||||
|
||||
// $output = [
|
||||
// 'mot_subassembly_code' => $motorTestingMaster->subassembly_code ?? '',
|
||||
// 'mot_model_name' => ($itemCode == '123456') ? 'SAMPLE TYPE' : $description,
|
||||
// 'mot_non_isi_model' => $motorTestingMaster->isi_model ? '0' : '1',
|
||||
// 'mot_phase' => $motorTestingMaster->phase ?? '',
|
||||
// 'mot_hp' => $motorTestingMaster->hp ?? '',
|
||||
// 'mot_kw' => $motorTestingMaster->kw ?? '',
|
||||
// 'mot_volt' => $motorTestingMaster->volt ?? '',
|
||||
// 'mot_cur' => $motorTestingMaster->current ?? '',
|
||||
// 'mot_rpm' => $motorTestingMaster->rpm ?? '',
|
||||
// 'mot_rate_torque_kg' => $motorTestingMaster->torque ?? '',
|
||||
// 'mot_freq' => $motorTestingMaster->frequency ?? '',
|
||||
// 'mot_conn' => $motorTestingMaster->connection ?? '',
|
||||
// 'mot_ins_res_limit' => $motorTestingMaster->ins_res_limit ?? '',
|
||||
// 'mot_ins_res_type' => $motorTestingMaster->ins_res_type ?? '',
|
||||
// 'mot_category' => $category,
|
||||
// 'mot_routine_test_time' => $motorTestingMaster->routine_test_time ?? '',
|
||||
// 'mot_res_ry_ll' => $motorTestingMaster->res_ry_ll ?? '',
|
||||
// 'mot_res_ry_ul' => $motorTestingMaster->res_ry_ul ?? '',
|
||||
// 'mot_res_yb_ll' => $motorTestingMaster->res_yb_ll ?? '',
|
||||
// 'mot_res_yb_ul' => $motorTestingMaster->res_yb_ul ?? '',
|
||||
// 'mot_res_br_ll' => $motorTestingMaster->res_br_ll ?? '',
|
||||
// 'mot_res_br_ul' => $motorTestingMaster->res_br_ul ?? '',
|
||||
// 'mot_lock_volt_limit' => $motorTestingMaster->lock_volt_limit ?? '',
|
||||
// 'mot_leak_cur_limit' => $motorTestingMaster->leak_cur_limit ?? '',
|
||||
// 'mot_lock_cur_ll' => $motorTestingMaster->lock_cur_ll ?? '',
|
||||
// 'mot_lock_cur_ul' => $motorTestingMaster->lock_cur_ul ?? '',
|
||||
// 'mot_noload_cur_ll' => $motorTestingMaster->noload_cur_ll ?? '',
|
||||
// 'mot_noload_cur_ul' => $motorTestingMaster->noload_cur_ul ?? '',
|
||||
// 'mot_noload_pow_ll' => $motorTestingMaster->noload_pow_ll ?? '',
|
||||
// 'mot_noload_pow_ul' => $motorTestingMaster->noload_pow_ul ?? '',
|
||||
// 'mot_noload_spd_ll' => $motorTestingMaster->noload_spd_ll ?? '',
|
||||
// 'mot_noload_spd_ul' => $motorTestingMaster->noload_spd_ul ?? '',
|
||||
// ];
|
||||
|
||||
// return response()->json($output, 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user