Refactor readFiles method to enhance CSV row validation and ensure unique remaining rows are collected
This commit is contained in:
@@ -183,14 +183,15 @@ class SapFileController extends Controller
|
|||||||
$serialNumbers = [];
|
$serialNumbers = [];
|
||||||
$remainingRows = [];
|
$remainingRows = [];
|
||||||
|
|
||||||
foreach ($lines as $line) {
|
foreach ($lines as $line)
|
||||||
|
{
|
||||||
$values = str_getcsv($line);
|
$values = str_getcsv($line);
|
||||||
|
|
||||||
// Debug: make sure CSV split is correct
|
// Debug: make sure CSV split is correct
|
||||||
if (count($values) < 6) {
|
if (count($values) < 6) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'status' => 'ERROR',
|
'status' => 'ERROR',
|
||||||
'message' => 'CSV row has fewer than 6 columns',
|
'message' => 'In text file has fewer than 6 columns',
|
||||||
'line' => $line
|
'line' => $line
|
||||||
], 400);
|
], 400);
|
||||||
}
|
}
|
||||||
@@ -199,15 +200,21 @@ class SapFileController extends Controller
|
|||||||
|
|
||||||
$remainingRow = $values;
|
$remainingRow = $values;
|
||||||
unset($remainingRow[5]);
|
unset($remainingRow[5]);
|
||||||
|
// $remainingRow = array_values($remainingRow); // reindex
|
||||||
|
// $remainingRows[] = $remainingRow;
|
||||||
$remainingRow = array_values($remainingRow); // reindex
|
$remainingRow = array_values($remainingRow); // reindex
|
||||||
$remainingRows[] = $remainingRow;
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json([
|
// Only add unique remaining rows
|
||||||
|
if (!in_array($remainingRow, $remainingRows, true)) {
|
||||||
|
$remainingRows[] = $remainingRow;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
'status' => 'SUCCESS',
|
'status' => 'SUCCESS',
|
||||||
'serial_numbers' => $serialNumbers,
|
'serial_numbers' => $serialNumbers,
|
||||||
'remaining_rows_sample' => array_slice($remainingRows, 0, 5), // just first 5 for debug
|
'remaining_rows_sample' => array_slice($remainingRows, 0, 5), // just first 5 for debug
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user