Skip to content

Commit

Permalink
Merge pull request #140 from fleetbase/dev-v0.5.22
Browse files Browse the repository at this point in the history
v0.5.22 ~ hotfix #139, urgent hotfix fleetbase#353
  • Loading branch information
roncodes authored Feb 21, 2025
2 parents abc3b40 + ed52e69 commit a25c7f9
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 15 deletions.
6 changes: 3 additions & 3 deletions addon/components/live-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ export default class LiveMapComponent extends Component {
* Creates an instance of LiveMapComponent.
* @memberof LiveMapComponent
*/
constructor(owner, { zoom = 12, darkMode = false }) {
constructor(owner, { zoom = 12 }) {
super(...arguments);

this.zoom = zoom;
this.changeTileSource(darkMode ? 'dark' : 'light');
this.changeTileSource('light');
this.movementTracker.registerTrackingMarker(owner);
this.setupComponent();
}
Expand Down Expand Up @@ -386,7 +386,7 @@ export default class LiveMapComponent extends Component {
this.triggerAction('onLoad', event);

// handle theme change
this._checkThemeChanged();
// this._checkThemeChanged();
}

_checkThemeChanged() {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fleetbase/fleetops-api",
"version": "0.5.21",
"version": "0.5.22",
"description": "Fleet & Transport Management Extension for Fleetbase",
"keywords": [
"fleetbase-extension",
Expand Down
2 changes: 1 addition & 1 deletion extension.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Fleet-Ops",
"version": "0.5.21",
"version": "0.5.22",
"description": "Fleet & Transport Management Extension for Fleetbase",
"repository": "https://github.com/fleetbase/fleetops",
"license": "AGPL-3.0-or-later",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fleetbase/fleetops-engine",
"version": "0.5.21",
"version": "0.5.22",
"description": "Fleet & Transport Management Extension for Fleetbase",
"fleetbase": {
"route": "fleet-ops"
Expand Down
165 changes: 165 additions & 0 deletions server/src/Events/VehicleLocationChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Fleetbase\FleetOps\Events;

use Fleetbase\FleetOps\Models\Vehicle;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;

class VehicleLocationChanged implements ShouldBroadcast
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* The event id.
*
* @var string
*/
public $eventId;

/**
* The datetime instance the broadcast ws triggered.
*
* @var string
*/
public $sentAt;

/**
* The uuid of the vehicle.
*
* @var string
*/
public $vehicleUuid;

/**
* The public id of the vehicle.
*
* @var string
*/
public $vehicleId;

/**
* The plate number of the vehicle.
*
* @var string
*/
public $vehiclePlateNumber;

/**
* The name of the vehicle.
*
* @var string
*/
public $vehicleName;

/**
* The new vehicle location.
*
* @var string
*/
public $location;

/**
* The vehicle altitude.
*
* @var string
*/
public $altitude;

/**
* The nvehicle heading.
*
* @var string
*/
public $heading;

/**
* The vehicle speed.
*
* @var string
*/
public $speed;

/**
* Optional, additional data.
*
* @var array
*/
public $additionalData = [];

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Vehicle $vehicle, array $additionalData = [])
{
$this->eventId = uniqid('event_');
$this->sentAt = Carbon::now()->toDateTimeString();
$this->additionalData = $additionalData;
$this->vehicleUuid = $vehicle->uuid;
$this->vehicleId = $vehicle->public_id;
$this->vehiclePlateNumber = $vehicle->plate_number;
$this->vehicleName = $vehicle->display_name;
$this->location = $vehicle->location;
$this->altitude = $vehicle->altitude;
$this->heading = $vehicle->heading;
$this->speed = $vehicle->speed;
}

/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return [
new Channel('company.' . session('company')),
new Channel('api.' . session('api_credential')),
new Channel('vehicle.' . $this->vehicleId),
new Channel('vehicle.' . $this->vehicleUuid),
];
}

/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastAs()
{
return 'vehicle.location_changed';
}

/**
* Get the data to broadcast.
*
* @return array
*/
public function broadcastWith()
{
return [
'id' => $this->eventId,
'api_version' => config('api.version'),
'event' => $this->broadcastAs(),
'created_at' => $this->sentAt,
'data' => [
'id' => $this->vehicleId,
'plate_number' => $this->vehiclePlateNumber,
'name' => $this->vehicleName,
'location' => $this->location,
'altitude' => $this->altitude,
'heading' => $this->heading,
'speed' => $this->speed,
'additionalData' => $this->additionalData,
],
];
}
}
4 changes: 3 additions & 1 deletion server/src/Http/Controllers/Api/v1/DriverController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fleetbase\FleetOps\Http\Controllers\Api\v1;

use Fleetbase\FleetOps\Events\DriverLocationChanged;
use Fleetbase\FleetOps\Events\VehicleLocationChanged;
use Fleetbase\FleetOps\Http\Requests\CreateDriverRequest;
use Fleetbase\FleetOps\Http\Requests\DriverSimulationRequest;
use Fleetbase\FleetOps\Http\Requests\UpdateDriverRequest;
Expand Down Expand Up @@ -323,6 +324,7 @@ public function track(string $id, Request $request)
'speed' => $speed,
]);
$driver->vehicle->createPositionWithOrderContext();
broadcast(new VehicleLocationChanged($driver->vehicle, ['driver' => $driver->public_id]));
}

if ($isGeocodable) {
Expand Down Expand Up @@ -405,7 +407,7 @@ public function login(Request $request)
$user = User::where(
function ($query) use ($identity) {
$query->where('phone', static::phone($identity));
$query->owWhere('email', $identity);
$query->orWhere('email', $identity);
}
)->whereHas('driver')->first();

Expand Down
39 changes: 39 additions & 0 deletions server/src/Http/Controllers/Api/v1/VehicleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fleetbase\FleetOps\Http\Controllers\Api\v1;

use Fleetbase\FleetOps\Events\VehicleLocationChanged;
use Fleetbase\FleetOps\Http\Requests\CreateVehicleRequest;
use Fleetbase\FleetOps\Http\Requests\UpdateVehicleRequest;
use Fleetbase\FleetOps\Http\Resources\v1\DeletedResource;
Expand All @@ -10,6 +11,7 @@
use Fleetbase\FleetOps\Models\Vehicle;
use Fleetbase\FleetOps\Support\Utils;
use Fleetbase\Http\Controllers\Controller;
use Fleetbase\LaravelMysqlSpatial\Types\Point;
use Illuminate\Http\Request;

class VehicleController extends Controller
Expand Down Expand Up @@ -208,4 +210,41 @@ public function delete($id)
// response the vehicle resource
return new DeletedResource($vehicle);
}

/**
* Update vehicles geolocation data.
*
* @return \Illuminate\Http\Response
*/
public function track(string $id, Request $request)
{
$latitude = $request->input('latitude');
$longitude = $request->input('longitude');
$altitude = $request->input('altitude');
$heading = $request->input('heading');
$speed = $request->input('speed');

try {
$vehicle = Vehicle::findRecordOrFail($id);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $exception) {
return response()->json(
[
'error' => 'Vehicle resource not found.',
],
404
);
}

$vehicle->update([
'location' => new Point($latitude, $longitude),
'altitude' => $altitude,
'heading' => $heading,
'speed' => $speed,
]);

broadcast(new VehicleLocationChanged($vehicle));
$vehicle->createPositionWithOrderContext();

return new VehicleResource($vehicle);
}
}
4 changes: 2 additions & 2 deletions server/src/Models/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public static function createUserFromContact(Contact $contact, bool $sendInvite
if ($contact->phone) {
$query->orWhere('phone', $contact->phone);
}
})->first();
})->whereNull('deleted_at')->first();
if ($existingUser) {
throw new UserAlreadyExistsException('User already exists, try to assigning the user to this contact.', $existingUser);
}
Expand Down Expand Up @@ -469,7 +469,7 @@ public function hasUser(): bool
{
$user = $this->getUser();

return Str::isUuid($this->user_uuid) && $user instanceof User;
return Str::isUuid($this->user_uuid) || $user instanceof User;
}

public function doesntHaveUser(): bool
Expand Down
2 changes: 1 addition & 1 deletion server/src/Models/Vehicle.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getActivitylogOptions(): LogOptions
public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom(['year', 'make', 'model', 'trim', 'plate_number', 'internal_id'])
->generateSlugsFrom(['year', 'make', 'model', 'trim', 'plate_number'])
->saveSlugsTo('slug');
}

Expand Down
10 changes: 5 additions & 5 deletions server/src/Observers/ContactObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ContactObserver
*
* @return void
*/
public function created(Contact $contact)
public function creating(Contact $contact)
{
// Create a user account for the contact
if ($contact->doesntHaveUser()) {
Expand All @@ -33,12 +33,12 @@ public function saving(Contact $contact)
}

// Validate email is available to user
if ($contact->isDirty('email') && $this->isEmailUnavailable($contact)) {
if (!empty($contact->email) && $contact->wasChanged('email') && $this->isEmailUnavailable($contact)) {
throw new \Exception('Email attempting to update for ' . $contact->type . ' is not available.');
}

// Validate phone is available to user
if ($contact->isDirty('phone') && $this->isPhoneUnavailable($contact)) {
if (!empty($contact->phone) && $contact->wasChanged('phone') && $this->isPhoneUnavailable($contact)) {
throw new \Exception('Phone attempting to update for ' . $contact->type . ' is not available.');
}

Expand All @@ -59,11 +59,11 @@ public function deleted(Contact $contact)

private function isEmailUnavailable(Contact $contact)
{
return User::where('email', $contact->email)->whereNot('uuid', $contact->user_uuid)->exists() || Contact::where('email', $contact->email)->whereNot('uuid', $contact->uuid)->exists();
return User::where('email', $contact->email)->whereNot('uuid', $contact->user_uuid)->exists() || Contact::where(['email' => $contact->email, 'company_uuid' => $contact->company_uuid])->whereNot('uuid', $contact->uuid)->exists();
}

private function isPhoneUnavailable(Contact $contact)
{
return User::where('phone', $contact->phone)->whereNot('uuid', $contact->user_uuid)->exists() || Contact::where('phone', $contact->phone)->whereNot('uuid', $contact->uuid)->exists();
return User::where('phone', $contact->phone)->whereNot('uuid', $contact->user_uuid)->exists() || Contact::where(['phone' => $contact->phone, 'company_uuid' => $contact->company_uuid])->whereNot('uuid', $contact->uuid)->exists();
}
}
1 change: 1 addition & 0 deletions server/src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ function ($router) {
$router->get('{id}', 'VehicleController@find');
$router->put('{id}', 'VehicleController@update');
$router->delete('{id}', 'VehicleController@delete');
$router->match(['put', 'patch', 'post'], '{id}/track', 'VehicleController@track');
});
// fleets routes
$router->group(['prefix' => 'fleets'], function () use ($router) {
Expand Down

0 comments on commit a25c7f9

Please sign in to comment.