Refactored alignment ObdController page
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 11s
All checks were successful
Scan for leaked secrets using Kingfisher / kingfisher-secrets-scan (push) Successful in 11s
This commit is contained in:
@@ -13,10 +13,7 @@ class ObdController extends Controller
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
|
||||
}
|
||||
public function index() {}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
@@ -34,26 +31,30 @@ class ObdController extends Controller
|
||||
// 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);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
$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($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 = 'Missing required field(s): '.implode(', ', $missing);
|
||||
|
||||
if (!empty($missing))
|
||||
{
|
||||
$message = "Missing required field(s): " . implode(', ', $missing);
|
||||
// return response($message, 400)->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
@@ -63,13 +64,12 @@ class ObdController extends Controller
|
||||
|
||||
// OBD Number validation
|
||||
$obdNumber = $data['obd_number'];
|
||||
if (Str::length($obdNumber) < 8 || !ctype_alnum($obdNumber))
|
||||
{
|
||||
if (Str::length($obdNumber) < 8 || ! ctype_alnum($obdNumber)) {
|
||||
// return response("ERROR: OBD Number should contain minimum 8 digits", 400)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD number should contain minimum 8 digit alpha-numeric values only!"
|
||||
'status_description' => 'OBD number should contain minimum 8 digit alpha-numeric values only!',
|
||||
], 400);
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ class ObdController extends Controller
|
||||
// ->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);
|
||||
}
|
||||
|
||||
@@ -89,27 +89,24 @@ class ObdController extends Controller
|
||||
->where('obd_number', $obdNumber)
|
||||
->exists();
|
||||
|
||||
if (!$obdRecords)
|
||||
{
|
||||
if (! $obdRecords) {
|
||||
// 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 = [];
|
||||
|
||||
foreach ($data['line_numbers'] as $line)
|
||||
{
|
||||
if ($line['line_number'] == '' || $line['line_number'] == null)
|
||||
{
|
||||
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');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Line Number can't be empty!"
|
||||
'status_description' => "Line Number can't be empty!",
|
||||
], 404);
|
||||
}
|
||||
|
||||
@@ -118,50 +115,46 @@ class ObdController extends Controller
|
||||
->where('line_number', $line['line_number'])
|
||||
->first();
|
||||
|
||||
if (!$record)
|
||||
{
|
||||
if (! $record) {
|
||||
$missingLines[] = $line['line_number'];
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$mandatoryFields = ['vehicle_number', 'bundle_number', 'heat_number', 'picked_weight', 'scanned_by'];
|
||||
$missingFields = [];
|
||||
|
||||
foreach ($mandatoryFields as $field)
|
||||
{
|
||||
if ($line[$field] == '' || $line[$field] == null)
|
||||
{
|
||||
foreach ($mandatoryFields as $field) {
|
||||
if ($line[$field] == '' || $line[$field] == null) {
|
||||
$missingFields[] = ucwords(str_replace('_', ' ', $field));
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($missingFields))
|
||||
{
|
||||
if (count($missingFields) == 1)
|
||||
{
|
||||
if (! empty($missingFields)) {
|
||||
if (count($missingFields) == 1) {
|
||||
$fieldsString = $missingFields[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$lastField = array_pop($missingFields);
|
||||
$fieldsString = implode(', ', $missingFields).' and '.$lastField;
|
||||
}
|
||||
|
||||
$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',
|
||||
'status_description' => $message
|
||||
'status_description' => $message,
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
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',
|
||||
'status_description' => $message
|
||||
'status_description' => $message,
|
||||
], 404);
|
||||
}
|
||||
|
||||
@@ -218,13 +211,11 @@ class ObdController extends Controller
|
||||
|
||||
// Return all errors if any duplicates found
|
||||
$allDuplicates = [];
|
||||
if (!empty($internalDuplicates))
|
||||
{
|
||||
$allDuplicates[] = "Duplicate(s) " . implode(', ', $internalDuplicates);
|
||||
if (! empty($internalDuplicates)) {
|
||||
$allDuplicates[] = 'Duplicate(s) '.implode(', ', $internalDuplicates);
|
||||
}
|
||||
if (!empty($dbDuplicates))
|
||||
{
|
||||
$allDuplicates[] = "Already exists in database: " . implode('; ', $dbDuplicates);
|
||||
if (! empty($dbDuplicates)) {
|
||||
$allDuplicates[] = 'Already exists in database: '.implode('; ', $dbDuplicates);
|
||||
}
|
||||
|
||||
if (! empty($allDuplicates)) {
|
||||
@@ -233,10 +224,11 @@ class ObdController extends Controller
|
||||
// 400
|
||||
// )->header('Content-Type', 'text/plain');
|
||||
|
||||
$retRes = implode(", and ", $allDuplicates);
|
||||
$retRes = implode(', and ', $allDuplicates);
|
||||
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => $retRes
|
||||
'status_description' => $retRes,
|
||||
], 400);
|
||||
}
|
||||
|
||||
@@ -247,8 +239,7 @@ class ObdController extends Controller
|
||||
$insertedLines = [];
|
||||
$lineTracker = [];
|
||||
|
||||
foreach ($data['line_numbers'] as $line)
|
||||
{
|
||||
foreach ($data['line_numbers'] as $line) {
|
||||
$lineNumber = $line['line_number'];
|
||||
$existing = WeightValidation::where('plant_id', $plantId)
|
||||
->where('obd_number', $obdNumber)
|
||||
@@ -259,8 +250,7 @@ class ObdController extends Controller
|
||||
})
|
||||
->first();
|
||||
|
||||
if($existing)
|
||||
{
|
||||
if ($existing) {
|
||||
$existing->update([
|
||||
'vehicle_number' => $line['vehicle_number'] ?? null,
|
||||
'bundle_number' => $line['bundle_number'] ?? null,
|
||||
@@ -272,9 +262,7 @@ class ObdController extends Controller
|
||||
$updated++;
|
||||
$updatedLines[] = $lineNumber;
|
||||
$lineTracker[$lineNumber] = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
$original = WeightValidation::where([
|
||||
'plant_id' => $plantId,
|
||||
'obd_number' => $obdNumber,
|
||||
@@ -306,11 +294,11 @@ class ObdController extends Controller
|
||||
$responseMessage = "OBD Number '{$obdNumber}'";
|
||||
|
||||
if ($updated > 0) {
|
||||
$responseMessage .= " updated successfully. Line Numbers: {" . implode(', ', $updatedLines) . "}";
|
||||
$responseMessage .= ' updated successfully. Line Numbers: {'.implode(', ', $updatedLines).'}';
|
||||
}
|
||||
|
||||
if ($inserted > 0) {
|
||||
$responseMessage .= "Inserted successfully. Line Numbers: {" . implode(', ', $insertedLines) . "}";
|
||||
$responseMessage .= 'Inserted successfully. Line Numbers: {'.implode(', ', $insertedLines).'}';
|
||||
}
|
||||
|
||||
// return response($responseMessage, 200)
|
||||
@@ -320,13 +308,11 @@ class ObdController extends Controller
|
||||
'status_description' => $responseMessage,
|
||||
], 200);
|
||||
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
} catch (\Exception $e) {
|
||||
// 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()
|
||||
'status_description' => 'Store OBD data internal server error : '.$e?->getCode(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
@@ -340,11 +326,10 @@ 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',
|
||||
'status_description' => 'Invalid authorization token!'
|
||||
'status_description' => 'Invalid authorization token!',
|
||||
], 403);
|
||||
}
|
||||
|
||||
@@ -374,48 +359,41 @@ class ObdController extends Controller
|
||||
// ], 400);
|
||||
// }
|
||||
|
||||
|
||||
$productionOrder = $request->header('production-order');
|
||||
if (empty($productionOrder))
|
||||
{
|
||||
if (empty($productionOrder)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Production order can't be empty!"
|
||||
'status_description' => "Production order can't be empty!",
|
||||
], 400);
|
||||
}
|
||||
else if(Str::length($productionOrder) < 7 || !is_numeric($productionOrder))
|
||||
{
|
||||
} elseif (Str::length($productionOrder) < 7 || ! is_numeric($productionOrder)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Production order should contain minimum 7 digits numeric values only!"
|
||||
'status_description' => 'Production order should contain minimum 7 digits numeric values only!',
|
||||
], 400);
|
||||
}
|
||||
|
||||
$prodOrderExist = ($productionOrder == '1234567' || $productionOrder == '7654321');
|
||||
|
||||
if (!$prodOrderExist)
|
||||
{
|
||||
if (! $prodOrderExist) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Production order not found!"
|
||||
'status_description' => 'Production order not found!',
|
||||
], 400);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
// return response("Successfully GET request Received", 200)->header('Content-Type', 'text/plain');
|
||||
$itemCode = Item::where('code', '123456')->where('plant_id', 1)->first();
|
||||
|
||||
return response()->json([
|
||||
'item_code' => $itemCode->code,
|
||||
'item_description' => $itemCode->description
|
||||
'item_description' => $itemCode->description,
|
||||
], 200);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
} catch (\Exception $e) {
|
||||
// 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()
|
||||
'status_description' => 'Get test data internal server error : '.$e?->getCode(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
@@ -433,42 +411,36 @@ class ObdController extends Controller
|
||||
// ->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);
|
||||
}
|
||||
|
||||
$plantName = $request->header('plant-name');
|
||||
$obdNumber = $request->header('obd-number');
|
||||
|
||||
if (empty($plantName))
|
||||
{
|
||||
if (empty($plantName)) {
|
||||
// return response("ERROR: Plant Name can't be empty", 400)
|
||||
// ->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))
|
||||
{
|
||||
} elseif (empty($obdNumber)) {
|
||||
// return response("ERROR: OBD Number can't be empty", 400)
|
||||
// ->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))
|
||||
{
|
||||
} elseif (Str::length($obdNumber) < 8 || ! ctype_alnum($obdNumber)) {
|
||||
// return response("ERROR: OBD Number should contain minimum 8 digits: '$obdNumber'", 400)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD number should contain minimum 8 digit alpha-numeric values only!"
|
||||
'status_description' => 'OBD number should contain minimum 8 digit alpha-numeric values only!',
|
||||
], 400);
|
||||
}
|
||||
|
||||
@@ -480,7 +452,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);
|
||||
}
|
||||
|
||||
@@ -493,13 +465,12 @@ class ObdController extends Controller
|
||||
->where('obd_number', $obdNumber)
|
||||
->exists();
|
||||
|
||||
if (!$exists)
|
||||
{
|
||||
if (! $exists) {
|
||||
// return response("ERROR: OBD number $obdNumber does not exist for plant '$plantName'", 404)
|
||||
// ->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);
|
||||
}
|
||||
|
||||
@@ -514,7 +485,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);
|
||||
}
|
||||
|
||||
@@ -530,18 +501,20 @@ class ObdController extends Controller
|
||||
'OBD_Number' => $obdNumber,
|
||||
'Line_Numbers' => $records->map(function ($item) use ($itemCodes) {
|
||||
$itemInfo = $itemCodes[$item->item_id] ?? null;
|
||||
|
||||
return [
|
||||
'Line' => $item->line_number ?? "",
|
||||
'Material_Code' => $itemInfo->code ?? "",
|
||||
'Material_Description' => $itemInfo->description ?? "",
|
||||
'Batch_Number' => $item->batch_number ?? "",
|
||||
'Line' => $item->line_number ?? '',
|
||||
'Material_Code' => $itemInfo->code ?? '',
|
||||
'Material_Description' => $itemInfo->description ?? '',
|
||||
'Batch_Number' => $item->batch_number ?? '',
|
||||
// 'Heat_Number' => $item->heat_number ?? "",
|
||||
'Actual_Weight' => $item->obd_weight ?? "",
|
||||
'Actual_Weight' => $item->obd_weight ?? '',
|
||||
];
|
||||
})->values()->toArray()
|
||||
]
|
||||
]
|
||||
})->values()->toArray(),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
return response()->json($ObdResponseStructure);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user