Skip to content

Commit

Permalink
implemented imports for fleet
Browse files Browse the repository at this point in the history
  • Loading branch information
doljko committed May 29, 2024
1 parent d922d35 commit 1183988
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function export(ExportRequest $request)
public function import(ImportRequest $request)
{
$disk = $request->input('disk', config('filesystems.default'));
$files = File::importsFromRequest($request);
$files = $request->filesFromIds();

foreach ($files as $file) {
try {
Expand Down
27 changes: 4 additions & 23 deletions server/src/Http/Controllers/Internal/v1/FleetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,35 +157,16 @@ public static function assignVehicle(FleetActionRequest $request)
public function import(ImportRequest $request)
{
$disk = $request->input('disk', config('filesystems.default'));
$files = $request->input('files');
$files = File::whereIn('uuid', $files)->get();
$validFileTypes = ['csv', 'tsv', 'xls', 'xlsx'];
$imports = collect();
$files = File::importsFromRequest($request);

foreach ($files as $file) {
// validate file type
if (!Str::endsWith($file->path, $validFileTypes)) {
return response()->error('Invalid file uploaded, must be one of the following: ' . implode(', ', $validFileTypes));
}

try {
$data = Excel::toArray(new FleetImport(), $file->path, $disk);
} catch (\Exception $e) {
Excel::import(new FleetImport(), $file->path, $disk);
} catch (\Throwable $e) {
return response()->error('Invalid file, unable to proccess.');
}

if (count($data) === 1) {
$imports = $imports->concat($data[0]);
}
}

$imports = $imports->map(
function ($row) {
return $row;
})->values()->toArray();

Fleet::bulkInsert($imports);

return response()->json(['status' => 'ok', 'message' => 'Import completed', 'count' => count($imports)]);
return response()->json(['status' => 'ok', 'message' => 'Import completed']);
}
}
10 changes: 8 additions & 2 deletions server/src/Imports/FleetImport.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

namespace Fleetbase\FleetOps\Imports;

use Fleetbase\FleetOps\Models\Fleet;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
Expand All @@ -13,6 +13,12 @@ class FleetImport implements ToCollection, WithHeadingRow
*/
public function collection(Collection $rows)
{
return $rows;
foreach ($rows as $row) {
if ($row instanceof Collection) {
$row = array_filter($row->toArray());
}

Fleet::createFromImport($row, true);
}
}
}
24 changes: 24 additions & 0 deletions server/src/Models/Fleet.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fleetbase\FleetOps\Models;

use Fleetbase\Models\Model;
use Fleetbase\FleetOps\Support\Utils;
use Fleetbase\Traits\HasApiModelBehavior;
use Fleetbase\Traits\HasPublicId;
use Fleetbase\Traits\HasUuid;
Expand Down Expand Up @@ -214,4 +215,27 @@ public function getVehiclesOnlineCountAttribute()
{
return $this->vehicles()->where('online', 1)->count();
}

public static function createFromImport(array $row, bool $saveInstance = false): Fleet
{
// Filter array for null key values
$row = array_filter($row);

// Get fleet columns
$name = Utils::or($row, ['name', 'full_name', 'first_name', 'contact', 'person']);

// Create contact
$fleet = new static([
'company_uuid' => session('company'),
'name' => $name,
'type' => 'fleet',
'vendor' => 'active',
]);

if ($saveInstance === true) {
$fleet->save();
}

return $fleet;
}
}

0 comments on commit 1183988

Please sign in to comment.