From ef089fa67a297fefa424628248920de867986260 Mon Sep 17 00:00:00 2001 From: "Ronald A. Richardson" Date: Tue, 24 Dec 2024 13:58:36 +0800 Subject: [PATCH] updates for api to work smooth --- composer.json | 2 +- extension.json | 2 +- package.json | 2 +- .../Commands/SimulateOrderRouteNavigation.php | 9 ++-- .../Controllers/Api/v1/ContactController.php | 9 ++++ .../Controllers/Api/v1/OrderController.php | 1 + .../Controllers/Api/v1/PlaceController.php | 41 ++++++++++++------- .../Http/Requests/CreateContactRequest.php | 2 +- server/src/Http/Resources/v1/Contact.php | 2 + server/src/Http/Resources/v1/Place.php | 8 ++-- server/src/Models/Place.php | 6 +-- 11 files changed, 54 insertions(+), 30 deletions(-) diff --git a/composer.json b/composer.json index 5209ee9e..ccf4d174 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "fleetbase/fleetops-api", - "version": "0.5.14", + "version": "0.5.15", "description": "Fleet & Transport Management Extension for Fleetbase", "keywords": [ "fleetbase-extension", diff --git a/extension.json b/extension.json index 502d85c7..c1cfdbeb 100644 --- a/extension.json +++ b/extension.json @@ -1,6 +1,6 @@ { "name": "Fleet-Ops", - "version": "0.5.14", + "version": "0.5.15", "description": "Fleet & Transport Management Extension for Fleetbase", "repository": "https://github.com/fleetbase/fleetops", "license": "AGPL-3.0-or-later", diff --git a/package.json b/package.json index 11ff49d0..200211a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fleetbase/fleetops-engine", - "version": "0.5.14", + "version": "0.5.15", "description": "Fleet & Transport Management Extension for Fleetbase", "fleetbase": { "route": "fleet-ops" diff --git a/server/src/Console/Commands/SimulateOrderRouteNavigation.php b/server/src/Console/Commands/SimulateOrderRouteNavigation.php index 9a9875d9..f1d21ff3 100644 --- a/server/src/Console/Commands/SimulateOrderRouteNavigation.php +++ b/server/src/Console/Commands/SimulateOrderRouteNavigation.php @@ -37,6 +37,7 @@ public function handle() $orderId = $this->argument('order'); $order = Order::where('public_id', $orderId)->orWhere('uuid', $orderId)->first(); + $driver = null; if (!$order) { return $this->error('Order not found to simulate driving for.'); } @@ -51,10 +52,12 @@ public function handle() } // Get the order driver - $order->loadAssignedDriver(); - $driver = $order->driverAssigned; if (!$driver) { - return $this->error('No driver found to simulate the order.'); + $order->loadAssignedDriver(); + $driver = $order->driverAssigned; + if (!$driver) { + return $this->error('No driver found to simulate the order.'); + } } // Inform diff --git a/server/src/Http/Controllers/Api/v1/ContactController.php b/server/src/Http/Controllers/Api/v1/ContactController.php index f6f27489..11137c35 100644 --- a/server/src/Http/Controllers/Api/v1/ContactController.php +++ b/server/src/Http/Controllers/Api/v1/ContactController.php @@ -8,6 +8,7 @@ use Fleetbase\FleetOps\Http\Resources\v1\DeletedResource; use Fleetbase\FleetOps\Models\Contact; use Fleetbase\Http\Controllers\Controller; +use Fleetbase\Support\Utils; use Illuminate\Http\Request; class ContactController extends Controller @@ -66,6 +67,14 @@ public function update($id, UpdateContactRequest $request) // get request input $input = $request->only(['name', 'type', 'title', 'email', 'phone', 'meta']); + // If setting a default location for the contact + if ($request->has('place')) { + $input['place_uuid'] = Utils::getUuid('places', [ + 'public_id' => $request->input('place'), + 'company_uuid' => session('company'), + ]); + } + // update the contact $contact->update($input); $contact->flushAttributesCache(); diff --git a/server/src/Http/Controllers/Api/v1/OrderController.php b/server/src/Http/Controllers/Api/v1/OrderController.php index 1bbf407e..74b3195a 100644 --- a/server/src/Http/Controllers/Api/v1/OrderController.php +++ b/server/src/Http/Controllers/Api/v1/OrderController.php @@ -996,6 +996,7 @@ public function updateActivity($id, Request $request) // also update for each order entities if not multiple drop order // all entities will share the same activity status as is one drop order if (!$order->payload->isMultipleDropOrder) { + // Only update entities belonging to the waypoint foreach ($order->payload->entities as $entity) { $entity->insertActivity($activity, $location, $proof); } diff --git a/server/src/Http/Controllers/Api/v1/PlaceController.php b/server/src/Http/Controllers/Api/v1/PlaceController.php index 6bf15174..8139826d 100644 --- a/server/src/Http/Controllers/Api/v1/PlaceController.php +++ b/server/src/Http/Controllers/Api/v1/PlaceController.php @@ -113,7 +113,8 @@ public function create(CreatePlaceRequest $request) [ 'public_id' => $id, 'company_uuid' => session('company'), - ] + ], + ['full' => true] ); if (is_array($owner)) { @@ -125,6 +126,7 @@ public function create(CreatePlaceRequest $request) /** @var \Fleetbase\Models\Place */ $place = Place::firstOrNew([ 'company_uuid' => session('company'), + 'owner_uuid' => data_get($input, 'owner_uuid'), 'name' => strtoupper(Utils::or($input, ['name', 'street1'])), 'street1' => strtoupper($input['street1']), ]); @@ -200,24 +202,33 @@ public function update($id, UpdatePlaceRequest $request) // owner assignment if ($request->has('owner')) { - $id = $request->input('owner'); + $owner = $request->input('owner'); - // check if customer_ based contact - if (Str::startsWith($id, 'customer')) { - $id = Str::replaceFirst('customer', 'contact', $id); + // Handle if owner is an object + if (is_array($owner) || is_object($owner)) { + $id = data_get($owner, 'id', data_get($owner, 'customer_id')); + } elseif (is_string($owner)) { + $id = $owner; } - $owner = Utils::getUuid( - ['contacts', 'vendors'], - [ - 'public_id' => $id, - 'company_uuid' => session('company'), - ] - ); + if ($id) { + // check if customer_ based contact + if (Str::startsWith($id, 'customer')) { + $id = Str::replaceFirst('customer', 'contact', $id); + } - if (is_array($owner)) { - $input['owner_uuid'] = Utils::get($owner, 'uuid'); - $input['owner_type'] = Utils::getModelClassName(Utils::get($owner, 'table')); + $owner = Utils::getUuid( + ['contacts', 'vendors'], + [ + 'public_id' => $id, + 'company_uuid' => session('company'), + ] + ); + + if (is_array($owner)) { + $input['owner_uuid'] = Utils::get($owner, 'uuid'); + $input['owner_type'] = Utils::getModelClassName(Utils::get($owner, 'table')); + } } } diff --git a/server/src/Http/Requests/CreateContactRequest.php b/server/src/Http/Requests/CreateContactRequest.php index 68cb2c3b..5b60e563 100644 --- a/server/src/Http/Requests/CreateContactRequest.php +++ b/server/src/Http/Requests/CreateContactRequest.php @@ -25,7 +25,7 @@ public function authorize() public function rules() { return [ - 'name' => ['required'], + 'name' => [new RequiredIf($this->isMethod('POST'))], 'type' => [new RequiredIf($this->isMethod('POST'))], 'email' => ['nullable', 'email'], 'phone' => ['nullable'], diff --git a/server/src/Http/Resources/v1/Contact.php b/server/src/Http/Resources/v1/Contact.php index bd7ae1af..9bf6b62d 100644 --- a/server/src/Http/Resources/v1/Contact.php +++ b/server/src/Http/Resources/v1/Contact.php @@ -5,6 +5,7 @@ use Fleetbase\Http\Resources\FleetbaseResource; use Fleetbase\Http\Resources\User; use Fleetbase\Support\Http; +use Illuminate\Support\Str; class Contact extends FleetbaseResource { @@ -19,6 +20,7 @@ public function toArray($request) { return [ 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id), + 'customer_id' => $this->when($this->type === 'customer' && Http::isPublicRequest(), Str::replace('contact', 'customer', $this->public_id)), 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid), 'company_uuid' => $this->when(Http::isInternalRequest(), $this->company_uuid), 'user_uuid' => $this->when(Http::isInternalRequest(), $this->user_uuid), diff --git a/server/src/Http/Resources/v1/Place.php b/server/src/Http/Resources/v1/Place.php index fcbd10db..67c288bf 100644 --- a/server/src/Http/Resources/v1/Place.php +++ b/server/src/Http/Resources/v1/Place.php @@ -2,8 +2,8 @@ namespace Fleetbase\FleetOps\Http\Resources\v1; +use Fleetbase\FleetOps\Support\Utils; use Fleetbase\Http\Resources\FleetbaseResource; -use Fleetbase\LaravelMysqlSpatial\Types\Point; use Fleetbase\Support\Http; use Fleetbase\Support\Resolve; @@ -18,6 +18,8 @@ class Place extends FleetbaseResource */ public function toArray($request) { + $this->loadMissing('owner'); + return [ 'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id), 'uuid' => $this->when(Http::isInternalRequest(), $this->uuid), @@ -26,7 +28,7 @@ public function toArray($request) 'owner_uuid' => $this->when(Http::isInternalRequest(), $this->owner_uuid), 'owner_type' => $this->when(Http::isInternalRequest(), $this->owner_type), 'name' => $this->name, - 'location' => data_get($this, 'location', new Point(0, 0)), + 'location' => Utils::getPointFromMixed($this->location), 'address' => $this->address, 'address_html' => $this->when(Http::isInternalRequest(), $this->address_html), 'avatar_url' => $this->avatar_url, @@ -43,7 +45,7 @@ public function toArray($request) 'country' => $this->country ?? null, 'country_name' => $this->when(Http::isInternalRequest(), $this->country_name), 'phone' => $this->phone ?? null, - 'owner' => $this->whenLoaded('owner', Resolve::resourceForMorph($this->owner_type, $this->owner_uuid)), + 'owner' => Resolve::resourceForMorph($this->owner_type, $this->owner_uuid), 'tracking_number' => $this->whenLoaded('trackingNumber', fn () => $this->trackingNumber), 'type' => $this->type, 'meta' => data_get($this, 'meta', []), diff --git a/server/src/Models/Place.php b/server/src/Models/Place.php index 109adcd4..92f7ae06 100644 --- a/server/src/Models/Place.php +++ b/server/src/Models/Place.php @@ -133,11 +133,7 @@ class Place extends Model */ public function owner() { - return $this->morphTo(__FILE__, 'owner_type', 'owner_uuid')->withDefault( - [ - 'name' => 'N/A', - ] - ); + return $this->morphTo(__FILE__, 'owner_type', 'owner_uuid'); } /**