From 4f4d0c266af2f00aaf516b501b13e9398311af69 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Wed, 25 Dec 2019 17:16:34 +0500 Subject: [PATCH] Allow nullable field and calculate distance if nulled for flight import #478 (#482) --- app/Services/ImportExport/FlightImporter.php | 11 +++++++++-- tests/ImporterTest.php | 5 +---- tests/data/flights.csv | 1 + 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/Services/ImportExport/FlightImporter.php b/app/Services/ImportExport/FlightImporter.php index 6984101f..825904ff 100644 --- a/app/Services/ImportExport/FlightImporter.php +++ b/app/Services/ImportExport/FlightImporter.php @@ -3,7 +3,6 @@ namespace App\Services\ImportExport; use App\Contracts\ImportExport; -use App\Models\Airport; use App\Models\Enums\Days; use App\Models\Enums\FlightType; use App\Models\Fare; @@ -37,7 +36,7 @@ class FlightImporter extends ImportExport 'dpt_time' => 'nullable', 'arr_time' => 'nullable', 'level' => 'nullable|integer', - 'distance' => 'required|numeric', + 'distance' => 'nullable|numeric', 'flight_time' => 'required|integer', 'flight_type' => 'required|alpha', 'route' => 'nullable', @@ -109,6 +108,14 @@ class FlightImporter extends ImportExport // Any specific transformations + // Check/calculate the distance + if (empty($row['distance'])) { + $row['distance'] = $this->airportSvc->calculateDistance( + $row['dpt_airport'], + $row['arr_airport'] + ); + } + // Check for a valid value $flight_type = $row['flight_type']; if (!array_key_exists($flight_type, FlightType::labels())) { diff --git a/tests/ImporterTest.php b/tests/ImporterTest.php index ac6a5b65..a51529c2 100644 --- a/tests/ImporterTest.php +++ b/tests/ImporterTest.php @@ -19,9 +19,6 @@ use App\Services\ImportExport\FlightExporter; use App\Services\ImportService; use Illuminate\Validation\ValidationException; -/** - * Class ImporterTest - */ class ImporterTest extends TestCase { private $importBaseClass; @@ -470,7 +467,7 @@ class ImporterTest extends TestCase $file_path = base_path('tests/data/flights.csv'); $status = $this->importSvc->importFlights($file_path); - $this->assertCount(1, $status['success']); + $this->assertCount(2, $status['success']); $this->assertCount(1, $status['errors']); // See if it imported diff --git a/tests/data/flights.csv b/tests/data/flights.csv index 640dcf7a..5210eb51 100644 --- a/tests/data/flights.csv +++ b/tests/data/flights.csv @@ -1,3 +1,4 @@ airline,flight_number,route_code,route_leg,dpt_airport,arr_airport,alt_airport,days,dpt_time,arr_time,level,distance,flight_time,flight_type,route,notes,active,subfleets,fares,fields VMS,1972,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41 " ",1972,,,KAUS,KJFK,KLGA,15,0810 CST,1235 EST,350,1477,207,J,ILEXY2 ZENZI LFK ELD J29 MEM Q29 JHW J70 STENT J70 MAGIO J70 LVZ LENDY6,"Just a flight",1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=4;Arrival Gate=C41 +VMS,113,,,KJFK,KAUS,KDFW,15,0810 EST,1035 CST,350,,207,J,,"Empty distance",1,A32X,Y?price=300&cost=100&capacity=130;F?price=600&cost=400;B?,Departure Gate=C41;Arrival Gate=2