Updated validation functionality and failure messages in controller files
This commit is contained in:
@@ -29,16 +29,16 @@ class ObdController extends Controller
|
||||
$header_auth = $request->header('Authorization');
|
||||
$expectedToken = $expectedUser . ':' . $expectedPw;
|
||||
|
||||
// if("Bearer " . $expectedToken !== $header_auth)
|
||||
// if("Bearer " . $expectedToken != $header_auth)
|
||||
// {
|
||||
// return response("ERROR: Unauthorized", 403)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
// }
|
||||
if ("Bearer " . $expectedToken !== $header_auth)
|
||||
if ("Bearer " . $expectedToken != $header_auth)
|
||||
{
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Invalid authorization token'
|
||||
'status_description' => 'Invalid authorization token!'
|
||||
], 403);
|
||||
}
|
||||
|
||||
@@ -69,18 +69,18 @@ class ObdController extends Controller
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD Number should contain minimum 8 digits: '$obdNumber'"
|
||||
'status_description' => "OBD number should contain minimum 8 digit alpha-numeric values only!"
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Lookup plant_id by plant_name
|
||||
$plantId = Plant::where('name', $data['plant_name'])->value('id');
|
||||
if (!$plantId) {
|
||||
// return response("ERROR: Plant '" . $data['plant_name'] . "' not found", 404)
|
||||
// return response("ERROR: Plant '{$data['plant_name']}' not found", 404)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant '" . $data['plant_name'] . "' not found"
|
||||
'status_description' => "Plant '{$data['plant_name']}' not found!"
|
||||
], 404);
|
||||
}
|
||||
|
||||
@@ -94,11 +94,10 @@ class ObdController extends Controller
|
||||
// return response( "ERROR: OBD Number '$obdNumber' not found for plant '{$data['plant_name']}'",404)->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD Number '$obdNumber' not found for plant '{$data['plant_name']}'"
|
||||
'status_description' => "OBD Number '$obdNumber' not found for plant '{$data['plant_name']}'!"
|
||||
], 404);
|
||||
}
|
||||
|
||||
|
||||
$missingLines = [];
|
||||
$alreadyUpdatedLines = [];
|
||||
|
||||
@@ -110,7 +109,7 @@ class ObdController extends Controller
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Line Number can't be empty"
|
||||
'status_description' => "Line Number can't be empty!"
|
||||
], 404);
|
||||
}
|
||||
|
||||
@@ -148,7 +147,7 @@ class ObdController extends Controller
|
||||
$fieldsString = implode(', ', $missingFields) . ' and ' . $lastField;
|
||||
}
|
||||
|
||||
$message = "" . $fieldsString . " can't be empty for line_number " . $line['line_number'];
|
||||
$message = $fieldsString . " can't be empty for line_number {$line['line_number']}!";
|
||||
// return response($message, 400)->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
@@ -158,7 +157,7 @@ class ObdController extends Controller
|
||||
}
|
||||
|
||||
if (!empty($missingLines)) {
|
||||
$message = "Line(s) " . implode(', ', $missingLines) . " not found for Plant '" . $data['plant_name'] . "' and OBD Number: '" . $data['obd_number'] . "'";
|
||||
$message = "Line(s) " . implode(', ', $missingLines) . " not found for Plant '{$data['plant_name']}' and OBD Number: '{$data['obd_number']}'!";
|
||||
// return response($message, 404)->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
@@ -324,16 +323,12 @@ class ObdController extends Controller
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return response("ERROR: Server error", 500)->header('Content-Type', 'text/plain');
|
||||
// response("ERROR: Server error", 500)->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Store OBD data internal server error : '.$e?->getCode()
|
||||
], 500);
|
||||
}
|
||||
// catch (\Exception $e) {
|
||||
// // Log error if needed, e.g. Log::error($e->getMessage());
|
||||
// return response()->json([
|
||||
// 'status' => 'error',
|
||||
// 'message' => 'Server error: ' . $e->getMessage(),
|
||||
// ], 500);
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
//Route::get('obd/get-test-datas', [ObdController::class, 'get_test']);
|
||||
@@ -345,7 +340,7 @@ class ObdController extends Controller
|
||||
$header_auth = $request->header('Authorization');
|
||||
$expectedToken = $expectedUser . ':' . $expectedPw;
|
||||
|
||||
if ("Bearer " . $expectedToken !== $header_auth)
|
||||
if ("Bearer " . $expectedToken != $header_auth)
|
||||
{
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
@@ -417,7 +412,11 @@ class ObdController extends Controller
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return response("ERROR: GET test data server error", 500)->header('Content-Type', 'text/plain');
|
||||
//return response("ERROR: GET test data server error", 500)->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Get test data internal server error : '.$e?->getCode()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -430,7 +429,7 @@ class ObdController extends Controller
|
||||
$header_auth = $request->header('Authorization');
|
||||
$expectedToken = $expectedUser . ':' . $expectedPw;
|
||||
|
||||
// if("Bearer " . $expectedToken !== $header_auth)
|
||||
// if("Bearer " . $expectedToken != $header_auth)
|
||||
// {
|
||||
// return response("ERROR: Unauthorized", 403)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
@@ -440,167 +439,14 @@ class ObdController extends Controller
|
||||
try
|
||||
{
|
||||
return response("Successfully Received", 200)->header('Content-Type', 'text/plain');
|
||||
// $data = $request->all();
|
||||
// // Validate required fields
|
||||
// $missing = [];
|
||||
// if (empty($data['plant_name'])) $missing[] = 'plant_name';
|
||||
// if (empty($data['obd_number'])) $missing[] = 'obd_number';
|
||||
// if (empty($data['line_numbers'])) $missing[] = 'line_numbers';
|
||||
|
||||
// if (!empty($missing))
|
||||
// {
|
||||
// $message = "ERROR: Missing required field(s): " . implode(', ', $missing);
|
||||
// return response($message, 400)->header('Content-Type', 'text/plain');
|
||||
// }
|
||||
|
||||
// // OBD Number validation
|
||||
// $obdNumber = $data['obd_number'];
|
||||
// if (Str::length($obdNumber) < 8 || !ctype_alnum($obdNumber))
|
||||
// {
|
||||
// return response("ERROR: OBD Number should contain minimum 8 digits", 400)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
// }
|
||||
|
||||
// // Lookup plant_id by plant_name
|
||||
// $plantId = Plant::where('name', $data['plant_name'])->value('id');
|
||||
// if (!$plantId) {
|
||||
// return response("ERROR: Plant '" . $data['plant_name'] . "' not found", 404)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
// }
|
||||
|
||||
// //Check if OBD number exists for that plant
|
||||
// $obdRecords = WeightValidation::where('plant_id', $plantId)
|
||||
// ->where('obd_number', $obdNumber)
|
||||
// ->select('id', 'line_number') // Only fetch required columns
|
||||
// ->get();
|
||||
// if ($obdRecords->isEmpty()) {
|
||||
// return response(
|
||||
// "ERROR: OBD Number '$obdNumber' not found for plant '{$data['plant_name']}'",
|
||||
// 404
|
||||
// )->header('Content-Type', 'text/plain');
|
||||
// }
|
||||
|
||||
|
||||
// $missingLines = [];
|
||||
// $alreadyUpdatedLines = [];
|
||||
|
||||
// foreach ($data['line_numbers'] as $line)
|
||||
// {
|
||||
// if ($line['line_number'] == '' || $line['line_number'] == null) {
|
||||
// return response("ERROR: Line Number can't be empty", 400)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
// }
|
||||
|
||||
// $record = WeightValidation::where('plant_id', $plantId)
|
||||
// ->where('obd_number', $data['obd_number'])
|
||||
// ->where('line_number', $line['line_number'])
|
||||
// ->first();
|
||||
|
||||
// if (!$record)
|
||||
// {
|
||||
// $missingLines[] = $line['line_number'];
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// $mandatoryFields = ['vehicle_number', 'bundle_number', 'picked_weight', 'scanned_by'];
|
||||
// $missingFields = [];
|
||||
|
||||
// foreach ($mandatoryFields as $field)
|
||||
// {
|
||||
// if ($line[$field] == '' || $line[$field] == null) {
|
||||
// $missingFields[] = ucwords(str_replace('_', ' ', $field));
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (!empty($missingFields))
|
||||
// {
|
||||
// if (count($missingFields) == 1)
|
||||
// {
|
||||
// $fieldsString = $missingFields[0];
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// $lastField = array_pop($missingFields);
|
||||
// $fieldsString = implode(', ', $missingFields) . ' and ' . $lastField;
|
||||
// }
|
||||
|
||||
// $message = "ERROR: " . $fieldsString . " can't be empty for line_number " . $line['line_number'];
|
||||
// return response($message, 400)->header('Content-Type', 'text/plain');
|
||||
// }
|
||||
// // Check if ANY of the fields are NOT empty in DB
|
||||
// $fields = ['vehicle_number', 'bundle_number', 'picked_weight', 'scanned_by'];
|
||||
// $alreadyUpdatedLines = [];
|
||||
|
||||
// foreach ($data['line_numbers'] as $line) {
|
||||
// // Fetch the record for this line
|
||||
// $record = WeightValidation::where('plant_id', $plantId)
|
||||
// ->where('obd_number', $data['obd_number'])
|
||||
// ->where('line_number', $line['line_number'])
|
||||
// ->first();
|
||||
|
||||
// //If No Record Found
|
||||
// if (!$record)
|
||||
// {
|
||||
// continue; // Skip to next line
|
||||
// }
|
||||
|
||||
// $hasAnyValue = false;
|
||||
// foreach ($fields as $field)
|
||||
// {
|
||||
// if (!empty($record->$field))
|
||||
// {
|
||||
// $hasAnyValue = true;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if ($hasAnyValue)
|
||||
// {
|
||||
// $alreadyUpdatedLines[] = $line['line_number'];
|
||||
// }
|
||||
// }
|
||||
|
||||
// // Only after checking all lines, return if any already updated
|
||||
// if (!empty($alreadyUpdatedLines)) {
|
||||
// return response(
|
||||
// "Already line number(s) updated for obd number '{$data['obd_number']}' is :" . implode(', ', $alreadyUpdatedLines),
|
||||
// 400
|
||||
// )->header('Content-Type', 'text/plain');
|
||||
// }
|
||||
// }
|
||||
|
||||
// // If any lines are missing, report
|
||||
// if (!empty($missingLines)) {
|
||||
// $message = "ERROR: Line(s) " . implode(', ', $missingLines) . " not found for Plant '" . $data['plant_name'] . "' and OBD Number: '" . $data['obd_number'] . "'";
|
||||
// return response($message, 404)->header('Content-Type', 'text/plain');
|
||||
// }
|
||||
|
||||
// // If we reach here, all lines are valid and need updating
|
||||
// $updated = 0;
|
||||
// foreach ($data['line_numbers'] as $line)
|
||||
// {
|
||||
// $count = WeightValidation::where([
|
||||
// 'plant_id' => $plantId,
|
||||
// 'obd_number' => $data['obd_number'],
|
||||
// 'line_number'=> $line['line_number'],
|
||||
// ])
|
||||
// ->update([
|
||||
// 'vehicle_number' => $line['vehicle_number'] ?? null,
|
||||
// 'bundle_number' => $line['bundle_number'] ?? null,
|
||||
// 'picked_weight' => $line['picked_weight'] ?? null,
|
||||
// 'scanned_by' => $line['scanned_by'] ?? null,
|
||||
// 'updated_at' => now(),
|
||||
// ]);
|
||||
|
||||
// $updated += $count;
|
||||
// }
|
||||
|
||||
// $message = "SUCCESS: OBD Number '" . $data['obd_number'] . "' updated successfully";
|
||||
// return response($message, 200)->header('Content-Type', 'text/plain');
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
return response("ERROR: Server error", 500)->header('Content-Type', 'text/plain');
|
||||
//response("ERROR: Server error", 500)->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Test internal server error : '.$e?->getCode()
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -612,17 +458,17 @@ class ObdController extends Controller
|
||||
$header_auth = $request->header('Authorization');
|
||||
$expectedToken = $expectedUser . ':' . $expectedPw;
|
||||
|
||||
// if ("Bearer " . $expectedToken !== $header_auth)
|
||||
// if ("Bearer " . $expectedToken != $header_auth)
|
||||
// {
|
||||
// return response("ERROR: Unauthorized", 403)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
// }
|
||||
|
||||
if ("Bearer " . $expectedToken !== $header_auth)
|
||||
if ("Bearer " . $expectedToken != $header_auth)
|
||||
{
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Invalid authorization token'
|
||||
'status_description' => 'Invalid authorization token!'
|
||||
], 403);
|
||||
}
|
||||
|
||||
@@ -635,7 +481,7 @@ class ObdController extends Controller
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant Name can't be empty"
|
||||
'status_description' => "Plant Name can't be empty!"
|
||||
], 400);
|
||||
}
|
||||
else if(empty($obdNumber))
|
||||
@@ -644,7 +490,7 @@ class ObdController extends Controller
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD Number can't be empty"
|
||||
'status_description' => "OBD Number can't be empty!"
|
||||
], 400);
|
||||
}
|
||||
else if(Str::length($obdNumber) < 8 || !ctype_alnum($obdNumber))
|
||||
@@ -653,7 +499,7 @@ class ObdController extends Controller
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD Number should contain minimum 8 digits: '$obdNumber'"
|
||||
'status_description' => "OBD number should contain minimum 8 digit alpha-numeric values only!"
|
||||
], 400);
|
||||
}
|
||||
|
||||
@@ -665,7 +511,7 @@ class ObdController extends Controller
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant not found"
|
||||
'status_description' => "Plant not found!"
|
||||
], 400);
|
||||
}
|
||||
|
||||
@@ -684,7 +530,7 @@ class ObdController extends Controller
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD number $obdNumber does not exist for plant '$plantName'"
|
||||
'status_description' => "OBD number $obdNumber does not exist for plant '$plantName'!"
|
||||
], 400);
|
||||
}
|
||||
|
||||
@@ -699,7 +545,7 @@ class ObdController extends Controller
|
||||
// return response("SUCCESS: Already scanning process completed for the OBD Number", 200)->header('Content-Type', values: 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'SUCCESS',
|
||||
'status_description' => "Already weight validation completed for the OBD Number"
|
||||
'status_description' => "Already weight validation completed for the OBD Number!"
|
||||
], 200);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user