From de0de426f056b9e4ef750954e2a5af082b97309a Mon Sep 17 00:00:00 2001 From: dhanabalan Date: Sat, 5 Jul 2025 18:25:11 +0530 Subject: [PATCH] Updated validation functionality and failure messages in controller files --- app/Http/Controllers/MachineController.php | 4 +- app/Http/Controllers/ObdController.php | 226 +++--------------- app/Http/Controllers/PalletController.php | 2 +- app/Http/Controllers/PlantController.php | 28 +-- .../Controllers/StickerMasterController.php | 73 +++--- .../Controllers/TestingPanelController.php | 214 +++++++++-------- app/Http/Controllers/UserController.php | 11 +- 7 files changed, 218 insertions(+), 340 deletions(-) diff --git a/app/Http/Controllers/MachineController.php b/app/Http/Controllers/MachineController.php index 208f6cf..d6cd574 100644 --- a/app/Http/Controllers/MachineController.php +++ b/app/Http/Controllers/MachineController.php @@ -33,11 +33,11 @@ class MachineController 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); } diff --git a/app/Http/Controllers/ObdController.php b/app/Http/Controllers/ObdController.php index 5ad976a..4e9780f 100644 --- a/app/Http/Controllers/ObdController.php +++ b/app/Http/Controllers/ObdController.php @@ -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); } diff --git a/app/Http/Controllers/PalletController.php b/app/Http/Controllers/PalletController.php index 6f143bf..e4f82ec 100644 --- a/app/Http/Controllers/PalletController.php +++ b/app/Http/Controllers/PalletController.php @@ -57,7 +57,7 @@ class PalletController extends Controller 'margin_right' => 0, 'margin_top' => 0, 'margin_bottom' => 0, - 'tempDir' => '/var/www/storage/mpdf-tmp', + //'tempDir' => '/var/www/storage/mpdf-tmp', ]); $mpdf->WriteHTML($html); diff --git a/app/Http/Controllers/PlantController.php b/app/Http/Controllers/PlantController.php index 81a845d..f071b1c 100644 --- a/app/Http/Controllers/PlantController.php +++ b/app/Http/Controllers/PlantController.php @@ -33,27 +33,27 @@ class PlantController 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); } - $plants = Plant::with('company')->orderBy('code')->get(); - $plantsData = $plants->map(function($plant) { - return [ - 'company' => $plant->company ? $plant->company->name : "", // Get company name - 'plant_code' => (String)$plant->code, - 'plant_name' => $plant->name, - 'plant_address' => $plant->address, - ]; - }); + $plants = Plant::with('company')->orderBy('code')->get(); + $plantsData = $plants->map(function($plant) { + return [ + 'company' => $plant->company ? $plant->company->name : "", // Get company name + 'plant_code' => (String)$plant->code, + 'plant_name' => $plant->name, + 'plant_address' => $plant->address, + ]; + }); - return response()->json([ - 'plants' => $plantsData - ]); + return response()->json([ + 'plants' => $plantsData + ]); } /** diff --git a/app/Http/Controllers/StickerMasterController.php b/app/Http/Controllers/StickerMasterController.php index c262814..69d722a 100644 --- a/app/Http/Controllers/StickerMasterController.php +++ b/app/Http/Controllers/StickerMasterController.php @@ -7,6 +7,7 @@ use App\Models\MotorTestingMaster; use App\Models\Plant; use App\Models\StickerMaster; use Illuminate\Http\Request; +use Str; class StickerMasterController extends Controller { @@ -36,19 +37,11 @@ class StickerMasterController extends Controller $header_auth = $request->header('Authorization'); $expectedToken = $expectedUser . ':' . $expectedPw; - // if ("Bearer " . $expectedToken !== $header_auth) - // { - // return response("Unauthorized", 403) - // ->header('Content-Type', 'text/plain'); - // } - - //$data = $request->all(); - - 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); } @@ -59,14 +52,28 @@ class StickerMasterController extends Controller { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant name cannot be empty" + 'status_description' => "Plant code can't be empty!" + ], 400); + } + else if (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); } else if ($itemCode == null || $itemCode == '') { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item code cannot be empty" + 'status_description' => "Item Code can't be empty!" + ], 400); + } + else if (Str::length($itemCode) < 6 || !ctype_alnum($itemCode)) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid item code found!" ], 400); } @@ -75,48 +82,56 @@ class StickerMasterController extends Controller if (!$plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant not found" + 'status_description' => "Plant not found!" ], 400); } $plantId = $plant->id; - $item = Item::where('plant_id', $plantId)->where('code', $itemCode)->first(); - - $description = $item ? $item->description : ''; - - $uom = $item ? $item->uom : ''; - - $category = $item ? $item->category : ''; - $item = Item::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" + 'status_description' => "Item Code not found in item table!" ], 404); } - $stickerMaster = StickerMaster::where('plant_id', $plant->id)->where('item_id', $item->id)->first(); + $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); + } + + $description = $item ? $item->description : ''; + + $uom = $item ? $item->uom : ''; + + $category = $item ? $item->category : ''; + + $stickerMaster = StickerMaster::where('plant_id', $plantId)->where('item_id', $item->id)->first(); if (!$stickerMaster) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item Code not found in master table for the plant : $plant->name" + 'status_description' => "Item Code not found in sticker master table for the plant : '$plant->name'!" ], 404); } $output = [ "item_description" => $description ?? "", "uom" => $uom ?? "", - "Part_Validation_1" => $stickerMaster->laser_part_validation1 ?? "", - "Part_Validation_2" => $stickerMaster->laser_part_validation2 ?? "", - "Part_Validation_3" => "", - "Part_Validation_4" => "", - "Part_Validation_5" => "" + "Part_Validation_1" => $stickerMaster?->laser_part_validation1 ?? "", + "Part_Validation_2" => $stickerMaster?->laser_part_validation2 ?? "", + "Part_Validation_3" => $stickerMaster?->laser_part_validation3 ?? "", + "Part_Validation_4" => $stickerMaster?->laser_part_validation4 ?? "", + "Part_Validation_5" => $stickerMaster?->laser_part_validation5 ?? "", ]; return response()->json($output, 200); diff --git a/app/Http/Controllers/TestingPanelController.php b/app/Http/Controllers/TestingPanelController.php index 46818cf..2365141 100644 --- a/app/Http/Controllers/TestingPanelController.php +++ b/app/Http/Controllers/TestingPanelController.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers; use App\Models\Item; +use App\Models\Line; +use App\Models\Machine; use App\Models\MotorTestingMaster; use App\Models\Plant; use App\Models\TestingPanelReading; @@ -14,6 +16,7 @@ use chillerlan\QRCode\QROptions; use chillerlan\QRCode\Output\QROutputInterface; use Mpdf\QrCode\Output; use Mpdf\QrCode\QrCode; +use Str; class TestingPanelController extends Controller { @@ -36,19 +39,31 @@ class TestingPanelController extends Controller $header_auth = $request->header('Authorization'); $expectedToken = $expectedUser . ':' . $expectedPw; - if("Bearer " . $expectedToken !== $header_auth) + if ("Bearer " . $expectedToken != $header_auth) { - return response("Unauthorized", 403) - ->header('Content-Type', 'text/plain'); + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid authorization token!' + ], 403); } + + $data = $request->all(); - if ($data['plant_code'] == '' || !ctype_digit($data['plant_code'])) { + if ($data['plant_code'] == null || $data['plant_code'] == '') + { // return response("ERROR: Please provide a valid plant code.", 400) // ->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Please provide a valid plant code.' + 'status_description' => "Plant code can't be empty!" + ], 400); + } + else if (Str::length($data['plant_code']) < 4 || !is_numeric($data['plant_code']) || !preg_match('/^[1-9]\d{3,}$/', $data['plant_code']))//!ctype_digit($data['plant_code']) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid plant code found!" ], 400); } @@ -57,88 +72,80 @@ class TestingPanelController extends Controller //return response("Plant not found.", 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Plant not found.' + 'status_description' => 'Plant not found!' ], 400); } + $plantId = $plant->id; - $line = \App\Models\Line::where('name', $data['line_name'])->first(); + $line = Line::where('group_work_center', $data['line_name'])->first(); if (!$line) { //return response("Line not found.", 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Line not found.' + 'status_description' => 'Group work center not found!' ], 400); } - $line = \App\Models\Line::where('name', $data['line_name']) - ->where('plant_id', $plantId) - ->first(); + $line = Line::where('group_work_center', $data['line_name'])->where('plant_id', $plantId)->first(); if (!$line) { //return response( "Line not found for the specified plant : {$data['plant_code']}",400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Line not found for the specified plant : {$data['plant_code']}." + 'status_description' => "Group work center not found for the specified plant : '{$data['plant_code']}'!" ], 400); } $lineId = $line->id; - $machine = \App\Models\Machine::where('name', $data['machine_name']) - ->first(); + $machine = Machine::where('work_center', $data['machine_name'])->first(); if (!$machine) { // return response('Machine not found', 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Machine not found.' + 'status_description' => 'Work center not found!' ], 400); } - $machine = \App\Models\Machine::where('name', $data['machine_name']) - ->where('plant_id', $plantId) - ->first(); + $machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->first(); if (!$machine) { // return response("Machine not found for the specified plant : {$data['plant_code']}", 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Machine not found for the specified plant : {$data['plant_code']}." + 'status_description' => "Work center not found for the specified plant : '{$data['plant_code']}'!" ], 400); } - $machine = \App\Models\Machine::where('name', $data['machine_name']) - ->where('line_id', $lineId) - ->first(); + $machine = Machine::where('work_center', $data['machine_name'])->where('line_id', $lineId)->first(); if (!$machine) { // return response("Machine not found for the specified line : {$data['line_name']}", 400)->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Machine not found for the specified line : {$data['line_name']}." + 'status_description' => "Work center not found for the specified Group work center : '{$data['line_name']}'!" ], 400); } - $machine = \App\Models\Machine::where('name', $data['machine_name']) - ->where('plant_id', $plantId) - ->where('line_id', $lineId) - ->first(); + $machine = Machine::where('work_center', $data['machine_name'])->where('plant_id', $plantId)->where('line_id', $lineId)->first(); if (!$machine) { // return response('Machine not found for the specified plant and line', 400) // ->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Machine not found for the specified plant and line.' + 'status_description' => 'Work center not found for the specified plant and group work center!' ], 400); } + $machineId = $machine->id; try @@ -148,20 +155,18 @@ class TestingPanelController extends Controller $duplicateItemCodes = []; $existSnoCount = []; - if (!empty($data['item_codes']) && is_array($data['item_codes'])) { - foreach ($data['item_codes'] as $item) { $code = $item['item_code'] ?? null; // Check if item_code is present - if ($code == '') + if ($code == '' || $code == null) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Item code cannot be empty.' + 'status_description' => "Item code can't be empty!" ], 400); } @@ -212,7 +217,7 @@ class TestingPanelController extends Controller return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : {$plant->name}" + 'status_description' => "Item codes : ". implode(', ', $uniqueInvalidCodes)." not found in master for the specified plant : '{$plant->name}'!" ], 400); } @@ -248,7 +253,7 @@ class TestingPanelController extends Controller ->select(TestingPanelReading::raw('MAX(CAST(update_count AS INTEGER)) AS max_update_count')) ->value('max_update_count'); - $newUpdateCount = ($maxUpdateCount === null || $maxUpdateCount === '') ? 0 : $maxUpdateCount + 1; + $newUpdateCount = ($maxUpdateCount == null || $maxUpdateCount == '') ? 0 : $maxUpdateCount + 1; $updateCountString = (string) $newUpdateCount; @@ -328,27 +333,20 @@ class TestingPanelController extends Controller } else { - // $missingSno = array_diff($existSnoCount,$insertedSnoCount); - // $messages[] = "Missed serial numbers are: " . implode(', ', $missingSno); - + $missingSno = array_diff($existSnoCount,$insertedSnoCount); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Missed serial numbers are: " . implode(', ', $missingSno)' + 'status_description' => "Missed serial numbers are: " . implode(', ', $missingSno) ], 400); } - } - - // return response("" . implode("\n", $messages), 200) - // ->header('Content-Type', 'text/plain'); } catch (\Exception $e) { // return response($e->getMessage(), 500)->header('Content-Type', 'text/plain'); - return response()->json([ 'status_code' => 'ERROR', - 'status_description' => 'Internal Sever Error : '.$e?->getCode() + 'status_description' => 'Store testing panel readings internal server error : '.$e?->getCode() ], 500); } } @@ -363,40 +361,34 @@ class TestingPanelController extends Controller $header_auth = $request->header('Authorization'); $expectedToken = $expectedUser . ':' . $expectedPw; - if("Bearer " . $expectedToken !== $header_auth) - { - return response("Unauthorized", 403) - ->header('Content-Type', 'text/plain'); - } - //$data = $request->all(); - 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); } $plantCode = $request->header('plant-code'); $itemCode = $request->header('item-code'); - // $description = $item ? $item->description : ''; + // $description = $item ? $item->description : ''; - // Fetch item by code - $item = Item::where('code', $itemCode)->first(); - - // Get description or empty string if not found - $description = $item ? $item->description : ''; - - $category = $item ? $item->category : ''; - if ($plantCode === null || $plantCode === '') + 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 Name cannot be empty" + 'status_description' => "Plant code can't be empty!" + ], 400); + } + else if (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); } else if($itemCode == null || $itemCode == '') @@ -405,7 +397,14 @@ class TestingPanelController extends Controller // ->header('Content-Type', 'text/plain'); return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item Code cannot be empty" + 'status_description' => "Item Code can't be empty!" + ], 400); + } + else if(Str::length($itemCode) < 6 || !ctype_alnum($itemCode)) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => "Invalid item code found!" ], 400); } @@ -414,7 +413,7 @@ class TestingPanelController extends Controller if (!$plant) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Plant not found" + 'status_description' => "Plant not found!" ], 400); } @@ -426,59 +425,70 @@ class TestingPanelController extends Controller { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item Code not found in item table for the plant : $plant->name" + 'status_description' => "Item Code not found in item table!" ], 404); } - $motorTestingMaster = MotorTestingMaster::where('plant_id', $plant->id) - ->where('item_id', $item->id) - ->first(); + $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); + } + + // Get description or empty string if not found + $description = $item ? $item->description : ''; + + $category = $item ? $item->category : ''; + + $motorTestingMaster = MotorTestingMaster::where('plant_id', $plantId)->where('item_id', $item->id)->first(); if (!$motorTestingMaster) { return response()->json([ 'status_code' => 'ERROR', - 'status_description' => "Item Code not found in master table for the plant : $plant->name" + 'status_description' => "Item Code not found in motor testing master table for the plant : '$plant->name'!" ], 404); } $output = [ - "mot_model_name" => $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 ?? "", + "mot_model_name" => $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); - } /** diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index cd386f2..223a3bc 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -37,7 +37,7 @@ class UserController extends Controller $header_pass = $request->header('User-Pass'); $expectedToken = $expectedUser . ':' . $expectedPw; - if ("Bearer " . $expectedToken !== $header_auth) + if ("Bearer " . $expectedToken != $header_auth) { return response()->json([ 'status_code' => 'ERROR', @@ -52,6 +52,13 @@ class UserController extends Controller 'status_description' => 'Invalid user name found!' ], 400); } + else if (!$header_pass) + { + return response()->json([ + 'status_code' => 'ERROR', + 'status_description' => 'Invalid password found!' + ], 400); + } $existUser = User::where('name', $header_user)->first(); @@ -62,7 +69,7 @@ class UserController extends Controller 'status_description' => 'Unknown user name found!' ], 400); } - // if ($header_user !== $existUser->name) + // if ($header_user != $existUser->name) // { // return response()->json([ // 'status_code' => 'ERROR',