@fleetbase/fleetops-engine 0.3.9 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/composer.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fleetbase/fleetops-api",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "Fleet & Transport Management Extension for Fleetbase",
5
5
  "keywords": [
6
6
  "fleetbase-extension",
@@ -22,7 +22,7 @@
22
22
  ],
23
23
  "require": {
24
24
  "php": "^7.4|^8.0",
25
- "fleetbase/core-api": "^1.3.7",
25
+ "fleetbase/core-api": "^1.3.10",
26
26
  "barryvdh/laravel-dompdf": "^2.0",
27
27
  "brick/geo": "0.7.2",
28
28
  "cknow/laravel-money": "^7.1",
package/extension.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "Fleet-Ops",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "Fleet & Transport Management Extension for Fleetbase",
5
5
  "repository": "https://github.com/fleetbase/fleetops",
6
6
  "license": "MIT",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fleetbase/fleetops-engine",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "Fleet & Transport Management Extension for Fleetbase",
5
5
  "fleetbase": {
6
6
  "route": "fleet-ops",
@@ -0,0 +1,65 @@
1
+ <?php
2
+
3
+ namespace Fleetbase\FleetOps\Console\Commands;
4
+
5
+ use Fleetbase\FleetOps\Models\Driver;
6
+ use Fleetbase\Models\Company;
7
+ use Fleetbase\Models\CompanyUser;
8
+ use Illuminate\Console\Command;
9
+
10
+ class FixDriverCompanies extends Command
11
+ {
12
+ /**
13
+ * The name and signature of the console command.
14
+ *
15
+ * @var string
16
+ */
17
+ protected $signature = 'fleetops:fix-driver-companies';
18
+
19
+ /**
20
+ * The console command description.
21
+ *
22
+ * @var string
23
+ */
24
+ protected $description = 'This is a command which checks driver\'s users to make sure they are assigned to company, if not it assigns the user to the drivers company';
25
+
26
+ /**
27
+ * Execute the console command.
28
+ *
29
+ * This method is responsible for the main logic of the command.
30
+ * It fetches dispatchable orders, notifies about the current time and number of orders,
31
+ * and then dispatches each order if there are nearby drivers.
32
+ *
33
+ * @return int
34
+ */
35
+ public function handle()
36
+ {
37
+ $drivers = Driver::whereHas('user', function ($query) {
38
+ $query->whereNull('company_uuid');
39
+ $query->withTrashed();
40
+ })->with(['user'])->get();
41
+
42
+ // Fix these drivers
43
+ foreach ($drivers as $driver) {
44
+ $user = $driver->user;
45
+ if ($user) {
46
+ $this->line('Found user ' . $user->name . ' (' . $user->email . ') which has no company assigned');
47
+ // Get company from driver profile
48
+ $company = Company::where('uuid', $driver->company_uuid)->first();
49
+ if ($company) {
50
+ $user->assignCompany($company);
51
+
52
+ // create company user
53
+ CompanyUser::create([
54
+ 'user_uuid' => $user->uuid,
55
+ 'company_uuid' => $company->uuid,
56
+ 'status' => 'active',
57
+ ]);
58
+
59
+ // Inform
60
+ $this->line('User ' . $user->email . ' was assigned to company: ' . $company->name);
61
+ }
62
+ }
63
+ }
64
+ }
65
+ }
@@ -7,32 +7,30 @@ use Illuminate\Contracts\Support\Responsable;
7
7
  use Illuminate\Http\JsonResponse;
8
8
 
9
9
  /**
10
- * Class IntegratedVendorException
10
+ * Class IntegratedVendorException.
11
11
  *
12
12
  * Custom exception class for IntegratedVendor-related exceptions.
13
- *
14
- * @package Fleetbase\FleetOps\Exceptions
15
13
  */
16
14
  class IntegratedVendorException extends \Exception implements Responsable
17
15
  {
18
16
  /**
19
- * @var IntegratedVendor|null The IntegratedVendor instance associated with the exception.
17
+ * @var IntegratedVendor|null the IntegratedVendor instance associated with the exception
20
18
  */
21
19
  public ?IntegratedVendor $integratedVendor;
22
20
 
23
21
  /**
24
- * @var string The trigger method that caused the exception.
22
+ * @var string the trigger method that caused the exception
25
23
  */
26
24
  public string $triggerMethod;
27
25
 
28
26
  /**
29
27
  * IntegratedVendorException constructor.
30
28
  *
31
- * @param string $message The exception message.
32
- * @param IntegratedVendor|null $integratedVendor The IntegratedVendor instance associated with the exception.
33
- * @param string|null $triggerMethod The trigger method that caused the exception.
34
- * @param int $code The exception code.
35
- * @param \Throwable|null $previous The previous throwable used for the exception chaining.
29
+ * @param string $message the exception message
30
+ * @param IntegratedVendor|null $integratedVendor the IntegratedVendor instance associated with the exception
31
+ * @param string|null $triggerMethod the trigger method that caused the exception
32
+ * @param int $code the exception code
33
+ * @param \Throwable|null $previous the previous throwable used for the exception chaining
36
34
  */
37
35
  public function __construct(string $message = '', IntegratedVendor $integratedVendor = null, string $triggerMethod = null, int $code = 400, \Throwable $previous = null)
38
36
  {
@@ -45,6 +43,7 @@ class IntegratedVendorException extends \Exception implements Responsable
45
43
  * Get the response representing the exception.
46
44
  *
47
45
  * @param \Illuminate\Http\Request $request
46
+ *
48
47
  * @return \Illuminate\Http\JsonResponse
49
48
  */
50
49
  public function toResponse($request)
@@ -53,8 +52,8 @@ class IntegratedVendorException extends \Exception implements Responsable
53
52
 
54
53
  return new JsonResponse(
55
54
  [
56
- 'errors' => [$errorMessage],
57
- 'integratedVendorId' => data_get($this->integratedVendor, 'uuid')
55
+ 'errors' => [$errorMessage],
56
+ 'integratedVendorId' => data_get($this->integratedVendor, 'uuid'),
58
57
  ],
59
58
  400
60
59
  );
@@ -360,32 +360,31 @@ class DriverController extends Controller
360
360
  {
361
361
  $identity = $request->input('identity');
362
362
  $password = $request->input('password');
363
- $attrs = $request->input(['name', 'phone', 'email']);
363
+ // $attrs = $request->input(['name', 'phone', 'email']);
364
364
 
365
- $user = User::where('email', $identity)->orWhere('phone', static::phone($identity))->first();
365
+ // Get driver attempting to authenticate via phone
366
+ $user = User::where(
367
+ function ($query) use ($identity) {
368
+ $query->where('phone', static::phone($identity));
369
+ $query->owWhere('email', $identity);
370
+ }
371
+ )->whereHas('driver')->first();
366
372
 
373
+ // Check password to authenticate driver
367
374
  if (!Hash::check($password, $user->password)) {
368
375
  return response()->apiError('Authentication failed using password provided.', 401);
369
376
  }
370
377
 
371
- // Get the company session for the user
372
- $company = Flow::getCompanySessionForUser($user);
378
+ // Get the user's company for this driver profile
379
+ $company = $this->_getDriverCompanyFromUser($user);
373
380
 
374
381
  // Get driver record
375
- $driver = Driver::firstOrCreate(
376
- [
377
- 'user_uuid' => $user->uuid,
378
- 'company_uuid' => $company->uuid,
379
- ],
382
+ $driver = Driver::where(
380
383
  [
381
384
  'user_uuid' => $user->uuid,
382
385
  'company_uuid' => $company->uuid,
383
- 'name' => data_get($attrs, 'name', $user->name),
384
- 'phone' => data_get($attrs, 'phone', $user->phone),
385
- 'email' => data_get($attrs, 'email', $user->email),
386
- 'location' => new Point(0, 0),
387
386
  ]
388
- );
387
+ )->first();
389
388
 
390
389
  // generate auth token
391
390
  try {
@@ -409,19 +408,18 @@ class DriverController extends Controller
409
408
  $phone = static::phone();
410
409
 
411
410
  // check if user exists
412
- $user = User::where('phone', $phone)->whereNull('deleted_at')->withoutGlobalScopes()->first();
413
-
411
+ $user = User::where('phone', $phone)->whereHas('driver')->whereNull('deleted_at')->withoutGlobalScopes()->first();
414
412
  if (!$user) {
415
413
  return response()->apiError('No driver with this phone # found.');
416
414
  }
417
415
 
418
- // get the current company session
419
- $company = Flow::getCompanySessionForUser($user);
416
+ // Get the user's company for this driver profile
417
+ $company = $this->_getDriverCompanyFromUser($user);
420
418
 
421
419
  // generate verification token
422
420
  try {
423
421
  VerificationCode::generateSmsVerificationFor($user, 'driver_login', function ($verification) use ($company) {
424
- return "Your {$company->name} verification code is {$verification->code}";
422
+ return 'Your ' . data_get($company, 'name', config('app.name')) . ' verification code is ' . $verification->code;
425
423
  });
426
424
  } catch (\Throwable $e) {
427
425
  return response()->apiError($e->getMessage());
@@ -458,29 +456,20 @@ class DriverController extends Controller
458
456
 
459
457
  // find and verify code
460
458
  $verificationCode = VerificationCode::where(['subject_uuid' => $user->uuid, 'code' => $code, 'for' => $for])->exists();
461
-
462
459
  if (!$verificationCode && $code !== config('fleetops.navigator.bypass_verification_code')) {
463
460
  return response()->apiError('Invalid verification code!');
464
461
  }
465
462
 
466
- // get the current company session
467
- $company = Flow::getCompanySessionForUser($user);
463
+ // Get the user's company for this driver profile
464
+ $company = $this->_getDriverCompanyFromUser($user);
468
465
 
469
466
  // get driver record
470
- $driver = Driver::firstOrCreate(
471
- [
472
- 'user_uuid' => $user->uuid,
473
- 'company_uuid' => $company->uuid,
474
- ],
467
+ $driver = Driver::where(
475
468
  [
476
469
  'user_uuid' => $user->uuid,
477
470
  'company_uuid' => $company->uuid,
478
- 'name' => data_get($attrs, 'name', $user->name),
479
- 'phone' => data_get($attrs, 'phone', $user->phone),
480
- 'email' => data_get($attrs, 'email', $user->email),
481
- 'location' => new Point(0, 0),
482
471
  ]
483
- );
472
+ )->first();
484
473
 
485
474
  // generate auth token
486
475
  try {
@@ -707,4 +696,24 @@ class DriverController extends Controller
707
696
 
708
697
  return response()->json($route);
709
698
  }
699
+
700
+ private function _getDriverCompanyFromUser(User $user): ?Company
701
+ {
702
+ // company defaults to null
703
+ $company = null;
704
+
705
+ // Load the driver profile to get the company
706
+ $driverProfile = Driver::where('user_uuid', $user->uuid)->first();
707
+ if ($driverProfile) {
708
+ // get company from driver profile
709
+ $company = Company::where('uuid', $driverProfile->company_uuid)->first();
710
+ }
711
+
712
+ // If unable to find company from driver profile, fallback to session flow
713
+ if (!$company) {
714
+ $company = Flow::getCompanySessionForUser($user);
715
+ }
716
+
717
+ return $company;
718
+ }
710
719
  }
@@ -13,10 +13,12 @@ use Fleetbase\FleetOps\Models\Driver;
13
13
  use Fleetbase\FleetOps\Models\Order;
14
14
  use Fleetbase\FleetOps\Support\Utils;
15
15
  use Fleetbase\Http\Requests\ExportRequest;
16
+ use Fleetbase\Models\CompanyUser;
16
17
  use Fleetbase\Models\Invite;
17
18
  use Fleetbase\Models\User;
18
19
  use Fleetbase\Models\VerificationCode;
19
20
  use Fleetbase\Notifications\UserInvited;
21
+ use Fleetbase\Support\Auth;
20
22
  use Grimzy\LaravelMysqlSpatial\Types\Point;
21
23
  use Illuminate\Http\Request;
22
24
  use Illuminate\Support\Facades\DB;
@@ -155,11 +157,25 @@ class DriverController extends FleetOpsController
155
157
  $userInput['password'] = Str::random(14);
156
158
  }
157
159
 
158
- $userInput['company_uuid'] = session('company');
160
+ // Get current session company
161
+ $company = Auth::getCompany();
162
+ $userInput['company_uuid'] = $company ? $company->uuid : session('company');
159
163
 
160
164
  // Create user account
161
165
  $user = User::create($userInput);
162
166
 
167
+ // Assign user to company
168
+ if ($company) {
169
+ $user->assignCompany($company);
170
+
171
+ // create company user
172
+ CompanyUser::create([
173
+ 'user_uuid' => $user->uuid,
174
+ 'company_uuid' => $company->uuid,
175
+ 'status' => 'active',
176
+ ]);
177
+ }
178
+
163
179
  // Set user type as driver
164
180
  $user->setUserType('driver');
165
181
 
@@ -42,7 +42,7 @@ class Driver extends FleetbaseResource
42
42
  'jobs' => $this->whenLoaded('jobs', CurrentJob::collection($this->jobs()->without(['driverAssigned'])->get())),
43
43
  'vendor' => $this->whenLoaded('vendor', new Vendor($this->vendor)),
44
44
  'fleets' => $this->whenLoaded('fleets', Fleet::collection($this->fleets()->without('drivers')->get())),
45
- 'location' => data_get($this, 'location', new Point(0, 0)),
45
+ 'location' => $this->wasRecentlyCreated ? new Point(0, 0) : data_get($this, 'location', new Point(0, 0)),
46
46
  'heading' => (int) data_get($this, 'heading', 0),
47
47
  'altitude' => (int) data_get($this, 'altitude', 0),
48
48
  'speed' => (int) data_get($this, 'speed', 0),
@@ -44,6 +44,7 @@ class FleetOpsServiceProvider extends CoreServiceProvider
44
44
  \Fleetbase\FleetOps\Console\Commands\DispatchAdhocOrders::class,
45
45
  \Fleetbase\FleetOps\Console\Commands\DispatchOrders::class,
46
46
  \Fleetbase\FleetOps\Console\Commands\TrackOrderDistanceAndTime::class,
47
+ \Fleetbase\FleetOps\Console\Commands\FixDriverCompanies::class,
47
48
  ];
48
49
 
49
50
  /**
@@ -6,6 +6,7 @@ use Fleetbase\FleetOps\Models\Driver;
6
6
  use Fleetbase\FleetOps\Models\Order;
7
7
  use Fleetbase\FleetOps\Models\Waypoint;
8
8
  use Fleetbase\Models\Company;
9
+ use Fleetbase\Models\CompanyUser;
9
10
  use Fleetbase\Models\Extension;
10
11
  use Fleetbase\Models\ExtensionInstall;
11
12
  use Fleetbase\Models\User;
@@ -550,9 +551,25 @@ class Flow
550
551
  return Company::currentSession();
551
552
  }
552
553
 
553
- public static function getCompanySessionForUser(User $user)
554
+ public static function getCompanySessionForUser(User $user): ?Company
554
555
  {
555
- return Company::where('uuid', $user->company_uuid)->first();
556
+ if (Str::isUuid($user->company_uuid)) {
557
+ $company = Company::where('uuid', $user->company_uuid)->first();
558
+ if ($company) {
559
+ return $company;
560
+ }
561
+ }
562
+
563
+ // fallback to get user's first company
564
+ $userCompany = CompanyUser::where('user_uuid', $user->uuid)->first();
565
+ if ($userCompany) {
566
+ $company = Company::where('uuid', $userCompany->company_uuid)->first();
567
+ if ($company) {
568
+ return $company;
569
+ }
570
+ }
571
+
572
+ return null;
556
573
  }
557
574
 
558
575
  public static function getDefaultOrderFlow()