Added Validations for post and obd data
This commit is contained in:
@@ -6,6 +6,7 @@ use App\Models\Item;
|
|||||||
use App\Models\Plant;
|
use App\Models\Plant;
|
||||||
use App\Models\WeightValidation;
|
use App\Models\WeightValidation;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Str;
|
||||||
|
|
||||||
class ObdController extends Controller
|
class ObdController extends Controller
|
||||||
{
|
{
|
||||||
@@ -51,6 +52,14 @@ class ObdController extends Controller
|
|||||||
return response($message, 400)->header('Content-Type', 'text/plain');
|
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
|
// Lookup plant_id by plant_name
|
||||||
$plantId = Plant::where('name', $data['plant_name'])->value('id');
|
$plantId = Plant::where('name', $data['plant_name'])->value('id');
|
||||||
if (!$plantId) {
|
if (!$plantId) {
|
||||||
@@ -63,7 +72,12 @@ class ObdController extends Controller
|
|||||||
|
|
||||||
foreach ($data['line_numbers'] as $line)
|
foreach ($data['line_numbers'] as $line)
|
||||||
{
|
{
|
||||||
if (empty($line['line_number'])) continue;
|
//if (empty($line['line_number'])) continue;
|
||||||
|
//!isset($line['line_number']) ||
|
||||||
|
if ($line['line_number'] == '' || $line['line_number'] == null) {
|
||||||
|
return response("ERROR: Line Number can't be empty", 400)
|
||||||
|
->header('Content-Type', 'text/plain');
|
||||||
|
}
|
||||||
|
|
||||||
$exists = WeightValidation::where('plant_id', $plantId)
|
$exists = WeightValidation::where('plant_id', $plantId)
|
||||||
->where('obd_number', $data['obd_number'])
|
->where('obd_number', $data['obd_number'])
|
||||||
@@ -76,6 +90,45 @@ class ObdController extends Controller
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// $mandatoryFields = ['vehicle_number', 'bundle_number', 'picked_weight', 'scanned_by'];
|
||||||
|
// $missingFields = [];
|
||||||
|
// foreach ($mandatoryFields as $field) {
|
||||||
|
// if ($line[$field] == '' || $line[$field] == null) { //!isset($line[$field]) ||
|
||||||
|
// $missingFields[] = $field;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if (!empty($missingFields)) {
|
||||||
|
// $message = "ERROR: Missing or empty field(s): " . implode(', ', $missingFields) .
|
||||||
|
// " for line_number " . $line['line_number'];
|
||||||
|
// return response($message, 400)->header('Content-Type', 'text/plain');
|
||||||
|
// }
|
||||||
|
|
||||||
|
$mandatoryFields = ['vehicle_number', 'bundle_number', 'picked_weight', 'scanned_by'];
|
||||||
|
$missingFields = [];
|
||||||
|
|
||||||
|
foreach ($mandatoryFields as $field) {
|
||||||
|
if ($line[$field] == '' || $line[$field] == null) {
|
||||||
|
// Convert field name to a more readable format, e.g. vehicle_number -> Vehicle Number
|
||||||
|
$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');
|
||||||
|
}
|
||||||
|
|
||||||
$count = WeightValidation::where([
|
$count = WeightValidation::where([
|
||||||
'plant_id' => $plantId,
|
'plant_id' => $plantId,
|
||||||
'obd_number' => $data['obd_number'],
|
'obd_number' => $data['obd_number'],
|
||||||
@@ -190,13 +243,24 @@ class ObdController extends Controller
|
|||||||
->header('Content-Type', 'text/plain');
|
->header('Content-Type', 'text/plain');
|
||||||
}
|
}
|
||||||
|
|
||||||
$plantName = $request->header('plant-name'); // use lowercase
|
$plantName = $request->header('plant-name');
|
||||||
$obdNumber = $request->header('obd-number');
|
$obdNumber = $request->header('obd-number');
|
||||||
|
|
||||||
if (empty($plantName) || empty($obdNumber)) {
|
if (empty($plantName))
|
||||||
return response("ERROR: Missing required headers (plant_name, obd_number)", 400)
|
{
|
||||||
|
return response("ERROR: Plant Name can't be empty", 400)
|
||||||
->header('Content-Type', 'text/plain');
|
->header('Content-Type', 'text/plain');
|
||||||
}
|
}
|
||||||
|
else if(empty($obdNumber))
|
||||||
|
{
|
||||||
|
return response("ERROR: OBD Number can't be empty", 400)
|
||||||
|
->header('Content-Type', 'text/plain');
|
||||||
|
}
|
||||||
|
else if(Str::length($obdNumber) < 8 || !ctype_alnum($obdNumber))
|
||||||
|
{
|
||||||
|
return response("ERROR: OBD Number should contain minimum 8 digits", 400)
|
||||||
|
->header('Content-Type', 'text/plain');
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch the plant id by name
|
// Fetch the plant id by name
|
||||||
$plantId = Plant::where('name', $plantName)->value('id');
|
$plantId = Plant::where('name', $plantName)->value('id');
|
||||||
@@ -206,12 +270,30 @@ class ObdController extends Controller
|
|||||||
->header('Content-Type', 'text/plain');
|
->header('Content-Type', 'text/plain');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// $records = WeightValidation::where('plant_id', $plantId)
|
||||||
|
// ->where('obd_number', $obdNumber)
|
||||||
|
// ->get();
|
||||||
|
// Fetch and filter records where vehicle_number is empty or null
|
||||||
|
|
||||||
|
$exists = WeightValidation::where('plant_id', $plantId)
|
||||||
|
->where('obd_number', $obdNumber)
|
||||||
|
->exists();
|
||||||
|
|
||||||
|
if (!$exists)
|
||||||
|
{
|
||||||
|
return response("ERROR: OBD number $obdNumber does not exist for plant '$plantName'", 404)
|
||||||
|
->header('Content-Type', 'text/plain');
|
||||||
|
}
|
||||||
|
|
||||||
$records = WeightValidation::where('plant_id', $plantId)
|
$records = WeightValidation::where('plant_id', $plantId)
|
||||||
->where('obd_number', $obdNumber)
|
->where('obd_number', $obdNumber)
|
||||||
->get();
|
->get()
|
||||||
|
->filter(function ($record) {
|
||||||
|
return $record->vehicle_number == '' || $record->vehicle_number == null;
|
||||||
|
});
|
||||||
|
|
||||||
if ($records->isEmpty()) {
|
if ($records->isEmpty()) {
|
||||||
return response("ERROR: No records found", 404)->header('Content-Type', 'text/plain');
|
return response("SUCCESS: Already scanning process completed for the OBD Number", 200)->header('Content-Type', 'text/plain');
|
||||||
}
|
}
|
||||||
|
|
||||||
$itemIds = $records->pluck('item_id')->unique();
|
$itemIds = $records->pluck('item_id')->unique();
|
||||||
@@ -234,7 +316,7 @@ class ObdController extends Controller
|
|||||||
'Heat_Number' => $item->heat_number ?? "",
|
'Heat_Number' => $item->heat_number ?? "",
|
||||||
'Actual_Weight' => $item->obd_weight ?? "",
|
'Actual_Weight' => $item->obd_weight ?? "",
|
||||||
];
|
];
|
||||||
})->toArray()
|
})->values()->toArray()
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user