Updated get and post method on obd controller
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:
@@ -19,7 +19,7 @@ class ObdController extends Controller
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
public function store_obd(Request $request)
|
||||
{
|
||||
$expectedUser = env('API_AUTH_USER');
|
||||
$expectedPw = env('API_AUTH_PW');
|
||||
@@ -42,51 +42,62 @@ class ObdController extends Controller
|
||||
try {
|
||||
$data = $request->all();
|
||||
|
||||
$plantCode = $data['plant_code'] ?? '';
|
||||
$obdNumber = $data['obd_number'] ?? '';
|
||||
|
||||
Log::info('OBD POST API >>', ['Post-Data' => $data]);
|
||||
|
||||
// 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 = 'Missing required field(s): '.implode(', ', $missing);
|
||||
|
||||
if (! $plantCode || $plantCode == null || $plantCode == '') {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant code can't be empty!",
|
||||
], 404);
|
||||
} elseif (! is_numeric($plantCode) || Str::length($plantCode) < 4 || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { // !ctype_digit($data['plant_code'])
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
||||
], 404);
|
||||
} elseif (! $obdNumber || $obdNumber == null || $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!",
|
||||
], 400);
|
||||
} elseif (Str::length($obdNumber) < 8) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD number '{$obdNumber}' should contain minimum 8 digits!",
|
||||
], 404);
|
||||
} elseif (! ctype_alnum($obdNumber)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD number '{$obdNumber}' should contain only alpha-numeric values!",
|
||||
], 404);
|
||||
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{8,}$/', $obdNumber)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD number '{$obdNumber}' should not begin with '0'!",
|
||||
], 404);
|
||||
} elseif (empty($data['line_numbers'])) {
|
||||
// return response($message, 400)->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => $message,
|
||||
'status_description' => 'Missing required field(s): line_numbers',
|
||||
], 400);
|
||||
}
|
||||
|
||||
// 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 code
|
||||
$plant = Plant::where('code', $plantCode)->first();
|
||||
if (! $plant) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'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)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant '{$data['plant_name']}' not found!",
|
||||
'status_description' => "Plant code '{$plantCode}' not found!",
|
||||
], 404);
|
||||
}
|
||||
$plantId = $plant?->id ?? null;
|
||||
$plantName = $plant?->name ?? null;
|
||||
|
||||
// Check if OBD number exists for that plant
|
||||
$obdRecords = WeightValidation::where('plant_id', $plantId)
|
||||
@@ -94,10 +105,10 @@ class ObdController extends Controller
|
||||
->exists();
|
||||
|
||||
if (! $obdRecords) {
|
||||
// return response( "ERROR: OBD Number '$obdNumber' not found for plant '{$data['plant_name']}'",404)->header('Content-Type', 'text/plain');
|
||||
// return response( "ERROR: OBD Number '$obdNumber' not found for plant '{$plantCode}'",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 the plant '{$plantName}'!",
|
||||
], 404);
|
||||
}
|
||||
|
||||
@@ -105,22 +116,23 @@ class ObdController extends Controller
|
||||
$alreadyUpdatedLines = [];
|
||||
|
||||
foreach ($data['line_numbers'] as $line) {
|
||||
if ($line['line_number'] == '' || $line['line_number'] == null) {
|
||||
$lineNumber = $line['line_number'] ?? null;
|
||||
if ($lineNumber == '' || $lineNumber == 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);
|
||||
}
|
||||
|
||||
$record = WeightValidation::where('plant_id', $plantId)
|
||||
->where('obd_number', $data['obd_number'])
|
||||
->where('line_number', $line['line_number'])
|
||||
->where('obd_number', $obdNumber)
|
||||
->where('line_number', $lineNumber)
|
||||
->first();
|
||||
|
||||
if (! $record) {
|
||||
$missingLines[] = $line['line_number'];
|
||||
$missingLines[] = $lineNumber;
|
||||
|
||||
continue;
|
||||
}
|
||||
@@ -142,23 +154,19 @@ class ObdController extends Controller
|
||||
$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' => $fieldsString." can't be empty for line_number {$lineNumber}!",
|
||||
], 400);
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($missingLines)) {
|
||||
$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' => 'Line(s) '.implode(', ', $missingLines)." not found for the plant '{$plantName}' and OBD number: '{$obdNumber}'!",
|
||||
], 404);
|
||||
}
|
||||
|
||||
@@ -168,8 +176,8 @@ class ObdController extends Controller
|
||||
|
||||
// Check for duplicates within the request
|
||||
foreach ($data['line_numbers'] as $line) {
|
||||
$lineNumber = $line['line_number'];
|
||||
$bundleNumber = trim((string) $line['bundle_number']);
|
||||
$lineNumber = $line['line_number'] ?? null;
|
||||
$bundleNumber = trim((string) $line['bundle_number'] ?? '');
|
||||
$pairKey = $lineNumber.'|'.$bundleNumber;
|
||||
|
||||
if (isset($seenPairs[$pairKey])) {
|
||||
@@ -228,11 +236,9 @@ class ObdController extends Controller
|
||||
// 400
|
||||
// )->header('Content-Type', 'text/plain');
|
||||
|
||||
$retRes = implode(', and ', $allDuplicates);
|
||||
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => $retRes,
|
||||
'status_description' => implode(', and ', $allDuplicates),
|
||||
], 400);
|
||||
}
|
||||
|
||||
@@ -244,7 +250,7 @@ class ObdController extends Controller
|
||||
$lineTracker = [];
|
||||
|
||||
foreach ($data['line_numbers'] as $line) {
|
||||
$lineNumber = $line['line_number'];
|
||||
$lineNumber = $line['line_number'] ?? null;
|
||||
$existing = WeightValidation::where('plant_id', $plantId)
|
||||
->where('obd_number', $obdNumber)
|
||||
->where('line_number', $lineNumber)
|
||||
@@ -267,11 +273,12 @@ class ObdController extends Controller
|
||||
$updatedLines[] = $lineNumber;
|
||||
$lineTracker[$lineNumber] = 1;
|
||||
} else {
|
||||
$original = WeightValidation::where([
|
||||
'plant_id' => $plantId,
|
||||
'obd_number' => $obdNumber,
|
||||
'line_number' => $lineNumber,
|
||||
])->orderBy('id')->first();
|
||||
$original = WeightValidation::where('plant_id', $plantId)
|
||||
->where('obd_number', $obdNumber)
|
||||
->where('line_number', $lineNumber)
|
||||
->orderBy('id')
|
||||
->first();
|
||||
// where(['plant_id' => $plantId, 'obd_number' => $obdNumber, 'line_number' => $lineNumber])
|
||||
|
||||
WeightValidation::create([
|
||||
'plant_id' => $plantId,
|
||||
@@ -302,7 +309,7 @@ class ObdController extends Controller
|
||||
}
|
||||
|
||||
if ($inserted > 0) {
|
||||
$responseMessage .= 'Inserted successfully. Line Numbers: {'.implode(', ', $insertedLines).'}';
|
||||
$responseMessage .= ' inserted successfully. Line Numbers: {'.implode(', ', $insertedLines).'}';
|
||||
}
|
||||
|
||||
// return response($responseMessage, 200)
|
||||
@@ -321,7 +328,6 @@ class ObdController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// Route::get('obd/get-test-datas', [ObdController::class, 'get_test']);
|
||||
public function get_test(Request $request)
|
||||
{
|
||||
$expectedUser = env('API_AUTH_USER');
|
||||
@@ -373,7 +379,7 @@ class ObdController extends Controller
|
||||
} 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 '{$productionOrder}' should contain minimum 7 digits numeric values only!",
|
||||
], 400);
|
||||
}
|
||||
|
||||
@@ -382,7 +388,7 @@ class ObdController extends Controller
|
||||
if (! $prodOrderExist) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Production order not found!',
|
||||
'status_description' => "Production order '{$productionOrder}' not found!",
|
||||
], 400);
|
||||
}
|
||||
|
||||
@@ -423,46 +429,56 @@ class ObdController extends Controller
|
||||
], 403);
|
||||
}
|
||||
|
||||
$plantName = $request->header('plant-name');
|
||||
$plantCode = $request->header('plant-code');
|
||||
$obdNumber = $request->header('obd-number');
|
||||
|
||||
Log::info('OBD GET API >>', ['plant-name' => $plantName, 'obd-number' => $obdNumber]);
|
||||
Log::info('OBD GET API >>', ['plant-code' => $plantCode, 'obd-number' => $obdNumber]);
|
||||
|
||||
if (empty($plantName)) {
|
||||
// return response("ERROR: Plant Name can't be empty", 400)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
if (! $plantCode || $plantCode == null || $plantCode == '') {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant Name can't be empty!",
|
||||
], 400);
|
||||
} elseif (empty($obdNumber)) {
|
||||
'status_description' => "Plant code can't be empty!",
|
||||
], 404);
|
||||
} elseif (! is_numeric($plantCode) || Str::length($plantCode) < 4 || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) { // !ctype_digit($data['plant_code'])
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Invalid plant code '{$plantCode}' found!",
|
||||
], 404);
|
||||
} elseif (! $obdNumber || $obdNumber == null || $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);
|
||||
} 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');
|
||||
} elseif (Str::length($obdNumber) < 8) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'OBD number should contain minimum 8 digit alpha-numeric values only!',
|
||||
], 400);
|
||||
}
|
||||
|
||||
// Fetch the plant id by name
|
||||
$plantId = Plant::where('name', $plantName)->value('id');
|
||||
|
||||
if (! $plantId) {
|
||||
// return response("ERROR: Plant not found", 400)
|
||||
// ->header('Content-Type', 'text/plain');
|
||||
'status_description' => "OBD number '{$obdNumber}' should contain minimum 8 digits!",
|
||||
], 404);
|
||||
} elseif (! ctype_alnum($obdNumber)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => 'Plant not found!',
|
||||
], 400);
|
||||
'status_description' => "OBD number '{$obdNumber}' should contain only alpha-numeric values!",
|
||||
], 404);
|
||||
} elseif (! preg_match('/^[a-zA-Z1-9][a-zA-Z0-9]{8,}$/', $obdNumber)) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "OBD number '{$obdNumber}' should not begin with '0'!",
|
||||
], 404);
|
||||
}
|
||||
|
||||
// Fetch the plant id by code
|
||||
$plant = Plant::where('code', $plantCode)->first();
|
||||
if (! $plant) {
|
||||
return response()->json([
|
||||
'status_code' => 'ERROR',
|
||||
'status_description' => "Plant code '{$plantCode}' not found!",
|
||||
], 404);
|
||||
}
|
||||
$plantId = $plant?->id ?? null;
|
||||
$plantName = $plant?->name ?? null;
|
||||
|
||||
// $records = WeightValidation::where('plant_id', $plantId)
|
||||
// ->where('obd_number', $obdNumber)
|
||||
// ->get();
|
||||
@@ -473,11 +489,11 @@ class ObdController extends Controller
|
||||
->exists();
|
||||
|
||||
if (! $exists) {
|
||||
// return response("ERROR: OBD number $obdNumber does not exist for plant '$plantName'", 404)
|
||||
// return response("ERROR: OBD number $obdNumber does not exist for plant '$plantCode'", 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);
|
||||
}
|
||||
|
||||
@@ -492,7 +508,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 '{$obdNumber}'!",
|
||||
], 200);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user